[活动专区] 【AT-START-L021测评】串口中断通信

[复制链接]
3954|3
 楼主| dami 发表于 2025-1-4 19:38 | 显示全部楼层 |阅读模式
                                 【AT-START-L021测评】串口中断通信


1,AT32 Work Bench v1.1.05 生成mdk代码
2,配置串口通信为发送中断,接收中断和中断允许。
5097967791c67f3b0a.png
代码如下
  1. void wk_usart1_init(void)
  2. {
  3.   /* add user code begin usart1_init 0 */

  4.   /* add user code end usart1_init 0 */

  5.   gpio_init_type gpio_init_struct;
  6.   gpio_default_para_init(&gpio_init_struct);

  7.   /* add user code begin usart1_init 1 */

  8.   /* add user code end usart1_init 1 */

  9.   /* configure the TX pin */
  10.   gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE9, GPIO_MUX_1);
  11.   gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
  12.   gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  13.   gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  14.   gpio_init_struct.gpio_pins = GPIO_PINS_9;
  15.   gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  16.   gpio_init(GPIOA, &gpio_init_struct);

  17.   /* configure the RX pin */
  18.   gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE10, GPIO_MUX_1);
  19.   gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
  20.   gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  21.   gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  22.   gpio_init_struct.gpio_pins = GPIO_PINS_10;
  23.   gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  24.   gpio_init(GPIOA, &gpio_init_struct);

  25.   /* config usart1 clock source */
  26.   crm_usart_clock_select(CRM_USART1, CRM_USART_CLOCK_SOURCE_PCLK);

  27.   /* configure param */
  28.   usart_init(USART1, 38400, USART_DATA_8BITS, USART_STOP_1_BIT);
  29.   usart_transmitter_enable(USART1, TRUE);
  30.   usart_receiver_enable(USART1, TRUE);
  31.   usart_parity_selection_config(USART1, USART_PARITY_NONE);

  32.   usart_hardware_flow_control_set(USART1, USART_HARDWARE_FLOW_NONE);

  33.   /**
  34.    * Users need to configure USART1 interrupt functions according to the actual application.
  35.    * 1. Call the below function to enable the corresponding USART1 interrupt.
  36.    *     --usart_interrupt_enable(...)
  37.    * 2. Add the user's interrupt handler code into the below function in the at32l021_int.c file.
  38.    *     --void USART1_IRQHandler(void)
  39.    */

  40.   /* add user code begin usart1_init 2 */
  41.    <font color="#ff0000">usart_interrupt_enable(USART1, USART_RDBF_INT,TRUE),</font>
  42.   /* add user code end usart1_init 2 */

  43.   <font color="#8b0000">usart_enable(USART1, TRUE);</font>

  44.   /* add user code begin usart1_init 3 */

  45.   /* add user code end usart1_init 3 */
  46. }


3,编写接收中断发送中断子程序。
  1. extern uint8_t usart1_tx_buffer[];
  2. //extern uint8_t usart2_rx_buffer[];
  3. extern uint8_t usart1_rx_buffer[];
  4. //extern uint8_t usart2_tx_counter;
  5. extern uint8_t usart1_tx_counter;
  6. //extern uint8_t usart2_rx_counter;
  7. extern uint8_t usart1_rx_counter;
  8. //extern uint8_t usart2_tx_buffer_size;
  9. extern uint8_t usart1_tx_buffer_size;


  1. /**
  2.   * [url=home.php?mod=space&uid=247401]@brief[/url]  this function handles USART1 handler.
  3.   * @param  none
  4.   * @retval none
  5.   */
  6. void USART1_IRQHandler(void)
  7. {
  8.   /* add user code begin USART1_IRQ 0 */

  9.   /* add user code end USART1_IRQ 0 */
  10.   /* add user code begin USART1_IRQ 1 */
  11. if(usart_interrupt_flag_get(USART1, USART_RDBF_FLAG) != RESET)
  12.   {
  13.     /* read one byte from the receive data register */
  14.     usart1_rx_buffer[usart1_rx_counter++] = usart_data_receive(USART1);

  15.     if(usart1_rx_counter == usart1_tx_buffer_size)
  16.     {
  17.       /* disable the usart1 receive interrupt */
  18. //     usart_interrupt_enable(USART1, USART_RDBF_INT, FALSE);
  19.                         usart1_rx_counter =0;
  20.     }
  21.   }

  22.   if(usart_interrupt_flag_get(USART1, USART_TDBE_FLAG) != RESET)
  23.   {
  24.     /* write one byte to the transmit data register */
  25.   //  usart_data_transmit(USART1, usart1_tx_buffer[usart1_tx_counter++]);

  26.     if(usart1_tx_counter == usart1_tx_buffer_size)
  27.     {
  28.       /* disable the usart1 transmit interrupt */
  29. //     usart_interrupt_enable(USART1, USART_TDBE_INT, FALSE);
  30.                         usart1_tx_counter=0;
  31.     }
  32.   }
  33.   /* add user code end USART1_IRQ 1 */
  34. }


