各位同志好,这一篇我们在电脑使用串口发送一个指定命令,将MCU配置为串口中断接收及判断数据,来控制板用LED灯,并将收到的字节发送回到电脑,我们采用第一篇的程序模板。既然测试串口,所以我们打开串口2外设,并且将它配置成Asynchronous模式,接着你可以调整波特率也可以不调整,我这里将波特率调到9600了,还需要打开串口2外设的中断并调整中断优先级。由于采用了第一篇文章的程序模板,这里仅仅对串口的配置进行记录。基本配置如下:



程序支持两种模式:模式1是当发送00 00时,板用LED灯全灭;

当发送01 00时LED1亮起;

当发送02 00时LED2亮起;

当发送03 00时,板用LED全部亮起。

模式2是发送01 xx时,LED1根据你的设置值来闪烁,延时时间为xx乘以10倍,最大设置到50有效,当设置50时,闪烁延时500ms

收到的串口回传信息:

程序基本原理是:MCU上电后先初始化一堆东西(将PC13配置成外部中断模式以及开启串口2外设及中断)并使用串口打印相关作者信息,接着进入主函数中,分别在发送对应数据时LED根据数据亮灭;同时串口将收到的16进制数回发给串口。
程序实现如下:
/*
*作者:下行路轨上的C70E 论坛ID钟详
*Nucleo-C092 demo程序任务3:使用串口接收一个定长字节,将收到的字节发出给串口助手,同时控制LED灯常亮、不同频次闪烁。
*使用意法半导体Nucleo板卡,配置外置晶体48M,波特率9600
*控制用字节采用16进制数进行发送/接收
*输入00 xx时全灭,输入01 00时LED1亮灯,输入02 00时LED2亮灯,输入03 00时全亮
*输入01 xx时LED1闪烁,具体延时为xx*10,最大值50;其余情况类似
*乘客们,列车马上就要进站了,本次列车6节编组,终点站,沈杜公路
*/
/*包含所需头文件*/
#include "main.h"
#include "usart.h"
#include "gpio.h"
#include "stdio.h"
#include "stm32c0xx_it.h"
/*定义传入数组*/
uint8_t Data[2];
/*函数声明*/
void SystemClock_Config(void);
/*主函数*/
int main(void){
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
/*打印作者信息*/
printf("\r\nST Chinese Forum Evaluation Plan\r\n");
printf("\r\nBoard Mode:Nucleo-C092\r\n");
printf("\r\nDemo3:USART Interrupt Transmit&Receive control LED\r\n");
printf("\r\nReviewer:Xiang Zhong Bli:C70E\r\n");
HAL_GPIO_WritePin(User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_SET);
/*打开串口接收中断*/
HAL_UART_Receive_IT(&huart2,Data,2);
/*进入循环体*/
while (1){
switch(Data[0]){
case 0:
HAL_GPIO_WritePin(User_LED1_GPIO_Port,User_LED1_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_SET);
break;
case 1:
if(Data[1]==0){
HAL_GPIO_WritePin(User_LED1_GPIO_Port,User_LED1_Pin,GPIO_PIN_SET);
}
if(Data[1]>50){
Data[1]=50;
}
if(Data[1]>0){
HAL_GPIO_TogglePin(User_LED1_GPIO_Port, User_LED1_Pin);
HAL_Delay(Data[1]*10);
}
HAL_GPIO_WritePin(User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_SET);
break;
case 2:
HAL_GPIO_WritePin(User_LED1_GPIO_Port,User_LED1_Pin,GPIO_PIN_RESET);
if(Data[1]==0){
HAL_GPIO_WritePin(User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_RESET);
}
if(Data[1]>50){
Data[1]=50;
}
if(Data[1]>0){
HAL_GPIO_TogglePin(User_LED2_GPIO_Port, User_LED2_Pin);
HAL_Delay(Data[1]*10);
}
break;
case 3:
if(Data[1]==0){
HAL_GPIO_WritePin(User_LED1_GPIO_Port,User_LED1_Pin,GPIO_PIN_SET);
HAL_GPIO_WritePin(User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_RESET);
}
if(Data[1]>50){
Data[1]=50;
}
if(Data[1]>0){
HAL_GPIO_WritePin(User_LED1_GPIO_Port,User_LED1_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_RESET);
HAL_Delay(Data[1]*10);
HAL_GPIO_WritePin(User_LED1_GPIO_Port,User_LED1_Pin,GPIO_PIN_SET);
HAL_GPIO_WritePin(User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_SET);
HAL_Delay(Data[1]*10);
}
break;
}
}
}
/*串口中断回调函数*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
HAL_UART_Transmit_IT(&huart2,Data,2);//中断式打印Data数组到串口助手
HAL_UART_Receive_IT(&huart2,Data,2);//打开串口接收中断
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_FLASH_SET_LATENCY(FLASH_LATENCY_1);
/** 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;
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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
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 */
}
/*printf函数支持(阻塞法打印数据)*/
int fputc(int ch,FILE *f)
{
HAL_UART_Transmit(&huart2,(uint8_t *)&ch,1,0xFFFF);//阻塞方式打印
return ch;
}
#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 */
好的,感谢各位的观看,这样我们就实现了基本的串口收发及控制LED的测试。