前言
使用52单片机实现独立键盘控制数码管计数
代码
c
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit key2 = P3^4;
sbit key3 = P3^5;
sbit key4 = P3^6;
sbit key5 = P3^7;
char code smg[] = {0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
sbit wei = P2^7;
sbit duan = P2^6;
void delayms(uint nms);
void display();
void keyCheck();
uint num = 0;
void main()
{
while(1)
{
display();
keyCheck();
}
}
void delayms(uint nms)
{
uint i,j;
for(i=nms;i>0;i--)
for(j=110;j>0;j--);
}
void display()
{
uint shi,ge;
shi = num/10;
ge = num%10;
wei = 1;
P0 = 0xFD;
wei = 0;
P0 = 0xFF;
duan = 1;
P0 = smg[ge];
duan = 0;
delayms(5);
P0 = 0xFF;
wei = 1;
P0 = 0xFE;
wei = 0;
P0 = 0xFF;
duan = 1;
P0 = smg[shi];
duan = 0;
delayms(5);
P0 = 0xFF;
}
void keyCheck()
{
if(key2 == 0)
{
delayms(10);
if(key2 == 0)
{
num++;
if(num >= 60)
num = 0;
}
while(!key2);
}
if(key3 == 0)
{
delayms(10);
if(key3 == 0)
{
num--;
if(num == 0)
num = 60;
}
while(!key3);
}
if(key4 == 0)
{
delayms(10);
if(key4 == 0)
{
num = 0;
}
while(!key4);
}
}
结论
key2按下:数码管增加计数
key3按下:数码管减小计数
key4按下:数码管清零