21ic问答首页 - GD32F103收发485通讯不进中断,收怎么也不行中断
GD32F103收发485通讯不进中断,收怎么也不行中断
Aist20182023-12-25
/* enable USART and GPIOA clock */
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_USART2);
gpio_pin_remap_config( GPIO_USART2_PARTIAL_REMAP,ENABLE); //引脚重映射
gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
gpio_bit_reset(GPIOB, GPIO_PIN_1);
/* configure USART2 Tx as alternate function push-pull */
gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
/* configure USART2 Rx as alternate function push-pull */
gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_11);
void initUART() {
/* USART interrupt configuration */
nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
nvic_irq_enable(USART2_IRQn, 1, 1);
/* configure COM0 */
//gd_eval_com_init(EVAL_COM0);
usart_baudrate_set(USART2, 115200U);
usart_parity_config(USART2, USART_PM_NONE);//无奇偶检验
usart_word_length_set(USART2, USART_WL_8BIT);//8位数据
usart_stop_bit_set(USART2, USART_STB_1BIT);//一个停止位
usart_hardware_flow_rts_config(USART2,USART_RTS_DISABLE);//无硬件数据流
usart_hardware_flow_cts_config(USART2, USART_CTS_DISABLE); // 硬件流控制CTS
usart_receive_config(USART2, USART_RECEIVE_ENABLE);//串口接收
usart_transmit_config(USART2, USART_TRANSMIT_ENABLE);//串口发送
//usart_halfduplex_enable(USART2);
usart_interrupt_enable(USART2, USART_INT_RBNE);
usart_enable(USART2);
}
void UART2_IRQHandler(void) {
static int dataLen = 0;
dataRev = 0x11;
if(SET == usart_interrupt_flag_get(USART2, USART_INT_FLAG_TBE)){
usart_interrupt_flag_clear(USART2,USART_INT_FLAG_TBE);
/* transmit data */
usart_data_transmit(USART2, txbuffer[txcount++]);
if(txcount == tx_size){
usart_interrupt_disable(USART2, USART_INT_TBE);
gpio_bit_reset(GPIOB, GPIO_PIN_1);
}
}
if((SET == usart_interrupt_flag_get(USART2, USART_INT_FLAG_RBNE))||(SET == usart_interrupt_flag_get(USART2, USART_INT_FLAG_RBNE_ORERR))) {
dataRev = usart_data_receive(USART2);
usart_interrupt_flag_clear(USART2,USART_INT_FLAG_RBNE);
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_USART2);
gpio_pin_remap_config( GPIO_USART2_PARTIAL_REMAP,ENABLE); //引脚重映射
gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
gpio_bit_reset(GPIOB, GPIO_PIN_1);
/* configure USART2 Tx as alternate function push-pull */
gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
/* configure USART2 Rx as alternate function push-pull */
gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_11);
void initUART() {
/* USART interrupt configuration */
nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
nvic_irq_enable(USART2_IRQn, 1, 1);
/* configure COM0 */
//gd_eval_com_init(EVAL_COM0);
usart_baudrate_set(USART2, 115200U);
usart_parity_config(USART2, USART_PM_NONE);//无奇偶检验
usart_word_length_set(USART2, USART_WL_8BIT);//8位数据
usart_stop_bit_set(USART2, USART_STB_1BIT);//一个停止位
usart_hardware_flow_rts_config(USART2,USART_RTS_DISABLE);//无硬件数据流
usart_hardware_flow_cts_config(USART2, USART_CTS_DISABLE); // 硬件流控制CTS
usart_receive_config(USART2, USART_RECEIVE_ENABLE);//串口接收
usart_transmit_config(USART2, USART_TRANSMIT_ENABLE);//串口发送
//usart_halfduplex_enable(USART2);
usart_interrupt_enable(USART2, USART_INT_RBNE);
usart_enable(USART2);
}
void UART2_IRQHandler(void) {
static int dataLen = 0;
dataRev = 0x11;
if(SET == usart_interrupt_flag_get(USART2, USART_INT_FLAG_TBE)){
usart_interrupt_flag_clear(USART2,USART_INT_FLAG_TBE);
/* transmit data */
usart_data_transmit(USART2, txbuffer[txcount++]);
if(txcount == tx_size){
usart_interrupt_disable(USART2, USART_INT_TBE);
gpio_bit_reset(GPIOB, GPIO_PIN_1);
}
}
if((SET == usart_interrupt_flag_get(USART2, USART_INT_FLAG_RBNE))||(SET == usart_interrupt_flag_get(USART2, USART_INT_FLAG_RBNE_ORERR))) {
dataRev = usart_data_receive(USART2);
usart_interrupt_flag_clear(USART2,USART_INT_FLAG_RBNE);
您需要登录后才可以回复 登录 | 注册