规范库函数的默许输出设备是显示器,要实现在串口或LCD输出,有必要重界说规范库函数里调用的与输出设备相关的函数.
例如:printf输出到串口,需要将fputc里边的输出指向串口(重定向),办法如下:
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to Yes) calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
return ch;
}
因printf()之类的函数,运用了半主机形式。运用规范库会导致程序无法运转,以下是解决办法:
办法1.运用微库,由于运用微库的话,不会运用半主机形式.
办法2.依然运用规范库,在主程序增加下面代码:
#pragmaimport(__use_no_semihosting)
_sys_exit(intx)
{
x=x;
}
struct__FILE
{
inthandle;
/*Whateveryourequirehere.Iftheonlyfileyouareusingis*/
/*standardoutputusingprintf()fordebugging,nofilehandling*/
/*isrequired.*/
};
/*FILEistypedef’dinstdio.h.*/
FILE__stdout;