设计介绍
这是一个基于STM32的RFID-RC522门禁系统,LCD1602显示当前状态,当检测到IC卡时,如果这个IC卡已经登记过,LCD1602上会显示IC卡的卡号,继电器打开,如果没有登记则报警。部分代码如下,需要源码的朋友可以在文章下方链接下载。
#include "sys.h"
#include "delay.h"
#include "lcd.h"
#include "led.h"
#include "usart.h"
#include "dht11.h"
#include "key.h"
#include "beep.h"
#include <stdio.h>
#include "stmflash.h"
#include "timer.h"
#include "exit.h"
#include "rc522.h"
void show1(void);
void show2(void);
void show3(void);
void show4(void);
void show5(void);
void show21(void);
void show31(void);
void show8(void);
u8 read_up=1;
u8 set1=1;
/*这三个标志位是用于按键的扫描程序*/
u8 key0_flag=0;
u8 key1_flag=0;
u8 key2_flag=0;
u8 beep_switch=0;
u8 beep_num=0;
u8 beepup_num=0;
u8 open_flag=0;
u8 down_flag=0;
u8 err_beepnum=0;
u8 table1[]="Please Bash Card";
u8 table11[]=" (()) ";
u8 table2[]=" Register Mode ";
u8 table22[]=" (()) ";
u8 table222[]="Register Success";
u8 table3[]=" Clear Mode ";
u8 table33[]=" Logout Success ";
u8 table5[]=" Door open ";
u8 table6[]=" Door close ";
u8 table8[]=" Dead Lock s ";
u8 err_num=0;
u8 eee_flag=0;
int kg_flag=0;//定时器4累加标志位,用于当RFID扫描到了并对门做出了相应的动作后,返回到扫描ID卡的计时标志位
u8 tim2_flag=0;//定时器2累加标志位,用于当按键按下之后,并完成了注册或注销卡之后,返回到扫描ID卡的计时标志位
u8 show_flag=0;//记录显示那个界面的标志位
unsigned char num;
unsigned char key_flag;//用于标识当前系统处于,扫描ID卡状态还是注册ID卡状态还是注销ID卡状态
unsigned char i;
unsigned int temp;
unsigned char status;
unsigned char g_ucTempbuf[20];
unsigned char g_ucTempbufid[20];//用于记录下来当前扫描到的ID
unsigned char lcd[]="0123456789ABCDEF";//用于显示ID时的16进制
unsigned char dataerr[16] = {0x11,0x11,0x11,0x78,0xED,0xCB,0xA9,0x87,0x12,0x34,0x56,0x78,0x01,0xFE,0x01,0xFE};//ID卡中错误的密码数据
unsigned char data1[16] = {0x12,0x34,0x56,0x78,0xED,0xCB,0xA9,0x87,0x12,0x34,0x56,0x78,0x01,0xFE,0x01,0xFE};//ID卡中正确的密码数据
unsigned char data2[4] = {0,0,0,0x01};
unsigned char DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
u8 RC522_flag=1;
u8 RC522_flag1=1;
u8 RC522_flag2=1;
u8 RC522_flag3=0;
/*按键的扫描函数*/
/*用于判断系统应该显示哪一个界面*/
int main(void)
{
unsigned char status,i;
unsigned int temp;
delay_init();
LCD1602_Init();
KEY_Init();
BEEP_Init();
LED_Init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(115200);
RC522_Init();
PcdReset();
PcdAntennaOff();
delay_ms(10);
PcdAntennaOn();
delay_ms(10);
TIM2_Int_Init(4999,7199);//10Khz的计数频率,计数到5000为500ms
TIM3_Int_Init(499,7199);//10Khz的计数频率,计数到5000为500ms
TIM4_Int_Init(4999,7199);//10Khz的计数频率,计数到5000为500ms
printf("Start \r\n");
key_flag=0;
show_flag=1;
beep_switch=0;
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
while(1)
{
// printf("%d",RC522_flag1);
show_num();
down_flag=0;
if(kg_flag==6 && err_num<3)
{
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
kg_flag=0;
TIM_Cmd(TIM4, DISABLE); //使能TIMx
GPIO_ResetBits(GPIOB,GPIO_Pin_11);
key_flag=0;
show_flag=1;
open_flag=0;
down_flag=0;
}
else if(kg_flag==360 && err_num==3)
{
kg_flag=0;
err_beepnum=0;
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
TIM_Cmd(TIM4, DISABLE); //使能TIMx
key_flag=0;
show_flag=1;
err_num=0;
}
if(tim2_flag==4)
{
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
tim2_flag=0;
TIM_Cmd(TIM2, DISABLE); //使能TIMx
key_flag=0;
show_flag=1;
}
if(key_flag==0)
{
status = PcdRequest(PICC_REQALL, g_ucTempbuf);//寻卡
if (status != MI_OK)
{
PcdReset();
PcdAntennaOff();
PcdAntennaOn();
printf("寻卡失败\r\n");
if(RC522_flag3<3) RC522_flag3++;
if(RC522_flag3==4)RC522_flag3=3;
continue;
}
else
{
if(RC522_flag3==3)
{
RC522_flag3=4;
RC522_flag=1;
}
}
if(RC522_flag==1)
{
RC522_flag=0;
printf("1231231231312312312312312312\r\n");
status = PcdAnticoll(g_ucTempbuf);//防冲撞
if(status != MI_OK)
{ printf("寻卡失败1"); continue; }
printf("卡序列号:"); //超级终端显示,
for(i=0;i<4;i++)
{
temp=g_ucTempbuf[i];
g_ucTempbufid[i]=temp;
printf("%X",temp);
}
status = PcdSelect(g_ucTempbuf);//选定卡片
if (status != MI_OK)
{ printf("寻卡失败2"); continue; }
status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, g_ucTempbuf);//验证卡片密码
if (status != MI_OK)
{ printf("寻卡失败3"); continue; }
status = PcdRead(1, g_ucTempbuf);//读块
if (status != MI_OK)
{ printf("寻卡失败4"); continue; }
printf("卡读块:"); //超级终端显示,
for(i=0;i<16;i++)
{
temp=g_ucTempbuf[i];
printf("%X",temp);
}
num=0;
for(i=0;i<16;i++)
{
if(data1[i]==g_ucTempbuf[i])
{num++;}
printf("\nnum %d\n",num);
}
if(num!=16)
{
err_num++;
if(err_num<3)
{
//不开门
show_flag=4;
kg_flag=0;
open_flag=2;
printf("downflag%d\n",down_flag);
TIM_Cmd(TIM4, ENABLE); //使能TIMx
}
}
else
{
//开门
err_num=0;
show_flag=5;
kg_flag=0;
open_flag=1;
TIM_Cmd(TIM4, ENABLE); //使能TIMx
GPIO_SetBits(GPIOB,GPIO_Pin_11);
}
}
//printf("\n");
PcdHalt();
}
else if(key_flag==1)
{
TIM_Cmd(TIM2, DISABLE); //使能TIMx
TIM_Cmd(TIM4, DISABLE); //使能TIMx
GPIO_ResetBits(GPIOB,GPIO_Pin_11);
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
show_flag=2;
status = PcdRequest(PICC_REQALL, g_ucTempbuf);//寻卡
if (status != MI_OK)
{
PcdReset();
PcdAntennaOff();
PcdAntennaOn();
key_flag=1; printf("寻卡失败111\r\n");RC522_flag1=1;
continue;
}
if(RC522_flag1==1)
{
RC522_flag3=0;
RC522_flag1=0;
printf("1231231231312312312312312312\r\n");
printf("卡的类型:");
for(i=0;i<2;i++)
{
temp=g_ucTempbuf[i];
printf("%X",temp);
}
status = PcdAnticoll(g_ucTempbuf);//防冲撞
if(status != MI_OK)
{
printf("防冲撞失败");
}
else
{
printf("卡序列号:"); //超级终端显示,
for(i=0;i<4;i++)
{
temp=g_ucTempbuf[i];
printf("%X",temp);
}
status = PcdSelect(g_ucTempbuf);//选定卡片
if (status != MI_OK)
{
printf("选定卡失败");
}
else
{
status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, g_ucTempbuf);//验证卡片密码
if (status != MI_OK)
{
printf("密码错误");
}
else
{
status = PcdWrite(1, data1);//写块
if (status != MI_OK)
{
printf("写错");
}
else
{
key_flag=3;
show_flag=6;
tim2_flag=0;
TIM_Cmd(TIM2, ENABLE); //使能TIMx
}
}
}
}
}
PcdHalt();
//
//RC522_flag1=1;
}
else if(key_flag==2)
{
TIM_Cmd(TIM2, DISABLE); //使能TIMx
TIM_Cmd(TIM4, DISABLE); //使能TIMx
GPIO_ResetBits(GPIOB,GPIO_Pin_11);
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
show_flag=3;
status = PcdRequest(PICC_REQALL, g_ucTempbuf);//寻卡
if (status != MI_OK)
{
PcdReset();
PcdAntennaOff();
PcdAntennaOn();
printf("寻卡失败\r\n");
key_flag=2;
RC522_flag2=1;
continue;
}
if(RC522_flag2==1)
{
RC522_flag3=0;
RC522_flag2=0;printf("1231231231312312312312312312\r\n");
printf("卡的类型:");
for(i=0;i<2;i++)
{
temp=g_ucTempbuf[i];
printf("%X",temp);
}
status = PcdAnticoll(g_ucTempbuf);//防冲撞
if(status != MI_OK)
{
printf("防冲撞失败");
}
else
{
printf("卡序列号:"); //超级终端显示,
for(i=0;i<4;i++)
{
temp=g_ucTempbuf[i];
printf("%X",temp);
}
status = PcdSelect(g_ucTempbuf);//选定卡片
if (status != MI_OK)
{
printf("选定卡失败");
}
else
{
status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, g_ucTempbuf);//验证卡片密码
if (status != MI_OK)
{
printf("密码错误");
}
else
{
status = PcdWrite(1, dataerr);//写块
if (status != MI_OK)
{
printf("写错");
}
else
{
key_flag=3;
show_flag=7;
tim2_flag=0;
TIM_Cmd(TIM2, ENABLE); //使能TIMx
}
}
}
}
}
PcdHalt();
}
else if(key_flag==3)
{
}
}
}