
引用正点原子的printf程序,串口1已正常配制了,发送接收数据正常,现想用printf信息,设置如下 #include "stdio.h"已在头文件中 //加入以下代码,支持printf函数,而不需要选择use MicroLIB #if 1 #pragma import(__use_no_semihosting) //标准库需要的支持函数 struct __FILE { int handle; /* Whatever you require here. If the only file you are using is */ /* standard output using printf() for debugging, no file handling */ /* is required. */ }; FILE __stdout; /* FILE is typedef’ d in stdio.h. */ _sys_exit(int x) //定义_sys_exit()以避免使用半主机模式 { x = x; } //重定向fputc函数 //printf的输出,指向fputc,由fputc输出到串口 //这里使用串口1(USART1)输出printf信息 int fputc(int ch, FILE *f) { while((USART1->SR&0X40)==0);//等待上一次串口数据发送完成 USART1->DR = (u8) ch; //写DR,串口1将发送数据 return ch; } #endif 在主函数里引用printf("OK\n"); 程序会跑不起来,去掉这条运行正常,有人遇到同样的问题吗? |
评分
查看全部评分
评分
查看全部评分
int fputc(int ch,FILE *f)
{
USART_SendData(USART1, ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET) { }
return(ch);
}
其中USART_SendData(USART1, ch);是库函数,如下:
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_DATA(Data));
/* Transmit Data */
USARTx->DR = (Data & (uint16_t)0x01FF);
}
其中USART_GetFlagStatus(USART1, USART_FLAG_TC)是标准库的,代码如下:
FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
{
FlagStatus bitstatus = RESET;
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_FLAG(USART_FLAG));
/* The CTS flag is not available for UART4 and UART5 */
if (USART_FLAG == USART_FLAG_CTS)
{
assert_param(IS_USART_123_PERIPH(USARTx));
}
if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)
{
bitstatus = SET;
}
else
{
bitstatus = RESET;
}
return bitstatus;
}
自己看看。代码虽然多了很多,但是是一个能打印的函数。在精简代码过程中,想想哪些能省,哪些不能省。
还有,最好先确定你的串口配置正确,也就是能发送正常。
评分
查看全部评分