看了一下,官方提供的例子好像没有重定向printf到串口的。
我来尝试实现
(1)在main.c包含必要的stdio.h头文件进来
(2)在工程的设置中启用MicroLIB库
查看stdio.h以确定要实现哪个函数即可完成重定向
- int fputc(int ch, FILE *stream)
- {
- //当串口0忙的时候等待,不忙的时候再发送传进来的字符
- while(DL_UART_isBusy(UART_0_INST)==true){}
- DL_UART_transmitData(UART_0_INST,ch);
- return ch;
- }
编写如上代码,经过测试,如果不判断串口是否忙,将无法收到printf发送来的内容。
所以,先判断串口0是否忙,如果忙就等待,如果不忙,就发送当前传递进来的字符。
- #include "ti_msp_dl_config.h"
- #include "stdio.h"
- int fputc(int ch, FILE *stream)
- {
- //当串口0忙的时候等待,不忙的时候再发送传进来的字符
- while(DL_UART_isBusy(UART_0_INST)==true){}
- DL_UART_transmitData(UART_0_INST,ch);
- return ch;
- }
- int main(void)
- {
- SYSCFG_DL_init();
- DL_GPIO_clearPins(Blinky_PORT,Blinky_RED_LED_PIN);
- for(int i=0;i<10;i++)
- {
- DL_GPIO_togglePins(Blinky_PORT,Blinky_RED_LED_PIN);
- delay_cycles(10000000);
- printf("Hello\n");
- }
- while(1)
- {
- if( DL_GPIO_readPins(Blinky_PORT,Blinky_Button_PIN) )
- {
- DL_GPIO_setPins(Blinky_PORT,Blinky_RED_LED_PIN);
- }
- else
- {
- DL_GPIO_clearPins(Blinky_PORT,Blinky_RED_LED_PIN);
- //按下按钮就发送一个字母A,然后等待松手。
- DL_UART_transmitData(UART_0_INST,'A');
- printf("Hello\n");
- while(DL_GPIO_readPins(Blinky_PORT,Blinky_Button_PIN)==0);
- }
- }
- }
经过测试可以正常哈喽了。
|