print函数 stm32_stm32中如何进行printf重定向用于串口调试输出

本文介绍了如何在STM32项目中将printf函数重定向到串口进行调试输出,包括:配置使用MicroLib,初始化UART1,设置相关参数,以及中断程序的实现。通过这些步骤,开发者可以在MDK的模拟串口中查看printf输出。
摘要由CSDN通过智能技术生成

1 在main中包含stdio.h 文件

2 Target选项框里选Use MicroLib 选项

3 在main中添加UART1_Configuration()初始化的代码

Uart1初始化,

void UART1_Configuration(void)

{

USART_InitTypeDef USART_InitStructure;

USART_ClockInitTypeDef  USART_ClockInitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 |RCC_APB2Periph_USART1, ENABLE  );

USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;                        // 时钟低电平活动

USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;                                // 时钟低电平

USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;                                // 时钟第二个边沿进行数据捕获

USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;                // 最后一位数据的时钟脉冲不从SCLK输出

/* Configure the USART1 synchronous paramters */

USART_ClockInit(USART1, &USART_ClockInitStructure);                                        // 时钟参数初始化设置

USART_InitStructure.USART_BaudRate =9600;                                                  // 波特率为:115200

USART_InitStructure.USART_WordLength = USART_WordLength_8b;                          // 8位数据

USART_InitStructure.USART_StopBits = USART_StopBits_1;                                  // 在帧结尾传输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;                  // 发送使能+接收使能

/* Configure USART1 basic and asynchronous paramters */

USART_Init(USART1, &USART_InitStructure);

/* Enable USART1 */

USART_ClearFlag(USART1, USART_IT_RXNE);                         //清中断,以免一启用中断后立即产生中断

USART_ITConfig(USART1,USART_IT_RXNE, ENABLE);                //使能USART1中断源

USART_Cmd(USART1, ENABLE);                                                        //USART1总开关:开启

}

4 子函数代码添加,  可以也加在main函数后面,也可以单独写一个uart.h,然后包含在main中。(亲测不加入这两个函数也可以printf重定向输出)

//int fputc(int ch, FILE *f)

//{

///* 发送一个字节数据到USART1 */

//USART_SendData(USART1, (u8) ch);

//

///* 等待发送完毕 */

//while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);

//

//return (ch);

//}

//

/ 重定向c库函数scanf到USART1

//int fgetc(FILE *f)

//{

///* 等待串口1输入数据 */

//while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);

//

//return (int)USART_ReceiveData(USART1);

//}

//

这个是printf函数调用串口输出字符的

5 添加串口中断程序,在stm32XX_it.c中,先定义几个用到的变量

#define TxBufferSize   (countof(TxBuffer) - 1)

#define RxBufferSize   0x20

/* Private macro -------------------------------------------------------------*/

#define countof(a)   (sizeof(a) / sizeof(*(a)))

/* Private variables ---------------------------------------------------------*/

u8 TxBuffer[] = "\n\rUSART Hyperterminal Interrupts Example: USART-Hyperterminal\

communication using Interrupt\n\r";

u8 RxBuffer[RxBufferSize];

u8 NbrOfDataToTransfer = TxBufferSize;

u8 NbrOfDataToRead = RxBufferSize;

u8 TxCounter = 0;

u16 RxCounter = 0;

然后是中断程序

/*******************************************************************************

* Function Name  : USART1_IRQHandler

* Description    : This function handles USART1 global interrupt request.

* Input          : None

* Output         : None

* Return         : None

*******************************************************************************/

void USART1_IRQHandler(void)

{

if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)

{

/* Read one byte from the receive data register */

RxBuffer[RxCounter++] = (USART_ReceiveData(USART1) & 0x7F);

if(RxCounter == NbrOfDataToRead)

{

/* Disable the USART Receive interrupt */

RxCounter = 0;

//USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);

}

}

if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)

{

/* Write one byte to the transmit data register */

USART_SendData(USART1, TxBuffer[TxCounter++]);

if(TxCounter == NbrOfDataToTransfer)

{

/* Disable the USART1 Transmit interrupt */

USART_ITConfig(USART1, USART_IT_TXE, DISABLE);

}

}

}

这部分不加好像也可以实现,有待我后续验证。

6上边ok之后,就可以随便printf()输出了,软件debug,

就可以在MDK自带的模拟串口中看到激动人心的输出了,本人亲测, 串口函数部分有些不同

串口调试非常实用的

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值