修改以下程序,使其接收指令后发送一个hello,world字符串。#include "main.h"#include "usart.h"#include "gpio.h"#include "stdio.h"#include "string.h"uint8_t aRxBuffer;void SystemClock_Config(void);int fputc(int ch, FILE *f){ uint8_t temp[1] = {ch}; HAL_UART_Transmit(&huart1, temp, 1, 0xffff);return ch;}int fgetc(FILE * f){ uint8_t ch = 0; HAL_UART_Receive(&huart1,&ch, 1, 0xffff); return ch;}int main(void){ HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1); user_main_printf(""); /* USER CODE END 2 */ while (1) { }}void SystemClock_Config(void){ RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; 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_DIV2; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } 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_0) != HAL_OK) { Error_Handler(); }}void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){ char *pCmd = NULL; uint8_t len; switch(aRxBuffer){ case '1': pCmd = "command 1\r\n"; len = strlen(pCmd); break; case '2': pCmd = "command 2\r\n"; len = strlen(pCmd); break; case '3': pCmd = "command 3\r\n"; len = strlen(pCmd); break; case '4': pCmd = "command 4\r\n"; len = strlen(pCmd); break; default: pCmd = "command cmd\r\n"; len = strlen(pCmd); break; } HAL_UART_Transmit(&huart1, (uint8_t *)pCmd, len,0xFFFF); HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1); }void Error_Handler(void){}#ifdef USE_FULL_ASSERTvoid assert_failed(uint8_t *file, uint32_t line){ }#endif
时间: 2023-12-28 20:04:29 浏览: 83
USART.zip_usart接收a0_串口 发送
请修改程序如下:
```
#include "main.h"
#include "usart.h"
#include "gpio.h"
#include "stdio.h"
#include "string.h"
uint8_t aRxBuffer;
void SystemClock_Config(void);
int fputc(int ch, FILE *f) {
uint8_t temp[1] = {ch};
HAL_UART_Transmit(&huart1, temp, 1, 0xffff);
return ch;
}
int fgetc(FILE * f) {
uint8_t ch = 0;
HAL_UART_Receive(&huart1,&ch, 1, 0xffff);
return ch;
}
int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1);
while (1) {
if (aRxBuffer != 0) {
HAL_UART_Transmit(&huart1, "hello, world\r\n", 14, 0xFFFF);
aRxBuffer = 0;
HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1);
}
}
}
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
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_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
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_0) != HAL_OK) {
Error_Handler();
}
}
void Error_Handler(void) {}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line) {}
#endif
```
修改后的程序会在收到任何字符后发送 "hello, world" 字符串,然后再继续等待下一个字符。
阅读全文