基于stm32f103rct6(秉火mini板)的rc522RFID读卡例程

主要代码

#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 = SPI_RC522_ReadByte 
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值