|
keil程序
#include <reg51.h> // 51单片机头文件
#include <stdio.h> // 用于串口通信相关功能
// 定义引脚
sbit LCD_RS = P2^0; // 假设LCD的RS引脚接在P2.0
sbit LCD_RW = P2^1; // 假设LCD的RW引脚接在P2.1
sbit LCD_EN = P2^2; // 假设LCD的EN引脚接在P2.2
// 假设密码按键引脚
sbit KEY1 = P3^0;
sbit KEY2 = P3^1;
// 假设刷卡模块数据引脚
sbit CARD_DATA = P3^2;
sbit LOCK = P1^0; // 假设控制门锁的引脚接在P1.0
// 定义密码数组,这里假设4位密码
unsigned char password[4] = {1, 2, 3, 4};
unsigned char inputPassword[4];
unsigned char passwordIndex = 0;
bit isCardValid = 0; // 刷卡是否有效标志位
// 延时函数
void delay(unsigned int time) {
unsigned int i, j;
for(i = time; i > 0; i--)
for(j = 110; j > 0; j--);
}
// LCD相关函数
void LCD_WriteCommand(unsigned char command) {
LCD_RS = 0;
LCD_RW = 0;
P0 = command;
delay(5);
LCD_EN = 1;
delay(5);
LCD_EN = 0;
}
void LCD_WriteData(unsigned char data) {
LCD_RS = 1;
LCD_RW = 0;
P0 = data;
delay(5);
LCD_EN = 1;
delay(5);
LCD_EN = 0;
}
void LCD_Init() {
LCD_WriteCommand(0x38); // 8位模式,2行显示,5x7字体
LCD_WriteCommand(0x0C); // 显示开,光标关
LCD_WriteCommand(0x06); // 光标自动右移
LCD_WriteCommand(0x01); // 清屏
delay(2);
}
// 密码按键扫描函数
void KeyScan() {
if(KEY1 == 0) { // 假设KEY1为数字1按键
delay(10); // 消抖
if(KEY1 == 0) {
while(!KEY1); // 等待按键释放
inputPassword[passwordIndex++] = 1;
if(passwordIndex >= 4) {
// 验证密码
if(inputPassword[0] == password[0] && inputPassword[1] == password[1] &&
inputPassword[2] == password[2] && inputPassword[3] == password[3]) {
// 密码正确,开门等操作
LOCK = 0; // 开锁
// 记录出入记录(这里简单示例,实际可通过串口等方式存储到外部设备)
printf("Password Correct, Access Granted\n");
} else {
// 密码错误提示
printf("Password Incorrect\n");
}
passwordIndex = 0;
}
}
}
// 类似处理其他按键
}
// 刷卡模块数据处理函数
void CardScan() {
if(CARD_DATA == 0) { // 假设低电平表示
|
|