蓝桥杯物联网第十二届省赛主观题

题目

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uLves32O-1681127404405)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410183008077.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BLCEcSwn-1681127404407)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410183025427.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VvBsP9EV-1681127404407)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410183041713.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4e5LTFx3-1681127404409)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410183058160.png)]

程序设计题

LoRa_A

cubemx配置

这里我们采用PLL(锁相环倍频),并且设置为最大32M主频,因为题目对通信反应速度有要求

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6c1BsNPZ-1681127404410)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410183308395.png)]

按键+灯+SPI两个控制IO+IIC一个控制IO

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SGKCIxJN-1681127404411)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410183348386.png)]

SPI设置成全双工模式

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-E5IGW346-1681127404412)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410183423818.png)]

IIC

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3HwdHIxe-1681127404412)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410183451272.png)]

ADC

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RGkWn2rX-1681127404413)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410183554491.png)]

RTC

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-J5ckHIpR-1681127404413)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410183615161.png)]

移植部分

把要用到的lora参考代码移植到工程里

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yNuVFA5E-1681127404414)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410183641821.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hXuf8mQS-1681127404415)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410183704716.png)]

将移植的源文件添加到工程

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-P61Qow60-1681127404415)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410183730846.png)]

代码部分

/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : Main program body
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under BSD 3-Clause license,
 * the "License"; You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                        opensource.org/licenses/BSD-3-Clause
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "i2c.h"
#include "rtc.h"
#include "spi.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "oled.h"
#include "stdio.h"
#include "string.h"
#include "lora.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
uint16_t usDly;    /* 延时 */
uint8_t ucState;   /* 状态 */
uint8_t ucKey;     /* 按键值 */
uint8_t ucLed;     /* LED值 */
uint8_t ucNum;     /* 闪烁次数 */
uint8_t ucBuf[20]; /* OLED显示值 */
uint8_t ucLrx[20]; /* LORA接收值 */
uint16_t usAdc[2]; /* ADC转换值 */

RTC_TimeTypeDef NowTime;
RTC_DateTypeDef NowDate;
// char lcdLine_1st_line[16];
char lcdLine_2nd_line[16];

/* 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 */
void ADC_Read(uint16_t *usData); // ADC1读取
uint8_t KEY_Read(void);
void LED_Disp(uint8_t ucLed); /* LED??? */
void KEY_Proc(void);          /* 按键处理 */
void OLED_Proc(void);         /* OLED处理 */
void LORA_Proc(void);         /* LoRa处理 */
void RTC_GET(void);           /* RTC处理 */
void ADC_GET(void);           /* ADC处理 */
void LORA_Proc(void);         /* LoRa处理 */
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* 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_ADC_Init();
  MX_I2C3_Init();
  MX_RTC_Init();
  MX_SPI1_Init();
  /* USER CODE BEGIN 2 */
  HAL_Delay(50); //
  HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED);

  OLED_Init();
  LORA_Init();

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    KEY_Proc();
    OLED_Proc();
    LORA_Proc(); /* LoRa处理 */
    /* 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};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Configure the main internal regulator output voltage
   */
  __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_HSI | RCC_OSCILLATORTYPE_LSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  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_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C3 | RCC_PERIPHCLK_RTC;
  PeriphClkInit.I2c3ClockSelection = RCC_I2C3CLKSOURCE_PCLK1;
  PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
void KEY_Proc(void) /* 按键处理 */
{
  uint8_t ucVal = 0;

  ucVal = KEY_Read(); /* 按键读取 */
  if (ucVal != ucKey) /* 键值变化 */
    ucKey = ucVal;    /* 保存键值 */
  else
    ucVal = 0; /* 清除键值 */

  if (ucVal == '0') /* 按键按下 */
    ucState ^= 1;   /* 切换状态 */
}

void OLED_Proc(void) /* OLED处理 */
{

  HAL_Delay(200);
  if (ucNum != 0)
  {
    ucNum--;
    ucLed ^= 1; /* LED闪烁 */
    LED_Disp(ucLed);
  }

  HAL_RTC_GetTime(&hrtc, &NowTime, RTC_FORMAT_BIN);
  HAL_RTC_GetDate(&hrtc, &NowDate, RTC_FORMAT_BIN);

  ADC_Read(usAdc);
  usAdc[0] = usAdc[0] * 330 / 4095;
  usAdc[1] = usAdc[1] * 330 / 4095;
  if (ucNum != 0)
  {
    ucNum--;
    ucLed ^= 1; /* LED闪烁 */
    LED_Disp(ucLed);
  }
  if (ucState == 0)
  {
    RTC_GET();
  }
  else
  {
    ADC_GET();
  }
}

