Professional Documents
Culture Documents
Awal Spi
Awal Spi
/**
******************************************************************************
* @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"
UART_HandleTypeDef huart1;
_Bool ver = 0;
uint8_t v;
//uint8_t x[1];
//uint16_t y,z;
/* USER CODE END PV */
void PCD_AntennaOn() {
uint8_t value = PCD_ReadRegister(0x14<<1);
HAL_GPIO_WritePin (GPIOB, GPIO_PIN_1, GPIO_PIN_RESET); // pull the pin low
HAL_SPI_Transmit (&hspi1, 0x80 | 0x14<<1 , 1, 100);
HAL_SPI_Receive (&hspi1, value, 1, 100);
HAL_GPIO_WritePin (GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
if ((value & 0x03) != 0x03) {
PCD_WriteRegister(0x14 << 1, value | 0x03);
}
}
void PCD_Reset() {
PCD_WriteRegister(0x01<<1, 0x0F);
uint8_t count = 0;
do {
HAL_Delay(50);
} while ((PCD_ReadRegister(0x01<<1) & (1 << 4)) && (++count) < 3);
}
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
// char uart_buf[50];
// int uart_buf_len;
// char spi_buf[20];
// uint8_t addr;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
data_rec[0] = 0;
data_rec[1] = 0;
data_rec[2] = 0;
PCD_WriteRegister(0x15<<1, 0x40);
PCD_WriteRegister(0x18<<1, 0xFF);
PCD_WriteRegister(0x0C<<1, 0x10);
PCD_WriteRegister(0x19<<1, 0x4D);
PCD_WriteRegister(0x1C<<1, 0x62);
PCD_WriteRegister(0x25<<1, 0x87);
PCD_WriteRegister(0x17<<1, 0x84);
PCD_WriteRegister(0x26<<1, 0x48);
PCD_WriteRegister(0x27<<1, 0x88);
PCD_WriteRegister(0x28<<1, 0x20);
PCD_WriteRegister(0x23<<1,0x88);
PCD_WriteRegister(0x29<<1,0x20);
PCD_WriteRegister(0x11<<1, 0x3D);
PCD_WriteRegister(0x0D<<1, 0x00);
PCD_WriteRegister(0x0D<<1, 64);
PCD_WriteRegister(0x1E<<1, 0);
PCD_WriteRegister(0x1C<<1, 0x8A);
PCD_AntennaOn();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_8);
HAL_Delay(100);
// rfid_write(0x11);
HAL_Delay(100);
// rfid_read(0x11);
HAL_UART_Transmit(&huart1,v,sizeof(v),100);
HAL_Delay(500);
ver=1;
//}
/* USER CODE END WHILE */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/**
* @brief SPI1 Initialization Function
* @param None
* @retval None
*/
static void MX_SPI1_Init(void)
{
/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/**
* @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 */