我在具有6个开关和2个晶体管(2x3)的试验电路板上进行了简单的多路复用设置。 当一个晶体管导通时、我从传感器的前半部分读取引脚值、然后将其关闭并打开另一个晶体管、并读取第二组传感器。 我的问题、我认为、与我的代码有关。 如果我单步执行调试器(或注释掉'check_key'函数)、一切工作正常、但如果我让它运行、则通过串行端口显示不正确的值。
以下是我单步执行调试器或注释掉'check_key'代码时的结果。 第一个值是晶体管(0或1)、第二个数字是来自 P3的寄存器值。 它正确显示值为"14"、 这意味着当晶体管#2 (1)导通时、它读取该晶体管上的开关#3读出值为4。
12:17:06.212 -> 14
12:17:06.212 -> 14
12:17:06.212 -> 14
以下是运行代码时的结果:
09:41:08.195 -> 14.
09:41:08.195 -> 14.
09:41:08.195 -> 17.
09:41:08.195 -> 17.
09:41:08.195 -> 17.
09:41:08.195 -> 17.
09:41:08.195 -> 17.
09:41:08.195 -> 14.
09:41:08.195 -> 17.
09:41:08.195 -> 07
总之、它会显示几次正确的值、但随后它开始读取非常不稳定的数字。 例如、"7"意味着晶体管上的所有三个传感器都已开启。 我还会获得随机读数、显示晶体管#1 (0)的所有3个传感器均已打开(07)。
以下是相关代码:
#include
typedef int bool;
#define true 1.
#define false 0
char left_hand_output[]={BIT2、BIT3};
volatile unsigned Leftint KeysStatus[]={
0x00、
0x00
};
char left_hand_input[]={BIT0、BIT1、BIT2};
unsigned int reg_values = 0;
void SerialPrint (char * TX_DATA、blineool Return); //将字符串输出到串行端口
void UARTInit_Send (void);
char* int_to_String (int i、char b[]);
void check_key (int reg、int group、bool on、bool Left);
int main (void){
WDTCTL = WDTPW | WDTHOLD;//停止看门狗计时器
UARTInit_Send();
//禁用 GPIO 上电默认高阻抗模式以激活
//先前配置的端口设置
PM5CTL0 &=~LOCKLPM5;
P1DIR |= BIT2;//| BIT3;//将 P1.2和 P1.3设置为输出
P1DIR |= BIT3;
P1OUT &=~BIT2;//|~BIT3;//最初关闭 P1.2和 P1.3
P1OUT &=~BIT3;
P3DIR &=~BIT0;//|~BIT1 |~BIT2;//将 P3.1、P3.2、P3.3设置为输入
P3DIR &=~BIT1;
P3DIR &=~BIT2;
P3REN |= BIT0 | BIT1 | BIT2;//启用上拉/下拉电阻器
P3OUT &= ~BIT0 |~BIT1 |~BIT2;//选择上拉电阻器
while (1) //轮询
{
volatile unsigned int i;
对于(i=0;i<2;++I){
unsigned int 引脚= left_hand_output[i];
P1OUT |=引脚;
//此处 需要轻微延迟、否则我们将读取上一个引脚
_DELAY_CYCLES (300);
REG_VALUES = P3IN;
P1OUT &=~Ω 引脚;
//检查是否有更改
if (reg_Values!= LeftKeysStatus[i]){
//---- 这将读取正确的值---
//char indexValue[12];
//Int_TO_String (I、indexValue);
//SerialPrint (indexValue、false);
//char dataValue[12];
//Int_TO_String (REG_VALUES、dataValue);
//SerialPrint (dataValue、true);
//如果字节值更大,我们将打开该开关;否则,将其关闭。
if (reg_Values > LeftKeysStatus[i]){
//使用逐位或仅发送修改后的位
CHECK_KEY (REG_VALUES ^ LeftKeysStatus[i]、i、true);
}
否则{
CHECK_KEY (REG_VALUES ^ LeftKeysStatus[i]、i、false);
}
}
}
}
}
//检查哪些位已更改并发送相应的 midi 消息
void check_key (int reg、int group、bool on){
//---- 这会读取错误的值---
字符索引值[12];
Int_TO_String (group、indexValue);
SerialPrint (indexValue、false);
字符 dataValue[12];
Int_TO_String (reg、dataValue);
SerialPrint (dataValue、true);
}
如果对正在进行的工作有任何帮助,将不胜感激。