woshizhangyiwei 于 2016.04.02 12:48 提问

#include
#define uchar unsigned char
#define uint unsigned int
sbit A=P3^0;
sbit B1=P3^1;
sbit C=P3^2;
sbit kin1=P0^0;
sbit kin2=P0^1;
sbit kin3=P0^2;
sbit kin4=P0^3;
sbit kout1=P0^4;
sbit kout2=P0^5;
sbit kout3=P0^6;
sbit kout4=P0^7;
uchar code ledchar[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar ledbuff[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
uchar keysta[4][4]={{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}};
uchar code keycodemap[4][4]={{0x31,0x32,0x33,0x26},{0x34,0x35,0x36,0x25},{0x37,0x38,0x39,0x28},{0x30,0x1b,0x0d,0x27}};
void keydriver();
void main()
{
EA=1;
TMOD=0x01;

TH0=0xFC;
TL0=0x67;
ET0=1;
TR0=1;
ledbuff[0]=ledchar[0];
while(1)
{
keydriver();

``````}
``````

}
void shownumber(unsigned long num)
{
signed char i;
uchar buff[8];
for(i=0;i {
buff[i]=num%10;
num=num/10;
}
for(i=7;i>=1;i--)
{
if(buff[i]==0)

``````      { ledbuff[i]=0x00;}

else
break;

}

for(i;i>=0;i--)
{
ledbuff[i]=ledchar[buff[i]];
}
``````

}
void keyaction(uchar keycode)
{
static unsigned long result=0;
if((keycode>=0x30)&&(keycode<=0x39))
{
}
else if(keycode==0x26)
{
shownumber(result);
}
else if(keycode==0x0d)
{
shownumber(result);
}
else if (keycode=0x1b)
{
result=0;
}
}
void keydriver()
{ uchar j;
uchar i;
uchar backup[4][4]={{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}};
for(i=0;i<4;i++)
{

for(j=0;j<4;j++)
{
if(backup[i][j]!=keysta[i][j])
{
if(backup[i][j]!=0)
{
keyaction(keycodemap[i][j]);
}
backup[i][j]=keysta[i][j];
}
}
}

}
void keyscan()
{
uchar i;
static uchar keyout=0;
uchar keybuff[4][4]={{0xff,0xff,0xff,0xff},{0xff,0xff,0xff,0xff},{0xff,0xff,0xff,0xff},{0xff,0xff,0xff,0xff}};
keybuff[keyout][0]=(keybuff[keyout][0]<<1)| kin1;
keybuff[keyout][1]=(keybuff[keyout][1]<<1)| kin2;
keybuff[keyout][2]=(keybuff[keyout][2]<<1)| kin3;
keybuff[keyout][3]=(keybuff[keyout][3]<<1)| kin4;
for(i=0;i<4;i++)
{ if((keybuff[keyout][i]&0x0f)==0x00)
{
keysta[keyout][i]=0;
}
else if((keybuff[keyout][i]&0x0f)==0x0f)
{

keysta[keyout][i]=1;
}

``````    }
keyout++;
keyout=keyout&0x03;
switch(keyout)
{
case 0: kout4=1;kout1=0;break;
case 1: kout1=1;kout2=0;break;
case 2: kout2=1;kout3=0;break;
case 3: kout3=1;kout4=0;break;
default :break;
}
``````

}
void ledscan()
{
uchar i=0;
switch(i)
{
case 0:A=1;B1=1;C=1;i++;P1=ledbuff[0];break;
case 1:A=0;B1=1;C=1;i++;P1=ledbuff[1];break;
case 2:A=1;B1=0;C=1;i++;P1=ledbuff[2];break;
case 3:A=0;B1=0;C=1;i++;P1=ledbuff[3];break;
case 4:A=1;B1=1;C=0;i++;P1=ledbuff[4];break;
case 5:A=0;B1=1;C=0;i++;P1=ledbuff[5];break;
case 6:A=1;B1=0;C=0;i++;P1=ledbuff[6];break;
case 7:A=0;B1=0;C=0;i=0;P1=ledbuff[7];break;
default:break;
}

}
void zd() interrupt 1
{
TH0=0xFC;
TL0=0x67;
keyscan();
ledscan();
}

4个回答

qq_28766327   2016.04.02 13:09

zanfeng   2016.04.02 14:51

woshizhangyiwei 有阿,一个简易加法计算器的程序

zanfeng   2016.04.02 22:13

cpu型号啥的都一样？？？

qq_29650681   2016.04.03 10:21