void RTC_GET(void) /* RTC处理 */
{

  HAL_RTC_GetTime(&hrtc, &NowTime, RTC_FORMAT_BIN);
  HAL_RTC_GetDate(&hrtc, &NowDate, RTC_FORMAT_BIN);
  // sprintf(lcdLine_1st_line, "%d : %d : %d ", NowDate.Year, NowDate.Month, NowDate.Date);
  sprintf(lcdLine_2nd_line, "%d : %d : %d ", NowTime.Hours, NowTime.Minutes, NowTime.Seconds);
  // OLED_ShowString(0, 0, (unsigned char *)lcdLine_1st_line, 16);
  OLED_ShowString(0, 0, (unsigned char *)"    RTC   ", 16);
  OLED_ShowString(0, 2, (unsigned char *)lcdLine_2nd_line, 16);
}

void ADC_GET(void) /* ADC处理 */
{

  ADC_Read(usAdc);
  usAdc[0] = usAdc[0] * 330 / 4095;
  usAdc[1] = usAdc[1] * 330 / 4095;
  sprintf((char *)ucBuf, "VRP1:%4.2fV  ", usAdc[1] / 100.0);
  OLED_ShowString(0, 0, ucBuf, 16);
  sprintf((char *)ucBuf, "VRP2:%4.2fV  ", usAdc[0] / 100.0);
  OLED_ShowString(0, 2, ucBuf, 16);
}

void LORA_Proc(void) /* LoRa处理 */
{
  if ((LORA_Rx(ucLrx) == 6) && (ucLrx[0] == '*'))
  { /* 接收6个字符 */
    ucNum = 10;
    if (ucLrx[4] == '1')
      sprintf((char *)ucBuf, "VRP1:%4.2f:%02u-%02u-%02u*",
              usAdc[1] / 100.0, NowTime.Hours, NowTime.Minutes, NowTime.Seconds);
    else
      sprintf((char *)ucBuf, "VRP2:%4.2f:%02u-%02u-%02u*",
              usAdc[0] / 100.0, NowTime.Hours, NowTime.Minutes, NowTime.Seconds);
    LORA_Tx(ucBuf, 19);
  }
}

uint8_t KEY_Read(void) 
{
  uint8_t ucVal = 0;
  if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_14) == 0)
  {                
    HAL_Delay(10); 
    if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_14) == 0)
      ucVal = '0'; 
  }
  return ucVal;
}

void LED_Disp(uint8_t ucLed) 
{
  if ((ucLed & 1) == 1) 
    HAL_GPIO_WritePin(GPIOC, GPIO_PIN_15, GPIO_PIN_RESET);
  else
    HAL_GPIO_WritePin(GPIOC, GPIO_PIN_15, GPIO_PIN_SET);

  if ((ucLed & 2) == 0)
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_RESET);
  else 
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_SET);

  if ((ucLed & 4) == 0) 
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET);
  else 
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_SET);
}
void ADC_Read(uint16_t *usData) // ADC1读取
{
  HAL_ADC_Start(&hadc);
  if (HAL_ADC_PollForConversion(&hadc, 10) == HAL_OK)
    usData[0] = HAL_ADC_GetValue(&hadc);
  if (HAL_ADC_PollForConversion(&hadc, 10) == HAL_OK)
    usData[1] = HAL_ADC_GetValue(&hadc);
}
void OLED_Write(uint8_t ucType, uint8_t ucData) /* OLEDд */
{
  uint8_t pData[2];

  pData[0] = ucType;
  pData[1] = ucData;
  HAL_I2C_Master_Transmit(&hi2c3, 0x78, pData, 2, 10);
}