4,编写主程序。
  1. /* private includes ----------------------------------------------------------*/
  2. /* add user code begin private includes */
  3. #define COUNTOF(a)                       (sizeof(a) / sizeof(*(a)))
  4. //#define USART2_TX_BUFFER_SIZE            (COUNTOF(usart2_tx_buffer) - 1)
  5. #define USART1_TX_BUFFER_SIZE            (COUNTOF(usart1_tx_buffer) - 1)
  6. //uint8_t usart2_tx_buffer[] = "usart transfer by interrupt: usart2 -> usart1 using interrupt";
  7. uint8_t usart1_rx_buffer[100];
  8. #define USART1_RX_BUFFER_SIZE            (COUNTOF(usart1_rx_buffer) - 1)
  9. uint8_t usart1_tx_buffer[] = "usart transfer by interrupt: usart1 using interrupt";
  10. //uint8_t usart2_rx_buffer[USART1_TX_BUFFER_SIZE];
  11. //volatile uint8_t usart2_tx_counter = 0x00;
  12. volatile uint8_t usart1_tx_counter = 0x00;
  13. //volatile uint8_t usart2_rx_counter = 0x00;
  14. volatile uint8_t usart1_rx_counter = 0x00;
  15. //uint8_t usart2_tx_buffer_size = USART2_TX_BUFFER_SIZE;
  16. uint8_t usart1_tx_buffer_size = USART1_TX_BUFFER_SIZE;
  17. uint8_t usart1_rx_buffer_size = USART1_RX_BUFFER_SIZE;
  1. int main(void)
  2. {
  3.   /* add user code begin 1 */

  4.   /* add user code end 1 */

  5.   /* system clock config. */
  6.   wk_system_clock_config();

  7.   /* config periph clock. */
  8.   wk_periph_clock_config();

  9.   /* nvic config. */
  10.   wk_nvic_config();

  11.   /* timebase config. */
  12.   wk_timebase_init();

  13.   /* init usart1 function. */
  14.   wk_usart1_init();

  15.   /* init gpio function. */
  16.   wk_gpio_config();

  17.   /* add user code begin 2 */

  18.   /* add user code end 2 */

  19.   while(1)
  20.   {
  21.                
  22.                                 /* add user code begin 3 */
  23.                 if((usart1_rx_counter>=4)&&(usart1_rx_buffer[0]=='s')&&(usart1_rx_buffer[1]=='e')&&(usart1_rx_buffer[2]=='n')&&(usart1_rx_buffer[3]=='d'))
  24.                 {       
  25.                                 int i =0;
  26.                           
  27.                                
  28.                                 gpio_bits_reset(led4_GPIO_PORT, led4_PIN);
  29.                                 gpio_bits_reset(led2_GPIO_PORT, led2_PIN | led3_PIN);
  30.                                 wk_delay_ms(100);//DelayMs(1000);
  31.                
  32.                                 gpio_bits_set(led4_GPIO_PORT, led4_PIN);
  33.                                 gpio_bits_set(led2_GPIO_PORT, led2_PIN | led3_PIN);
  34.                                 wk_delay_ms(100);//DelayMs(1000);
  35.                                 usart1_tx_counter=0;
  36.                           for(i =0;i<usart1_tx_buffer_size;i++)
  37.                                 {       
  38.                                         usart_data_transmit(USART1, usart1_tx_buffer[usart1_tx_counter++]);
  39.                                         wk_delay_ms(1);
  40.                                 }
  41.                                 usart1_rx_counter=0;
  42.                           usart1_rx_buffer[0]=0;
  43.                                 usart1_rx_buffer[1]=0;
  44.                                 usart1_rx_buffer[2]=0;
  45.                           usart1_rx_buffer[3]=0;
  46.                         }
  47.     /* add user code end 3 */
  48.   }
  49. }


5,编译调试。
6,测试OK。
1484867791d93e5fd7.png
问天少年 发表于 2025-1-7 15:37 | 显示全部楼层
用这个工具配置之后,写代码好简单
我只会加减乘除 发表于 2025-1-8 17:06 | 显示全部楼层
可以加空闲中断,接收就方便点
 楼主| dami 发表于 2025-1-9 17:16 | 显示全部楼层
好的。 谢谢。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

66

主题

1080

帖子

6

粉丝
快速回复 在线客服 返回列表 返回顶部