操作:
实现按下一个按键,对应的数码管和点阵显示相应的数字
代码:
/* *******************数码管按键综合项目********************* */
#include <reg52.h>
int local(unsigned int i);
sbit ADDR0 = P1^0; sbit ADDR1 = P1^1; sbit ADDR2 = P1^2; sbit ADDR3 = P1^3; sbit ENLED = P1^4;
sbit key1 = P2^4; sbit key2 = P2^5; sbit key3 = P2^6;
unsigned char leds[] = { 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E };
unsigned char ledss[10][8]={ {0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55}, {0xE7,0xE3,0xE7,0xE7,0xE7,0xE7,0xE7,0xC3}, {0x81,0x81,0x9F,0x81,0x81,0xF9,0x81,0x81}, {0x81,0x81,0x9F,0x81,0x81,0x9F,0x81,0x81}, {0x8F,0x87,0x93,0x99,0x9C,0x00,0x00,0x9F}, {0x81,0x81,0xF9,0x81,0x81,0x9F,0x81,0x81}, {0x81,0x81,0xF9,0x81,0x81,0x99,0x81,0x81}, {0x83,0x83,0x9F,0x9F,0x9F,0x9F,0x9F,0x9F}, {0x81,0x81,0x99,0x81,0x81,0x99,0x81,0x81}, {0x81,0x81,0x99,0x81,0x81,0x9F,0x81,0x81}, };
unsigned int locals = 0;
void main() {
ADDR3 = 0;
ENLED = 0;
EA = 1;
TMOD = 0x01;
ET0 = 1;
TH0 = 0xFC;
TL0 = 0x67;
TR0 = 1;
//开启九位输入键,其它键关闭
P2 = 0xF1;
while(1) {
//得到键位所在KeyIn1输入列
if(key1==0)
{ locals = local(1);
TR0 = 1;
}
//得到键位所在KeyIn2输入列
else if(key2==0) {
locals = local(2);
TR0 = 1; }
//得到键位所在KeyIn3输入列
else if(key3==0) {
locals = local(3);
TR0 = 1;
}
}
//对应键位赋值数码管
}
void InterruptTimer0() interrupt 1 {
static unsigned char i = 0;
TH0 = 0xFC; TL0 = 0x67; P0 = 0xFF; ADDR3 = 0;
switch(i) {
case 0: ADDR0 = 0;ADDR1 = 0;ADDR2 = 0; P0 = ledss[locals][i];i++;break;
case 1: ADDR0 = 1;ADDR1 = 0;ADDR2 = 0; P0 = ledss[locals][i];i++;break;
case 2: ADDR0 = 0;ADDR1 = 1;ADDR2 = 0; P0 = ledss[locals][i];i++;break;
case 3: ADDR0 = 1;ADDR1 = 1;ADDR2 = 0; P0 = ledss[locals][i];i++;break;
case 4: ADDR0 = 0;ADDR1 = 0;ADDR2 = 1; P0 = ledss[locals][i];i++;break;
case 5: ADDR0 = 1;ADDR1 = 0;ADDR2 = 1; P0 = ledss[locals][i];i++;break;
case 6: ADDR0 = 0;ADDR1 = 1;ADDR2 = 1; P0 = ledss[locals][i];i++;break;
case 7: ADDR0 = 1;ADDR1 = 1;ADDR2 = 1; P0 = ledss[locals][i];i++;break;
case 8: ADDR3 = 1;ADDR0 = 0;ADDR1 = 0;ADDR2 = 0; P0 = leds[locals];i++;break;
case 9: ADDR3 = 1;ADDR0 = 1;ADDR1 = 0;ADDR2 = 1; P0 = leds[locals];i=0;break; default: break; } }
int local(unsigned int i) {
unsigned int b;
//判断当前键位
switch(i) {
//得到键位所在KeyIn1输入列
case 1:
P2 = 0xF7; //仅仅开启out1
if(key1 == 0){ b = 1; }
P2 = 0xFB; //仅仅开启out2
if(key1 == 0){ b = 4; }
P2 = 0xFD; //仅仅开启out3
if(key1 == 0){ b = 7; } break;
//得到键位所在KeyIn2输入列
case 2:
P2 = 0xF7; //仅仅开启out1
if(key2 == 0){ b = 2; }
P2 = 0xFB; //仅仅开启out2
if(key2 == 0){ b = 5; }
P2 = 0xFD; //仅仅开启out3
if(key2 == 0){ b = 8; }
break;
//得到键位所在KeyIn3输入列
case 3:
P2 = 0xF7; //仅仅开启out1
if(key3 == 0){ b = 3; }
P2 = 0xFB; //仅仅开启out2
if(key3 == 0){ b = 6; }
P2 = 0xFD; //仅仅开启out3
if(key3 == 0){ b = 9; }
break;
default: break; }
//重置开启九位输入键,其它键关闭
P2 = 0xF1;
return b; }