uint8_t SPI_WriteRead(uint8_t ucAddr, uint8_t ucData) /* SPI读写 */
{
  uint8_t pTxData[2], pRxData[2];

  pTxData[0] = ucAddr;
  pTxData[1] = ucData;
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
  HAL_SPI_TransmitReceive(&hspi1, pTxData, pRxData, 2, 10);
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
  return pRxData[1];
}
/* 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 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/



LoRa_B

cubemx配置

SPI两个控制IO

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ioCAszbG-1681127404417)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410183949083.png)]

SPI

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-w79FPpaK-1681127404417)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410184010100.png)]

串口设置成异步串行通信,波特率9600

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7zozPGqg-1681127404418)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410184027922.png)]

移植部分

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-A7BmU8wt-1681127404419)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410184100790.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Bwd9yO8r-1681127404420)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410184128428.png)]

将移植的源文件添加到工程

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CKAz5iD7-1681127404421)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410184147817.png)]

勾选这里,串口用

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CfGITN4K-1681127404422)(C:\Users\tangz\AppData\Roaming\Typora\typora-user-images\image-20230410184211932.png)]

代码部分

/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : Main program body
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under BSD 3-Clause license,
 * the "License"; You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                        opensource.org/licenses/BSD-3-Clause
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "spi.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "lora.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
uint16_t usDly;                   /* 延时 */
uint8_t ucSec;                    /* 秒计时 */
uint8_t ucBuf[17];                /* OLED显示值 */
uint8_t ucLrx[20];                /* LORA接收值 */
uint8_t ucUrx[10], ucUno, ucSec1; /* UART接收值,接收计数 */
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
void UART_Transmit(uint8_t *ucData, uint8_t ucSize);
uint8_t UART_Receive(uint8_t *ucData);
void OLED_Proc(void); /* OLED处理 */
void LORA_Proc(void); /* LoRa处理 */
void UART_Proc(void); /* UART处理 */
/* 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 */

/* 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_USART2_UART_Init();
  MX_I2C3_Init();
  MX_SPI1_Init();
  /* USER CODE BEGIN 2 */
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET); /* PA4置位 */
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET); /* PA9置位 */
  LORA_Init();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    LORA_Proc(); /* LoRa处理 */
    UART_Proc(); /* UART处理 */
    /* 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};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Configure the main internal regulator output voltage
   */
  __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_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  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_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_I2C3;
  PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  PeriphClkInit.I2c3ClockSelection = RCC_I2C3CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
// 18数据+1校验
void LORA_Proc(void) /* LoRa处理 */
{
  if ((LORA_Rx(ucLrx) == 19) && (ucLrx[18] == '*'))
  { /* 接收19个字符 */
    ucLrx[18] = ' ';
    printf("%s\r\n", ucLrx);
  }
}

void UART_Proc(void) /* UART处理 */
{
  if (UART_Receive(ucUrx) == 0) /* 接收到字符 */
  {
    if (ucUrx[0] == 'V')
      ucUno = 0;

    ucUrx[++ucUno] = ucUrx[0]; /* 保存字符 */
    if (ucUno >= 5)
    {
      if ((ucUrx[1] == 'V') && (ucUrx[2] == 'R') && (ucUrx[3] == 'P') &&
          ((ucUrx[4] == '1') || (ucUrx[4] == '2')) && (ucUrx[5] == '?'))
      {
        ucUrx[0] = '*';
        LORA_Tx(ucUrx, 6);
      }
      else
        printf("ERROR\r\n");
    }
  }
}

int fputc(int ch /*c*/, FILE *f /*stream*/)
{

  UART_Transmit((uint8_t *)&ch, 1);
  return ch;
}
uint8_t SPI_WriteRead(uint8_t ucAddr, uint8_t ucData) /* SPI读写 */
{
  uint8_t pTxData[2], pRxData[2];

  pTxData[0] = ucAddr;
  pTxData[1] = ucData;
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
  HAL_SPI_TransmitReceive(&hspi1, pTxData, pRxData, 2, 10);
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
  return pRxData[1];
}

void UART_Transmit(uint8_t *pData, uint8_t Size)
{
  HAL_UART_Transmit(&huart2, pData, Size, 100);
}

uint8_t UART_Receive(uint8_t *pData)
{
  return HAL_UART_Receive(&huart2, pData, 1, 0);
}

/* 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 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

  {
  }
  /* 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 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/


在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HongCode嵌入式

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值