近期在调试国民技术N32L406CB时遇到了printf无法打印问题,后面与国民技术的demo相对比终于发现了问题。
由于我们使用了printf C库函数,需要在MDK的Target面板中选中Use MicroLIB,将C库添加到工程中。
新建一个工程时时不会勾选Use MicroLIB,所以我们需要在工程文件中把其勾选上。
代码如下
int main(void)
{
USART1_Configuration();
USART2_Configuration();
NVIC_Uart_Configuration();
Sys_GPIO_Init();
ADC_Initial();
I2C1_GPIO_Init();
I2C1_AW9523_Init();
I2C2_GPIO_Init();
I2C2_AW9523_Init();
I2C3_GPIO_Init();
I2C3_AW9523_Init();
DEBUG_MSG("Project Starting\r\n");
while (1)
{
}
}
串口初始化,串口重定向
#define DBG(format, ...) printf(format, ##__VA_ARGS__)
#define DEBUG_TRACE() printf("%s(%d)%s()\r\n", __MODULE__, __LINE__, __FUNCTION__)
#define DEBUG_WARNING(fmt, arg...) do {printf("\r\n%s(%d)WARNING:"fmt, __MODULE__, __LINE__,##arg);}while(0)
#define DEBUG_ERROR(fmt, arg...) do {printf("\r\n%s(%d)ERROR:"fmt, __MODULE__, __LINE__,##arg);}while(0)
#define DEBUG_MSG(fmt, arg...) printf("%s(%d):"fmt, __MODULE__, __LINE__, ##arg);
#define DEBUG_PRINTF(fmt, arg...) printf(fmt, ##arg)
void NVIC_Uart_Configuration(void)
{
NVIC_InitType NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ConfigInt(USART1, USART_INT_RXDNE, ENABLE);
}
void USART1_Configuration(void)
{
USART_InitType USART_InitStructure;
GPIO_InitType GPIO_InitStructure;
GPIO_InitStruct(&GPIO_InitStructure);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_USART1, ENABLE);
GPIO_InitStructure.Pin = GPIO_PIN_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF4_USART1;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_10;
GPIO_InitStructure.GPIO_Pull = GPIO_Pull_Up;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF4_USART1;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
USART_StructInit(&USART_InitStructure);
USART_InitStructure.BaudRate = 115200;
USART_InitStructure.WordLength = USART_WL_8B;
USART_InitStructure.StopBits = USART_STPB_1;
USART_InitStructure.Parity = USART_PE_NO;
USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
USART_InitStructure.Mode = USART_MODE_RX | USART_MODE_TX;
USART_Init(USART1, &USART_InitStructure);
USART_Enable(USART1, ENABLE);
}
void USART2_Configuration(void)
{
USART_InitType USART_InitStructure;
GPIO_InitType GPIO_InitStructure;
GPIO_InitStruct(&GPIO_InitStructure);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_USART2, ENABLE);
GPIO_InitStructure.Pin = GPIO_PIN_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF4_USART2;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_5;
GPIO_InitStructure.GPIO_Pull = GPIO_Pull_Up;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF4_USART2;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
USART_StructInit(&USART_InitStructure);
USART_InitStructure.BaudRate = 115200;
USART_InitStructure.WordLength = USART_WL_8B;
USART_InitStructure.StopBits = USART_STPB_1;
USART_InitStructure.Parity = USART_PE_NO;
USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
USART_InitStructure.Mode = USART_MODE_RX | USART_MODE_TX;
USART_Init(USART2, &USART_InitStructure);
USART_Enable(USART2, ENABLE);
}
int fputc(int ch, FILE* f)
{
USART_SendData(USART2, (uint8_t)ch);
while (USART_GetFlagStatus(USART2, USART_FLAG_TXDE) == RESET);
return (ch);
}
void USART1_SendData_Pack(uint8_t* Data, uint16_t len)
{
uint16_t temp = 0;
while(len--)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TXDE) == RESET);
USART_SendData(USART1, Data[temp++]);
}
}