#include <reg52.h>
#include "delay.h"
#define KEYPORT P0
#define SEGPORT P1
sbit seg_select = P2^0;
sbit bit_select = P2^1;
unsigned char tempdata[2] = {0x3f,0x3f};
unsigned char segdata[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char bitdata[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void timer0_init(void)
{
EA = 1;
TMOD |= 0x01;
TH0 = (65536 - 10000) / 256;
TL0 = (65536 - 10000) % 256;
ET0 = 1;
TR0 = 1;
}
void display()
{
static unsigned char i = 0;
SEGPORT = 0x0;
seg_select = 1;
seg_select = 0;
SEGPORT = 0xff;
bit_select = 1;
bit_select = 0;
SEGPORT = tempdata[i];
seg_select = 1;
seg_select = 0;
SEGPORT = bitdata[i];
bit_select = 1;
bit_select = 0;
i++;
if(2 == i)
{
i = 0;
}
}
void timer0_isr(void) interrupt 1
{
TH0 = (65536 - 10000) / 256;
TL0 = (65536 - 10000) % 256;
display();
}
unsigned char key_scan(void)
{
unsigned char keyvalue;
KEYPORT = 0xf0;
if(0xf0 != KEYPORT)
{
delay_ms(10);
if(0xf0 != KEYPORT)
{
KEYPORT = 0xfe;
if(0xfe != KEYPORT)
{
keyvalue = KEYPORT;
keyvalue &= 0xf0;
keyvalue += 0x0e;
while(0xfe != KEYPORT);
delay_ms(10);
while(0xfe != KEYPORT);
return keyvalue;
}
KEYPORT = 0xf7;
if(0xf7 != KEYPORT)
{
keyvalue = KEYPORT;
keyvalue &= 0xf0;
keyvalue += 0x07;
while(0xf7 != KEYPORT);
delay_ms(10);
while(0xf7 != KEYPORT);
return keyvalue;
}
KEYPORT = 0xfd;
if(0xfd != KEYPORT)
{
keyvalue = KEYPORT;
keyvalue &= 0xf0;
keyvalue += 0x0d;
while(0xfd != KEYPORT);
delay_ms(10);
while(0xfd != KEYPORT);
return keyvalue;
}
KEYPORT = 0xfb;
if(0xfd != KEYPORT)
{
keyvalue = KEYPORT;
keyvalue &= 0xf0;
keyvalue += 0x0b;
while(0xfb != KEYPORT);
delay_ms(10);
while(0xfb != KEYPORT);
return keyvalue;
}
}
}
return 0xff;
}
void main()
{
unsigned char key;
timer0_init();
while(1)
{
key = key_scan();
if(0xff != key)
{
tempdata[0] = segdata[key / 16];
tempdata[1] = segdata[key % 16];
}
}
}
单片机==矩阵键盘-物理键值(13)
最新推荐文章于 2023-10-23 16:58:30 发布