GD32串口通信PB6,PB7

我发现GD32很多接口都需要冲映射,刚开始还是不习惯,还要打开要选打开AFIO时钟。算了,直接看代码:

1,usart.c

//#include "usart.h"

//void USART_GPIO_init(void)
//{
//	//初始化引脚
//	rcu_periph_clock_enable(RCU_USART1);
	gpio_pin_remap_config(GPIO_USART1_REMAP,ENABLE); 
//	
//	rcu_periph_clock_enable(GPIO_TX_CLK);//使能时钟
//	gpio_init(GPIO_RX_PORT,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_RX_PIN);//配置引脚;模式;速度
//	gpio_init(GPIO_TX_PORT,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_TX_PIN);
//	
//	
//	//配置串口
//	usart_deinit(USART1);
//	usart_baudrate_set(USART1,115200);//115200
//	usart_parity_config(USART1,USART_PM_NONE);//没有校验位
//	usart_word_length_set(USART1,USART_WL_8BIT);//8位传输
//	usart_stop_bit_set(USART1,USART_STB_1BIT);//1位停止位
//	usart_hardware_flow_rts_config(USART1, USART_RTS_DISABLE);
//    usart_hardware_flow_cts_config(USART1, USART_CTS_DISABLE);
//    usart_receive_config(USART1, USART_RECEIVE_ENABLE);
//    usart_transmit_config(USART1, USART_TRANSMIT_ENABLE);
//	usart_enable(USART1);
//}


/*********************************************************************
 * INCLUDES
 */
#include <stdio.h>
#include "gd32f10x.h"

#include "usart.h"

/*********************************************************************
 * PUBLIC FUNCTIONS
 */
/**
 @brief 串口驱动初始化
 @param 无
 @return 无
*/
void USART_Init(void)
{
	

	/*要选打开AFIO时钟,然后再打开GPIO时钟,然后再重映射GPIO,然后再使能UART0时钟,最后再配置UART0参数。*/
	  rcu_periph_clock_enable(RCU_AF);//Need!!!!
	
	  rcu_periph_clock_enable(RCU_GPIOB);
 
	  gpio_pin_remap_config(GPIO_USART0_REMAP,ENABLE);
	
    /* enable USART clock */
    rcu_periph_clock_enable(RCU_USART0);
	
    /* connect port to USARTx_Tx */
    gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
 
    /* connect port to USARTx_Rx */
    gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
	
		gpio_pin_remap_config(GPIO_USART0_REMAP,ENABLE);
	
 
    /* USART configure */
    usart_deinit(USART0);
    usart_baudrate_set(USART0, 115200U);
    usart_receive_config(USART0, USART_RECEIVE_ENABLE);
    usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
		
		//usart_parity_config(USART0,USART_PM_EVEN);
		//usart_word_length_set(USART0,USART_WL_8BIT);
		//usart_stop_bit_set(USART0,USART_STB_1BIT);
		
		
    usart_enable(USART0);
		
		//nvic_irq_enable(USART0_IRQn, 3U, 3U);
		//usart_interrupt_enable(USART0,USART_INT_RBNE);
}

/**
 @brief 串口写入数据
 @param pData -[in] 写入数据
 @param dataLen -[in] 写入数据长度
 @return 无
*/
void UART_Write(uint8_t *pData, uint32_t dataLen)
{
    uint8_t i;	
    for(i = 0; i < dataLen; i++)
    {
        usart_data_transmit(USART0, pData[i]);                  // 发送一个字节数据
        while(RESET == usart_flag_get(USART0, USART_FLAG_TBE)); // 发送完成判断
    }
}

/**
  * @brief 重定向c库函数printf到USARTx
  * @retval None
  */
int fputc(int ch, FILE *f)
{
    usart_data_transmit(USART0, (uint8_t)ch);
    while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
    return ch;
}

/**
  * @brief 重定向c库函数getchar,scanf到USARTx
  * @retval None
  */
int fgetc(FILE *f)
{
    uint8_t ch = 0;
    ch = usart_data_receive(USART0);
    return ch;
}

/****************************************************END OF FILE****************************************************/

2,main.c

/*!
    \file    main.c
    \brief   led spark with systick, USART print and key example

    \version 2024-01-05, V2.3.0, firmware for GD32F10x
*/

/*
    Copyright (c) 2024, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/

#include "gd32f10x.h"
#include "systick.h"
#include <stdio.h>
#include "main.h"
#include "gd32f10x_eval.h"
#include "led.h"
#include "usart.h"


/*!
    \brief      toggle the led every 500ms
    \param[in]  none
    \param[out] none
    \retval     none
*/
void led_spark(void)
{
    static __IO uint32_t timingdelaylocal = 0U;

    if(timingdelaylocal){

        if(timingdelaylocal < 500U){
            gd_eval_led_on(LED2);
        }else{
            gd_eval_led_off(LED2);
        }

        timingdelaylocal--;
    }else{
        timingdelaylocal = 1000U;
    }
}

/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/

void delay(void)
{
	uint16_t i=0,j=0;
	for(i=0;i<0xffff;i++)
		for(j=0;j<30;j++);
}




int main(void)
{
	
	 LedGPIO_init();
	 USART_Init();

	printf("usart1_test\r\n");
	while(1)
	{
		printf("usart1_test\r\n");
//		usart_data_transmit(USART0,1);
		delay();
//		  gpio_bit_set(GPIOB,GPIO_PIN_4);
//			delay();
//			gpio_bit_reset(GPIOB,GPIO_PIN_4);
//			delay();
	}
}


 

3,实验结果

 

本文主要记录代码,不讲原理。 

相关推荐

  1. linux下,PC串口通信开发

    2024-03-23 22:14:03       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-23 22:14:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-23 22:14:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-23 22:14:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-23 22:14:03       18 阅读

热门阅读

  1. RedisTemplate的一些常用方法

    2024-03-23 22:14:03       19 阅读
  2. C++基础入门(命名空间,函数,引用)

    2024-03-23 22:14:03       20 阅读
  3. 正则化和贝叶斯的关系

    2024-03-23 22:14:03       17 阅读
  4. SpringMVC 中的常用注解和用法

    2024-03-23 22:14:03       17 阅读
  5. Python-元组(Tuple)列表(List)的区别

    2024-03-23 22:14:03       16 阅读
  6. 如何了解AI基础概念

    2024-03-23 22:14:03       17 阅读
  7. Python-元祖-Tuple

    2024-03-23 22:14:03       17 阅读
  8. 富格林:出金不顺谨防虚假受害

    2024-03-23 22:14:03       21 阅读
  9. 大模型日报2024-03-23

    2024-03-23 22:14:03       22 阅读
  10. sentinel系统规则

    2024-03-23 22:14:03       18 阅读
  11. React——class组件中setState修改state

    2024-03-23 22:14:03       17 阅读
  12. 中国公司在美国上市公司统计

    2024-03-23 22:14:03       20 阅读
  13. LeetCode162. 寻找峰值

    2024-03-23 22:14:03       18 阅读
  14. NAT技术

    NAT技术

    2024-03-23 22:14:03      17 阅读
  15. 【AIGC工具】MonicAi — 可定制的AI学习工具

    2024-03-23 22:14:03       21 阅读