栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

1.国民技术N32G45X例程之-串口打印

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

1.国民技术N32G45X例程之-串口打印

国民技术N32G45X例程之-串口打印
提示:use MicroLIB,printf串口打印

文章目录
  • 前言
  • 一、国民技术N32G45X串口配置
  • 二、printf函数
    • 1.国民技术N32G45X官方库默认勾选use MicroLIB,所以代码中没有printf函数支持库。
    • 2.改成加入printf函数支持库,这样才能正常运行。
    • 3.在KEIL中取消勾选use MicroLIB
  • 三、 例程下载


前言

提示:国民技术N32G45X官方库默认都是选择use MicroLIB,当取消勾选use MicroLIB后,程序不能正常运行,主要的问题还是在串口打印printf函数上,KEIL在编译时没有找到printf函数支持的库。


提示:以下是本篇文章正文内容,下面案例可供参考

一、国民技术N32G45X串口配置

示例:配置USART1。

void Uart1_Init(void)
{
    GPIO_InitType GPIO_InitStructure;
    USART_InitType USART_InitStructure;
    
    // Turn on the clock of usart port GPIO
    USART_GPIO_APBxClkCmd(USART_GPIO_CLK, ENABLE);

    // Turn on the clock of usart peripheral
    USART_APBxClkCmd(USART_CLK, ENABLE);

    // Configure GPIO of USART TX as push pull multiplexing mode
    GPIO_InitStructure.Pin              = USART_TX_GPIO_PIN;
    GPIO_InitStructure.GPIO_Mode        = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed       = GPIO_Speed_50MHz;
    GPIO_InitPeripheral(USART_TX_GPIO_PORT, &GPIO_InitStructure);

    // Configure GPIO of USART RX as floating input mode
    GPIO_InitStructure.Pin              = USART_RX_GPIO_PIN;
    GPIO_InitStructure.GPIO_Mode        = GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Speed       = GPIO_INPUT;
    GPIO_InitPeripheral(USART_RX_GPIO_PORT, &GPIO_InitStructure);

    // Configure parameters of usart port
    // Configure baud rate
    USART_InitStructure.BaudRate = USART_BAUDRATE;
    // Configure the length of frame data
    USART_InitStructure.WordLength = USART_WL_8B;
    // Configure stop bits
    USART_InitStructure.StopBits = USART_STPB_1;
    // Configure check bit
    USART_InitStructure.Parity = USART_PE_NO;
    // Configure hardware flow control
    USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
    // Configure working mode, send and receive together
    USART_InitStructure.Mode = USART_MODE_RX | USART_MODE_TX;
    // Complete initialization configuration of usart port
    USART_Init(USARTx, &USART_InitStructure);

    // Configuration interrupt priority of the usart port
//    NVIC_Configuration();

    // Enable usart port receive interrupt
    //  USART_ConfigInt(DEBUG_USARTx, USART_INT_RXDNE, ENABLE);

    // Enable usart
    USART_Enable(USARTx, ENABLE);
}
二、printf函数 1.国民技术N32G45X官方库默认勾选use MicroLIB,所以代码中没有printf函数支持库。

代码如下(示例):

int fputc(int ch, FILE* f)
{
    USART_SendData(USARTx, (uint8_t)ch);
    while (USART_GetFlagStatus(USARTx, USART_FLAG_TXDE) == RESET)
        ;

    return (ch);
}
2.改成加入printf函数支持库,这样才能正常运行。

代码如下(示例):

//加入以下代码,支持printf函数,而不需要选择use MicroLIB	  
#if 1

#pragma import(__use_no_semihosting) 
	
//标准库需要的支持函数                 
struct __FILE 
{ 
	int handle; 
}; 

FILE __stdout; 

//定义_sys_exit()以避免使用半主机模式    
void _sys_exit(int x) 
{ 
	x = x; 
} 

//重定向c库函数printf到串口,重定向后可使用printf函数
int fputc(int ch, FILE *f)
{
	
	USART_SendData(USARTx, (uint8_t) ch);
		
	
	while (USART_GetFlagStatus(USARTx, USART_FLAG_TXDE) == RESET);		
	
	return (ch);
}

///重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数
int fgetc(FILE *f)
{
	
	while (USART_GetFlagStatus(USARTx, USART_FLAG_TXDE) == RESET);

	return (int)USART_ReceiveData(USARTx);
}
#endif

3.在KEIL中取消勾选use MicroLIB


三、 例程下载

国民技术N32G45X例程之-串口打印
https://download.csdn.net/download/suqingxiao/68829874

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/690401.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号