用8051单片机编程实现以下功能(1)系统上电后,按下按键,数码管可显示按键对应的字符,如按下“0”键,数码管 显示字符0,按下“F”键,数码管显示 F。 (2)模拟密码锁,设计程序。假设密码为“12AB”,输入 4 位密码后,若输入正确,则 数码管显示“P”;若输入错误,则数码管显示“E”,表示密码错误。在系统不断电情况下, 密码锁在上次开锁或者输入密码错误之后,能继续工作。
时间: 2023-05-28 17:06:27 浏览: 67
以下是8051单片机的程序代码实现:
(1)按键对应字符显示程序
```C
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71};
void delay(uint xms){
uint i, j;
for(i = xms; i > 0; i--)
for(j = 110; j > 0; j--);
}
void main(){
uchar key_value;
P1 = 0x00; // 数码管段选
P2 = 0xFF; // 按键检测
while(1){
if(P2 != 0xFF){
delay(10); // 消抖
if(P2 != 0xFF){
switch(P2){
case 0xFE: key_value = 0; break;
case 0xFD: key_value = 1; break;
case 0xFB: key_value = 2; break;
case 0xF7: key_value = 3; break;
case 0xEF: key_value = 4; break;
case 0xDF: key_value = 5; break;
case 0xBF: key_value = 6; break;
case 0x7F: key_value = 7; break;
case 0xFE: key_value = 8; break;
case 0xFD: key_value = 9; break;
case 0xFB: key_value = 10; break;
case 0xF7: key_value = 11; break;
case 0xEF: key_value = 12; break;
case 0xDF: key_value = 13; break;
case 0xBF: key_value = 14; break;
case 0x7F: key_value = 15; break;
default: key_value = 0xFF; break;
}
P1 = table[key_value]; // 显示对应字符
}
}
}
}
```
(2)密码锁程序
```C
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code password[] = {'1', '2', 'A', 'B'}; // 正确密码
uchar code table[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71};
void delay(uint xms){
uint i, j;
for(i = xms; i > 0; i--)
for(j = 110; j > 0; j--);
}
void main(){
uchar password_input[4]; // 输入密码
uchar password_index = 0; // 输入密码的下标
uchar i;
P1 = 0x00; // 数码管段选
P2 = 0xFF; // 按键检测
while(1){
if(P2 != 0xFF){ // 按键检测
delay(10); // 消抖
if(P2 != 0xFF){
switch(P2){
case 0xFE: password_input[password_index] = '0'; break;
case 0xFD: password_input[password_index] = '1'; break;
case 0xFB: password_input[password_index] = '2'; break;
case 0xF7: password_input[password_index] = '3'; break;
case 0xEF: password_input[password_index] = '4'; break;
case 0xDF: password_input[password_index] = '5'; break;
case 0xBF: password_input[password_index] = '6'; break;
case 0x7F: password_input[password_index] = '7'; break;
case 0xFE: password_input[password_index] = '8'; break;
case 0xFD: password_input[password_index] = '9'; break;
case 0xFB: password_input[password_index] = 'A'; break;
case 0xF7: password_input[password_index] = 'B'; break;
case 0xEF: password_input[password_index] = 'C'; break;
case 0xDF: password_input[password_index] = 'D'; break;
case 0xBF: password_input[password_index] = 'E'; break;
case 0x7F: password_input[password_index] = 'F'; break;
default: password_input[password_index] = 0xFF; break;
}
P1 = table[password_input[password_index] - '0']; // 显示输入字符
password_index++;
if(password_index == 4){ // 输入完4位密码
password_index = 0;
for(i = 0; i < 4; i++){
if(password_input[i] != password[i]){ // 密码错误
P1 = table['E' - '0'];
delay(1000);
break;
}
}
if(i == 4){ // 密码正确
P1 = table['P' - '0'];
delay(1000);
}
}
}
}
}
}
```
阅读全文