|
该系统以STM32F103C8T6为核心(可自行修改成103系列其他芯片,修改编译参数)。通过OLED 0.96显示相关信息。通过对RFID模块(RC522)的控制,读取RFID标签的信息,并通过串口上传(可配套相应的上位机,完成简单的考勤,安保等系统)
单片机源程序如下:
- #include "stm32f10x.h"
- #include "oled.h"
- #include "MAIN.h"
- #include "mfrc522.h"
- #include "usart.h"
- #include "string.h"
- unsigned char card1[4] = {0xF0,0x1C,0x70,0x3B}; //卡序列号
- unsigned char card2[4] = {0xDE,0x8A,0xDF,0x2B}; //卡序列号
- unsigned char card3[4] = {0xD2,0xB2,0x73,0x5B};
- unsigned char card4[4] = {0xB7,0x29,0xDF,0x2B};
- unsigned char snr, buf[16]="liu-bridge",bufr[16], TagType[2], SelectedSnr[4], DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
- unsigned char Read_Data[16],PassWd[6],WriteData[16],RevBuffer[30], MLastSelectedSnr[4],NewKey[16];
- char cardID[4];
- unsigned char kahao;
- void findcard();
- void GPIO_ini()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /* Enable the GPIO Clock */
- RCC_APB2PeriphClockCmd(MF522_RST_CLK, ENABLE);
- /* Configure the GPIO pin */
- GPIO_InitStructure.GPIO_Pin = MF522_RST_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(MF522_RST_PORT, &GPIO_InitStructure);
-
- /* Enable the GPIO Clock */
- RCC_APB2PeriphClockCmd(MF522_MISO_CLK, ENABLE);
- /* Configure the GPIO pin */
- GPIO_InitStructure.GPIO_Pin = MF522_MISO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(MF522_MISO_PORT, &GPIO_InitStructure);
-
- /* Enable the GPIO Clock */
- RCC_APB2PeriphClockCmd(MF522_MOSI_CLK, ENABLE);
- /* Configure the GPIO pin */
- GPIO_InitStructure.GPIO_Pin = MF522_MOSI_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(MF522_MOSI_PORT, &GPIO_InitStructure);
-
- /* Enable the GPIO Clock */
- RCC_APB2PeriphClockCmd(MF522_SCK_CLK, ENABLE);
- /* Configure the GPIO pin */
- GPIO_InitStructure.GPIO_Pin = MF522_SCK_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(MF522_SCK_PORT, &GPIO_InitStructure);
-
- /* Enable the GPIO Clock */
- RCC_APB2PeriphClockCmd(MF522_NSS_CLK, ENABLE);
- /* Configure the GPIO pin */
- GPIO_InitStructure.GPIO_Pin = MF522_NSS_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(MF522_NSS_PORT, &GPIO_InitStructure);
-
- /* Enable the GPIO Clock */
- RCC_APB2PeriphClockCmd(LED_CLK, ENABLE);
- /* Configure the GPIO pin */
- GPIO_InitStructure.GPIO_Pin = LED_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(LED_PORT, &GPIO_InitStructure);
- }
- void InitializeSystem()
- {
- LED_OFF;
- delay_10ms(10);
- PcdReset();
- PcdAntennaOff();
- PcdAntennaOn();
- M500PcdConfigISOType( 'A' );
- LED_ON;
- delay_10ms(10);
- LED_OFF;
- delay_10ms(10);
- LED_ON;
- delay_10ms(10);
- LED_OFF;
- }
- int i,j;
- int main(void)
- {
- char status;
- unsigned char rfidflag;
- unsigned char discnt;
- kahao=0;
- GPIO_ini();
- InitializeSystem( );
- uart_init(9600);
- OLED_Init();
- //Draw_Logo();
- for(i=0;i<2000;i++)
- for(j=0;j<2000;j++);
- OLED_Fill(0x00);
- OLED_ShowString1(0,0," RFID EPC SYS");
- OLED_ShowString1(0,3," Status: OK");
- printf("\r\n This is a RFID System \r\n");
- while(1)
- { status= PcdRequest(REQ_ALL,TagType);
- if(!status)
- {
- status = PcdAnticoll(SelectedSnr);
- if(!status)
- {
- status=PcdSelect(SelectedSnr);
- if(!status)
- {
- snr = 1; //扇区号1
- status = PcdAuthState(KEYA, (snr*4+3), DefaultKey, SelectedSnr);// 校验1扇区密码,密码位于每一扇区第3块
- {
- if(!status)
- {
- //status = PcdRead((snr*4+0), buf); // 读卡,读取1扇区0块数据到buf[0]-buf[16]
- // buf[0]=2;buf[1]=2;buf[2]=3;buf[3]=4;buf[4]=5;buf[6]=1;;buf[7]=1;buf[8]=1;buf[9]=9;
- // status = PcdWrite((snr*4+0), buf); // 写卡,将buf[0]-buf[16]写入1扇区0块
- if(!status)
- {
- //读写成功,点亮LED
- LED_ON;
- PcdAuthState(KEYA, (snr*4+3), DefaultKey, SelectedSnr);
- status = PcdRead((snr*4+0), bufr);
-
- //printf(" %s \r\n",bufr );
- //printf("%s", SelectedSnr);
- findcard();
- if(kahao==1)
- { OLED_ShowString1(0,5," ID 1 Pass");
- printf("%s","#1001[ DISCUZ_CODE_0 ]quot;);
- discnt=0;
- rfidflag=1;
- }
- else if(kahao==2)
- { OLED_ShowString1(0,5," ID 2 Pass");
- printf("%s","#1002[ DISCUZ_CODE_0 ]quot;);
- discnt=0;
- rfidflag=1;
- }
- else if(kahao==3)
- { OLED_ShowString1(0,5," ID 3 Pass");
- printf("%s","#1003[ DISCUZ_CODE_0 ]quot;);
- discnt=0;
- rfidflag=1;
- }
- else if(kahao==4)
- { OLED_ShowString1(0,5," ID 4 Pass");
- printf("%s","#1004[ DISCUZ_CODE_0 ]quot;);
- discnt=0;
- rfidflag=1;
- }
- //printf(" %s %s",bufr,SelectedSnr);
- WaitCardOff();
- }
- }
- }
- }
- }
- }
- LED_OFF;
- if(rfidflag==1)
- { discnt++;
- if(discnt>200)
- { OLED_ShowString1(0,5," ");
- discnt=0;
- rfidflag=0;
- }
- OLED_DLY_ms(5);
- }
- }
- }
- void findcard()
- { unsigned char ii;
- for (ii=0;ii<4;ii++) //比较卡序列号
- { if (SelectedSnr[ii] != card1[ii])
- { kahao=0;
- break;
- }
- else
- { if (ii==3)
- { kahao=1;
- goto A;
- break;
- }
- }
- }
- for (ii=0;ii<4;ii++)
- { if (SelectedSnr[ii] != card2[ii])
- { kahao=0;
- break;
- }
- else
- { if (ii==3)
- { kahao=2;
- goto A;
- break;
- }
- }
- }
- for (ii=0;ii<4;ii++)
- { if (SelectedSnr[ii] != card3[ii])
- { kahao=0;
- break;
- }
- else
- { if (ii==3)
- { kahao=3;
- goto A;
- break;
- }
- }
- }
- for (ii=0;ii<4;ii++)
- { if (SelectedSnr[ii] != card4[ii])
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
STM32_RFID&OLED.rar
(300.1 KB, 下载次数: 818)
|
评分
-
查看全部评分
|