2011年10月12日 星期三

在MDK使用printf&scanf


在project的option for...分頁Target在Code Generation勾選Use MicroLIB.



在C Code加入程式碼,將printf、scanf底層函式fputc、fgetc導向USART

/* Includes ------------------------------------------------------------------*/

#include "stdio.h"

/* Private function prototypes -----------------------------------------------*/
#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)
  #define GETCHAR_PROTOTYPE int __io_getchar(void)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  #define GETCHAR_PROTOTYPE int fgetc(FILE *f)
#endif /* __GNUC__ */

/*******************************************************************************
* Function Name  : PUTCHAR_PROTOTYPE
* Description    : Retargets the C library printf function to the USART.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
PUTCHAR_PROTOTYPE
{
  /* Write a character to the USART */
  USART_SendData(USARTx, (u8) ch);

  /* Loop until the end of transmission */
  while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET)
  {
  }

  return ch;
}

/*******************************************************************************
* Function Name  : GETCHAR_PROTOTYPE
* Description    : Retargets the C library scanf function from the USART.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
GETCHAR_PROTOTYPE
{
   /* Loop until the end of receive */
   while(!(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET))
   {
   }
  
   /* Read a character to the USART */
   return (USART_ReceiveData(USART1));
}

沒有留言:

張貼留言