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;
//¿ªÆôUSART1ʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
//ÅäÖÃPA2×÷ΪUSART1¡¡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×÷ΪUSART1¡¡Rx
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOA
tiNG;
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
//Öжϱ»ÆÁ±ÎÁË
USART_InitStructure.USART_BaudRate = br_num;//br_num; //²¨ÌØÂÊ¿ÉÒÔͨ¹ýµØÃæÕ¾ÅäÖÃ
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8λÊý¾Ý
USART_InitStructure.USART_StopBits = USART_StopBits_1; //ÔÚÖ¡½áβ´«Êä1¸öֹͣλ
USART_InitStructure.USART_Parity = USART_Parity_No; //½ûÓÃÆæżУÑé
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //Ó²¼þÁ÷¿ØÖÆʧÄÜ
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //·¢ËÍ¡¢½ÓÊÕʹÄÜ
// //ÅäÖÃUSART1ʱÖÓ
// USART_ClockInitStruct.USART_Clock = USART_Clock_Disable; //ʱÖӵ͵çƽ»î¶¯
// USART_ClockInitStruct.USART_CPOL = USART_CPOL_Low; //SLCKÒý½ÅÉÏʱÖÓÊä³öµÄ¼«ÐÔ->µÍµçƽ
// USART_ClockInitStruct.USART_CPHA = USART_CPHA_2Edge; //ʱÖÓµÚ¶þ¸ö±ßÑؽøÐÐÊý¾Ý²¶»ñ
// USART_ClockInitStruct.USART_LastBit = USART_LastBit_Disable; //×îºóһλÊý¾ÝµÄʱÖÓÂö³å²»´ÓSCLKÊä³ö
USART_Init(USART2, &USART_InitStructure);
// USART_ClockInit(USART2, &USART_ClockInitStruct);
//ʹÄÜUSART1½ÓÊÕÖжÏ
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
//ʹÄÜ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);
}
//·¢ËÍÖжÏ
if((USART2->SR & (1<<7))&&(USART2->CR1 & USART_CR1_TXEIE))//if(USART_GetITStatus(USART1,USART_IT_TXE)!=RESET)
{
USART2->DR = TxBuffer[TxCounter++]; //дDRÇå³ýÖжϱêÖ¾
if(TxCounter == count)
{
USART2->CR1 &= ~USART_CR1_TXEIE; //¹Ø±ÕTXEÖжÏ
//USART_ITConfig(USART1,USART_IT_TXE,DISABLE);
}
}
//½ÓÊÕÖÐ¶Ï (½ÓÊռĴæÆ÷·Ç¿Õ)
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;/*´ò¿ªPM2.5*/
OLED_Display_On();
}
if(RxBuffer[0] == 0x73 ) //'s'
{ readFile();/*·¢ËÍÊý¾Ý */ }
// printf("$%d$",RxBuffer[0]);
RxBuffer[0] = 0x00; //»º´æÇå¿Õ
}
上述程序曾置于定时器中断中,也不能行!程序有3个定时器中断(先占优先级:0, 从占:1 2 3),一个串口中断(先占:0,从占:0)
求大神指点!!!!!!!!!!!!!!
一周热门 更多>