06----GD32E103RBT6----USART测试代码[测试通过]

bsp_usart.h

#ifndef __BSP_USART_H
#define __BSP_USART_H

#include "gd32e10x.h"

  
// USART GPIO 引脚宏定义  
#define  DEBUG_USART_TX_GPIO_CLK   RCU_GPIOA 
#define  DEBUG_USART_TX_GPIO_PORT       GPIOA   
#define  DEBUG_USART_TX_GPIO_PIN        GPIO_PIN_2

#define  DEBUG_USART_RX_GPIO_CLK   RCU_GPIOA  
#define  DEBUG_USART_RX_GPIO_PORT       GPIOA  
#define  DEBUG_USART_RX_GPIO_PIN        GPIO_PIN_3

//MAX485使能 引脚
#define  MAX485_EN_GPIO_CLK     RCU_GPIOC
#define  MAX485_EN_GPIO_PORT       GPIOC
#define  MAX485_EN_GPIO_PIN        GPIO_PIN_8


// 串口1-USART1
#define  DEBUG_USARTx                   USART1
#define  DEBUG_USART_CLK                RCU_USART1
#define  DEBUG_USART_BAUDRATE           9600


#define  DEBUG_USART_IRQ                USART1_IRQn
#define  DEBUG_USART_IRQHandler         USART1_IRQHandler

void USART_TX_GPIO_Config(void);
void USART_RX_GPIO_Config(void);
void MAX485_EN_GPIO_Config(void);
void USART_Config(void);
void USART_SendByte(uint8_t ch);
void USART_SendArray(uint8_t *array, uint16_t num);

#endif

bsp_usart.c

#include "bsp_usart.h"

static void NVIC_Configuration(void)
{
	nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2 );
	nvic_irq_enable(DEBUG_USART_IRQ,1,1);
}

/*!
    \brief      USART_TX引脚 GPIO配置
    \param[in]  none
    \param[out] none
    \retval     none
*/
void USART_TX_GPIO_Config(void)
{
	rcu_periph_clock_enable(DEBUG_USART_TX_GPIO_CLK);
	gpio_init(DEBUG_USART_TX_GPIO_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, DEBUG_USART_TX_GPIO_PIN);
}

/*!
    \brief      USART_RX引脚 GPIO配置
    \param[in]  none
    \param[out] none
    \retval     none
*/
void USART_RX_GPIO_Config(void)
{
	rcu_periph_clock_enable(DEBUG_USART_RX_GPIO_CLK);
	gpio_init(DEBUG_USART_RX_GPIO_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, DEBUG_USART_RX_GPIO_PIN);
}

/*!
    \brief      MAX485 发送/接收切换引脚 GPIO配置
    \param[in]  none
    \param[out] none
    \retval     none
*/
void MAX485_EN_GPIO_Config(void)
{
	rcu_periph_clock_enable(MAX485_EN_GPIO_CLK);
	gpio_init(MAX485_EN_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, MAX485_EN_GPIO_PIN);
}

/*!
    \brief      USART工作参数配置
    \param[in]  none
    \param[out] none
    \retval     none
*/
void USART_Config(void)
{
	rcu_periph_clock_enable(DEBUG_USART_CLK);
	NVIC_Configuration();
	
	usart_baudrate_set(DEBUG_USARTx,DEBUG_USART_BAUDRATE);
	usart_parity_config(DEBUG_USARTx,USART_PM_NONE);
	usart_word_length_set(DEBUG_USARTx,USART_WL_8BIT);
	usart_stop_bit_set(DEBUG_USARTx, USART_STB_1BIT);
	usart_hardware_flow_rts_config(DEBUG_USARTx, USART_RTS_DISABLE); 
  usart_hardware_flow_cts_config(DEBUG_USARTx, USART_CTS_DISABLE); 
  usart_receive_config(DEBUG_USARTx, USART_RECEIVE_ENABLE); 
  usart_transmit_config(DEBUG_USARTx, USART_TRANSMIT_ENABLE); 
	usart_data_first_config(DEBUG_USARTx, USART_MSBF_LSB );
	usart_enable(DEBUG_USARTx); 
	
	usart_interrupt_enable(DEBUG_USARTx, USART_INT_RBNE);
}

/*!
    \brief     发送一个字节的数据
    \param[in]  ch:需发送的单字节数据
    \param[out] none
    \retval     none
*/
void USART_SendByte(uint8_t ch)
{
	usart_data_transmit(DEBUG_USARTx,ch);
	while (RESET == usart_flag_get(DEBUG_USARTx, USART_FLAG_TBE)); 
}

/*!
    \brief     发送一个数字的数据
    \param[in]  array:数组
    \param[in]  num:数组大小
    \param[out] none
    \retval     none
*/
void USART_SendArray(uint8_t *array, uint16_t num)
{
	uint8_t i;
	for(i=0; i<num; i++)
     USART_SendByte(array[i]);
	/* 等待发送完成,下面这句不能省,否则不能接受到ADU */
	while(usart_flag_get(DEBUG_USARTx,USART_FLAG_TC)==RESET);
}

main.c

uint8_t ay[8]={0x01,0x06,0x00,0xB6,0x00,0x01,0xA9,0xEC};
uint8_t by[8]={0x01,0x06,0x00,0x56,0x04,0x4C,0x6A,0xEF};
uint8_t hui[20];

void test_usart(void)
{
	USART_TX_GPIO_Config();
	USART_RX_GPIO_Config();
	MAX485_EN_GPIO_Config();
	USART_Config();
	
	gpio_bit_set(MAX485_EN_GPIO_PORT,MAX485_EN_GPIO_PIN);
	USART_SendArray(by,8);
	gpio_bit_reset(MAX485_EN_GPIO_PORT,MAX485_EN_GPIO_PIN);
	
}

gd32e10x_it.c

extern uint8_t hui[20];
uint8_t hui_element_Index=0;

void  DEBUG_USART_IRQHandler (void)
{
	//当读取数据非空时执行下面操作
	if(usart_interrupt_flag_get(DEBUG_USARTx,USART_INT_FLAG_RBNE)==SET)
		hui[hui_element_Index++] = usart_data_receive(DEBUG_USARTx);
	usart_interrupt_flag_clear(DEBUG_USARTx,USART_INT_FLAG_RBNE);
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值