国民技术N32L40X之printf无法打印问题

近期在调试国民技术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++]);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值