(1)問題描述: 多串口共用printf函數(shù),百度到的資料大部分是建議重新寫一個xx_printf(format, …)。但是使用起來還是不方便,就此問題而言加上一個判斷語句便可解決。
(2)解決方法: printf函數(shù)最后調(diào)用的是int fputc(int ch, FILE *f),那么重新改寫此函數(shù)便可。
(3)代碼:
//標志量定義
int USART_PRINTF_FLAG = 2;//默認串口2
//改寫
fputcint fputc(int ch, FILE *f)
{
if (USART_PRINTF_FLAG == 2)
{
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
USART_SendData(USART2,(uint8_t)ch);
}
else
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
USART_SendData(USART1,(uint8_t)ch);
}
return ch;
}
//中斷處理
void USART1_IRQHandler(void)
{
USART_PRINTF_FLAG = 1;
//your coding here.。.
}
void USART2_IRQHandler(void)
{
USART_PRINTF_FLAG = 2;
//your coding here.。.
}
-
STM32
+關(guān)注
關(guān)注
2293文章
11032瀏覽量
364781 -
串口
+關(guān)注
關(guān)注
15文章
1588瀏覽量
79917
原文標題:STM32多串口共用printf打印串口數(shù)據(jù)
文章出處:【微信號:gh_dae0718828df,微信公眾號:gh_dae0718828df】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
如何利用51單片機實現(xiàn)自己的串口發(fā)送函數(shù)和printf函數(shù)同時共用?
求大神解答如何去實現(xiàn)STM32多串口的printf函數(shù)呢
基于STM32的printf串口數(shù)據(jù)輸出

STM32中使用printf打印串口數(shù)據(jù)的實現(xiàn)原理及方法
STM32單片機IAR環(huán)境下重定向printf函數(shù)

STM32使用串口重定向系統(tǒng)printf函數(shù)輸出時出現(xiàn)一初始化或使用printf函數(shù)系統(tǒng)卡死的原因及解決辦法

STM32F103串口1 printf函數(shù)的實現(xiàn)

STM32單片機基礎(chǔ)09——重定向printf函數(shù)到串口輸出的多種方法

stm32單片機串口使用printf及u3_printf

stm32printf函數(shù)的串口輸出代碼

STM32中串行通訊中printf函數(shù)的使用

stm32使用printf實現(xiàn)串口打印原理

評論