用的STM32F407ZG,这是一段读取按键的程序,程序每次运行到这里不管if条件是等于还是不等于都会进入if语句中运行,没遇到过这个问题,哪位大神可以帮忙看看?
u16 read_key(void)
{
u8 key_tmp;
u16 key_state;
key_tmp = pcf8574_read_data(I2C8574_RD_IC1);
key_state |= (u16)(key_tmp << 8);
key_tmp = pcf8574_read_data(I2C8574_RD_IC2);
key_state |= (u16)key_tmp ;
if(key_old_state != key_state)//°′¼ü×′쬷¢éú±ä»ˉ
{
if(read_key_num < prevent_shake_num)//·à¶¶6′Î
{
if(key_old1_state !=key_state)//óëéÏò»′Î2é¼ˉμÄ°′¼ü×′ì¬2»í¬£¬ÔòÖØD¼Æêy¡£
{
read_key_num = 0;
key_old1_state = key_state;
}
else if(key_old1_state == key_state)//óëéÏò»′Î2é¼ˉμÄ°′¼ü×′ì¬Ïàí¬
{
read_key_num++;
}
}
else if(read_key_num >= prevent_shake_num)//·à¶¶¶ˉíê±Ï
{
read_key_num = 0;
key_old_state = key_state;
if(key_state !=0xffff)//μ±°′¼ü×′ì¬Îa°′ÏÂÄ3¼üê±£¬èÏÎaêÇò»′ΰ′¼ü¶ˉ×÷
{
key_change_flag = 1;
}
}
}
else if(key_old_state == key_state)
{
read_key_num = 0;
}
return(key_old_state);
}
此帖出自
小平头技术问答
定义的时候加个赋值试试,u16 key_state=0;
因为定义的时候没赋值有的单片机值是不固定的,你后面又用的是异或,所以判断的时候基本上不会相等
一周热门 更多>