#include"STC12C5A.h"
void keyscan();void find(unsigned char num);void process();
unsigned char keynum=0;
unsigned char const seg[10] = {0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6}; //段选
sbit HC595_SCK = P0^4;sbit HC595_RCK = P0^5;sbit HC595_RST = P0^6;sbit HC595_DAT = P0^7;
void sendbyte(unsigned char num);
void show(unsigned char num);
void diaplay(unsigned char num);
void delay_ms(unsigned char time);
void main(){ P0 &=~(1<<6);
while(1){ keyscan(); process(); }
}
void delay_ms(unsigned char time){ unsigned char t; while(time--) { t=1500; while(t--); }}
void diaplay(unsigned char num)
{ show(num/1000);
0 = (P0&0xf0)|1;
delay_ms(2);
show(num%1000/100);
0 = (P0&0xf0)|2; //数码管位选 delay_ms(2);
show(num%1000/100); 0 = (P0&0xf0)|4; delay_ms(2);
show(num%1000/100);
0 = (P0&0xf0)|8; delay_ms(2);
}
void show(unsigned char num){ sendbyte(seg[num]);}
void sendbyte(unsigned char num){ unsigned char i,temp; for(i=0;i<=7;i++) { temp = num&1<<i; IF(temp) { HC595_DAT = 1; //串行输入1 } else { HC595_DAT=0; } HC595_SCK=1; //上升沿,移位 delay_ms(1); HC595_SCK=0; delay_ms(1); } HC595_RCK=0; //上升沿,并行输出 delay_ms(1); HC595_RCK=1; }
void find(unsigned char num){ keynum=0; 2=0X0F; switch((~P2)&0X0F) { case 1: keynum=1+num;break;case 2: keynum=4+num;break;case 4: keynum=8+num;break;case 8: keynum=12+num;break;default: break; }}
void keyscan(){ 2=0X80; //将p2^7置高电平,若p2^7不再为高电平,说明这一列有按键按下 if(P2&0X80!=0X80) { delay_ms(15); if(P2&0X80!=0X80) find(0); } 2=0X40; if(P2&0X40!=0X40) { delay_ms(15); if(P2&0X40!=0X40) find(4); } 2=0X20; if(P2&0X20!=0X20) { delay_ms(15); if(P2&0X20!=0X20) find(8); } 2=0X10; if(P2&0X10!=0X10) { delay_ms(15); if(P2&0X10!=0X10) find(12); }}
void process()
{
switch(process)
{
case 1: show(1);break;
case 2: show(2);break;
case 3: show(3);break;
case 4: show(4);break;
case 5: show(5);break;
case 6: show(6);break;
case 7: show(7);break;
case 8: show(8);break;
case 9: show(9);break;
case 10: show(10);break;
case 11: show(11);break;
case 12: show(12);break;
case 13: show(13);break;
case 14: show(14);break;
case 15: show(15);break;
case 16: show(16);break;
}
}
单片机型号是STC12C5A60S2,4位数码管,就是想扫描矩阵键盘,但数码管显示的不知道什么东西
求解答!!!
|