STM32RCT6+RC522+OLED, RFID class is equipped with an access control system

STM32RCT6+RC522+OLED, swipe the card to display the card number

Only so much has been done for the time being, and it will continue to be updated later

This article mainly provides ideas, detailed code can leave a message email, I will send it one by one.

preparation

Prepare the picture on the left c Documents

Tip: the following is the main content of this article. The following cases can be used for reference

1, RC522


2, OLED

3, Main function

Interpretation of key codes

		u8 UID_str[10] = {'0','0','0','0','0','0','0','0','\0',0};

		UID_str[0] = ((g_ucTempbuf[0]>>4)>9)	?(g_ucTempbuf[0]>>4)-10+'a'	:(g_ucTempbuf[0]>>4)+'0';
		UID_str[1] = ((g_ucTempbuf[0]&0x0f)>9)?(g_ucTempbuf[0]&0x0f)-10+'a':(g_ucTempbuf[0]&0x0f)+'0';
		UID_str[2] = ((g_ucTempbuf[1]>>4)>9)	?(g_ucTempbuf[1]>>4)-10+'a'	:(g_ucTempbuf[1]>>4)+'0';
		UID_str[3] = ((g_ucTempbuf[1]&0x0f)>9)?(g_ucTempbuf[1]&0x0f)-10+'a':(g_ucTempbuf[1]&0x0f)+'0';
		UID_str[4] = ((g_ucTempbuf[2]>>4)>9)	?(g_ucTempbuf[2]>>4)-10+'a'	:(g_ucTempbuf[2]>>4)+'0';
		UID_str[5] = ((g_ucTempbuf[2]&0x0f)>9)?(g_ucTempbuf[2]&0x0f)-10+'a':(g_ucTempbuf[2]&0x0f)+'0';
		UID_str[6] = ((g_ucTempbuf[3]>>4)>9)	?(g_ucTempbuf[3]>>4)-10+'a'	:(g_ucTempbuf[3]>>4)+'0';
		UID_str[7] = ((g_ucTempbuf[3]&0x0f)>9)?(g_ucTempbuf[3]&0x0f)-10+'a':(g_ucTempbuf[3]&0x0f)+'0';
		UID_str[9] = 1;

This section is mainly introduced
Because our UID is 4 bytes (for example: 07 87 E8 76)
We mainly use void OLED in OLED_ ShowChar(u8 x,u8 y,u8 chr); void OLED_ ShowString(u8 x,u8 y, u8 *p); Two functions
However, two hexadecimal digits are two characters, and we need to display them separately on the OLED screen
At this time, we need to be divided into high and low to display
As we know, MCU only stores binary data
Therefore, 0xE8 corresponds to binary number 1110 1000
To get the high position, we need to shift 0xE8 right by 4 bits to get 0x0E
To get the low order, we do the and (&) operation on 0xE8 and 0x0f to get 0x08
Then put the high and low bits of each byte of four byte data in the UID_str array
UID_ The ninth bit of STR array is the stop bit, which is in OLED_ It can be seen from the showstring function that the premise of continuing to print the string content is that the current read place is not '\ 0'
UID_ The tenth bit of STR array is the flag bit. When the flag is 0, the contents of the array will not be printed, and when the flag is 1, it will be printed

if(UID_str[9] != 0)
OLED_ShowString(32,2,UID_str);

Then we convert the numerical value into ASCII through the ternary operator operation, when printing a, b, C Some changes need to be made when F
The number we get can be used as an offset. We want to output 8, which is to use the ASCII of character 0 plus 8
However, when we output b, the offset is 11, so we need to subtract 10 from the offset and add the ASCII value of character a to achieve the effect of outputting character b

#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "rc522.h"
#include "led.h"
#include "key.h"
#include "exti.h" 
#include "bmp.h"
#include "oled.h"
#include "pwm.h"


/**
*   Connection Description:
*   1--SDA  <----->PA4
*   2--SCK  <----->PA5
*   3--MOSI <----->PA7
*   4--MISO <----->PA6
*   5--Hang in the air
*   6--GND <----->GND
*   7--RST <----->PB0
*   8--VCC <----->VCC
**/

unsigned char g_ucTempbuf[20];

