基本设置
串口回环:
串口IO设置
IO的配置:
void gd_eval_com_init(uint32_t com)
{
/* enable GPIO clock */
uint32_t COM_ID;
if(EVAL_COM1 == com)
{
COM_ID = 0U;
}
rcu_periph_clock_enable( EVAL_COM_GPIO_CLK);
/* enable USART clock */
rcu_periph_clock_enable(COM_CLK[COM_ID]);
/* connect port to USARTx_Tx */
gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF, COM_TX_PIN[COM_ID]);
/* connect port to USARTx_Rx */
gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF, COM_RX_PIN[COM_ID]);
/* configure USART Tx as alternate function push-pull */
gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP,COM_TX_PIN[COM_ID]);
gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,COM_TX_PIN[COM_ID]);
/* configure USART Rx as alternate function push-pull */
gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP,COM_RX_PIN[COM_ID]);
gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,COM_RX_PIN[COM_ID]);
/* USART configure */
usart_deinit(com);
usart_baudrate_set(com,115200U);
usart_receive_config(com, USART_RECEIVE_ENABLE);
usart_transmit_config(com, USART_TRANSMIT_ENABLE);
usart_enable(com);
nvic_irq_enable(USART1_IRQn, 0, 0);
}
配置的是COM1;
PA2 —TX ;
PA3 — RX;
串口1 PA9、PA10不知道为什么实现不了;在GD32450Demo板上实现不了;
接收设置
接收可中断接收、也可以论询接收;
中断接收
注意在中断中接收的话:
要打开NVIC并设置USART1的优先级;
nvic_irq_enable(USART1_IRQn, 0, 0);
基本配置 :
设置能发送、能接收;
波特率、停止位;
中断接收:
接收重点是要打开串口函数:
一旦有数据就会有串口中断,
usart_interrupt_enable(USART1 , USART_INTEN_RBNEIE);
然后在中断中接收:
void USART1_IRQHandler(void)
{
unsigned char data;
if(RESET != usart_interrupt_flag_get(USART1, USART_INT_RBNEIE))
{
data = usart_data_receive(USART1);
usart_data_transmit(USART1, (uint8_t)data);
while(RESET == usart_flag_get(USART1, USART_FLAG_TBE));
}
}
接收又回复之后立马回复、完成回环测试;;
论询接收
就是不打开中断 要在程序中论询:
一直查询接收buff是否是为空状态;
if(RESET != usart_interrupt_flag_get(USART1, USART_INT_RBNEIE))
{
data = usart_data_receive(USART1);
usart_data_transmit(USART1, (uint8_t)data);
while(RESET == usart_flag_get(USART1, USART_FLAG_TBE));
}
基本设置
串口回环:
串口IO设置
IO的配置:
void gd_eval_com_init(uint32_t com)
{
/* enable GPIO clock */
uint32_t COM_ID;
if(EVAL_COM1 == com)
{
COM_ID = 0U;
}
rcu_periph_clock_enable( EVAL_COM_GPIO_CLK);
/* enable USART clock */
rcu_periph_clock_enable(COM_CLK[COM_ID]);
/* connect port to USARTx_Tx */
gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF, COM_TX_PIN[COM_ID]);
/* connect port to USARTx_Rx */
gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF, COM_RX_PIN[COM_ID]);
/* configure USART Tx as alternate function push-pull */
gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP,COM_TX_PIN[COM_ID]);
gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,COM_TX_PIN[COM_ID]);
/* configure USART Rx as alternate function push-pull */
gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP,COM_RX_PIN[COM_ID]);
gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,COM_RX_PIN[COM_ID]);
/* USART configure */
usart_deinit(com);
usart_baudrate_set(com,115200U);
usart_receive_config(com, USART_RECEIVE_ENABLE);
usart_transmit_config(com, USART_TRANSMIT_ENABLE);
usart_enable(com);
nvic_irq_enable(USART1_IRQn, 0, 0);
}
配置的是COM1;
PA2 —TX ;
PA3 — RX;
串口1 PA9、PA10不知道为什么实现不了;在GD32450Demo板上实现不了;
接收设置
接收可中断接收、也可以论询接收;
中断接收
注意在中断中接收的话:
要打开NVIC并设置USART1的优先级;
nvic_irq_enable(USART1_IRQn, 0, 0);
基本配置 :
设置能发送、能接收;
波特率、停止位;
中断接收:
接收重点是要打开串口函数:
一旦有数据就会有串口中断,
usart_interrupt_enable(USART1 , USART_INTEN_RBNEIE);
然后在中断中接收:
void USART1_IRQHandler(void)
{
unsigned char data;
if(RESET != usart_interrupt_flag_get(USART1, USART_INT_RBNEIE))
{
data = usart_data_receive(USART1);
usart_data_transmit(USART1, (uint8_t)data);
while(RESET == usart_flag_get(USART1, USART_FLAG_TBE));
}
}
接收又回复之后立马回复、完成回环测试;;
论询接收
就是不打开中断 要在程序中论询:
一直查询接收buff是否是为空状态;
if(RESET != usart_interrupt_flag_get(USART1, USART_INT_RBNEIE))
{
data = usart_data_receive(USART1);
usart_data_transmit(USART1, (uint8_t)data);
while(RESET == usart_flag_get(USART1, USART_FLAG_TBE));
}
举报