|
姓名: 張? 李四 王五
張三不能正常顯示三,其他還沒(méi)有嘗試
以下是我的代碼
#include <reg52.h>
#include "lcd1602.h"
#include "mfrc522.h"
#include "uart.h"
#include <stdio.h>
// 定義蜂鳴器控制端口
sbit BUZZER = P3^6;
// 系統(tǒng)時(shí)鐘頻率(Hz),用于計(jì)算延時(shí)
#define FOSC 11059200
// 學(xué)生信息結(jié)構(gòu)體定義
typedef struct {
unsigned char card_id[4]; // 卡片ID
unsigned char student_id[10]; // 學(xué)生學(xué)號(hào)
unsigned char name[20]; // 學(xué)生姓名
} StudentInfo;
// 預(yù)定義學(xué)生信息 (3名學(xué)生)
StudentInfo students[3] = {
{{0xE4, 0xCA, 0x18, 0x05}, "2501", "張三"},
{{0x16, 0x41, 0xC0, 0x01}, "2502", "李四"},
{{0x70, 0x02, 0x53, 0xF4}, "2503", "王五"},
};
// 定義卡片數(shù)量
#define CARD_COUNT 3
// 定義系統(tǒng)狀態(tài)
#define SYSTEM_IDLE 0 // 空閑狀態(tài)
#define SYSTEM_SUCCESS 1 // 簽到成功狀態(tài)
#define SYSTEM_FAILURE 2 // 簽到失敗狀態(tài)
volatile unsigned char systemState = SYSTEM_IDLE;
volatile unsigned int displayTimer = 0; // 顯示計(jì)時(shí)器
// 定義卡片數(shù)據(jù)緩沖區(qū)
unsigned char card_data[16];
unsigned char current_card_id[4];
unsigned char display_card_id_str[13]; // 顯示用卡號(hào)字符串
// 存儲(chǔ)當(dāng)前學(xué)生信息
unsigned char current_student_id[10]; // 當(dāng)前學(xué)生學(xué)號(hào)
unsigned char current_student_name[20]; // 當(dāng)前學(xué)生姓名
// 蜂鳴器狀態(tài)定義
#define BEEP_IDLE 0 // 空閑狀態(tài)
#define BEEP_ON 1 // 蜂鳴器開(kāi)啟
#define BEEP_OFF 2 // 蜂鳴器關(guān)閉
volatile unsigned char beepState = BEEP_IDLE; // 蜂鳴器狀態(tài)
volatile unsigned char beepCount = 0; // 剩余鳴叫次數(shù)
volatile unsigned int beepTimer = 0; // 計(jì)時(shí)計(jì)數(shù)器
// 蜂鳴器發(fā)聲函數(shù) - 成功時(shí)短鳴
void beep_success(void) {
unsigned int i, j;
for(i = 0; i < 1000; i++) {
BUZZER = ~BUZZER;
for(j = 0; j < 30; j++); // 約0.5ms延時(shí),1000次約500ms
}
BUZZER = 0; // 確保蜂鳴器最終關(guān)閉
}
/**
* 蜂鳴器失敗提示音 - 短鳴三聲(終極優(yōu)化版)
* 通過(guò)精確計(jì)時(shí)確保三聲?shū)Q叫一致且立即停止
*/
void beep_failure(void) {
if(beepState == BEEP_IDLE) { // 僅在空閑狀態(tài)下啟動(dòng)
beepState = BEEP_ON; // 切換到開(kāi)啟狀態(tài)
beepCount = 3; // 設(shè)置鳴響3次
beepTimer = 500; // 首次鳴響500ms
}
}
// 蜂鳴器任務(wù)
void beepTask(void) {
static unsigned int beepCounter = 0; // 新增計(jì)數(shù)器,跟蹤精確計(jì)時(shí)
// 如果處于空閑狀態(tài),直接返回,不執(zhí)行任何操作
if(beepState == BEEP_IDLE) {
return;
}
switch(beepState) {
case BEEP_ON:
BUZZER = ~BUZZER; // 翻轉(zhuǎn)電平發(fā)聲
// 精確計(jì)時(shí)控制
if(++beepCounter >= 200) { // 500次計(jì)數(shù)約500ms
beepCounter = 0;
BUZZER = 0; // 關(guān)閉蜂鳴器
// 檢查是否為最后一聲
if(beepCount == 1) {
beepState = BEEP_IDLE; // 最后一聲后直接進(jìn)入空閑狀態(tài)
BUZZER = 0; // 雙重確保蜂鳴器關(guān)閉
} else {
beepState = BEEP_OFF; // 否則進(jìn)入間隔狀態(tài)
beepTimer = 300; // 間隔300ms
}
}
break;
case BEEP_OFF:
if(++beepCounter >= 300) { // 300次計(jì)數(shù)約300ms
beepCounter = 0;
beepCount--; // 剩余次數(shù)減1
beepState = BEEP_ON; // 繼續(xù)鳴響
beepTimer = 500; // 鳴響500ms
}
break;
default:
beepCounter = 0; // 重置計(jì)數(shù)器
beepState = BEEP_IDLE;
BUZZER = 0; // 確保蜂鳴器關(guān)閉
break;
}
}
// 顯示歡迎信息
void display_welcome(void) {
LCDInit();
DisplayListChar(0, 0, "Student Attend");
DisplayListChar(0, 1, "Place Card Here");
}
// 顯示狀態(tài)處理函數(shù)
void displayTask(void) {
switch(systemState) {
case SYSTEM_SUCCESS:
if(displayTimer > 0) {
displayTimer--;
} else {
systemState = SYSTEM_IDLE;
display_welcome();
}
break;
case SYSTEM_FAILURE:
if(displayTimer > 0) {
displayTimer--;
} else {
systemState = SYSTEM_IDLE;
display_welcome();
}
break;
default: break;
}
}
// 查找學(xué)生信息
bit find_student_info(unsigned char *card_id) {
unsigned char i, j;
// 遍歷所有學(xué)生信息
for(i = 0; i < CARD_COUNT; i++) {
// 逐個(gè)字節(jié)比較卡號(hào)
for(j = 0; j < 4; j++) {
if(card_id[j] != students[i].card_id[j]) {
break;
}
}
// 如果4個(gè)字節(jié)都匹配,找到學(xué)生信息
if(j == 4) {
// 復(fù)制學(xué)生ID和姓名
for(j = 0; j < 10 && students[i].student_id[j] != '\0'; j++) {
current_student_id[j] = students[i].student_id[j];
}
current_student_id[j] = '\0';
for(j = 0; j < 20 && students[i].name[j] != '\0'; j++) {
current_student_name[j] = students[i].name[j];
}
current_student_name[j] = '\0';
return 1;
}
}
// 未找到匹配的學(xué)生信息
return 0;
}
// 顯示簽到成功信息
void display_success(unsigned char *card_id) {
unsigned char i, j;
// 轉(zhuǎn)換卡號(hào)為字符串(保持LCD顯示不變)
j = 0;
for(i = 0; i < 4; i++) {
display_card_id_str[j++] = (card_id[i] >> 4) + ((card_id[i] >> 4) < 10 ? '0' : 'A' - 10);
display_card_id_str[j++] = (card_id[i] & 0x0F) + ((card_id[i] & 0x0F) < 10 ? '0' : 'A' - 10);
display_card_id_str[j++] = ' ';
}
display_card_id_str[j] = '\0';
// 發(fā)出成功提示音
beep_success();
// 設(shè)置顯示狀態(tài)
LCDInit();
DisplayListChar(0, 0, "Attend Success!");
DisplayListChar(0, 1, display_card_id_str);
systemState = SYSTEM_SUCCESS;
displayTimer = 5000; // 5000個(gè)滴答 = 約5秒
// 查找學(xué)生信息
if(find_student_info(card_id)) {
// 通過(guò)串口發(fā)送學(xué)生信息
Uart_Send_String("簽到成功 - ");
Uart_Send_String("學(xué)號(hào): ");
Uart_Send_String(current_student_id);
Uart_Send_String(" 姓名: ");
Uart_Send_String(current_student_name);
Uart_Send_String("\r\n");
} else {
// 未找到學(xué)生信息,發(fā)送卡號(hào)
Uart_Send_String("簽到成功 - 卡號(hào): ");
for(i = 0; i < 4; i++) {
Sent_UART((card_id[i] >> 4) + ((card_id[i] >> 4) < 10 ? '0' : 'A' - 10));
Sent_UART((card_id[i] & 0x0F) + ((card_id[i] & 0x0F) < 10 ? '0' : 'A' - 10));
Sent_UART(' ');
}
Sent_UART('\r');
Sent_UART('\n');
}
}
// 顯示簽到失敗信息
void display_failure(unsigned char *card_id) {
unsigned char i, j;
// 轉(zhuǎn)換卡號(hào)為字符串
j = 0;
for(i = 0; i < 4; i++) {
display_card_id_str[j++] = (card_id[i] >> 4) + ((card_id[i] >> 4) < 10 ? '0' : 'A' - 10);
display_card_id_str[j++] = (card_id[i] & 0x0F) + ((card_id[i] & 0x0F) < 10 ? '0' : 'A' - 10);
display_card_id_str[j++] = ' ';
}
display_card_id_str[j] = '\0';
// 發(fā)出失敗提示音
beep_failure();
// 設(shè)置顯示狀態(tài)
LCDInit();
DisplayListChar(0, 0, "Invalid Card!");
DisplayListChar(0, 1, display_card_id_str);
systemState = SYSTEM_FAILURE;
displayTimer = 5000; // 5000個(gè)滴答 = 約5秒
// 通過(guò)串口發(fā)送未授權(quán)卡片信息
Uart_Send_String("未授權(quán)卡片 - 卡號(hào): ");
for(i = 0; i < 4; i++) {
Sent_UART((card_id[i] >> 4) + ((card_id[i] >> 4) < 10 ? '0' : 'A' - 10));
Sent_UART((card_id[i] & 0x0F) + ((card_id[i] & 0x0F) < 10 ? '0' : 'A' - 10));
Sent_UART(' ');
}
Sent_UART('\r');
Sent_UART('\n');
}
// 核對(duì)卡片ID是否合法
bit check_card_id(unsigned char *card_id) {
unsigned char i, j;
// 遍歷所有學(xué)生信息
for(i = 0; i < CARD_COUNT; i++) {
// 逐個(gè)字節(jié)比較卡號(hào)
for(j = 0; j < 4; j++) {
if(card_id[j] != students[i].card_id[j]) {
break;
}
}
// 如果4個(gè)字節(jié)都匹配,返回合法
if(j == 4) {
return 1;
}
}
// 未找到匹配的卡片,返回不合法
return 0;
}
// 主函數(shù)
void main(void) {
unsigned char card_detected = 0;
// 初始化系統(tǒng)
PcdReset(); // 初始化RC522
Uart_Init(); // 初始化串口
LCDInit(); // 初始化LCD
BUZZER = 0; // 初始化蜂鳴器端口
// 串口提示系統(tǒng)啟動(dòng)
Uart_Send_String("RFID Attendance System Started\r\n");
display_welcome(); // 顯示歡迎信息
while(1) {
// 處理蜂鳴器狀態(tài)
beepTask();
// 處理顯示狀態(tài)
displayTask();
// 檢測(cè)卡片(僅在空閑狀態(tài)下檢測(cè))
if(systemState == SYSTEM_IDLE &&
PcdRequest(PICC_REQIDL, &card_data[0]) == MI_OK) {
// 防沖突,獲取卡片序列號(hào)
if(PcdAnticoll(current_card_id) == MI_OK) {
// 選卡
if(PcdSelect(current_card_id) == MI_OK) {
card_detected = 1;
// 核對(duì)卡片ID
if(check_card_id(current_card_id)) {
// 合法卡片,顯示簽到成功
display_success(current_card_id);
} else {
// 非法卡片,顯示失敗
display_failure(current_card_id);
}
// 讓卡片進(jìn)入休眠狀態(tài)
PcdHalt();
}
}
}
// 延時(shí)降低CPU使用率(精確延時(shí))
delay_ms(1); // 1ms延時(shí),提高響應(yīng)速度
}
}
|
|