1资料
RFID-RC522是一款基于13.56MHz频率的射频识别(RFID)模块,广泛应用于非接触式智能卡和标签的读写操作。
组成部分
- 天线:用于发射和接收射频信号。
- 模拟前端:处理天线接收到的信号,并将其转换为数字信号,或者将数字信号转换为射频信号发射出去。
- 数字基带控制器:处理所有的数字信号,执行协议编码/解码,进行数据的加密/解密等。
- 串行通信接口:如SPI、I2C或UART,用于与外部微控制器通信。
2结构

3硬件


硬件连接
首先,确保RFID-RC522模块与STM32单片机的连接正确。以下是一个常见的连接方式:
- RC522 STM32
- SDA 某个GPIO(如PA4)(片选CS)
- SCK SPI时钟线(如PA5)
- MOSI SPI MOSI线(如PA7)
- MISO SPI MISO线(如PA6)
- RST 某个GPIO(如PA1)
- GND GND
- 3.3V 3.3V
4实现读取卡号卡配置
1配置时钟

2定义UART和SPI
uart

SPI注意此处尽量设置大一点,使通讯的速度小一点


3定义两个gpio当片选和复位io
5源码
main.h
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "spi.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "RC522.h"
#include <stdio.h>
#include <string.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//读取卡号,做数据回填,成功读取到卡号就退出 ,另一个是回调功能函数
uint8_t readCard(uint8_t *readUid,void(*funCallBack)(void))
{
uint8_t Temp[5];
if (PCD_Request(0x52, Temp) == 0)
{
if (PCD_Anticoll(readUid) == 0)
{
if(funCallBack != NULL)
funCallBack();
return 0;
}
}
return 1;
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SPI1_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
uint8_t readUid[5];
MFRC_Init();
PCD_Reset();
printf("RC522初始化成功\r\n");
while (1)
{
if(!readCard(readUid,NULL)){
printf("卡号:%x-%x-%x-%x\n",readUid[0],readUid[1],readUid[2],readUid[3]);
}else{
printf("没有识别到卡");
}
HAL_Delay(500);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
RC522.c
/*************笔记****************
1、CubeMX 定义任意两个引脚,作为复位脚和片选脚,并对引脚作出如下配置:
GPlO output level --High
GPIO mode --Output Open Drain
GPIO Pull-up/Pull-down --No pull-up and no pull-down
Maximum output speed --LOW
User label --RC522_RST/RC522_CS
---------------------------------------------------------
开启SPI功能,模式选择-->Full-Duplex Master(全双工),其他配置如下:
Basic Parameters
Frame format-->Motorola

最低0.47元/天 解锁文章
7万+

被折叠的 条评论
为什么被折叠?



