这是是基于STM32F103ZET6的MFRC522读卡实验
程序正常运行时,LED0一闪一闪
读到卡片时,LED1灯点亮,并串口打印卡片类型和UID
接口定义如下:
SPI2_SCK PB13
SPI2_MISO PB14
SPI2_MOSI PB15
RCC522_NSS(SDA PB0
RCC522_RST(CE) PC4
LED0 PD13
LED1 PG14
stm32单片机源程序如下:
- #include "main.h"
- u8 Byte_HexDispaly(u8 dat)
- {
- if(dat>=10&&dat<16) dat+=0x07; //A-F +0x37
- dat+=0x30; //0-9 +0x30
- return dat;
- }
- void Uart1_SendByte(u8 ch)
- {
- while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
- USART1->DR = (u8) ch;
- }
- void Uart1_SendHexDisplay(u8 ch)
- {
- Uart1_SendByte(Byte_HexDispaly(ch>>4)); //高半字节
- Uart1_SendByte(Byte_HexDispaly(ch&0x0f)); //低半字节
- }
- void Uart1_SendString(char *s)
- {
- while (*s) //
- {
- Uart1_SendByte(*s++);
- }
- }
- //==============================================================================
- //读取卡的类型
- //读取卡的ID号
- //==============================================================================
- void ReaderCard(void)
- {
- if(PcdRequest(PICC_REQALL,Temp)==MI_OK) //选卡
- {
- if(Temp[0]==0x04&&Temp[1]==0x00)
- printf("MFOne-S50");
- else if(Temp[0]==0x02&&Temp[1]==0x00)
- printf("MFOne-S70");
- else if(Temp[0]==0x44&&Temp[1]==0x00)
- printf("MF-UltraLight");
- else if(Temp[0]==0x08&&Temp[1]==0x00)
- printf("MF-Pro");
- else if(Temp[0]==0x44&&Temp[1]==0x03)
- printf("MF Desire");
- else
- printf("Unknown");
- if(PcdAnticoll(UID)==MI_OK) //防冲撞
- {
- printf("Card Id is:");
- Uart1_SendHexDisplay(UID[0]);
- Uart1_SendHexDisplay(UID[1]);
- Uart1_SendHexDisplay(UID[2]);
- Uart1_SendHexDisplay(UID[3]);
- printf("\r\n"); //发送换行指令
-
- LED0=1;
- delay_ms(500);
- LED0=0;
- delay_ms(500);
- }
- else LED0=0; ;
- }
- }
- int main(void)
- {
- delay_init(); //延时函数初始化
- NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- uart_init(9600); //串口初始化为9600
- SPI2_Init();
- LED_Init(); //LED端口初始化
- KEY_Init();
-
- delay_ms(50);
- LED0=0;
- PcdReset();//复位RC522读卡器
- delay_ms(10);
-
- PcdReset();//复位RC522读卡器
- delay_ms(10);
-
- PcdAntennaOff();
- delay_ms(10);
- PcdAntennaOn();//开启天线发射
- printf("RFID-MFRC522 TEST\r\nFindCard Starting ...\r\n");
- while(1)
- {
- ReaderCard();
- delay_ms(200);
- LED1=!LED1;
-
- }
- }
复制代码
所有资料51hei提供下载:
65520772STM32-RC522.rar
(616.07 KB, 下载次数: 125)
|