本次教程目标:
1.上电LED灯点亮并熄灭,进行灯光检测。
2.向上位机发送字符串“欢迎001A”。
3.发送完字符串“欢迎001A”后启动计时。
4.上位机发送打开和关闭LED灯指令。命令帧为“0xAB 控制字 0xBA”
0xA1:打开LED,返回信息“XX:XX:XX LED打开。”
0x01:关闭LED,返回信息“XX:XX:XX LED关闭。”
其他:返回信息“XX:XX:XX 命令错误”。
首先配置STM32CubeMX
计时时间500ms
计算公示:计时时间={(预分频器+1)*(自动重装寄存器+1)} /定时器始终频率
1s=(7200*10000)/72M
使用定时中断时切记将TIM2的全局中断开启。
中断即没定时时间到进入一次中断。
首先开始写LED灯开机检测功能。
先定义一个LED灯开关的宏定义
#define LED_ON HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET)
#define LED_OFF HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET)
接着写LED灯检测函数。
void Check_LED(void)
{
LED_ON;
HAL_Delay(500);
LED_OFF;
HAL_Delay(500);
};
在主函数里调用Check_LED();函数。
写一个开机播报数组,发送欢迎001A。
发送这个数组
在上位机上显示欢迎001A
写计时中断函数。
定义时间变量,小时、分钟、秒钟
重写计时中断回调函数
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM2)
{
s++;
if(s>60)
{
s=0;
m++;
if(m>60)
{
m=0;
m++;
}
}
}
}
定义数据接收缓存数组,给定数组长度是3,也是3个字节,因为是uint8-t类型的。需要注意的是,帧头帧尾都是占一个字节,控制字占1个字节。缓存数组的大小不能随意定,否则数据传输会出现错误的情况。
重写串口数据接收完成回调函数。HAL_UART_Transmit_IT(&huart1,str,sizeof(str));
HAL_UART_Receive_IT(&huart1,Rxdata,sizeof(Rxdata));要放在回调函数的最后,否则会出现问题。这里目前有一个bug,就是每次接收到错误命令后又接收到正确命令时还是维持上次的str数组的数据发送“命令错误”,要多接收几次正确数据才会显示正确的内容。
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance==USART1)
{
if(Rxdata[0]==0xAB&&Rxdata[2]==0xBA)
{
switch(Rxdata[1])
{
case 0xA1:
LED_ON;
sprintf((char *)str,"%d:%d:%d LED打开",h,m,s);
break;
case 0x01:
LED_OFF;
sprintf((char *)str,"%d:%d:%d LED关闭",h,m,s);
break;
default:
sprintf((char *)str,"%d:%d:%d 命令错误",h,m,s);
break;
}
}
}
HAL_UART_Transmit_IT(&huart1,str,sizeof(str));
HAL_UART_Receive_IT(&huart1,Rxdata,sizeof(Rxdata));
}
全部主程序
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 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 "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.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 */
#define LED_ON HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET)
#define LED_OFF HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET)
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint8_t helloword[]={"欢迎001A"};
uint8_t Rxdata[3];
uint8_t str[64];
uint8_t h=0,m=0,s=0;
/* 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 */
void Check_LED(void)
{
LED_ON;
HAL_Delay(500);
LED_OFF;
HAL_Delay(500);
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM2)
{
s++;
if(s>60)
{
s=0;
m++;
if(m>60)
{
m=0;
m++;
}
}
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance==USART1)
{
if(Rxdata[0]==0xAB&&Rxdata[2]==0xBA)
{
switch(Rxdata[1])
{
case 0xA1:
LED_ON;
sprintf((char *)str,"%d:%d:%d LED打开",h,m,s);
break;
case 0x01:
LED_OFF;
sprintf((char *)str,"%d:%d:%d LED关闭",h,m,s);
break;
default:
sprintf((char *)str,"%d:%d:%d 命令错误",h,m,s);
break;
}
}
}
HAL_UART_Transmit_IT(&huart1,str,sizeof(str));
HAL_UART_Receive_IT(&huart1,Rxdata,sizeof(Rxdata));
}
/* 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_USART1_UART_Init();
MX_TIM2_Init();
/* USER CODE BEGIN 2 */
Check_LED();
HAL_UART_Transmit(&huart1,helloword,sizeof(helloword),10000);
HAL_TIM_Base_Start_IT(&htim2);
HAL_UART_Receive_IT(&huart1,Rxdata,sizeof(Rxdata));
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* 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};
/** 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.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
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_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != 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 */