单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "lcd.h"
- #include <stdio.h>
- #include "timer.h"
- #include "lcd1602.h"
- #include "key.h"
- #include "rc522.h"
- /**************/
- unsigned char idCard[4]={0x51,0xd5,0xc1,0x2e}; //有效卡号
- /**************/
- u8 rekey = 0;//按键防止重复
- u8 i;
- unsigned char UID[5]; //读取到的卡号
- unsigned char Temp[4] ;
- unsigned int relayLater =0 ;//延时计数
- unsigned char reportFlag = 0; //上报标志
- int main(void)
- {
- delay_init(); //延时函数初始化
- uart_init(9600); //串口初始化为9600
- TIM3_Int_Init(499,7199);//50ms
- LED_Init(); //初始化与LED连接的硬件接口
- KEY_Init(); //初始化按键
-
- relay1 = 1;
- delay_ms(200);
- relay1 =0 ;
-
- Rc522IoInit();
- PcdReset();//复位RC522
- PcdAntennaOn();//开启天线发射
- while(1)
- {
- if(key1==0) //检测到按键按下
- {
- delay_ms(10); //小抖动
- if(rekey==0)
- {
- if(key1==0) //检测是否按下
- {
- rekey=1;
- relay1 =1;//打开继电器
- relayLater = 30; //延时3s
- }
- }
- }
- else
- {rekey = 0;}//防止按键重复操作
- if(PcdRequest(0x52,Temp)==MI_OK)//读取到卡
- {
- if(PcdAnticoll(UID)==MI_OK)//卡号获取成功
- {
- if((UID[0]==idCard[0])&&(UID[2]==idCard[2])&&(UID[3]==idCard[3]))//匹配卡号
- {
- relay1 =1;//打开继电器
- relayLater = 30; //延时3s
- }
- delay_ms(10);
- }
- }
- if(reportFlag == 1)//计时操作标志
- {
- reportFlag = 0; //清空标志
- if(relayLater>0){relayLater--;relay1=1;}//倒计时
- else{relay1 = 0;}
- }
-
- }
- }
复制代码
所有资料51hei提供下载:
stm32-20、程序20181023.rar
(315.26 KB, 下载次数: 146)
|