微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > STM32串口USART2(接蓝牙)不能接收手机端发送的字符,求解决,感激不尽!

STM32串口USART2(接蓝牙)不能接收手机端发送的字符,求解决,感激不尽!

时间:10-02 整理:3721RD 点击:

STM32串口USART2(接蓝牙)不能接收手机端发送的字符,求解决,感激不尽!
急急急急急急!谢谢
此函数能够通过STM32发送字符串给串口(蓝牙HC06),然后发送给手机端,但是手机端发送字符串给STM32,STM32不能接收解析,求解决方案
usart函数:
void uart_init(u32 br_num)
{
USART_InitTypeDef USART_InitStructure;
//  USART_ClockInitTypeDef USART_ClockInitStruct;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//?a??USART1ê±?ó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
//????PA2×÷?aUSART1??Tx
GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA , &GPIO_InitStructure);
//????PA3×÷?aUSART1??Rx
GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA , &GPIO_InitStructure);
   //Usart2 NVIC
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 0 ;//************//
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  //0-3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
NVIC_Init(&NVIC_InitStructure);
//????USART2
//?D??±??á±?á?
USART_InitStructure.USART_BaudRate = br_num;//br_num;       //2¨ì??ê?éò?í¨1yμ?????????
USART_InitStructure.USART_WordLength = USART_WordLength_8b;  //8??êy?Y
USART_InitStructure.USART_StopBits = USART_StopBits_1;   //?ú???á?2′?ê?1??í£?1??
USART_InitStructure.USART_Parity = USART_Parity_No;    //??ó?????D£?é
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //ó2?tá÷????ê§?ü
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;  //·¢?í?¢?óê?ê1?ü
//  //????USART1ê±?ó
//  USART_ClockInitStruct.USART_Clock = USART_Clock_Disable;  //ê±?óμíμ??????ˉ
//  USART_ClockInitStruct.USART_CPOL = USART_CPOL_Low;  //SLCKòy??é?ê±?óê?3?μ???D?->μíμ???
//  USART_ClockInitStruct.USART_CPHA = USART_CPHA_2Edge;  //ê±?óμú?t??±?????DDêy?Y2???
//  USART_ClockInitStruct.USART_LastBit = USART_LastBit_Disable; //×?oóò???êy?Yμ?ê±?ó??3?2?′óSCLKê?3?
USART_Init(USART2, &USART_InitStructure);
//  USART_ClockInit(USART2, &USART_ClockInitStruct);
//ê1?üUSART1?óê??D??
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
//ê1?üUSART1
USART_Cmd(USART2, ENABLE);
}
u8 TxBuffer[256];
u8 TxCounter=0;
u8 count=0;
u8 RxBuffer[16]={0};
static u8 RxState = 0;
void Uart2_IRQ(void)
{
if (USART_GetFlagStatus(USART2, USART_FLAG_ORE) != RESET)//?!?if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)?
    {
        USART_ReceiveData(USART2);
    }
  
//·¢?í?D??
if((USART2->SR & (1<<7))&&(USART2->CR1 & USART_CR1_TXEIE))//if(USART_GetITStatus(USART1,USART_IT_TXE)!=RESET)
{
  USART2->DR = TxBuffer[TxCounter++]; //D′DR??3y?D??±ê??         
  if(TxCounter == count)
  {
   USART2->CR1 &= ~USART_CR1_TXEIE;  //1?±?TXE?D??
   //USART_ITConfig(USART1,USART_IT_TXE,DISABLE);
  }
}
//?óê??D?? (?óê???′??÷·???)
if(USART2->SR & (1<<5))//if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)   
{
  u8 com_data = USART2->DR;
  static u8 _data_len = 0,_data_cnt = 0;
  if(RxState==0)//if(RxState==0&&com_data==0xAA)
  {
   RxState=1;
   RxBuffer[0]=com_data;
  }
  else if(RxState==1)//if(RxState==1&&com_data==0xAF)
  {
   RxState=0;
   
   RxBuffer[1]=com_data;
  }
}
}
主函数中循环:
while(1)
{
  if(RxBuffer[0] == 'c' | RxBuffer[1] == 'c' )//0x64 'c' && stateXoled == 0
  {
   printf("$%d*",RxBuffer[0]);
   BELL=1;
   delay_ms(500);
   BELL=0;
   stateXoled = 0;
   FAN=1;  
   OLED_Display_Off();  
  }
  if(RxBuffer[0] == 'o' | RxBuffer[1] == 'o')//0x6f 'o' && stateXoled == 1
  {
//      printf("$%d?",RxBuffer[0]);
   stateXoled = 1;
   FAN=0;/*′ò?aPM2.5*/
   OLED_Display_On();   
  }
  
  if(RxBuffer[0] == 0x73 ) //'s'
  { readFile();/*·¢?íêy?Y */     }
//     printf("$%d$",RxBuffer[0]);
  RxBuffer[0] = 0x00;  //?o′?????   
}  
上述程序曾置于定时器中断中,也不能行!程序有3个定时器中断(先占优先级:0, 从占:1 2 3),一个串口中断(先占:0,从占:0)
求大神指点!

说下个人看法,代码中的2个疑点,小编不妨检查一下
1、不知道使用的具体型号,管脚情况如何,使用的是PA2、PA3,又打开了AFIO时钟,如果使用重映射功能,那么应该在PA6和PA7才对,那么原理图上究竟是那一个管脚连接到蓝牙模块呢?这可能是主要原因。
2、中断优先级0,0,推荐使用其他的优先级,这一个不知道行不行,这只是可能,并不是绝对。

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top