1.STM32F103库函数里面,想用printf 而不是USARTSendData,这样好吗?
2.我在网上搜了这么一段程序,确实可以,但是不知道为什么,USART2的printf打印了12次后就再也不出数字了,串口1可以?为什么啊??
#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__ */
#define CMD_BUFFER_LEN 511
//u8 rev_stop = 0; //接收停止标志
//u8 gps_flag = 0; //GPS处理标志
u8 gpgga_flag = 0;
u8 gprmc_flag = 0;
u8 gpgsv_flag = 0;
GPS_INFO GPS;
void USART2_printf (char *fmt, ...)
{
char buffer[CMD_BUFFER_LEN+1]; // CMD_BUFFER_LEN长度自己定义吧
u8 i = 0;
va_list arg_ptr;
va_start(arg_ptr, fmt);
vsnprintf(buffer, CMD_BUFFER_LEN+1, fmt, arg_ptr);
while ((i < CMD_BUFFER_LEN) && buffer)
{
USART_ClearFlag(USART2,USART_FLAG_TC);
USART_SendData(USART2, (u8) buffer[i++]);
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
}
va_end(arg_ptr);
}
int main()
{
char a ='c';int b = 0;
NVIC_Configuration();
USART1_Configuration();
USART2_Configuration();
USART3_Configuration();
UART4_Configuration();
printf("configure finshed\r\n");
USART2_printf("%c,%d\r\n",a,b ); //12次后不出现了 为什么?
while(1)
{
if((gpgga_flag == 1) || (gprmc_flag == 1) || (gpgsv_flag == 1))
{
printf("latitude:%d.%d.%d_%c\r\n",GPS.latitude_Degree,GPS.latitude_Cent,GPS.latitude_Second,GPS.NS);
printf("longitude:%d.%d.%d_%c\r\n",GPS.longitude_Degree,GPS.longitude_Cent,GPS.longitude_Second,GPS.EW);
printf("speed:%f\r\n",GPS.speed);
printf("direction:%f\r\n",GPS.direction);
printf("satellite_num:%d\r\n",GPS.satellite);
printf("height_ground:%f\r\n",GPS.height_ground);
printf("height_sea:%f\r\n",GPS.height_sea);
printf("Time :%d%d%d-%d%d%d\r\n",GPS.D.year,GPS.D.month,GPS.D.day,GPS.D.hour,GPS.D.minute,GPS.D.second);
gpgga_flag = 0;
gprmc_flag = 0;
gpgsv_flag = 0;
USART_ITConfig(UART4,USART_IT_RXNE,ENABLE); //开启USART2 接收中断
}
}
}
#if 1
#pragma import(__use_no_semihosting)
struct __FILE
{
int handle;
};
FILE __stdout;
_sys_exit(int x)
{
x = x;
}
#endif
/**
* @brief Retargets the C library printf function to the USART1.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_SendData(USART1, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
return ch;
}
|