int main(void)
{
	
	unsigned char status,i,j=0;
	unsigned int temp;
  u8 num = 0;
	u8 KEY_A[6]= {0xff,0xff,0xff,0xff,0xff,0xff};
	u8 data[16] = {0};
	u8 UID[4]= {0x49, 0xe0, 0x05, 0x98};
	u8 UID_str[10] = {'0','0','0','0','0','0','0','0','\0',0};
  delay_init();	    	 //Delay function initialization
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//Set the interrupt priority group as 2: 2-bit preemptive priority and 2-bit response priority
  uart_init(9600);	 //The serial port is initialized to 9600
  LED_Init();
  RC522_Init();       //Initialize RF card module
	//KEY_Init();           	// Initialize the hardware interface connected with the key
	
 	EXTIX_Init();		//External interrupt initialization  
	//NVIC_Configuration();  	 // Set NVIC interrupt packet 2: 2-bit preemption priority and 2-bit response priority 	 LED_Init(); 			     // Led port initialization
	OLED_Init();			//Initialize OLED  
	OLED_Clear()  	; 
  LED0=0;				//Turn on the LED
//	TIM4_PWM_Init(199,7199);// No frequency division. PWM frequency = 72000/(899+1)=80Khz 
	
	delay_ms(3000);

	
	
	
	// UID is the UID key of the card you want to modify_ Type: 0 is KEYA, non-0 is keyb, key is key RW:1 is read, 0 is write data_addr is the modified address and data is the data content
	IC_RW ( UID, 0, KEY_A, 1, 0x10, data );
	
    while(1)
    {
			
			printf("\r\n start ");
			//Card searching
			OLED_ShowString(0,0,"please put card          ");
			OLED_ShowString(0,2,"UID:");
			if(UID_str[9] != 0)
			OLED_ShowString(32,2,UID_str);
			status = PcdRequest(PICC_REQALL, g_ucTempbuf);//Card search
			printf(" %X ",status);
			if (status != MI_OK)
        {    
					PcdReset();
					PcdAntennaOff(); 
					PcdAntennaOn(); 
					continue;
				}		     
		 printf("\r\n Type of card:");
	    for(i=0;i<2;i++)
		{
			temp=g_ucTempbuf[i];
			printf("%X",temp);
			
		}	
         status = PcdAnticoll(g_ucTempbuf);//anti-collision 
         if(status != MI_OK)
         {    continue;    }
				 printf("\r\n Card serial number:");	//Terminal display
				 
		for(i=0;i<4;i++)
		{
			temp=g_ucTempbuf[i];
			printf("%X ",temp);
			
		}
		UID_str[0] = ((g_ucTempbuf[0]>>4)>9)	?(g_ucTempbuf[0]>>4)-10+'a'	:(g_ucTempbuf[0]>>4)+'0';
		UID_str[1] = ((g_ucTempbuf[0]&0x0f)>9)?(g_ucTempbuf[0]&0x0f)-10+'a':(g_ucTempbuf[0]&0x0f)+'0';
		UID_str[2] = ((g_ucTempbuf[1]>>4)>9)	?(g_ucTempbuf[1]>>4)-10+'a'	:(g_ucTempbuf[1]>>4)+'0';
		UID_str[3] = ((g_ucTempbuf[1]&0x0f)>9)?(g_ucTempbuf[1]&0x0f)-10+'a':(g_ucTempbuf[1]&0x0f)+'0';
		UID_str[4] = ((g_ucTempbuf[2]>>4)>9)	?(g_ucTempbuf[2]>>4)-10+'a'	:(g_ucTempbuf[2]>>4)+'0';
		UID_str[5] = ((g_ucTempbuf[2]&0x0f)>9)?(g_ucTempbuf[2]&0x0f)-10+'a':(g_ucTempbuf[2]&0x0f)+'0';
		UID_str[6] = ((g_ucTempbuf[3]>>4)>9)	?(g_ucTempbuf[3]>>4)-10+'a'	:(g_ucTempbuf[3]>>4)+'0';
		UID_str[7] = ((g_ucTempbuf[3]&0x0f)>9)?(g_ucTempbuf[3]&0x0f)-10+'a':(g_ucTempbuf[3]&0x0f)+'0';
		UID_str[9] = 1;
		//OLED_ShowString(0,2,UID_str);

		RC522_Handle();
		
		
		
		
/*		if(g_ucTempbuf[0]==0x07&&g_ucTempbuf[1]==0x87&&g_ucTempbuf[2]==0xE8&&g_ucTempbuf[3]==0x76)		//0787E876,4396C597,83B062C9
		{
			printf("\r\nDoor Opened.\r\n");
			delay_ms(2000);
		}
		else
		{	
			printf("\r\nThe illegal card!\r\n");
			delay_ms(2000);
		}
*/
		// Important reminder: the card writing operation is risky. Please do not try it at will. The safety of the program cannot be guaranteed. I am not responsible for any program problems, not limited to any loss or damage caused by any program error
		// Read / write test completed
    // RC522_Handle();
		// Carefully use test program 1 to complete 0x0F block verification KEY_A,KEY_B read / write RFID1 verification KEY_A1,KEY_B1 read / write RFID2
		// RC522_Handle1();

        if(num % 20 == 0)
            LED0 = !LED0;
        num++;
    }
}




Final effect

Display the UID of the card after swiping the card

Tags: Embedded system Single-Chip Microcomputer stm32

Posted by Nicolas T on Fri, 13 May 2022 22:13:49 +0300