【Proteus仿真】51单片机矩阵按键+中断+数码管显示+串口输出
- 📌相关篇《【Proteus仿真】51单片机矩阵按键扫描+外部中断使用》
- 📍《【Proteus仿真】51单片机串口输出实验》
- 🎈 《【Proteus仿真】51单片机定时器计数+共阴数码管驱动显示》
- 🔖Proteus仿真基础实验综合篇 -
矩阵按键+中断+数码管显示+串口输出
。
- 🌿Proteus
8.12
平台 - 🌿本实验代码基于VSM Studio,采用SDCC编译器。
- 🎬仿真演示:
📝示例代码
#include <mcs51reg.h>
#include <stdio.h>
#define FOSC 11059200UL
#define BAUD 9600
#define T1MS (65536-FOSC/12/1000)
#define COL1 (0xEF)
#define COL2 (0xDF)
#define COL3 (0xBF)
#define COL4 (0x7F)
#define wei1 (P2_0)
#define wei2 (P2_1)
#define LED (P2_2)
unsigned char key_val[] = {
COL1, COL2, COL3, COL4
};
unsigned char __code key_data[] = {
0xEE, 0xED, 0xEB, 0xE7,
0xDE, 0xDD, 0xDB, 0xD7,
0xBE, 0xBD, 0xBB, 0xB7,
0x7E, 0x7D, 0x7B, 0x77,
};
unsigned char __code table[] = {
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f
};
unsigned char key_row = 0;
unsigned char key_col = 0;
unsigned char Button_value = 0;
void delayms(unsigned int ms)
{
unsigned int x;
unsigned int y;
for (y=ms; y>0; y--)
{
for (x=227; x>0; x--)
;
}
}
void uart_init()
{
SCON |= 0x50;
TMOD |= 0x20;
TH1 = (unsigned char)(((65536 - (FOSC/32/BAUD) )>>8) - 2) ;
TL1 = (unsigned char)(65536- (FOSC/32/BAUD) - 2) ;
TR1 = 1;
}
void SendData(char data1)
{
SBUF = data1;
while (!TI) ;
TI = 0;
}
void SendString(unsigned char *buffer)
{
unsigned char *p = buffer;
while (*p)
{
SendData(*p);
p++;
}
}
int putchar(int data1)
{
SendData(data1);
return data1;
}
void int0_init()
{
IT0 = 1;
EX0 = 1;
EA = 1;
}
void key_scan()
{
unsigned char i = 0;
for (i=0; i<4; i++)
{
key_col = key_val[i];
P1 = key_col;
delayms(2);
}
}
void key_value(unsigned char value)
{
unsigned char i = 0;
for (i=0; i<16; i++)
{
if (value == key_data[i])
{
Button_value = i + 1;
printf("Keyvalue:%d \r\n",Button_value);
}
}
}
void int0_isr(void) __interrupt 0 __using 1
{
EX0 = 0;
delayms(10);
key_row = P1;
key_value(key_row);
EX0 = 1;
}
void led_off()
{
wei1= 1;
wei2= 1;
}
void Display()
{
TR0 = 0;
wei1=0,wei2=1;
P0= table[Button_value/10];
delayms(6);
led_off();
delayms(1);
NOP();NOP();
wei1=1,wei2=0;
P0= table[Button_value%10];
delayms(6);
led_off();
delayms(1);
TR0 = 1;
}
void Init_Timer0(void)
{
TMOD |= 0x01;
TL0 = T1MS;
TH0 = T1MS >> 8;
TR0 = 1;
ET0 = 1;
EA = 1;
}
void Timer0_isr(void) __interrupt 1 __using 2
{
TL0 = T1MS;
TH0 = T1MS >> 8;
Display();
LED = ! LED;
}
void main(void)
{
Init_Timer0();
int0_init();
uart_init();
while (1)
{
key_scan();
}
}
📚仿真资源
链接: https:
提取码: vetd