STM32 基于keil5的printf打印设置

1、因为使用串口来打印,所以将fputc函数和fgetc函数放在usart.c源文件中;

2、在usart.c源文件中添加stdio.h头文件;

3、打印信息常用于调试,不建议使用中断;

4、在usart.c源文件中添加如下代码

int fputc(int ch, FILE *f){
    HAL_UART_Transmit(&huart1,(uint8_t*)&ch, 1, 10);
    return ch;
}

int fgetc(FILE *f){
    uint8_t ch = 0;
    HAL_UART_Receive(&huart1,&ch, 1, 10);
    return (int)ch;
}

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用以下代码实现在KEIL5中使用GETCHAR,并将串口输入的CH值输出: ```c #include <stdio.h> #include <stm32f4xx.h> int main(void) { // Enable the USART2 peripheral clock RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // Configure the GPIO pins for USART2 GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); // Configure USART2 USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART2, &USART_InitStructure); // Enable USART2 USART_Cmd(USART2, ENABLE); while(1) { // Wait for input from the user while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET); // Read the input character char ch = USART_ReceiveData(USART2); // Print the input character to the USART2 USART_SendData(USART2, ch); // Wait for the transmission to complete while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); } } ``` 这段代码使用USART2的串口通信接收输入字符,并将输入字符原样输出到串口。请注意,此代码适用于使用STM32F4xx系列控制器的嵌入式系统,如果您使用其他控制器或计算机系统,则应相应地修改代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值