- /*
- @hehung
- 2023-5-22
- email: 1398660197@qq.com
- wechat: hehung95
- reproduced and please indicate the source @hehung
- */
- #include <stdbool.h>
- #include "ac780x_gpio.h"
- #include "ac780x_uart.h"
- #include "ac780x_uart_reg.h"
- #include "app_uart.h"
- // UART initialization
- void UART_Cfg_Init(void)
- {
- UART_ConfigType uart_config;
- GPIO_SetFunc(GPIOA, GPIO_PIN4, GPIO_FUN3); /*! uart tx */
- GPIO_SetFunc(GPIOA, GPIO_PIN5, GPIO_FUN3); /*! uart rx */
- uart_config.baudrate = 115200; /*! baudrate 115200 */
- uart_config.dataBits = UART_WORD_LEN_8BIT; /*! data leng th8bit */
- uart_config.stopBits = UART_STOP_1BIT; /*! 停止位1bit */
- uart_config.fifoByteEn = DISABLE;
- uart_config.sampleCnt = UART_SMP_CNT0; /*! 16倍采样 */
- uart_config.callBack = NULL; /*! 不设置回调函数 */
- UART_Init(UART1,&uart_config);
- }
- // fputs for printf or other print function in standard
- int fputc(int ch, FILE *f)
- {
- UART_SendData(UART1, ch); /*! 发送数据 */
- while (!UART_TxIsEmpty(UART1)) {}; /*! 等待发送缓冲区为空 */
- return ch;
- }
- // fgets for scanf or other input function in standard
- int fgetc(FILE *f)
- {
- (void)f;
- uint8_t ch;
- while(!UART_RxIsDataReady(UART1)) {}; /*! 等待串口1接受到数据 */
- ch = UART_ReceiveData(UART1); /*! 读取数据 */
- return (int)ch;
- }
主函数实现进行测试:
- int main(void)
- {
- InitDelay();
-
- UART_Cfg_Init(); /*! 串口1初始化 */
- printf ("This is a printf and scanf TEST for ac7802x\r\n");
- int a;
- printf ("Please input a number with int type\r\n");
- scanf ("%d", &a);
- printf ("The number you input is: %d\r\n", a);
- while(1)
- {
-
- }
- }
# 3 注意事项
## 3.1 勾选Use MicroLIB
需要使用KEIL下的printf和scanf功能,还需要勾选 `Use MicroLIB`,不然程序会卡死。
# 4 实验效果
如下为测试结果,可以看到,试验成功,后续就可以使用printf来输出一些调试信息,使用scanf来接受控制命令进行调试了。