赞
踩
TX----->PB10 RX----->PB11 VCC----->3.3V GND---->GND
不用烧录代码,直接将c8t6和HC-05相连接,通过XCOM或者SSCOM软件便可以进入调试模式,
进入AT模式的方法:长按HC-05上面的黑按钮,同时上电,此时指示灯会处于一个慢闪的状态,此时便进入了AT模式。
紧接着可以在串口软件上输入AT相应指令进行蓝牙模块的初始化。
我们通过手机上面的蓝牙软件将信息发送到蓝牙的初始化串口上,然后单片机会读取串口上面的信息,紧接着别的串口就可以通过读取单片机的信息,从而将蓝牙串口的信息,发送到别的串口。本文代码便是蓝牙使用的是串口3,然后会读取出来,然后显示在串口1中,同时将信息显示在OLED上面。
串口1的初始化,以及串口中断的处理【正点原子例程】
- u8 USART_RX_BUF[USART_REC_LEN]; //能够接收的最大字节数
- u16 USART_RX_STA=0; //当前接收状态的标记
-
- void uart_init(u32 bound){
- //GPIO端口设置
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
-
- //USART1_TX GPIOA.9
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
- GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.9
-
- //USART1_RX GPIOA.10
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
- GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.10
-
- //Usart1 NVIC 中断配置
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//ÇÀÕ¼ÓÅÏȼ¶3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //×ÓÓÅÏȼ¶3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
- NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
-
- //USART 初始化配置
-
- USART_InitStructure.USART_BaudRate = bound;//波特率的设置,一般设置为9600
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
-
- USART_Init(USART1, &USART_InitStructure);
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启中断
- USART_Cmd(USART1, ENABLE); //使能串口
-
- }
-
- void USART1_IRQHandler(void) //中断处理
- {
- u8 Res;
- if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(数据必须为OXod,OXoa结尾)
- {
- Res =USART_ReceiveData(USART1); //接收一个字节赋值给变量res
-
- if((USART_RX_STA&0x8000)==0)//接收未完成
- {
- if(USART_RX_STA&0x4000)//接收到了0x0d
- {
- if(Res!=0x0a)USART_RX_STA=0;//未接收到0Xoa
- else USART_RX_STA|=0x8000; //接收完成
- }
- else //还未收到OXOD
- {
- if(Res==0x0d)USART_RX_STA|=0x4000;
- else
- {
- USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
- USART_RX_STA++;
- if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//长度溢出,数据出错
- }
- }
- }
- }
串口3的初始化及中断
- void uart3_init(u32 bound)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
- //USART3_TX
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB.10
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- //USART3_RX
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//PB11
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- //USART
- USART_InitStructure.USART_BaudRate = bound;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART3, &USART_InitStructure);
- USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
- USART_Cmd(USART3, ENABLE);
- }
-
- u16 USART3_RX_STA=0;
- u8 USART3_RX_BUF[USART3_REC_LEN];
- void USART3_IRQHandler(void)
- {
- if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
- {
- recieve_bluetooth_DATA=USART_ReceiveData(USART3);
- if((USART3_RX_STA&0x8000)==0)
- {
- if(USART3_RX_STA&0x4000)
- {
- if(recieve_bluetooth_DATA!=0x0a)
- USART3_RX_STA=0;
- else
- {
- USART3_RX_STA|=0x8000;
- }
- }
- else
- {
- if(recieve_bluetooth_DATA==0x0d)USART3_RX_STA|=0x4000;
- else
- {
- USART3_RX_BUF[USART3_RX_STA&0X3FFF]=recieve_bluetooth_DATA ;
- USART3_RX_STA++;
- if(USART3_RX_STA>(USART3_REC_LEN-1))
- USART3_RX_STA=0;
- }
- }
-
- }
- }
- }
主函数代码:
蓝牙软件的使用
蓝牙软件的配置
一定要先把软件配置做好,小心出现乱码!!!!
OLED显示
逻辑很简单,就是将串口3的信息显示在OELD上面,只需要读取,然后存入一个变量数组里面就可以。
代码分析:
- int main(void)
- {
- u8 t;
- u8 len;
- delay_init(); //=====延时函数初始化
- NVIC_Configuration(); //=====中断优先级分组
- uart1_init(9600); //=====串口1初始化
- uart3_init(115200); //=====串口3初始化即蓝牙初始化
- delay_ms(100);
- LED_Init(); //=====初始化与 LED 连接的IO
- KEY_Init(); //=====按键初始化
- OLED_Init(); //=====OLED初始化
- OLED_Clear(); //=====OLED清屏
- while(1)
- {
- delay_ms(50); //=====50ms刷一次屏幕,频率就是20HZ,不需要一直刷。
- OLED_ShowString(0,2,"Bluetooth_Test",12);
- OLED_ShowString(0,4,"Recieve:",12);
- //USART_SendData(USART3,recieve_bluetooth_DATA);
- if(USART3_RX_STA&0x8000)
- {
- len=USART3_RX_STA&0x3fff;//得到此次接收到的数据长度
- Uart3SendStr("\r\n您发送的消息为:\r\n");
- for(t=0;t<len;t++)
- {
- USART3->DR=USART3_RX_BUF[t];
-
- while((USART3->SR&0X40)==0);//等待发送结束
- }
- Uart3SendStr("\r\n");//插入换行
- USART3_RX_STA=0;
- }
- OLED_ShowString(50,4,USART3_RX_BUF,12);
- LED=~LED; //表明程序一直处于运行?
- }
- }
遇到的问题【BUG】
1)每次串口3只可以打印两个字节
原来是:
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
改为:
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
解决方法:(i)提升串口的接收的优先级,让其先执行;(ii)删除中断函数的printf语句,可能是超时导致的(iii)尽力不要在中断函数中进行接发操作,在中断函数中将数据存储起来便可以。
效果图:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。