|
最近在学习32系MCU编程,在学习中遇到了一些问题,就是gd32e230的rs485问题。
原本硬件上使用了一颗自动换向RS485PHY,就是RE和DE脚直接拉高就行了,现在新硬件换回了常规的需要控制方向的RS485 PHY,然后我看了固件库使用指南,发现有个rs485 driver的相关代码。但是串口一直没有数据发送
自动换向的代码:
- void rs485_config(void) {
- rcu_periph_clock_enable(RS485_GPIO_RCU);
- rcu_periph_clock_enable(RS485_RCU);
- gpio_af_set(RS485_GPIO_PORT, GPIO_AF_1, RS485_TX_PIN | RS485_RX_PIN);
- /* configure USART Tx&Rx as alternate function push-pull */
- gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, RS485_TX_PIN | RS485_RX_PIN);
- gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, RS485_TX_PIN | RS485_RX_PIN);
- /* configure RS485 EN Pin */
- gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, RS485_EN_PIN);
- gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN);
- gpio_bit_write(RS485_GPIO_PORT, RS485_EN_PIN, RESET);
- /* USART configure */
- usart_deinit(RS485_PHY);
- usart_baudrate_set(RS485_PHY, RS485_BAUDRATE);
- usart_parity_config(RS485_PHY, USART_PM_NONE);
- usart_word_length_set(RS485_PHY, USART_WL_8BIT);
- usart_stop_bit_set(RS485_PHY, USART_STB_1BIT);
- usart_receive_config(RS485_PHY, USART_RECEIVE_ENABLE);
- usart_transmit_config(RS485_PHY, USART_TRANSMIT_ENABLE);
- // usart_driver_assertime_config(RS485_PHY, 0x01);
- // usart_driver_deassertime_config(RS485_PHY, 0x01);
- // usart_depolarity_config(USART0, USART_DEP_HIGH);
- // usart_depolarity_config(USART0, USART_DEP_LOW);
- usart_enable(RS485_PHY);
- // usart_rs485_driver_enable(RS485_PHY);
- nvic_irq_enable(USART0_IRQn, 0);
- usart_interrupt_enable(RS485_PHY, USART_INT_RBNE);
- usart_interrupt_enable(RS485_PHY, USART_INT_IDLE);
- }
复制代码
然后尝试了使用usart_rs485_driver_enable(RS485_PHY);来使能rs485 driver,具体代码如下
- void rs485_config(void) {
- rcu_periph_clock_enable(RS485_GPIO_RCU);
- rcu_periph_clock_enable(RS485_RCU);
- gpio_af_set(RS485_GPIO_PORT, GPIO_AF_1, RS485_TX_PIN | RS485_RX_PIN | RS485_EN_PIN);
- /* configure USART Tx&Rx as alternate function push-pull */
- gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, RS485_TX_PIN | RS485_RX_PIN);
- gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, RS485_TX_PIN | RS485_RX_PIN);
- /* configure RS485 EN Pin */
- gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, RS485_EN_PIN);
- gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN);
- // gpio_bit_write(RS485_GPIO_PORT, RS485_EN_PIN, RESET);
- /* USART configure */
- usart_deinit(RS485_PHY);
- usart_baudrate_set(RS485_PHY, RS485_BAUDRATE);
- usart_parity_config(RS485_PHY, USART_PM_NONE);
- usart_word_length_set(RS485_PHY, USART_WL_8BIT);
- usart_stop_bit_set(RS485_PHY, USART_STB_1BIT);
- usart_receive_config(RS485_PHY, USART_RECEIVE_ENABLE);
- usart_transmit_config(RS485_PHY, USART_TRANSMIT_ENABLE);
- usart_driver_assertime_config(RS485_PHY, 0x01);
- usart_driver_deassertime_config(RS485_PHY, 0x01);
- usart_depolarity_config(USART0, USART_DEP_HIGH);
- // usart_depolarity_config(USART0, USART_DEP_LOW);
- // usart_enable(RS485_PHY);
- usart_rs485_driver_enable(RS485_PHY);
- nvic_irq_enable(USART0_IRQn, 0);
- usart_interrupt_enable(RS485_PHY, USART_INT_RBNE);
- usart_interrupt_enable(RS485_PHY, USART_INT_IDLE);
- }
复制代码
让后串口没有一点输出,用示波器看了tx,什么都没有
还有,只要加入了usart_rs485_driver_enable(RS485_PHY);这一行就tx就没有输出,不管rs485 driver的配置相关的其他代码是否在,只要一开driver_enable就不行。
实在没理解哪里配置有问题,麻烦大佬们指点一二
|
|