- Use MicroLIB
[Project] -> Options for Target ... -> [Target] -> [Use MicroLIB]
圖一 |
- Implement PUTCHAR_PROTOTYPE & GETCHAR_PROTOTYPE
- 主程式 initial usart & 直接使用printf & scanf
#include <stdio.h>
#define USART_TYPE USART1
#ifdef __GNUC__
#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__ */
// Implement
PUTCHAR_PROTOTYPE
{
/* Write a character to the USART */
USART_SendData(USART_TYPE, (u8) ch);
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART_TYPE, USART_FLAG_TXE) == RESET);
return ch;
}
GETCHAR_PROTOTYPE
{
/* Loop until the end of receive */
while(USART_GetFlagStatus(USART_TYPE, USART_FLAG_RXNE) == RESET);
/* Read a character to the USART */
return (USART_ReceiveData(USART_TYPE));
}
#define USART_TYPE USART1
#ifdef __GNUC__
#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__ */
// Implement
PUTCHAR_PROTOTYPE
{
/* Write a character to the USART */
USART_SendData(USART_TYPE, (u8) ch);
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART_TYPE, USART_FLAG_TXE) == RESET);
return ch;
}
GETCHAR_PROTOTYPE
{
/* Loop until the end of receive */
while(USART_GetFlagStatus(USART_TYPE, USART_FLAG_RXNE) == RESET);
/* Read a character to the USART */
return (USART_ReceiveData(USART_TYPE));
}
#include<stdio.h>
int main()
{
uint8_t data;
USART_Init(); // 注意,記得要啟動要導向裝置
while(1) {
scanf("%c\n",&data);
printf("Data = %c",data);
}
}
int main()
{
uint8_t data;
USART_Init(); // 注意,記得要啟動要導向裝置
while(1) {
scanf("%c\n",&data);
printf("Data = %c",data);
}
}
Reference:
[1] 在MDK使用printf&scanf
請問 scanf() 是否屬於 polling 方式, 會導致CPU 執行至 scanf() 等待輸入?
回覆刪除這個方法確實是使用polling的方式,由於當初只是為了debug,故並沒有考慮任何的效能問題。
回覆刪除没有microlib
回覆刪除