TA的每日心情 | 开心 2020-11-23 14:54 |
---|
签到天数: 19 天 连续签到: 1 天 [LV.4]偶尔看看III
|
呵呵,有个函数很关键:- static char _buffer[256];
- void EvbUart1Printf(char* fmt, ...)
- {
- int i;
- va_list ap;
- va_start(ap, fmt);
- vsprintf(_buffer, fmt, ap);
- va_end(ap);
- for (i = 0; _buffer[i] != '\0'; i++)
- {
- EvbUart1WriteByte(_buffer[i]);
- }
- }
复制代码 该函数必须包含
#include <stdio.h>#include <stdlib.h>
#include <stdarg.h>
以上三个头文件,有了它,你就可以直接用printf了,不需要选上MicroLib
原理很简单,高手略过,见笑了,嘻嘻~~~- /*************************************************************************************************
- * Trochili RTOS Kernel *
- * Copyright(C) 2015 LIUXUMING *
- * www.trochili.com *
- *************************************************************************************************/
- #include "colibri_bsp_uart.h"
- void Uart1Config(u32 buad)
- {
- /* Configure the GPIO ports */
- GPIO_InitPara GPIO_InitStructure;
- USART_InitPara USART_InitStructure;
- //NVIC_InitPara NVIC_InitStructure;
- /* Enable GPIOA clock */
- RCC_APB2PeriphClock_Enable( RCC_APB2PERIPH_GPIOB, ENABLE );
- /* Enable USART1 APB clock */
- RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_USART1, ENABLE );
- RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_AF, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_PIN_6 ;
- GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
- GPIO_Init( GPIOB , &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_PIN_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IN_FLOATING;;
- GPIO_Init( GPIOB , &GPIO_InitStructure);
- GPIO_PinRemapConfig(GPIO_REMAP_USART1, ENABLE);
-
- //USART_DeInit(USART1 );
- USART_InitStructure.USART_BRR = 115200;
- USART_InitStructure.USART_WL = USART_WL_8B;
- USART_InitStructure.USART_STBits = USART_STBITS_1;
- USART_InitStructure.USART_Parity = USART_PARITY_RESET;
- USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE;
- USART_InitStructure.USART_RxorTx = USART_RXORTX_RX | USART_RXORTX_TX;
- USART_Init(USART1, &USART_InitStructure);
- /* USART enable */
- USART_Enable(USART1, ENABLE);
-
- #if 0
- /* Enable the USARTx Interrupt */
- NVIC_InitStructure.NVIC_IRQ = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQPreemptPriority = 0;
- NVIC_InitStructure.NVIC_IRQSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQEnable = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- /* Enable the USART1 Receive interrupt */
- USART_INT_Set(USART1, USART_INT_RBNE, ENABLE );
- USART_INT_Set(USART1, USART_INT_IDLEF, ENABLE );
-
- NVIC_InitPara NVIC_InitStructure;
-
- #endif
- }
- /*************************************************************************************************
- * 功能:向串口1发送一个字符 *
- * 参数:(1) 需要被发送的字符 *
- * 返回: *
- * 说明: *
- *************************************************************************************************/
- void EvbUart1WriteByte(char c)
- {
- USART_DataSend(USART1, c);
- while (USART_GetBitState(USART1, USART_FLAG_TBE) == RESET);
- }
- void EvbUart1WriteByte2(char c)
- {
- USART_DataSend(USART1, c);
- }
- /*************************************************************************************************
- * 功能:向串口1发送一个字符串 *
- * 参数:(1) 需要被发送的字符串 *
- * 返回: *
- * 说明: *
- *************************************************************************************************/
- void EvbUart1WriteStr(const char* str)
- {
- while (*str)
- {
- USART_DataSend(USART1, * str++);
- while (USART_GetBitState(USART1, USART_FLAG_TBE) == RESET);
- }
- }
- /*************************************************************************************************
- * 功能:从串口1接收一个字符 *
- * 参数:(1) 存储接收到的字符 *
- * 返回: *
- * 说明: *
- *************************************************************************************************/
- void EvbUart1ReadByte(char* c)
- {
- while (USART_GetBitState(USART1, USART_FLAG_RBNE) == RESET);
- *c = (USART_DataReceive(USART1));
- }
- static char _buffer[256];
- void EvbUart1Printf(char* fmt, ...)
- {
- int i;
- va_list ap;
- va_start(ap, fmt);
- vsprintf(_buffer, fmt, ap);
- va_end(ap);
- for (i = 0; _buffer[i] != '\0'; i++)
- {
- EvbUart1WriteByte(_buffer[i]);
- }
- }
- void EvbUart1EnableRxInt(void)
- {
- /* Enable the USART1 Receive interrupt */
- USART_INT_Set( USART1, USART_INT_RBNE, ENABLE );
- }
- void EvbUart1EnableTxInt(void)
- {
- /* Enable the USART1 Transmoit interrupt */
- USART_INT_Set( USART1, USART_INT_TBE, ENABLE );
- }
复制代码 头文件:- #ifndef _TCL_COLIBRI_UART_H
- #define _TCL_COLIBRI_UART_H
- #include "gd32f20x.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdarg.h>
- #define printf EvbUart1WriteStr
- extern void Uart1Config(u32 buad);
- extern void EvbUart1ReadByte(char* c);
- extern void EvbUart1WriteByte(char c);
- extern void EvbUart1WriteStr(const char* str);
- extern void EvbUart2Config(void);
- extern void EvbUart2ReadByte(char* c);
- extern void EvbUart2WriteByte(char c);
- extern void EvbUart2WriteStr(const char* str);
- extern void EvbUart1Printf(char* fmt, ...);
- #endif /* _TCL_COLIBRI_UART_H */
复制代码
原工程代码下载:
|
|