返回列表 发新帖我要提问本帖赏金: 50.00元(功能说明)

[活动专区] 【AT-START-L021测评】10、串口蓝牙模块通信实现无线控制

[复制链接]
 楼主| sujingliang 发表于 2024-12-5 14:40 | 显示全部楼层 |阅读模式
安信可PB-03F kit蓝牙模块开发板烧录有原厂AT固件,AT-START-L021通过串口与之连接可以实现蓝牙无线控制。既可以实现AT-START-L021控制蓝牙模块,也可以实现蓝牙模块控制AT-START-L021。


730067514651ce9e9.png

1.UART初始化
使用USART4与蓝牙模块通信,配置USART4发送接收使能,中断使能
  1. void wk_usart4_init(void)
  2. {
  3.   /* add user code begin usart4_init 0 */
  4.         /* enable usart4 periph clock */
  5.   crm_periph_clock_enable(CRM_USART4_PERIPH_CLOCK, TRUE);
  6.         crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
  7.   /* add user code end usart4_init 0 */

  8.   gpio_init_type gpio_init_struct;
  9.   gpio_default_para_init(&gpio_init_struct);

  10.   /* add user code begin usart4_init 1 */

  11.   /* add user code end usart4_init 1 */

  12.   /* configure the TX pin */
  13.   gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE0, GPIO_MUX_4);
  14.   gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
  15.   gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  16.   gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  17.   gpio_init_struct.gpio_pins = GPIO_PINS_0;
  18.   gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  19.   gpio_init(GPIOA, &gpio_init_struct);

  20.   /* configure the RX pin */
  21.   gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE1, GPIO_MUX_4);
  22.   gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
  23.   gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  24.   gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  25.   gpio_init_struct.gpio_pins = GPIO_PINS_1;
  26.   gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  27.   gpio_init(GPIOA, &gpio_init_struct);


  28.         nvic_irq_enable(USART4_3_IRQn, 0, 0);
  29.   /* configure param */
  30.   usart_init(USART4, 115200, USART_DATA_8BITS, USART_STOP_1_BIT);
  31.   usart_transmitter_enable(USART4, TRUE);
  32.   usart_receiver_enable(USART4, TRUE);
  33.   usart_parity_selection_config(USART4, USART_PARITY_NONE);

  34.   usart_hardware_flow_control_set(USART4, USART_HARDWARE_FLOW_NONE);

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

  42.   /* add user code begin usart4_init 2 */
  43.         usart_interrupt_enable(USART4, USART_RDBF_INT, TRUE);
  44. /* add user code end usart4_init 2 */
  45.        
  46.   usart_enable(USART4, TRUE);
  47.   
  48.   /* add user code begin usart4_init 3 */
  49.        
  50.   /* add user code end usart4_init 3 */
  51. }
2.USART4中断处理函数收到蓝牙模块发送信息,保存到usart4_rx_buffer。如果收到"S1",翻转LED2,调用show_digit()改变LCD显示
  1. void USART4_3_IRQHandler(void)
  2. {
  3.         char c;
  4.          if(usart_flag_get(USART4, USART_RDBF_FLAG) != RESET)
  5.   {
  6.     /* read one byte from the receive data register */
  7.                 c=usart_data_receive(USART4);
  8.     usart4_rx_buffer[usart4_rx_counter++] = c;
  9.                 printf("%c",c);
  10.                
  11.                 if(strstr((char*)usart4_rx_buffer,"S1")){
  12.                         at32_led_toggle(LED2);
  13.                         Clear_Buff();
  14.                         show_digit();
  15.                 };
  16. }

  17. }

还可以实现AT-START-L021对蓝牙模块的控制
3.通过USART4向蓝牙模块发送指令函数
  1. void PB03_AT_Send(const char *cmd )
  2. {
  3.                 if (cmd == NULL) {
  4.         // 处理错误或返回
  5.         return;
  6.     }
  7.                 Clear_Buff();
  8.     uart4_receive_complete_flag = FALSE;  // 根据需要设置标志
  9.     while (*cmd) {
  10.                         while(usart_flag_get(USART4, USART_TDBE_FLAG) == RESET);
  11.                         usart_data_transmit(USART4, (uint8_t)*cmd++);
  12.                         while(usart_flag_get(USART4, USART_TDC_FLAG) == RESET);
  13.     }
  14. }
4.发送LEDTEST指令(AT-START-L021控制蓝牙模块)
AT+LEDTEST=1
板载的5个LED循环点亮,可以用于测试
  1. void PB03_LEDTEST( uint8_t timeout )
  2. {
  3.   PB03_AT_Send ( "AT+LEDTEST=1\r\n" );

  4.         PB03_wait_complete(timeout,(char*)__func__);
  5. }
其中PB03_wait_complete为等待发送结束,timeout时间有输入参数决定
  1. void PB03_wait_complete(uint8_t timeout,char* fun)
  2. {
  3.         uint8_t i;
  4.        
  5.         for(i = 0; i <= timeout; i++)
  6.         {
  7.                 //printf("%d,%s\r\n",i,usart4_rx_buffer);
  8.                         if (strstr( usart4_rx_buffer , "OK" ))
  9.                         {
  10.                                         //printf("\r\n %s success\r\n",fun);
  11.                                         Clear_Buff();      //
  12.                                         return;
  13.                         }
  14.                         delay_ms(10);
  15.         }
  16.         //printf("%s timeout\r\n",fun);
  17. }
5.发送点蓝牙kit上板载灯指令(AT-START-L021控制蓝牙模块)
  1. void PB03_LED_CONTRL(PNTypeDef pinnum,uint8_t on,uint8_t timeout)
  2. {
  3.         char cmd[22];
  4.         sprintf(cmd,"AT+SYSIOMAP=1,%d\r\n",LED_PINS[pinnum]);
  5.        
  6.         PB03_AT_Send(cmd);
  7.         PB03_wait_complete(timeout,(char*)cmd);
  8.        
  9.         if(on==1||on==0){
  10.                 sprintf(cmd,"AT+SYSGPIOWRITE=0,%d\r\n",on);
  11.                 PB03_AT_Send(cmd);
  12.                 PB03_wait_complete(timeout,(char*)cmd);
  13.         }else return;

  14. }
6.发送PWM点灯指令(AT-START-L021控制蓝牙模块)

  1. void PBC03_PWM_CONTRL(uint8_t timeout)
  2. {
  3.         char cmd[30];
  4.         sprintf(cmd,"AT+PWMCFG=0,100000,50000\r\n");
  5.        
  6.         PB03_AT_Send(cmd);
  7.         PB03_wait_complete(timeout,(char*)__func__);

  8. }
7.蓝牙模块具有AT串口穿透功能,因此可以在第2步中调用show_digit函数改变LCD的显示:(蓝牙模块控制AT-START-L021)
  1. void show_digit(void)
  2. {
  3.         static uint8_t i;
  4.         char buff[3];
  5.         sprintf(buff,"%d",i++);
  6.         GUI_WriteASCII_BIG(98,60,(unsigned char*)buff,VGA_RED,VGA_BLACK);
  7. }

1.手机连接蓝牙模块,向蓝牙写入S1;
2.蓝牙通过串口将S1转发给AT-START-L021;
3.AT-START-L021判断收到S1,改变LCD显示:
tutieshi_480x270_4s (2).gif

打赏榜单

ArteryMCU 打赏了 50.00 元 2025-01-09
理由:[L021开发板评测活动]内容优质

您需要登录后才可以回帖 登录 | 注册

本版积分规则

84

主题

146

帖子

3

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