stm32 +tm1638 只能显示,不能读键 为何?那地方出了问题 求大神指点。相关代码如下:
void TM1638_Write(unsigned char DATA) //D′êy¾Yoˉêy
{
unsigned char i;
for(i=0;i<8;i++)
{
CLK=0;
if(DATA&0X01)
DIO=1;
else
DIO=0;
DATA>>=1;
CLK=1;
}
}
unsigned char TM1638_Read(void) //¶áêy¾Yoˉêy
{
unsigned char i;
unsigned char temp=0;
DIO=1; //éèÖÃÎaêäèë
for(i=0;i<8;i++)
{
delay_us(5) ;
temp>>=1;
CLK=0;
if(DIO)
temp|=0x80;
CLK=1;
}
return temp;
}
void Write_COM(unsigned char cmd) //·¢ËíÃüáî×Ö
{
STB=0;
TM1638_Write(cmd);
STB=1;
}
unsigned char Read_key(void)
{
unsigned char c[4],i,key_value=0;
STB=0;
delay_ms(100);
TM1638_Write(0x42);
delay_ms(100);
for(i=0;i<4;i++)
{
c=TM1638_Read();
}
STB=1; //4¸ö×Ö½úêy¾YoÏ3éò»¸ö×Ö½ú
if(c[0]==0x04) key_value=1;
if(c[0]==0x40) key_value=2;
if(c[1]==0x04) key_value=3;
if(c[1]==0x40) key_value=4;
if(c[2]==0x04) key_value=5;
if(c[2]==0x40) key_value=6;
if(c[3]==0x04) key_value=7;
if(c[3]==0x40) key_value=8;
if(c[0]==0x02) key_value=9;
if(c[0]==0x20) key_value=10;
if(c[1]==0x02) key_value=11;
if(c[1]==0x20) key_value=12;
if(c[2]==0x02) key_value=13;
if(c[2]==0x20) key_value=14;
if(c[3]==0x02) key_value=15;
if(c[3]==0x20) key_value=16;
return (key_value);
}
网上的代码,显示没问题。
while (1)
{
LedDisplay(1,3,7,0,2,1,0,2);
delay_ms(1000);
key_value=Read_key();
switch (key_value)
{
case 1:
LedDisplay(1,0,0,0,0,0,0,0);
break;
case 2 :
LedDisplay(2,0,0,0,0,0,0,0);
break;
case 3 :
LedDisplay(3,0,0,0,0,0,0,0);
break;
case 4 :
LedDisplay(4,0,0,0,0,0,0,0);
break;
case 5 :
LedDisplay(5,0,0,0,0,0,0,0);
break;
case 6 :
LedDisplay(6,0,0,0,0,0,0,0);
break;
case 7 :
LedDisplay(7,0,0,0,0,0,0,0);
break;
case 8 :
LedDisplay(8,0,0,0,0,0,0,0);
break;
case 9:
LedDisplay(9,0,0,0,0,0,0,0);
break;
case 10 :
LedDisplay(0,1,0,0,0,0,0,0);
break;
case 11 :
LedDisplay(1,1,0,0,0,0,0,0);
break;
case 12 :
LedDisplay(2,1,0,0,0,0,0,0);
break;
case 13 :
LedDisplay(3,1,0,0,0,0,0,0);
break;
case 14 :
LedDisplay(4,1,0,0,0,0,0,0);
break;
case 15:
LedDisplay(5,1,0,0,0,0,0,0);
break;
case 16 :
LedDisplay(6,1,0,0,0,0,0,0);
break;
default :
break;//LedDisplay(0,0,0,0,0,0,0,0);
}
此帖出自
小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>