题目
程序设计题
LoRa_A
cubemx配置
这里我们采用PLL(锁相环倍频),并且设置为最大32M主频,因为题目对通信反应速度有要求
按键+灯+SPI两个控制IO+IIC一个控制IO
SPI设置成全双工模式
IIC
ADC
RTC
移植部分
把要用到的lora参考代码移植到工程里
将移植的源文件添加到工程
代码部分
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© 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
SPI
串口设置成异步串行通信,波特率9600
移植部分
将移植的源文件添加到工程
勾选这里,串口用
代码部分
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© 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****/