当前位置:   article > 正文

STM32蓝牙模块配置与代码_蓝牙模块程序编写

蓝牙模块程序编写

        蓝牙可以当作无线串口使用。使用USB-TTL连接,上电前按住小按键进入AT模式(慢闪,小红灯每2秒闪烁一次),打开串口助手,选择波特率为38400即可发送AT指令到蓝牙。

常用配置:

AT+NAME=名字      AT+ROLE=1(主机模式)     AT+ROLE=0(从机模式) 

AT+PSWD=1234   AT+ORGL(恢复出场设置) AT+ADDR?(查询B蓝牙的地址)

AT+UART=9600,0,0(设置波特率)

        连接单片机串口2,正常上电(没有按小按键,快闪,每1秒闪一次),使用手机蓝牙助手连接该蓝牙,若蓝牙每2秒闪一次,一次连续闪亮下则表示配对成功。

usart2.c配置,波特率默认为9600

  1. #include "sys.h"
  2. #include "usart.h"
  3. //
  4. //Èç¹ûʹÓÃucos,Ôò°üÀ¨ÏÂÃæµÄÍ·Îļþ¼´¿É.
  5. #if SYSTEM_SUPPORT_OS
  6. #include "includes.h" //ucos ʹÓÃ
  7. #endif
  8. u8 res; //ÉèÖÃÈ«¾Ö±äÁ¿
  9. //#define tbuf 50
  10. void My_USART2_Init(void)
  11. {
  12. GPIO_InitTypeDef GPIO_InitStrue;
  13. USART_InitTypeDef USART_InitStrue;
  14. NVIC_InitTypeDef NVIC_InitStrue;
  15. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//GPIO¶Ë¿ÚʹÄÜ
  16. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//´®¿Ú¶Ë¿ÚʹÄÜ
  17. GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
  18. GPIO_InitStrue.GPIO_Pin=GPIO_Pin_2;
  19. GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
  20. GPIO_Init(GPIOA,&GPIO_InitStrue);
  21. GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  22. GPIO_InitStrue.GPIO_Pin=GPIO_Pin_3;
  23. GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
  24. GPIO_Init(GPIOA,&GPIO_InitStrue);
  25. USART_InitStrue.USART_BaudRate=9600;
  26. USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  27. USART_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
  28. USART_InitStrue.USART_Parity=USART_Parity_No;
  29. USART_InitStrue.USART_StopBits=USART_StopBits_1;
  30. USART_InitStrue.USART_WordLength=USART_WordLength_8b;
  31. USART_Init(USART2,&USART_InitStrue);
  32. USART_Cmd(USART2,ENABLE); //ʹÄÜ´®¿Ú2
  33. USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//¿ªÆô½ÓÊÕÖжÏ
  34. NVIC_InitStrue.NVIC_IRQChannel=USART2_IRQn;
  35. NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE;
  36. NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=0;
  37. NVIC_InitStrue.NVIC_IRQChannelSubPriority=1;
  38. NVIC_Init(&NVIC_InitStrue);
  39. }
  40. //u8 RX_buffer[tbuf];
  41. //u8 RX_num=0;
  42. void USART2_IRQHandler(void)
  43. {
  44. if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
  45. {
  46. res= USART_ReceiveData(USART2);
  47. //USART_SendData(USART2,res); //´®¿Ú2·¢ËÍÊý¾Ý¸øÀ¶ÑÀÄ£¿é½ÓÊÕ£¬Ò²¾ÍÊÇÊÖ»úapp½ÓÊÕµ½µÄÊý¾Ý
  48. }
  49. }

主函数,通过手机端发送“1”(ascii格式)拉低PB5电平,并且单片机向蓝牙模块(也可以理解为手机)发送“abcdefg”,发送“2”拉低PB5电平

  1. #include "led.h"
  2. #include "delay.h"
  3. #include "key.h"
  4. #include "sys.h"
  5. #include "usart.h"
  6. #include "usart2.h"
  7. extern u8 res; //ÉèÖÃÍⲿȫ¾Ö±äÁ¿
  8. int main(void)
  9. {
  10. int i;
  11. char a[]="abcdefg";
  12. delay_init(); //ÑÓʱº¯Êý³õʼ»¯
  13. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //ÉèÖÃNVICÖжϷÖ×é2:2λÇÀÕ¼ÓÅÏȼ¶£¬2λÏìÓ¦ÓÅÏȼ¶
  14. uart_init(115200); //´®¿Ú³õʼ»¯Îª115200
  15. LED_Init(); //LED¶Ë¿Ú³õʼ»¯
  16. KEY_Init();
  17. My_USART2_Init();
  18. while(1)
  19. {
  20. if(res=='1')
  21. {
  22. GPIO_ResetBits(GPIOB,GPIO_Pin_5); //PA.1ָʾµÆÁÁ
  23. delay_ms(500); //¼ÙÈçÒªÓÃÑÓʱº¯Êý£¬×îºÃ¾ÍÔÚÉÏÃæ³õʼ»¯ÑÓʱº¯Êý£¬²»È»³ÌÐòºÜÈÝÒ×¾ÍÅÜ·É
  24. for(i=0;i<7;i++)
  25. {
  26. USART_SendData(USART2, a[i]); //Ïò´®¿Ú2·¢ËÍÊý¾Ý¸øÀ¶ÑÀÄ£¿é
  27. while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);//µÈ´ý·¢ËͽáÊø
  28. }
  29. res=0;
  30. }
  31. if(res=='2')
  32. {
  33. GPIO_SetBits(GPIOB,GPIO_Pin_5); //PA.1ָʾµÆÃð
  34. res=0;
  35. }
  36. }
  37. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/483708
推荐阅读
相关标签
  

闽ICP备14008679号