数码管亮,读不到按键扫描数据。求大神看看程序哪里出问题了。
#include
#include
typedef unsigned char uchar;
typedef unsigned int uint;
uchar v=123;
#define ADDRESS_FIX_MODE 0X40
#define READ_KEY 0X42
#define ADDRESS_INCREASE_MODE 0X44
#define TM1638_DIG0 0XC0
#define TM1638_DIG1 0XC2
#define TM1638_DIG2 0XC4
#define TM1638_DIG3 0XC6
#define TM1638_DIG4 0XC8
#define TM1638_DIG5 0XCA
#define TM1638_DIG6 0XCC
#define TM1638_DIG7 0XCE
#define DISPLAY_OFF 0X80
#define DISPLAY_ON 0X88
#define SET_PLUS_WIDTH1 0X88
#define SET_PLUS_WIDTH2 0X89
#define SET_PLUS_WIDTH4 0X8A
#define SET_PLUS_WIDTH10 0X8B
#define SET_PLUS_WIDTH11 0X8C
#define SET_PLUS_WIDTH12 0X8D
#define SET_PLUS_WIDTH13 0X8E
#define SET_PLUS_WIDTH14 0X8F
***it STB = P1^0;
***it CLK = P1^1;
***it DIO = P1^2;
uchar temp[9]={0,0,0,0,0,0,0,0,0};
uchar temp2[9]={0,0xc0,0xc2,0xc4,0xc6,0xc8,0xca,0xcc,0xce};
uchar temp3[3]={0x10,0x20,0x40};
uchar num[3]={0,0,0};
const uchar CathodeCode[ 0x10 ] = { 0X3F, 0X06, 0X5B, 0X4F, 0X66, 0X6D, 0X7D,
0X07, 0X7F, 0X6F, 0X77, 0X7C, 0X58, 0X5E,
0X79, 0X71 };
void DelayUs(uint Us)
{
while(--Us);
}
void DelayMs(uint Ms)
{
while(Ms--)
{
DelayUs(200);
DelayUs(200);
DelayUs(200);
DelayUs(200);
DelayUs(200);
}
}
void TM1638_WriteCmd(uchar Cmd)
{
uchar i;
STB=0;
for(i=0;i<8;i++)
{
CLK=0;
DelayUs(1);
Cmd >>=1;
DIO=CY;
CLK=1;
DelayUs(1);
}
STB=1;
DelayUs(2);
}
void TM1638_WriteData(uchar Dig,uchar Data)
{
uchar i;
STB=0;
for(i=0;i<8;i++)
{
CLK=0;
DelayUs(1);
Dig >>=1;
DIO=CY;
CLK=1;
DelayUs(2);
}
for(i=0;i<8;i++)
{
CLK=0;
DelayUs(1);
Data >>=1;
DIO=CY;
CLK=1;
DelayUs(2);
}
STB=1;
DelayUs(2);
}
void deal()
{
uchar t,i,j;
TM1638_WriteCmd(0x44 );
// TM1638_WriteCmd( ADDRESS_FIX_MODE );
for(j=0;j<3;j++)
{
t=temp3[j];
switch(num[j])
{
case 0:
temp[6]=temp[6]+t;
temp[1]=temp[1]+t;
temp[2]=temp[2]+t;
temp[3]=temp[3]+t;
temp[4]=temp[4]+t;
temp[5]=temp[5]+t;
break;
case 1:
temp[2]=temp[2]+t;
temp[3]=temp[3]+t;
break;
case 2:
temp[1]=temp[1]+t;
temp[2]=temp[2]+t;
temp[4]=temp[4]+t;
temp[5]=temp[5]+t;
temp[7]=temp[7]+t;
break;
case 3:
temp[1]=temp[1]+t;
temp[2]=temp[2]+t;
temp[3]=temp[3]+t;
temp[4]=temp[4]+t;
temp[7]=temp[7]+t;
break;
case 4:
temp[2]=temp[2]+t;
temp[3]=temp[3]+t;
temp[6]=temp[6]+t;
temp[7]=temp[7]+t;
break;
case 5:
temp[1]=temp[1]+t;
temp[3]=temp[3]+t;
temp[4]=temp[4]+t;
temp[6]=temp[6]+t;
temp[7]=temp[7]+t;
break;
case 6:
temp[1]=temp[1]+t;
temp[6]=temp[6]+t;
temp[7]=temp[7]+t;
temp[3]=temp[3]+t;
temp[4]=temp[4]+t;
temp[5]=temp[5]+t;
break;
case 7:
temp[1]=temp[1]+t;
temp[2]=temp[2]+t;
temp[3]=temp[3]+t;
break;
case 8:
temp[1]=temp[1]+t;
temp[2]=temp[2]+t;
temp[3]=temp[3]+t;
temp[4]=temp[4]+t;
temp[5]=temp[5]+t;
temp[6]=temp[6]+t;
temp[7]=temp[7]+t;
break;
case 9:
temp[1]=temp[1]+t;
temp[2]=temp[2]+t;
temp[3]=temp[3]+t;
temp[4]=temp[4]+t;
temp[6]=temp[6]+t;
temp[7]=temp[7]+t;
break;
} }
for(i=1;i<8;i++)
{
TM1638_WriteData(temp2[i], temp[i]);
}
for(i=1;i<8;i++)
{
temp[i]=0;
}
}
void key()
{
uchar i,Cmd,Date;
TM1638_WriteCmd(0x8f);
Cmd=0x42;
STB=0;
for(i=0;i<8;i++)
{
CLK=0;
DelayUs(1);
Cmd >>=1;
DIO=CY;
CLK=1;
DelayUs(1);
}
DelayUs(2);
Date=0x00;
for(i=0;i<8;i++)
{
Date >>=1;
CLK=0;
DelayUs(1);
CLK=1;
if(DIO==1)
{
Date|=0x80;
}
DelayUs(2);
}
v=v+Date;
DelayUs(5);
Date=0x00;
for(i=0;i<8;i++)
{
Date >>=1;
CLK=0;
DelayUs(1);
CLK=1;
if(DIO==1)
{
Date|=0x80;
}
DelayUs(2);
}
v=v+Date;
DelayUs(5);
Date=0x00;
for(i=0;i<8;i++)
{
Date >>=1;
CLK=0;
DelayUs(1);
CLK=1;
if(DIO==1)
{
Date|=0x80;
}
DelayUs(2);
}
v=v+Date;
DelayUs(5);
Date=0x00;
for(i=0;i<8;i++)
{
Date >>=1;
CLK=0;
DelayUs(1);
CLK=1;
if(DIO==1)
{
Date|=0x80;
}
DelayUs(2);
}
v=v+Date;
STB=1;
DelayUs(2);
}
void main()
{
while(1)
{
num[0]=v%10;
num[1]=v/10%10;
num[2]=v/100;
deal();
key();
}
}
|