STM32F767驱动蓝牙模块不成功,但是用STM32F103可以

2019-03-23 14:41发布

444.jpg


用的UART7,PE7、PE8
程序如下:

//初始化IO 串口1
//bound:波特率
void uart_init(u32 bound)
{        
        //UART 初始化设置
        UART7_Handler.Instance=UART7;                                            //USART1
        UART7_Handler.Init.BaudRate=bound;                                    //波特率
        UART7_Handler.Init.WordLength=UART_WORDLENGTH_8B;   //字长为8位数据格式
        UART7_Handler.Init.StopBits=UART_STOPBITS_1;            //一个停止位
        UART7_Handler.Init.Parity=UART_PARITY_NONE;                    //无奇偶校验位
        UART7_Handler.Init.HwFlowCtl=UART_HWCONTROL_NONE;   //无硬件流控
        UART7_Handler.Init.Mode=UART_MODE_TX_RX;                    //收发模式
        HAL_UART_Init(&UART7_Handler);                                            //HAL_UART_Init()会使能UART1
        
        HAL_UART_Receive_IT(&UART7_Handler, (u8 *)aRxBuffer, RXBUFFERSIZE);//该函数会开启接收中断:标志位UART_IT_RXNE,并且设置接收缓冲以及接收缓冲接收最大数据量

}

//UART底层初始化,时钟使能,引脚配置,中断配置
//此函数会被HAL_UART_Init()调用
//huart:串口句柄

void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
    //GPIO端口设置
        GPIO_InitTypeDef GPIO_Initure;
        
        if(huart->Instance==UART7)//如果是串口1,进行串口1 MSP初始化
        {
                __HAL_RCC_GPIOE_CLK_ENABLE();                        //使能GPIOA时钟
                __HAL_RCC_UART7_CLK_ENABLE();                        //使能USART1时钟
        
                GPIO_Initure.Pin=GPIO_PIN_8;                        //PA9
                GPIO_Initure.Mode=GPIO_MODE_AF_PP;                //复用推挽输出
                GPIO_Initure.Pull=GPIO_PULLUP;                        //上拉
                GPIO_Initure.Speed=GPIO_SPEED_FAST;                //高速
                GPIO_Initure.Alternate=GPIO_AF7_UART7;        //复用为USART1
                HAL_GPIO_Init(GPIOE,&GPIO_Initure);                   //初始化PA9

                GPIO_Initure.Pin=GPIO_PIN_7;                        //PA10
                HAL_GPIO_Init(GPIOE,&GPIO_Initure);                   //初始化PA10
               
#if EN_UART7_RX
                HAL_NVIC_EnableIRQ(UART7_IRQn);                                //使能USART1中断通道
                HAL_NVIC_SetPriority(UART7_IRQn,3,3);                        //抢占优先级3,子优先级3
#endif        
        }

}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
        if(huart->Instance==UART7)//如果是串口1
        {
                if((USART_RX_STA&0x8000)==0)//接收未完成
                {
                        if(USART_RX_STA&0x4000)//接收到了0x0d
                        {
                                if(aRxBuffer[0]!=0x0a)USART_RX_STA=0;//接收错误,重新开始
                                else USART_RX_STA|=0x8000;        //接收完成了
                        }
                        else //还没收到0X0D
                        {        
                                if(aRxBuffer[0]==0x0d)USART_RX_STA|=0x4000;
                                else
                                {
                                        USART_RX_BUF[USART_RX_STA&0X3FFF]=aRxBuffer[0] ;
                                        USART_RX_STA++;
                                        if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收         
                                }                 
                        }
                }

        }
}

//串口1中断服务程序
void UART7_IRQHandler(void)                        
{
        u32 timeout=0;
    u32 maxDelay=0x1FFFF;
#if SYSTEM_SUPPORT_OS                 //使用OS
        OSIntEnter();   
#endif
        
        HAL_UART_IRQHandler(&UART7_Handler);        //调用HAL库中断处理公用函数
        
        timeout=0;
    while (HAL_UART_GetState(&UART7_Handler)!=HAL_UART_STATE_READY)//等待就绪
        {
        timeout++;////超时处理
        if(timeout>maxDelay) break;               
        }

        timeout=0;
        while(HAL_UART_Receive_IT(&UART7_Handler,(u8 *)aRxBuffer, RXBUFFERSIZE)!=HAL_OK)//一次处理完成之后,重新开启中断并设置RxXferCount为1
        {
        timeout++; //超时处理
        if(timeout>maxDelay) break;        
        }
#if SYSTEM_SUPPORT_OS                 //使用OS
        OSIntExit();                                                                                          
#endif
}
#endif        

。。。。。。。。。。。。。。
uart_init(9600);                        //串口初始化
。。。。。。。。。。。。。。。。
HAL_UART_Transmit(&UART7_Handler,(uint8_t*)USART_RX_BUF,1,1000);        //发送接收到的数据
                }
                if(USART_RX_STA&0x8000)
                {                                          
                        len=USART_RX_STA&0x3fff;//得到此次接收到的数据长度
                        if(USART_RX_BUF[0]=='0')                                        //开始
                                ;
                        else if(USART_RX_BUF[0]=='1')                                //停止
                                StopDispose();
                        else if(USART_RX_BUF[0]=='2')                                //上松
                                ;
                        else if(USART_RX_BUF[0]=='3')                                //下松
                                ;
                        else if(USART_RX_BUF[0]=='4')                                //上升
                                UpDispose();
                        USART_RX_STA=0;
                }


这个蓝牙模块我用STM32F103驱动成功了,可以和手机蓝牙助手连接互相通讯。
可是用STM32F767一点反应都没有。
请问高手,哪里有错?谢谢


 

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
懒猫爱飞
1楼-- · 2019-03-23 19:48

1) 看你描述,应该是通过串口与蓝牙模块通讯,然后蓝牙模块转发给手机APP
2) 首先第一步,用103测试是可以,说明蓝牙模块没有问题
3) 用串口助手 测试一下STM32F767所配置的串口是否可以用,所发的命令是否正确,发送的机制是否是103一致,另外还要查看一下接线方式是否一样

chenbingjy
2楼-- · 2019-03-23 23:51
 精彩回答 2  元偷偷看……

一周热门 更多>