#include
sfr P4 = 0xC0; //reg52.h 头文件里没有P4口的定义,如果要使用,需要自己添加
***it R1 = P3^0; //定义矩阵键盘的行
***it R2 = P3^1;
***it R3 = P3^2;
***it R4 = P3^3;
***it C1 = P4^4; //定义矩阵键盘的列
***it C2 = P4^2;
***it C3 = P3^5;
***it C4 = P3^4;
unsigned char count = 0; //计数
unsigned char code SEG_code[18] =
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,
0xbf,0x7f}; //定义共阳数码管段码内容:0~F,—,.
void Delay_keys() //延时去抖动
{
unsigned char i = 108,j = 145;
while(--i)
{
while(--j);
}
}
void Select_HC138(unsigned char n)
{
switch(n)
{
case 5:
P2 = (P2 & 0x1f) | 0xa0;
break;
case 6:
P2 = (P2 & 0x1f) | 0xc0;
break;
case 7:
P2 = (P2 & 0x1f) | 0xe0;
break;
}
}
void Show_tube(unsigned char value)
{
Select_HC138(6);
P0 = 0x01;
Select_HC138(7);
P0 = value;
}
void Scan_keys()
{
C1 = C2 = C3 = C4 = 1;
if(C1 == 0)
{
Delay_keys();
if(C1 == 0)
{
Show_tube(SEG_code[count+0]);
}
while(C1 == 0);
}
else if(C2 == 0)
{
Delay_keys();
if(C2 == 0)
{
Show_tube(SEG_code[count+1]);
}
while(C2 == 0);
}
else if(C3 == 0)
{
Delay_keys();
if(C3 == 0)
{
Show_tube(SEG_code[count+2]);
}
while(C3 == 0);
}
else if(C4 == 0)
{
Delay_keys();
if(C4 == 0)
{
Show_tube(SEG_code[count+3]);
}
while(C4 == 0);
}
}
void main()
{
Select_HC138(5);
P0 = 0x00; //关闭蜂鸣器和继电器
P3 = 0x0fe; //R1 = 0;
while(1)
{
Scan_keys();
//x是要循环左移的目标数据,y是变量x的位数,z是期望循环左移的位数:x = (x >> (y-z)) | (x << z);
P3 = (P3 >> 7)| (P3 << 1); //将0循环左移一位,依次将R2、R3、R4置为0
count += 4;
if(count > 12)
{
P3 = 0xfe;
count = 0;
}
}
}
#include
sfr P4 = 0xC0; //reg52.h 头文件里没有P4口的定义,如果要使用,需要自己添加
***it R1 = P3^0; //定义矩阵键盘的行
***it R2 = P3^1;
***it R3 = P3^2;
***it R4 = P3^3;
***it C1 = P4^4; //定义矩阵键盘的列
***it C2 = P4^2;
***it C3 = P3^5;
***it C4 = P3^4;
unsigned char count = 0; //计数
unsigned char code SEG_code[18] =
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,
0xbf,0x7f}; //定义共阳数码管段码内容:0~F,—,.
void Delay_keys() //延时去抖动
{
unsigned char i = 108,j = 145;
while(--i)
{
while(--j);
}
}
void Select_HC138(unsigned char n)
{
switch(n)
{
case 5:
P2 = (P2 & 0x1f) | 0xa0;
break;
case 6:
P2 = (P2 & 0x1f) | 0xc0;
break;
case 7:
P2 = (P2 & 0x1f) | 0xe0;
break;
}
}
void Show_tube(unsigned char value)
{
Select_HC138(6);
P0 = 0x01;
Select_HC138(7);
P0 = value;
}
void Scan_keys()
{
C1 = C2 = C3 = C4 = 1;
if(C1 == 0)
{
Delay_keys();
if(C1 == 0)
{
Show_tube(SEG_code[count+0]);
}
while(C1 == 0);
}
else if(C2 == 0)
{
Delay_keys();
if(C2 == 0)
{
Show_tube(SEG_code[count+1]);
}
while(C2 == 0);
}
else if(C3 == 0)
{
Delay_keys();
if(C3 == 0)
{
Show_tube(SEG_code[count+2]);
}
while(C3 == 0);
}
else if(C4 == 0)
{
Delay_keys();
if(C4 == 0)
{
Show_tube(SEG_code[count+3]);
}
while(C4 == 0);
}
}
void main()
{
Select_HC138(5);
P0 = 0x00; //关闭蜂鸣器和继电器
P3 = 0x0fe; //R1 = 0;
while(1)
{
Scan_keys();
//x是要循环左移的目标数据,y是变量x的位数,z是期望循环左移的位数:x = (x >> (y-z)) | (x << z);
P3 = (P3 >> 7)| (P3 << 1); //将0循环左移一位,依次将R2、R3、R4置为0
count += 4;
if(count > 12)
{
P3 = 0xfe;
count = 0;
}
}
}
举报