内容来自小蜜蜂老师~
独立按键
题目要求:
代码:
#include "reg52.h"
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L4 = P0^3;
sbit L5 = P0^4;
sbit L6 = P0^5;
void HC138(unsigned char m)
{
switch(m)
{
case 4:P2 = (P2 & 0x1f) | 0x80;break;
case 5:P2 = (P2 & 0x1f) | 0xa0;break;
case 6:P2 = (P2 & 0x1f) | 0xc0;break;
case 7:P2 = (P2 & 0x1f) | 0xe0;break;
}
}
void DelayK(unsigned char t)
{
while(t--);
}
unsigned char state = 0;
void ScanKeys()
{
if(S7 == 0)
{
DelayK(100);
if(S7 == 0)
{
if(state == 0)
{
L1 = 0;
state = 1;
}
else if(state == 1)
{
L1 = 1;
state = 0;
}
while(S7 == 0);
}
}
if(S6 == 0)
{
DelayK(100);
if(S6 == 0)
{
if(state == 0)
{
L2 = 0;
state = 2;
}
else if(state == 2)
{
L2 = 1;
state = 0;
}
while(S6 == 0);
}
}
if(S5 == 0)
{
DelayK(100);
if(S5 == 0)
{
if(state == 1)
{
L3 = 0;
while(S5 == 0);
L3 = 1;
}
else if(state == 2)
{
L5 = 0;
while(S5 == 0);
L5 = 1;
}
}
}
if(S4 == 0)
{
DelayK(100);
if(S4 == 0)
{
if(state == 1)
{
L4 = 0;
while(S4 == 0);
L4 = 1;
}
else if(state == 2)
{
L6 = 0;
while(S4 == 0);
L6 = 1;
}
}
}
}
void Initsystem()
{
HC138(5);
P0=0x00;
HC138(4);
P0=0xff;
}
void main()
{
Initsystem();
while(1)
{
ScanKeys();
}
}
矩阵键盘
#include "reg52.h"
sfr P4=0xC0;
sbit L1 = P3^4;
sbit L2 = P3^5;
sbit L3 = P4^2;
sbit L4 = P4^4;
sbit R1 = P3^0;
sbit R2 = P3^1;
sbit R3 = P3^2;
sbit R4 = P3^3;
unsigned char code SMG_Duan[] = {0xC0,0xF9,0xA4,0xB0,0x99,
0x92,0x82,0xF8,0x80,0x90,0x88,0x80,0xC6,0xc0,0x86,0x8E,0xBF,0x7F};
void HC138(unsigned char m)
{
switch(m)
{
case 4:P2 = (P2 & 0x1f) | 0x80;break;
case 5:P2 = (P2 & 0x1f) | 0xa0;break;
case 6:P2 = (P2 & 0x1f) | 0xc0;break;
case 7:P2 = (P2 & 0x1f) | 0xe0;break;
}
}
void SMG_display(unsigned char pos,unsigned char value)
{
HC138(6);
P0=0x01<<pos;
HC138(7);
P0=value;
}
unsigned char Key_Num=0;
void ScanKeys()
{
R1=0;
R2=R3=R4=1;
L1=L2=L3=L4=1;
if(L1==0)
{
while(L1==0);
Key_Num=0;
SMG_display(0,SMG_Duan[Key_Num]);
}
else if(L2==0)
{
while(L2==0);
Key_Num=1;
SMG_display(1,SMG_Duan[Key_Num]);
}
else if(L3==0)
{
while(L3==0);
Key_Num=2;
SMG_display(2,SMG_Duan[Key_Num]);
}
else if(L4==0)
{
while(L4==0)
Key_Num=3;
SMG_display(3,SMG_Duan[Key_Num]);
}
R2=0;
R1=R3=R4=1;
L1=L2=L3=L4=1;
if(L1==0)
{
while(L1==0);
Key_Num=4;
SMG_display(4,SMG_Duan[Key_Num]);
}
else if(L2==0)
{
while(L2==0);
Key_Num=5;
SMG_display(5,SMG_Duan[Key_Num]);
}
else if(L3==0)
{
while(L3==0);
Key_Num=6;
SMG_display(6,SMG_Duan[Key_Num]);
}
else if(L4==0)
{
while(L4==0)
Key_Num=7;
SMG_display(7,SMG_Duan[Key_Num]);
}
R3=0;
R1=R2=R4=1;
L1=L2=L3=L4=1;
if(L1==0)
{
while(L1==0);
Key_Num=8;
SMG_display(7,SMG_Duan[Key_Num]);
}
else if(L2==0)
{
while(L2==0);
Key_Num=9;
SMG_display(6,SMG_Duan[Key_Num]);
}
else if(L3==0)
{
while(L3==0);
Key_Num=10;
SMG_display(5,SMG_Duan[Key_Num]);
}
else if(L4==0)
{
while(L4==0)
Key_Num=11;
SMG_display(4,SMG_Duan[Key_Num]);
}
R4=0;
R1=R3=R2=1;
L1=L2=L3=L4=1;
if(L1==0)
{
while(L1==0);
Key_Num=12;
SMG_display(3,SMG_Duan[Key_Num]);
}
else if(L2==0)
{
while(L2==0);
Key_Num=13;
SMG_display(2,SMG_Duan[Key_Num]);
}
else if(L3==0)
{
while(L3==0);
Key_Num=14;
SMG_display(1,SMG_Duan[Key_Num]);
}
else if(L4==0)
{
while(L4==0)
Key_Num=15;
SMG_display(0,SMG_Duan[Key_Num]);
}
}
void Initsystem()
{
HC138(5);
P0=0x00;
HC138(4);
P0=0xff;
}
void main()
{
Initsystem();
while(1)
{
ScanKeys();
}
}