📄 键盘0-9 循环显示.txt
字号:
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define Command1 0x03
#define Command2 0x40
#define Command2_1 0x42
#define Command3 0xc0
#define Command4 0x8a
#define error 20
uchar timer=20,k1=0;
uchar Read_all_key(void);
uchar Read_ET6202(void);
uchar Read_Key();
uchar ET6202_KeyTab[5];
uchar ET6202_DispTab[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
sbit ET6202_stb=P0^0;
sbit ET6202_clk=P0^1;
sbit ET6202_dio=P0^2;
void Tx_ET6202Com(uchar);
void Tx_ET6202_dio(uchar);
void Key_arrange(uchar k,uchar j);
void delay(uint z);
void main()
{
uchar k1=0,Key_num,i=0;
SP=0x30;
delay(400);
Tx_ET6202Com(Command2); // 写数据
Tx_ET6202Com(Command3); // RAM地址设置
Tx_ET6202Com(Command1); // 10段七位显示模式
Tx_ET6202Com(Command4); // 显示开启
while(1)
{
Tx_ET6202Com(Command2_1); //读键扫描数据
Key_num=Read_Key(); //读出数据
Tx_ET6202Com(Command2); // 写数据
Tx_ET6202Com(Command3+i*2); //RAM地址设置
Key_arrange(Key_num,i); //向ET6202发显示数据,使其显示
Tx_ET6202Com(Command1); //10段七位显示模式
Tx_ET6202Com(Command4); //显示开启
if(Key_num!=error)
{
delay(700);
i++;
if(i==7)
i=0;
}
}
}
/*********向ET6202中写入命令************/
void Tx_ET6202Com(uchar Com)
{ uchar i;
ET6202_stb=1;
ET6202_clk=1;
_nop_();
_nop_();
ET6202_stb=0;
for(i=0;i<8;i++)
{
ET6202_clk=0;
_nop_();
_nop_();
if(Com&0x01)
ET6202_dio=1;//退出写入 显示开启
else
ET6202_dio=0;//数据输入 键扫描继续
Com >>=1;
ET6202_clk=1;
_nop_();
_nop_();
}
}
/*********向ET6202中写入数据************/
void Tx_ET6202_dio(uchar dio)
{ uchar i;
ET6202_clk=1;
_nop_();
_nop_();
_nop_();
_nop_();
ET6202_stb=0;
ET6202_clk=0;
for(i=0;i<8;i++)
{
ET6202_clk=0;
_nop_();
_nop_();
_nop_();
_nop_();
if(dio&0x01)
ET6202_dio=1;
else
ET6202_dio=0;
dio >>=1;
ET6202_clk=1;
}
}
/********从ET6202中读出数据*************/
uchar Read_ET6202()
{
uchar i,Key_num;
ET6202_clk=1;
ET6202_dio=1;
_nop_();
_nop_();
_nop_();
_nop_();
for(i=0;i<7;i++)
{
ET6202_clk=1;
_nop_();
_nop_();
_nop_();
_nop_();
ET6202_clk=0;
_nop_();
_nop_();
_nop_();
_nop_();
if(ET6202_dio)
{
Key_num|=0x80;
Key_num>>=1;
}
else
Key_num>>=1;
}
ET6202_clk=1;
_nop_();
_nop_();
_nop_();
_nop_();
ET6202_clk=0;
_nop_();
_nop_();
_nop_();
_nop_();
if(ET6202_dio)
Key_num|=0x80;
// else
Key_num&=0x1B;
return(Key_num);
}
/********键值处理*************/
uchar Read_Key()
{
uchar num,Key_num;
for(num=0;num<5;num++)
ET6202_KeyTab[num]=Read_ET6202();
Key_num=error;
for(num=0;num<5;num++)
{ if(ET6202_KeyTab[num])
switch(ET6202_KeyTab[num])
{
case 1: Key_num=(10+2*num);break;
case 2: Key_num=(2*num);break;
case 8: Key_num=(11+2*num);break;
case 16: Key_num=(1+2*num);break;
default: Key_num=error;break;
}
}
return(Key_num);
}
/*********向ET6202中发显示数据,使其显示************/
void Key_arrange(uchar k,uchar j)
{ uchar num;
if(k<20)
{
k1=k;
num=0;
if(num==0)
Tx_ET6202_dio(ET6202_DispTab[k1]);
else if(num==1)
Tx_ET6202_dio(ET6202_DispTab[k1]);
else if(num==2)
Tx_ET6202_dio(ET6202_DispTab[k1]);
else if(num==3)
Tx_ET6202_dio(ET6202_DispTab[k1]);
else if(num==4)
Tx_ET6202_dio(ET6202_DispTab[k1]);
else if(num==5)
Tx_ET6202_dio(ET6202_DispTab[k1]);
else if(num==6)
Tx_ET6202_dio(ET6202_DispTab[k1]);
Tx_ET6202_dio(0);
num++;
if(num==7)
num=0;
}
}
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=125;y>0;y--);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -