int main(void) { char *ch; LED_Init(); USART_Init_Config(); while(1) { ch = getchar(); printf("½ÓÊÕµ½µÄÖ¸Á %c \n",ch); switch(ch) { case 'N': GPIO_SetBits(GPIOB,GPIO_Pin_6); printf("ϨÃðLEDµÆ\n"); break; case 'Y': GPIO_ResetBits(GPIOB,GPIO_Pin_6); printf("µãÁÁLEDµÆ \n"); break; default: printf("ÇëÊäÈëÕýÈ·µÄÖ¸ÁNϨÃðµÆ£¬YµãÁÁµÆ!\n"); break; } } }把输入字符替换成字符串,后续也可以通过字符串识别
时间: 2024-01-14 22:02:25 浏览: 70
可以把输入的字符替换成字符串,例如:
```
char input[10]; // 定义一个长度为10的字符数组
fgets(input, 10, stdin); // 从标准输入流中读取字符串,最多读取9个字符,最后一个字符为'\0'
```
然后在 `switch` 语句中,把 `ch` 替换成 `input` 即可识别字符串。例如:
```
switch(input[0]) // 只判断第一个字符
{
case 'N':
GPIO_SetBits(GPIOB,GPIO_Pin_6);
printf("ϨÃðLEDµÆ\n");
break;
case 'Y':
GPIO_ResetBits(GPIOB,GPIO_Pin_6);
printf("µãÁÁLEDµÆ \n");
break;
default:
printf("ÇëÊäÈëÕýÈ·µÄÖ¸ÁNϨÃðµÆ£¬YµãÁÁµÆ!\n");
break;
}
```
相关问题
修改以下程序,使其接收指令后发送一个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
请修改程序如下:
```
#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" 字符串,然后再继续等待下一个字符。
stm32 CUBEMX使用pa0、pa1引脚开启usart1串口并实现打印函数的全部流程
1. 打开 STM32 CUBEMX 软件,选择对应的芯片型号和工程路径。
2. 在 Pinout 视图中选择需要使用的引脚,例如 PA0 和 PA1 作为 USART1 的 RX 和 TX 引脚。将引脚设置为 Alternate function 模式,并选择对应的串口功能。
3. 在 Configuration 视图中配置 USART1 的波特率、数据位、停止位、校验位等参数。同时,开启 USART1 的 DMA 功能,以提高数据传输效率。
4. 在 Code Generator 视图中选择使用 HAL 库,并生成代码。
5. 在生成的代码中,找到 `main.c` 文件,添加以下代码:
```c
#include "stdio.h"
#include "stm32f4xx_hal.h"
#include "usart.h"
void SystemClock_Config(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_USART1_UART_Init();
printf("Hello World!\n");
while (1)
{
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__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_NONE;
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_HSI;
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_0) != HAL_OK)
{
Error_Handler();
}
}
```
6. 在 `main.c` 文件中添加 `stdio.h` 头文件,以便使用标准输入输出函数。同时,在 `usart.h` 文件中添加 `printf()` 函数的声明:
```c
int __io_putchar(int ch);
#define printf(...) do { \
char buffer[256]; \
int size = sprintf(buffer, __VA_ARGS__); \
HAL_UART_Transmit_DMA(&huart1, (uint8_t*)buffer, size); \
} while (0)
```
7. 编译并下载程序到开发板上,即可在串口调试助手中看到输出的 "Hello World!"。
阅读全文