串口打印数据输出

使用MDK的情况

1.使用printf库函数时,要加入头文件

2.另外在keil里面需要把:use MicroLIB 勾选上,不然程序没办法在线调试。编译的时候不会报错。

3.当然可以不用库函数,自己写。正点原子里面的代码:

 //加入以下代码,支持printf函数,而不需要选择use MicroLIB

#if 1
 #pragma import(__use_no_semihosting) 
 //标准库需要的支持函数 
struct __FILE 
{ 
  int handle;

};

FILE __stdout; 
//定义_sys_exit()以避免使用半主机模式 
_sys_exit(int x) 
{ 
  x = x; 
} 
//重定义fputc函数 
int fputc(int ch, FILE *f)
{ 
  while((USART1->SR&0X40)==0);//循环发送,直到发送完毕 
  USART1->DR = (u8) ch; 
  return ch;
}
#endif


/*使用microLib的方法*/

/* 
int fputc(int ch, FILE *f)
{
  USART_SendData(USART1, (uint8_t) ch);

  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}	
  return ch;
}
int GetKey (void)

{

  while (!(USART1->SR & USART_FLAG_RXNE));

  return ((int)(USART1->DR & 0x1FF));
}
*/


不使用printf 自己使用vsprint函数

#include 

uint8_t g_uartPrintStr[256];
void shellPrint(const char *fmt, ...)
{
    va_list ap;
    int i;
	
    va_start(ap, fmt);
    vsprintf((char *)g_uartPrintStr, fmt, ap);
	
    va_end(ap);


    for (i = 0; i < 128; i++)
    {
        if (g_uartPrintStr[i] == '\0')
        {
             break;
        }
    }


    shellSnd((uint8_t *)g_uartPrintStr , i);
}


再增加一个:

/*******************************************************************************
* Function Name  : int fputc(int ch, FILE *f)
* Description    : Retargets the C library printf function to the USART.printfÖض¨Ïò
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
//int fputc(int ch, FILE *f)
//{
//  UART_SendByte(UART_0, (uint8_t) ch); /* Write a character to the USART */
//  while((LPC_UART0->LSR&0x40)==0){}; /* Loop until the end of transmission */
//  return ch;
//}

/*******************************************************************************
* Function Name  : int fgetc(FILE *f)
* Description    : Retargets the C library printf function to the USART.fgetcÖض¨Ïò
* Input          : None
* Output         : None
* Return         : Read a character from the USART
*******************************************************************************/
//int fgetc(FILE *f)
//{
//  while((LPC_UART0->LSR&0x1)==0){};//* Loop until received a char */
//  return (UART_ReceiveByte(UART_0));/* Read a character from the USART and RETURN */
//}


你可能感兴趣的:(串口打印数据输出)