主要代码
#main.c
#include "stm32f10x.h"
#include "bsp_SysTick.h"
#include "bsp_usart1.h"
#include "rc522_config.h"
#include "rc522_function.h"
#include <stdbool.h>
#include "bsp_lcd.h"
#include "./font/fonts.h"
void IC_test ( void )
{
char cStr [ 30 ];
uint8_t ucArray_ID [ 4 ]; /*ÏÈºó´æ·ÅIC¿¨µÄÀàÐͺÍUID(IC¿¨ÐòÁкÅ)*/
uint8_t ucStatusReturn; /*·µ»Ø×´Ì¬ */
static uint8_t ucLineCount = 0;
while ( 1 )
{
/*Ѱ¿¨*/
if ( ( ucStatusReturn = PcdRequest ( PICC_REQALL, ucArray_ID ) ) != MI_OK )
/*Èôʧ°ÜÔÙ´ÎѰ¿¨*/
ucStatusReturn = PcdRequest ( PICC_REQALL, ucArray_ID );
if ( ucStatusReturn == MI_OK )
{
/*·À³åײ£¨µ±ÓжàÕÅ¿¨½øÈë¶ÁдÆ÷²Ù×÷·¶Î§Ê±£¬·À³åÍ»»úÖÆ»á´ÓÆäÖÐÑ¡ÔñÒ»ÕŽøÐвÙ×÷£©*/
if ( PcdAnticoll ( ucArray_ID ) == MI_OK )
{
sprintf ( cStr, "The Card ID is: %02X%02X%02X%02X",
ucArray_ID [ 0 ],
ucArray_ID [ 1 ],
ucArray_ID [ 2 ],
ucArray_ID [ 3 ] );
printf ( "%s\r\n",cStr );
ILI9341_DispStringLine_EN(LINE(ucLineCount), cStr );
ucLineCount ++;
if ( ucLineCount == 10 ) ucLineCount = 0;
}
}
}
}
int main ( void )
{
/*µÎ´ðʱÖÓ³õʼ»¯*/
SysTick_Init ();
/*USART1 ÅäÖÃģʽΪ 115200 8-N-1£¬ÖжϽÓÊÕ */
USART1_Config ();
/*RC522Ä£¿éËùÐèÍâÉèµÄ³õʼ»¯ÅäÖÃ*/
RC522_Init ();
/*LCD ³õʼ»¯*/
LCD_Init ();
ILI9341_GramScan ( 6 );
printf ( "RC522 Test\n" );
LCD_SetFont(&Font8x16);
LCD_SetColors(RED,BLACK);
/* ÇåÆÁ£¬ÏÔʾȫºÚ */
ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);
ILI9341_DispStringLine_EN(LINE(13),
"please put your ic card in rc522 area" );
LCD_SetTextColor(BLUE);
PcdReset ();
/*ÉèÖù¤×÷·½Ê½*/
M500PcdConfigISOType ( 'A' );
while ( 1 )
{
/*IC¿¨¼ì²â */
IC_test ();
}
}
#rc522_function.c
#include "rc522_function.h"
#include "rc522_config.h"
#include "stm32f10x.h"
#include "bsp_SysTick.h"
/*0ÉÈÇøµÄ¿é0ÓÃÓÚ´æ·Å³§ÉÌ´úÂ룬²»¿É¸ü¸Ä£¬Îª32룬4byte*/
/*ÿ¸öÉÈÇøµÄ¿é0£¬¿é1£¬2Êý¾Ý¿é£¬¿ÉÓÃÓÚ´æ´¢Êý¾Ý£¬Ã¿¿é16×Ö½Ú*/
/*ÿ¸öÉÈÇøµÄ¿é3Ϊ¿ØÖƿ飬°üº¬ÃÜÂëA£¬´æÈ¡¿ØÖÆ£¬ÃÜÂëB*/
/*ÀýÈç
A0 A1 A2 A3 A4 A5 | FF 07 80 69 | B0 B1 B2 B3 B4 B5
ÃÜÂëA£¨6×Ö½Ú£© | ´æÈ¡¿ØÖÆ | ÃÜÂëB£¨6×Ö½Ú£©
*/
/*M1¿¨ÓÐ16¸öÉÈÇø¡£¹²ÓÐ64¸ö¿é£¬°´¾ø¶ÔµØÖ·±àºÅΪ¡°0~63*/
#define RC522_DELAY() Delay_us ( 200 )
/**
* @brief ÏòRC522·¢ËÍ1 Byte Êý¾Ý
* @param byte£¬Òª·¢Ë͵ÄÊý¾Ý
* @retval RC522·µ»ØµÄÊý¾Ý
*/
void SPI_RC522_SendByte ( uint8_t byte )/*Ïòrc522·¢ËÍ1 byteÊý¾Ý*/
{
uint8_t counter;/*¼ÆÊý*/
for(counter=0;counter<8;counter++)
{
if ( byte & 0x80 )/*Èôbyte×î¸ßλΪ1£¬ÔòMOSIΪ1£»byte×î¸ßλΪ0£¬ÄÇôMOSIΪ0*/
RC522_MOSI_1 ();
else
RC522_MOSI_0 ();
RC522_DELAY();/*Delay_us ( 200 )*/
RC522_SCK_0 ();/*ͬMOSIÒý½Å*/
RC522_DELAY();/*ͬÉÏ*/
RC522_SCK_1();
RC522_DELAY();
byte <<= 1; /*¼´Îªbyte=byte<<1£¬Ò²¾ÍÊÇbyte=byte+1*/
}
}
/**
* @brief ´ÓRC522·¢ËÍ1 Byte Êý¾Ý
* @param ÎÞ
* @retval RC522·µ»ØµÄÊý¾Ý
*/
uint8_t SPI_RC522_ReadByte ( void )
{
uint8_t counter;
uint8_t SPI_Data;
/*¼ÆÊý*/
for(counter=0;counter<8;counter++)
{
SPI_Data <<= 1;/*DATA=DATA+1*/
RC522_SCK_0 ();
RC522_DELAY();
if ( RC522_MISO_GET() == 1)
SPI_Data |= 0x01;
RC522_DELAY();
RC522_SCK_1 ();
RC522_DELAY();
}
return SPI_Data;
}
/**
* @brief ¶ÁRC522¼Ä´æÆ÷
* @param ucAddress£¬¼Ä´æÆ÷µØÖ·
* @retval ¼Ä´æÆ÷µÄµ±Ç°Öµ
*/
uint8_t ReadRawRC ( uint8_t ucAddress )
{
uint8_t ucAddr, ucReturn;
ucAddr = ( ( ucAddress << 1 ) & 0x7E ) | 0x80;
RC522_CS_Enable();
SPI_RC522_SendByte ( ucAddr );
ucReturn