csad键盘练习怎么是转向

stc单片机用数码管显示i love you(键盘版)
15:37:35编辑:什么鱼 关键字:&&&&&&
#include<12c5a.h>
#define uchar unsigned char
#define uint unsigned int
uchar led[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar key[3]={0xfe,0xfd,0xfb};
unsigned char get()
& &uchar x,y,lie,i;
& &x=0;y=0;
& &for(i=0;i<3;i++)
& & & P1=key[i];
&&lie=P1&0x38;
&&if(lie!=0x38)
&& & x=i+1;
&switch(lie)
&& case 0x30:{y=1;}
&& case 0x28:{y=2;}
&& case 0x18:{y=3;}
&& default:{x=0;}
& &if(x!=0)
& &return (3*(x-1)+y);
& &else return 0;
void main()
&uchar play,i;
&P2M1=0x00;
&P1M1=0x00;
& & play=get();
& & switch(play)
&& P2=led[1];
&& P0=0x07;
&& for(i=0;i<100;i++);
&& P2=0x38;
&& P0=0x0e;
&& for(i=0;i<100;i++);
&& P0=0x0f;
&& P2=0x3f;
&& for(i=0;i<100;i++);
&& P0=0x0f;
&& P2=0x3e;
&& P0=0x0b;
&& for(i=0;i<100;i++);
&& P0=0x0f;
&& P2=0x79;
&& P0=0x07;
&& for(i=0;i<100;i++);
&& &P2=0x6e;
for(i=0;i<100;i++);
for(i=0;i<100;i++);
for(i=0;i<100;i++);
&&default :{P2=led[0];}
关键字:&&&&&&
来源: 51hei
引用地址:
本网站转载的所有的文章、图片、音频视频文件等资料的版权归版权所有人所有,本站采用的非本站原创文章及图片等内容无法一一联系确认版权者。如果本网所选内容的文章作者及编辑认为其作品不宜公开自由传播,或不应无偿使用,请及时通过电子邮件或电话通知我们,以迅速采取适当措施,避免给双方造成不必要的经济损失。
关注eeworld公众号快捷获取更多信息
关注eeworld服务号享受更多官方福利
宏晶STC很多系列的单片机片内都集成了EEPROM存储器(其实是Data Flash),方便用户使用,想得真周到!今天写了个小程序,测试了一下,工作正常。下面就上我自己写的源程序。本人在STC89C52RC单片机上试验成功。首先是stcEEPROM.h头文件内容:/*When & & & & Who & & Remarks----------------------------------2011-Oct-06 &黄长浩 &初始版本&*/&#ifndef __STCEEPROM_H__#define
最近做了个小东西,使用的是很简单的一个单片机STC15F104W,但是功能很好用。这里简单介绍下本人使用STC15F104W实现的PWM脉冲高电平方式。本人要捕获的PWM脉冲周期是20ms,高电平介于1~2ms。主要使用的是外部中断和定时器,STC15F104W有4个外部中断,这里采用外部中断0,可以响应上升沿和下降沿。(P3.2)外部中断0捕获引脚P3.2的上升沿和下降沿:上升沿开启定时器0计时,下降沿终止定时器0计时,这样读取计时寄存器的值即可获得高电平时间:TH0*256+TL0;具体程序段://*******************************************************//函数名
#include&reg52.h&&&#include&stdio.h&&&&&#define&GPIO_KEY&P0&&#define&GPIO_LED&P1&&unsigned&char&KeyV//存储键值&&unsigned&char&code&DIG_CODE[]={&&0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,&nbsp
CSDA BIT P3.2 &WRR BIT P3.6 &WELA BIT P2.7 &DULA BIT P2.6 &&&ORG 0000H &LOOP0: &MOV P0,#0FFH ;关数码管显示 &SETB WELA &CLR WELA &CLR DULA &SETB CSDA &SETB WR &MOV A,#0 &LOOP1: &MOV P0,A &CLR CSDA &&CLR WRR &LCALL DELAY &S
,#0FFH&;关数码管显示&&SETB&WELA&&SETB&RDD&&SETB&WRR&&STARTAD:&;开始转换&&CLR&CSAD&&//NOP&&CLR&WRR&&//NOP&&SETB&WRR&&//NOP&&SETB&CSAD&&LCALL&DELAY&;延时,等待转换完成&nbsp
;;共阴数码管 &DB &3FH,06H,5BH,4FH &DB &66H,6DH,7DH,07H &DB &7FH,6FH,77H,7CH &DB &39H,5EH,79H,71H &&&END&
热门资源推荐
最新单片机文章
最新视频课程
何立民专栏
北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。
相关热搜器件键盘: 基于STC89C52的键盘设计详解
10:24:44编辑:什么鱼 关键字:&&
一.&& 硬件设计详解
1. 独立键盘部分
S2~S5为4个独立键盘, 与单片机的P3.4~P3.7分别相连
2. 矩阵键盘部分
S6~S21为16个矩阵键盘,8条线分别与单片机的P3口相连, 其中矩阵键盘的4行分别与单片机的P3.0~P3.3相连,矩阵键盘的4列分别与单片机的P3.4~P3.7相连.
二. 程序设计详解
1. 用数码管的前两位显示一个十进制数,变化范围为00-59.
用数码管的前两位显示一个十进制数,变化范围为00-59,开始时显示00,每按下S2键一次,数值加1;每按下S3键一次,数值减1;每按下S4键一次,数值归零;按下S5键一次,利用定时器功能使数值开始每秒加1,再次按下S5键,数值停止自动加1,保持原数.
#define uchar unsigned char
#define uint unsigned int
sbit key1 = P3 ^ 4;
sbit key2 = P3 ^ 5;
sbit key3 = P3 ^ 6;
sbit key4 = P3 ^ 7;
sbit dula = P2 ^ 6;
sbit wela = P2 ^ 7;
uchar code table[] = {
&&&&&& 0x3f,0x06,0x5b,0x4f,
&&&&&& 0x66,0x6d,0x7d,0x07,
&&&&&& 0x7f,0x6f,0x77,0x7c,
&&&&&& 0x39,0x5e,0x79,0x71
void init();
void keyscan();
void display(uchar);
void delayms(uint);
void main()
&&&&&& init();
&&&&&& while (1)
&&&&&&&&&&&&& keyscan();
&&&&&&&&&&&&& display(num);
void init()
&&&&&& TMOD = 0x01;
&&&&&& TH0 = (65536 - 45872) / 256;
&&&&&& TL0 = (65536 - 45872) % 256;
&&&&&& EA = 1;
&&&&&& ET0 = 1;
void keyscan()
&&&&&& if (key1 == 0)
&&&&&&&&&&&&& delayms(10);
&&&&&&&&&&&&& if (key1 == 0)
&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&& if (num == 60)
&&&&&&&&&&&&&&&&&&&&&&&&&&& num = 0;
&&&&&&&&&&&&&&&&&&&& num++;
&&&&&&&&&&&&&&&&&&&& while (!key1)
& & & & & & & & & & & & & &display(num);
&&&&&&&&&&&&& }
&&&&&& if (key2 == 0)
&&&&&&&&&&&&& delayms(10);
&&&&&&&&&&&&& if (key2 == 0)
&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&& if (num == 0)
&&&&&&&&&&&&&&&&&&&&&&&&&&& num = 60;
&&&&&&&&&&&&&&&&&&&& num--;
&&&&&&&&&&&&&&&&&&&& while (!key2)
& & & & & & & & & & & & & & display(num);
&&&&&&&&&&&&& }
&&&&&& if (key3 == 0)
&&&&&&&&&&&&& delayms(10);
&&&&&&&&&&&&& if (key3 == 0)
&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&& num = 0;
&&&&&&&&&&&&&&&&&&&& while (!key3)
& & & & & & & & & & & & & display(num);
&&&&&&&&&&&&& }
&&&&&& if (key4 == 0)
&&&&&&&&&&&&& delayms(10);
&&&&&&&&&&&&& if (key4 == 0)
&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&& TR0 = ~TR0;
&&&&&&&&&&&&&&&&&&&& while (!key4)
& & & & & & & & & & & & & &display(num);
&&&&&&&&&&&&& }
void display(uchar numDis)
&&&&&& dula = 1;
&&&&&& P0 = table[numDis / 10];
&&&&&& dula = 0;
&&&&&& P0 = 0
&&&&&& wela = 1;
&&&&&& P0 = 0
&&&&&& wela = 0;
&&&&&& delayms(5);
&&&&&& dula = 1;
&&&&&& P0 = table[numDis % 10];
&&&&&& dula = 0;
&&&&&& P0 = 0
&&&&&& wela = 1;
&&&&&& P0 = 0
&&&&&& wela = 0;
&&&&&& delayms(5);
void delayms(uint xms)
&&&&&& uint i,
&&&&&& for (i = i > 0; i--)
&&&&&&&&&&&&& for (j = 110; j > 0; j--)
&&&&&&&&&&&&&&&&&&&& ;
void T0_timer() interrupt 1
&&&&&& TH0 = (65536 - 45872) / 256;
&&&&&& TL0 = (65536 - 45872) % 256;
&&&&&& t0++;
&&&&&& if (t0 == 20)
&&&&&&&&&&&&& t0 = 0;
&&&&&&&&&&&&& num++;
&&&&&&&&&&&&& if (num == 60)
&&&&&&&&&&&&&&&&&&&& num = 0;
a. 键盘按键检测需要做两次 (每个键盘按键用了两个if)
b. 键盘按键退出也需要检测 (每次按键退出用了一个while(!key))
c. 这段程序包含按键加1,按键减1,按键归零,按键开始计数,再按停止计数
2. 按矩阵键盘,在数码管上显示0~F,6个数码管同时静态显示即可.
上电不显示, 按矩阵键盘,在数码管上显示0~F,6个数码管同时静态显示即可.
#define uchar unsigned char
#define uint unsigned int
sbit dula = P2 ^ 6;
sbit wela = P2 ^ 7;
uchar code table[] = {
&&&&&& 0x3f,0x06,0x5b,0x4f,
&&&&&& 0x66,0x6d,0x7d,0x07,
&&&&&& 0x7f,0x6f,0x77,0x7c,
&&&&&& 0x39,0x5e,0x79,0x71
void delayms(uint);
void display(uchar);
void matrixkeyscan();
void main()
&&&&&& dula = 1;
&&&&&& P0 = 0;
&&&&&& dula = 0;
&&&&&& wela = 1;
&&&&&& P0 = 0xc0;
&&&&&& wela = 0;
&&&&&& while (1)
&&&&&&&&&&&&& matrixkeyscan();
void delayms(uint xms)
&&&&&& uint i,
&&&&&& for (i = i > 0; i--)
&&&&&&&&&&&&& for (j = 110; j > 0; j--)
&&&&&&&&&&&&&&&&&&&& ;
void display(uchar num)
&&&&&& dula = 1;
&&&&&& P0 = table[num];
&&&&&& dula = 0;
void matrixkeyscan()
&&&&&& uchar temp,
&&&&&& P3 = 0
&&&&&& temp = P3;
&&&&&& temp = temp & 0xf0;
&&&&&& if (temp != 0xf0)
&&&&&&&&&&&&& delayms(10);
&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&& temp = temp & 0xf0;
&&&&&&&&&&&&& if (temp != 0xf0)
&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&&&&&&&&& switch(temp)
&&&&&&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&&&&&&&&& case 0xee:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 0;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&& case 0xde:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 1;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&& case 0xbe:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 2;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&& case 0x7e:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 3;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&&& while (temp != 0xf0)
&&&&&&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&&&&&&&&&&&&&&&& temp = temp & 0xf0;
&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&&& display(key);
&&&&&&&&&&&&& }
&&&&&& P3 = 0
&&&&&& temp = P3;
&&&&&& temp = temp & 0xf0;
&&&&&& if (temp != 0xf0)
&&&&&&&&&&&&& delayms(10);
&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&& temp = temp & 0xf0;
&&&&&&&&&&&&& if (temp != 0xf0)
&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&&&&&&&&& switch(temp)
&&&&&&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&&&&&&&&& case 0xed:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 4;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&& case 0xdd:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 5;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&& case 0xbd:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 6;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&& case 0x7d:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 7;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&&& while (temp != 0xf0)
&&&&&&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&&&&&&&&&&&&&&&& temp = temp & 0xf0;
&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&&& display(key);
&&&&&&&&&&&&& }
&&&&&& P3 = 0
&&&&&& temp = P3;
&&&&&& temp = temp & 0xf0;
&&&&&& if (temp != 0xf0)
&&&&&&&&&&&&& delayms(10);
&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&& temp = temp & 0xf0;
&&&&&&&&&&&&& if (temp != 0xf0)
&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&&&&&&&&& switch(temp)
&&&&&&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&&&&&&&&& case 0xeb:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 8;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&& case 0xdb:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 9;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&& case 0xbb:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 10;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&& case 0x7b:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 11;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&&& while (temp != 0xf0)
&&&&&&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&&&&&&&&&&&&&&&& temp = temp & 0xf0;
&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&&& display(key);
&&&&&&&&&&&&& }
&&&&&& P3 = 0xf7;
&&&&&& temp = P3;
&&&&&& temp = temp & 0xf0;
&&&&&& if (temp != 0xf0)
&&&&&&&&&&&&& delayms(10);
&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&& temp = temp & 0xf0;
&&&&&&&&&&&&& if (temp != 0xf0)
&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&&&&&&&&& switch(temp)
&&&&&&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&&&&&&&&& case 0xe7:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 12;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&& case 0xd7:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 13;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&& case 0xb7:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 14;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&& case 0x77:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 15;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&&& while (temp != 0xf0)
&&&&&&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&&&&&&&&&&&&&&&& temp = temp & 0xf0;
&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&&& display(key);
&&&&&&&&&&&&& }
a. 先送行线低电平,检测列线信号
b. 通过延时来消抖
c. 需要检查释放
3. 数码管前三位显示一个跑表,从000到999之间以1%秒速度运行
当按下一个独立键盘时跑表停止,松开手后跑表继续运行。用定时器设计表。
#define uint unsigned int
#define uchar unsigned char
sbit dula = P2 ^ 6;
sbit wela = P2 ^ 7;
sbit s2 = P3 ^ 4;
uchar code table[] = {
&&&&&& 0x3f,0x06,0x5b,0x4f,
&&&&&& 0x66,0x6d,0x7d,0x07,
&&&&&& 0x7f,0x6f,0x77,0x7c,
&&&&&& 0x39,0x5e,0x79,0x71,
&&&&&& 0x76,0x79,0x38,0x3f,0
uchar flag, t0, bai, shi,
void init();
void display(uchar aa, uchar bb, uchar cc);
void delayms(uint);
void main()
&&&&&& init();
&&&&&& while (1)
&&&&&&&&&&&&& display(bai, shi, ge);
&&&&&&&&&&&&& if (s2 == 0)
&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&& delayms(10);
&&&&&&&&&&&&&&&&&&&& if (s2 == 0)
&&&&&&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&&&&&&&&& TR0 = 0;
&&&&&&&&&&&&&&&&&&&&&&&&&&& while (!s2)
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& display(bai, shi, ge);
&&&&&&&&&&&&&&&&&&&&&&&&&&& TR0 = 1;
&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&& }
void init()
&&&&&& TMOD = 0x01;
&&&&&& TH0 = (65536 - 10000) / 256;
&&&&&& TL0 = (65536 - 10000) % 256;
&&&&&& EA = 1;
&&&&&& ET0 = 1;
&&&&&& TR0 = 1;
void T0_timer() interrupt 1
&&&&&& TH0 = (65536 - 10000) / 256;
&&&&&& TL0 = (65536 - 10000) % 256;
&&&&&& t0++;
&&&&&& if (t0 == 1)
&&&&&&&&&&&&& t0 = 0;
&&&&&&&&&&&&& shu++;
&&&&&&&&&&&&& if (shu == 1000)
&&&&&&&&&&&&&&&&&&&& shu = 0;
&&&&&&&&&&&&& bai = shu / 100;
&&&&&&&&&&&&& shi = shu % 100 / 10;
&&&&&&&&&&&&& ge = shu % 10;
void display(uchar aa, uchar bb, uchar cc)
&&&&&& dula = 1;
&&&&&& P0 = table[aa];
&&&&&& dula = 0;
&&&&&& P0 = 0
&&&&&& wela = 1;
&&&&&& P0 = 0
&&&&&& wela = 0;
&&&&&& delayms(1);
&&&&&& dula = 1;
&&&&&& P0 = table[bb];
&&&&&& dula = 0;
&&&&&& P0 = 0
&&&&&& wela = 1;
&&&&&& P0 = 0
&&&&&& wela = 0;
&&&&&& delayms(1);
&&&&&& dula = 1;
&&&&&& P0 = table[cc];
&&&&&& dula = 0;
&&&&&& P0 = 0
&&&&&& wela = 1;
&&&&&& P0 = 0
&&&&&& wela = 0;
&&&&&& delayms(1);
void delayms(uint xms)
&&&&&& uint i,
&&&&&& for (i = i > 0; i--)
&&&&&&&&&&&&& for (j = 110; j > 0; j--)
&&&&&&&&&&&&&&&&&&&& ;
a. 松开检测 while (!s2)
b. 1%秒速度运行: 选择定时基数为1000(即10ms), 定时倍数为1,相乘为10ms
&& 说明: 由于间隔时间太短,所以这里出现的问题是低位数据显示看不清楚,可以选用定时倍数为10 (但定时数变成了100ms, 可能与题意不符)
4. 数码管前三位显示一个跑表,从000到999之间以1%秒速度运行
当按下第一个独立键盘时跑表停止,松开手后跑表继续运行。
当按下第二个独立键盘时计时停止,
当按下第三个独立键盘时计时开始,
当按下第三个独立键盘时计数值清零从头开始
#define uint unsigned int
#define uchar unsigned char
sbit dula = P2 ^ 6;
sbit wela = P2 ^ 7;
sbit s2 = P3 ^ 4;
sbit s3 = P3 ^ 5;
sbit s4 = P3 ^ 6;
sbit s5 = P3 ^ 7;
uchar code table[] = {
& & 0x3f,0x06,0x5b,0x4f,
& & 0x66,0x6d,0x7d,0x07,
& & 0x7f,0x6f,0x77,0x7c,
& & 0x39,0x5e,0x79,0x71,
& & 0x76,0x79,0x38,0x3f,0
uchar flag, t0;
void init();
void display(uint num);
void delayms(uint);
void keyscan();
void main()
& & init();
& & while (1)
& & & & display(shu);
& & & & keyscan();
void init()
& & TMOD = 0x01;
& & TH0 = (65536 - 10000) / 256;
& & TL0 = (65536 - 10000) % 256;
& & EA = 1;
& & ET0 = 1;
& & TR0 = 1;
void keyscan()
& & if (s2 == 0)
& & & & delayms(10);
& & & & if (s2 == 0)
& & & & & & TR0 = 0;
& & & & & & while (!s2)
& & & & & & & & display(shu);
& & & & & & TR0 = 1;
& & if (s3 == 0)
& & & & delayms(10);
& & & & if (s3 == 0)
& & & & & & TR0 = 0;
& & & & & & while (!s3)
& & & & & & & & display(shu);
& & if (s4 == 0)
& & & & delayms(10);
& & & & if (s4 == 0)
& & & & & &TR0 = 1;
& & & & & &while (!s4)
& & & & & & & &display(shu);
& & if (s5 == 0)
& & & & delayms(10);
& & & & if (s5 == 0)
& & & & & & shu = 0;
& & & & & & while (!s5)
& & & & & & & & display(shu);
void T0_timer() interrupt 1
& & TH0 = (65536 - 10000) / 256;
& & TL0 = (65536 - 10000) % 256;
& & if (t0 == 1)
& & & & t0 = 0;
& & & & shu++;
& & & & if (shu == 1000)
& & & & & & shu = 0;
void display(uint num)
& & dula = 1;
& & P0 = table[num / 100];
& & dula = 0;
& & P0 = 0
& & wela = 1;
& & P0 = 0
& & wela = 0;
& & delayms(1);
& & dula = 1;
& & P0 = table[num % 100 / 10];
& & dula = 0;
& & P0 = 0
& & wela = 1;
& & P0 = 0
& & wela = 0;
& & delayms(1);
& & dula = 1;
& & P0 = table[num % 100 % 10];
& & dula = 0;
& & P0 = 0
& & wela = 1;
& & P0 = 0
& & wela = 0;
& & delayms(1);
void delayms(uint xms)
& & &uint i,
& & for (i = i > 0; i--)
& & & & for (j = 110; j > 0; j--)
& & & & & & ;
1. 按键退出检测时需要加上display(shu), 否则数码管显示前两位为空白
2. 按键退出检测与执行动作的顺序问题
1) 如果要求一按键马上执行动作, 应该时先执行动作,再做按键退出检测
&&&&&&&&&&&&&&&&&&&& shu = 0;
&&&&&&&&&&&&&&&&&&&& while (!s5)
&&&&&&&&&&&&&&&&&&&&&&&&&&& display(shu);
2) 如果要求按键退出后再执行动作, 应该时先检查按键退出,再执行动作
&&&&&&&&&&&&& &&&&&& while (!s5)
&&&&&&&&&&&&&&&&&&&&&&&&&&& display(shu);
&&&&&&&&&&&&&&&&&&&& shu = 0;
5. 按下16个矩阵键盘,依次在前三个数码管上显示1~16的平方
按下16个矩阵键盘,依次在前三个数码管上显示1~16的平方,即按下第一个显示1,按下第二个显示4,&按下第16个显示16*16 (256)
#define uchar unsigned char
#define uint unsigned int
sbit dula = P2 ^ 6;
sbit wela = P2 ^ 7;
uchar code table[] = {
&&&& 0x3f,0x06,0x5b,0x4f,
&&&& 0x66,0x6d,0x7d,0x07,
&&&& 0x7f,0x6f,0x77,0x7c,
&&&& 0x39,0x5e,0x79,0x71
void delayms(uint);
void display(uint);
void matrixkeyscan();
void main()
&&&& while (1)
&&&&&& matrixkeyscan();
void delayms(uint xms)
&&&& uint i,
&&&& for (i = i > 0; i--)
&&&&&& for (j = 110; j > 0; j--)
&&&&&&&&&&&&& ;
void display(uint num)
&&&& dula = 1;
&&&& P0 = table[num / 100];
&&&& dula = 0;
&&&& P0 = 0
&&&& wela = 1;
&&&& P0 = 0
&&&& wela = 0;
&&&& delayms(1);
&&&& dula = 1;
&&&& P0 = table[num % 100 / 10];
&&&& dula = 0;
&&&& P0 = 0
&&&& wela = 1;
&&&& P0 = 0
&&&& wela = 0;
&&&& delayms(1);
&&&& dula = 1;
&&&& P0 = table[num % 100 % 10];
&&&& dula = 0;
&&&& P0 = 0
&&&& wela = 1;
&&&& P0 = 0
&&&& wela = 0;
&&&& delayms(1);
void matrixkeyscan()
&&&& uchar temp,
&&&& P3 = 0
&&&& temp = P3;
&&&& temp = temp & 0xf0;
&&&& if (temp != 0xf0)
&&&&&& delayms(10);
&&&&&& temp = P3;
&&&&&& temp = temp & 0xf0;
&&&&&& if (temp != 0xf0)
&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&& switch(temp)
&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&& case 0xee:
&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 1;
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&& case 0xde:
&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 2;
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&& case 0xbe:
&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 3;
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&& case 0x7e:
&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 4;
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&& }
&&&&&&&&&&&&& while (temp != 0xf0)
&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&&&&&&&&& temp = temp & 0xf0;
&&&&&&&&&&&&& }
&&&& P3 = 0
&&&& temp = P3;
&&&& temp = temp & 0xf0;
&&&& if (temp != 0xf0)
&&&&&& delayms(10);
&&&&&& temp = P3;
&&&&&& temp = temp & 0xf0;
&&&&&& if (temp != 0xf0)
&&&& &&&&&&&& temp = P3;
&&&&&&&&&&&&& switch(temp)
&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&& case 0xed:
&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 5;
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&& case 0xdd:
&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 6;
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&& case 0xbd:
&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 7;
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&& case 0x7d:
&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 8;
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&& }
&&&&&&&&&&&&& while (temp != 0xf0)
&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&&&&&&&&& temp = temp & 0xf0;
&&&&&&&&&&&&& }
&&&& P3 = 0
&&&& temp = P3;
&&&& temp = temp & 0xf0;
&&&& if (temp != 0xf0)
&&&&&& delayms(10);
&&&&&& temp = P3;
&&&&&& temp = temp & 0xf0;
&&&&&& if (temp != 0xf0)
&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&& switch(temp)
&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&& case 0xeb:
&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 9;
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&& case 0xdb:
&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 10;
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&& case 0xbb:
&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 11;
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&& case 0x7b:
&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 12;
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&& }
&&&&&&&&&&&&& while (temp != 0xf0)
&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&&&&&&&&& temp = temp & 0xf0;
&&&&&&&&&&&&& }
&&&& P3 = 0xf7;
&&&& temp = P3;
&&&& temp = temp & 0xf0;
&&&& if (temp != 0xf0)
&&&&&& delayms(10);
&&&&&& temp = P3;
&&&&&& temp = temp & 0xf0;
&&&&&& if (temp != 0xf0)
&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&& switch(temp)
&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&& case 0xe7:
&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 13;
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&& case 0xd7:
&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 14;
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&& case 0xb7:
&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 15;
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&& case 0x77:
&&&&&&&&&&&&&&&&&&&&&&&&&&& key = 16;
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&& }
&&&&&&&&&&&&& while (temp != 0xf0)
&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&& temp = P3;
&&&&&&&&&&&&&&&&&&&& temp = temp & 0xf0;
&&&&&&&&&&&&& }
&&&& display(key * key);
1. display(key * key)不能马上放到每一个按键退出检测后,如果那样做,数码管上只会显示最后一位数字(即个位)
&& 换句话说,
&& 程序不能这样:
&& while (temp != 0xf0)
&&&&&& temp = P3;
&&&&&& temp = temp & 0xf0;
display(key*key);
而是应该放到程序最后结束处
关键字:&&
来源: eefocus
引用地址:
本网站转载的所有的文章、图片、音频视频文件等资料的版权归版权所有人所有,本站采用的非本站原创文章及图片等内容无法一一联系确认版权者。如果本网所选内容的文章作者及编辑认为其作品不宜公开自由传播,或不应无偿使用,请及时通过电子邮件或电话通知我们,以迅速采取适当措施,避免给双方造成不必要的经济损失。
关注eeworld公众号快捷获取更多信息
关注eeworld服务号享受更多官方福利
#include&reg52.h&&&#include&stdio.h&&&&&#define&GPIO_KEY&P0&&#define&GPIO_LED&P1&&unsigned&char&KeyV//存储键值&&unsigned&char&code&DIG_CODE[]={&&0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,&nbsp
CSDA BIT P3.2 &WRR BIT P3.6 &WELA BIT P2.7 &DULA BIT P2.6 &&&ORG 0000H &LOOP0: &MOV P0,#0FFH ;关数码管显示 &SETB WELA &CLR WELA &CLR DULA &SETB CSDA &SETB WR &MOV A,#0 &LOOP1: &MOV P0,A &CLR CSDA &&CLR WRR &LCALL DELAY &S
,#0FFH&;关数码管显示&&SETB&WELA&&SETB&RDD&&SETB&WRR&&STARTAD:&;开始转换&&CLR&CSAD&&//NOP&&CLR&WRR&&//NOP&&SETB&WRR&&//NOP&&SETB&CSAD&&LCALL&DELAY&;延时,等待转换完成&nbsp
WELA BIT P2.7 &DULA BIT P2.6 &DIOLA BIT P2.5 &FM BIT P2.3 &&&ORG 0000H &SETB DIOLA &MOV SP,#0E0H &LOOP0: &LCALL key_Scan &JZ LOOP0 &LCALL DELAY20MS &LCALL key_Scan &JZ LOOP0 &MOV P1,A &LCALL Smg &HERE0: &LCALL key_S等待按键释放 &nb
;SETB&SDA&&IIC8:&&SETB&SCL&&DJNZ&R0,IIC6&&CLR&SCL&;检测ACK&&SETB&SDA&&JB&SDA,$&&SETB&SCL&&CLR&SCL&;STOP信号&&CLR&SDA&&&SETB&SCL&&SETB&SDA&&nbsp
热门资源推荐
最新单片机文章
最新视频课程
何立民专栏
北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。
相关热搜器件

我要回帖

更多关于 键盘 的文章

 

随机推荐