|
- //===============================================================
- //程序编写人:
- //编写地点:湖南源点IC培训机构
- //时间:2009年7月6日下午 星期一
- //功能:本程序实现扫描4X8键盘,显示键值0-31.
- //===============================================================
- #include <reg51.h>
- #define uchar unsigned char
- #define uint long unsigned int
- #define ShuChu P1 //输出口
- #define PianXuan P0 //片选口
- #define Jian1 P2 //行输入口
- #define Jian2 P3 //列输入口
- bit AnJian;
- uchar n;
- uint code tempt1[]={0x0efe,0x0dfe,0x0bfe,0x07fe,
- 0x0efd,0x0dfd,0x0bfd,0x07fd,
- 0x0efb,0x0dfb,0x0bfb,0x07fb,
- 0x0ef7,0x0df7,0x0bf7,0x07f7,
- 0x0eef,0x0def,0x0bef,0x07ef,
- 0x0edf,0x0ddf,0x0bdf,0x07df,
- 0x0ebf,0x0dbf,0x0bbf,0x07bf,
- 0x0e7f,0x0d7f,0x0b7f,0x077f};//反转码
- uchar code tempt2[]={0xc0,0xf9,0xa4,0xb0,0x99,
- 0x92,0x82,0xf8,0x80,0x90};//共阳数码管数据
- void Delay(uchar M)//延时函数
- {
- uchar i,j;
- for(j=M;j>0;j--)
- for(i=125;i>0;i--);
- }
- void Display(uchar N)//显示函数
- {
- ShuChu=tempt2[N/10]; //显示十位
- PianXuan=0x01;
- Delay(20);
- PianXuan=0x00;
- ShuChu=tempt2[N%10]; //显示个位
- PianXuan=0x02;
- Delay(20);
- PianXuan=0x00;
- }
- uint SaoMiao(void)//扫描函数
- {
- uchar a,b;
- uint d;
- Jian1=0x00;
- Jian2=0x0f;
- a=Jian2&0x0f;
- d=a<<8;
- Jian1=0xff;
- Jian2=0x00;
- b=Jian1;
- return (d=d|b); //返回读取的键值
- }
- uchar DuJianZhi(uint K)//读键值函数
- {
- uchar i=0,b;
- if(K!=0x0fff)
- {
- AnJian=1;
- while(K!=tempt1)
- {
- i++;
- }
- return b=i;
- }
- else
- return b=n; //返回键值位置
- }
- main()//主函数
- {
- AnJian=0;
- while(1)
- {
- P3=0x0f;
- if(AnJian==1)
- {
- Display(n); //显示键值
- n=DuJianZhi(SaoMiao());
- }
- else
- {
- n=DuJianZhi(SaoMiao());
- }
- }
- }
复制代码
|
|