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) { // 假設低電平表示
|