9.19键盘的应用
作业1:数码管前三位数显示一个跑表,从000到999,之间以百分之一秒的速度运行,当按下key1时跑表停止,再次按下时跑表开始
#include<reg52.h> #define uint unsigned int #define uchar unsigned char sbit dula =P2^6; sbit wela = P2^7; sbit key1 = P3^7; uchar aa,flag,bai,shi,ge,tt; uint num,bb; void init(); void keyscan(); void delay(uint z); void display(uchar bai,uchar shi,uchar ge); uchar code table[]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71}; main() { init(); P3 = 0xff; while(1) { display(bai,shi,ge); if(key1==0)//检测是否按下 { delay(10); if(key1==0)//确实按下了 { TR0 = 0;//定时器停止 bb ++;//计算按下的次数 while(!key1) display(bai,shi,ge); if(bb %2==0) { TH0 =(65536-50000)/256;//装初值 TL0 =(65536-50000)%256; TR0 = 1;//重启定时器 } } } } } void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void init() { TMOD = 0x01; TH0 =(65536-50000)/256; TL0 =(65536-50000)%256; EA = 1; ET0 = 1; TR0 = 1; } void time0()interrupt 1 { TH0 =(65536-50000)/256; TL0 =(65536-50000)%256; aa++; if(aa==2) { aa = 0; num ++; if(num==1000) num=0; } bai = num/100; shi = num/10%10; ge = num%10; } void display(uchar bai,uchar shi,uchar ge) { wela = 1; P0 = 0xfe; wela = 0; P0 = 0x0;//消影操作 dula = 1; P0 = table[bai]; dula = 0; tt = 25; while(tt--); dula = 1;//关灯操作,高速度扫描时必备 P0 = 0; dula = 0; wela = 1 ; P0 = 0xfd; wela = 0; P0= 0x0;//消影操作 dula = 1; P0 = table[shi]; dula = 0; tt = 25; while(tt--); dula = 1;//关灯操作,高速度扫描时必备 P0 = 0; dula = 0; wela = 1; P0 = 0xfb; wela = 0; P0 = 0x0;//消影操作 dula = 1; P0 = table[ge]; dula =0; tt=25; while(tt --); dula = 1;//关灯操作,高速度扫描时必备 P0 = 0; dula = 0; }
在上题目的基础上,用另外三个独立键盘实现按下第一个时计数停止,按下第二个时计数开始,按下第三个时计数清零重新开始
#include<reg52.h> #define uint unsigned int #define uchar unsigned char sbit dula =P2^6; sbit wela = P2^7; sbit key1 = P3^3; sbit key2 = P3^4; sbit key3 = P3^5; sbit key4 = P3^6; uchar aa,flag,bai,shi,ge,tt; uint num,bb; void init(); void keyscan(); void delay(uint z); void display(uchar bai,uchar shi,uchar ge); uchar code table[]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71}; main() { init(); P3 = 0xff; while(1) { display(bai,shi,ge); if(key1==0)//检测是否按下 { delay(10); if(key1==0)//确实按下了 { TR0 = 0;//定时器停止 while(!key1) display(bai,shi,ge); TR0 = 1;//重启定时器 } } if(key2==0) { delay(10); if(key2==0) { TR0 = 0;//定时器停止 while(!key2) display(bai,shi,ge); } } if(key3==0) { delay(10); if(key3==0) { TH0=(65536-50000)/256; TL0 = (65536-50000)%256; TR0 = 1;//重启定时器 while(!key3) display(bai,shi,ge);//不加入这句话有很大的bug } } if(key4==0) { delay(10); if(key4==0) { num = 0;//重新开始计数 while(!key4) display(bai,shi,ge); } } } } void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void init() { TMOD = 0x01; TH0 =(65536-50000)/256; TL0 =(65536-50000)%256; EA = 1; ET0 = 1; TR0 = 1; } void time0()interrupt 1 { TH0 =(65536-50000)/256; TL0 =(65536-50000)%256; aa++; if(aa==2) { aa = 0; num ++; if(num==1000) num=0; } bai = num/100; shi = num/10%10; ge = num%10; } void display(uchar bai,uchar shi,uchar ge) { wela = 1; P0 = 0xfe; wela = 0; P0 = 0x0;//消影操作 dula = 1; P0 = table[bai]; dula = 0; tt = 25; while(tt--); dula = 1;//关灯操作,高速度扫描时必备 P0 = 0; dula = 0; wela = 1 ; P0 = 0xfd; wela = 0; P0= 0x0;//消影操作 dula = 1; P0 = table[shi]; dula = 0; tt = 25; while(tt--); dula = 1;//关灯操作,高速度扫描时必备 P0 = 0; dula = 0; wela = 1; P0 = 0xfb; wela = 0; P0 = 0x0;//消影操作 dula = 1; P0 = table[ge]; dula =0; tt=25; while(tt --); dula = 1;//关灯操作,高速度扫描时必备 P0 = 0; dula = 0; }
作业3:按下16个矩阵键盘依次在数码管上显示1到16的平方
#include<reg52.h> #define uint unsigned int #define uchar unsigned char sbit dula = P2^6; sbit wela = P2^7; uchar num1,num,temp,tt; uint aa; uchar keyscan(); void display(uchar bai,uchar shi,uchar ge); void delay(uint z); uchar code table[]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71}; main() { while(1) { display(aa/100,aa/10%10,aa%10); num1 = keyscan(); aa = num1*num1; } } void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void display(uchar bai,uchar shi,uchar ge) { wela = 1; P0 = 0xfe; wela = 0; P0 = 0x0;//消影操作 dula = 1; P0 = table[bai]; dula = 0; tt = 25; while(tt--); dula = 1;//关灯操作,高速度扫描时必备 P0 = 0; dula = 0; wela = 1 ; P0 = 0xfd; wela = 0; P0= 0x0;//消影操作 dula = 1; P0 = table[shi]; dula = 0; tt = 25; while(tt--); dula = 1;//关灯操作,高速度扫描时必备 P0 = 0; dula = 0; wela = 1; P0 = 0xfb; wela = 0; P0 = 0x0;//消影操作 dula = 1; P0 = table[ge]; dula =0; tt=25; while(tt --); dula = 1;//关灯操作,高速度扫描时必备 P0 = 0; dula = 0; } uchar keyscan() { P3 = 0xfe;//1111 1110 temp = P3; temp = temp&0xf0;//11110000 while(temp!=0xf0)//如果有键摁下 { delay(5);//再次检测P3口 temp = P3; temp = temp&0xf0; while(temp!=0xf0) { temp = P3; switch(temp) { case 0x7e:num = 1; break; case 0xbe:num = 2; break; case 0xde:num = 3; break; case 0xee:num = 4; break; } while(temp!=0xf0)//松手检测 { temp = P3; temp = temp&0xf0; display(aa/100,aa/10%10,aa%10);//这句话不加,有很大的bug } } } P3 = 0xfd;//1111 1101 temp = P3; temp = temp&0xf0;//11110000 while(temp!=0xf0)//如果有键摁下 { delay(5);//再次检测P3口 temp = P3; temp = temp&0xf0; while(temp!=0xf0) { temp = P3; switch(temp) { case 0x7d:num = 5; break; case 0xbd:num = 6; break; case 0xdd:num = 7; break; case 0xed:num = 8; break; } while(temp!=0xf0)//松手检测 { temp = P3; temp = temp&0xf0; display(aa/100,aa/10%10,aa%10); } } } P3 = 0xfb;//1111 1011 temp = P3; temp = temp&0xf0;//11110000 while(temp!=0xf0)//如果有键摁下 { delay(5);//再次检测P3口 temp = P3; temp = temp&0xf0; while(temp!=0xf0) { temp = P3; switch(temp) { case 0x7b:num = 9; break; case 0xbb:num = 10; break; case 0xdb:num = 11; break; case 0xeb:num = 12; break; } while(temp!=0xf0)//松手检测 { temp = P3; temp = temp&0xf0; display(aa/100,aa/10%10,aa%10); } } } P3 = 0xf7;//1111 0111 temp = P3; temp = temp&0xf0;//11110000 while(temp!=0xf0)//如果有键摁下 { delay(5);//再次检测P3口 temp = P3; temp = temp&0xf0; while(temp!=0xf0) { temp = P3; switch(temp) { case 0x77:num = 13; break; case 0xb7:num = 14; break; case 0xd7:num = 15; break; case 0xe7:num = 16; break; } while(temp!=0xf0)//松手检测 { temp = P3; temp = temp&0xf0; display(aa/100,aa/10%10,aa%10); } } } return num; }
其实,单片机的功能模块有很多的示例代码可以参考,也正是由于c语言具有极好的移植性,所以大大加快了现在点子产品的开发周期
版权所有,转载请注明链接地址:http://www.cnblogs.com/fengdashen/