1、#include#define uchar unsigned char#define uint unsigned int /宏定义/-数码管 start-/uchar d1=0;uchar d2=0;uchar d3=0;uchar d4=0;uint p=0; /所要显示的数据uchar disp4=0;/PE 2-SCL1 3-SCL2 4-SDA1 sbit SCL1=P10;sbit SCL2=P11;sbit SDA1=P12;/sbit LED=P13;/-4X4键盘 定义start-/键盘用 口sbit v1=P20; /列sbit v2=P21;sbit v3=P22;sbit
2、 v4=P23;sbit h1=P24; /行sbit h2=P25;sbit h3=P26;sbit h4=P27;/-4X4键盘 定义end-/ uchar temp,num=0;/ uchar code wei=0x01,0x02,0x04,0x08; /四位数码管位选 共阳uchar code wei=0x01,0x02,0x04,0x08; /四位数码管位选 共阴 /uchar code duan=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90; /共阳数码管 CA uchar code duan=0x3f,0x06,0x5b,0x
3、4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f; /共阴数码管 CC / 0 1 2 3 4 5 6 7 8 9 void delay(uint z) /12M 约为 1ms uint x,y; for(x=z;x0;x-) for(y=110;yy-);/*-4X4 键盘 输入 & 4 位数码管 输出 start-*/void HC595_in(unsigned char Data) /*-74hc595串行数据输入-*/unsigned char i;for(i = 0; i 8; i+) /循环8次,刚好移完8位 SCL1 = 0; /先将移位寄存器控制引脚置为低 SD
4、A1 = Data & 0x80; /取数据的最高位 / SDA1 = Data ; Data = 1; /将数据的次高位移到最高位 SCL1 = 1; /再置为高,产生移位时钟上升沿,上升沿时数据寄存器的数据移位void HC595_out(void) /*-74hc595并行数据输出-*/SCL2 = 0; /先将存储寄存器引脚置为低_nop_();SCL2 = 1; /再置为高,产生移位时钟上升沿,上升沿时移位寄存器的数据进入数据存储寄存器,更新显示数据。 void digital_display(void) /*4位数码管显示数据*/ uchar i; / uint p=7367; /
5、 显示数据 p d1=p/1000; d2=p/100%10; d3=p/10%10; d4=p%10; disp0=duand1; disp1=duand2; disp2=duand3; disp3=duand4;/ LED=0; for(i=0;i4;i+) /有四位数码管,四位依次扫描 HC595_in(weii); /先传位码 / HC595_in(duani); /再传段码 HC595_in(dispi); HC595_out(); delay(2); /延迟时间2ms以内unsigned int keyboard() /*-key board-线反转法-*/ static unsi
6、gned int a=0; unsigned char a1=0,b1=0; /*行线 P2.4-7为输入线,列线 P2.0-3为输出线 */ v1=v2=v3=v4=0; h1=h2=h3=h4=1; if(P20xf0)/*检查行是否有按键按下 */ delay(20);0xf0) /*行线为输入线,列线为输出线 */ a1=P2; /*行线为输出线,列线为输入线 */ h1=h2=h3=h4=0; v1=v2=v3=v4=1; b1=P2; a=a1|b1; /*与0x03相或是为了屏蔽p2.0和p2.1的影响*/ if(a=0xe7)return 0;/ else if(a=0xeb)
7、return 4; else if(a=0xed)return 8; else if(a=0xee)return 12; else if(a=0xd7)return 1; else if(a=0xdb)return 5; else if(a=0xdd)return 9; else if(a=0xde)return 13; else if(a=0xb7)return 2; else if(a=0xbb)return 6; else if(a=0xbd)return 10; else if(a=0xbe)return 14; else if(a=0x77)return 3; else if(a=0
8、x7b)return 7; else if(a=0x7d)return 11; else if(a=0x7e)return 15; else return 8888;/ uchar keyscan() /*-键盘扫描-*/ P2=0xfe; temp=P2; temp=temp&0xf0; while(temp!=0xf0) delay(5); temp=P2; temp=temp& while(temp! temp=P2; switch(temp) case 0xee:num=12; break; case 0xde:num=13; case 0xbe:num=14; case 0x7e:n
9、um=15; temp=P2; temp=temp& P2=0xfd; case 0xed:num=8; case 0xdd:num=9; case 0xbd:num=10; case 0x7d:num=11; P2=0xfb; case 0xeb:num=4; case 0xdb:num=5; case 0xbb:num=6; case 0x7b:num=7; P2=0xf7; case 0xe7:num=0; case 0xd7:num=1; case 0xb7:num=2; case 0x77:num=3;return num; 8 位数码管 输出 end-*/-主函数-/void main(void)/unsigned char i;while(1) p=keyscan(); / p=6789; digital_display();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1