Stm32_Standard library_15_Serial port & Bluetooth module_Communication between mobile phone and Bluetooth module_BUG repair

Code:

#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
#include "Time.h"
#include "Function.h"
#include <stdio.h>
#include <stdlib.h>


char *News = NULL;//Save data
unsigned char time[] = {<!-- -->22, 59, 30};
unsigned int date[] = {<!-- -->2023, 12, 31};
char month[] = {<!-- -->0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 

void TIM2_IRQHandler(void){<!-- -->//Timer 2
//Main application time update
if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET){<!-- -->
\t\t\t  
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);//Clear the flag bit
Time_Control(time, month, date);
}
\t\t  
}
 

 
int main(void){<!-- -->
\t 
OLED_Init();//Initialize OLED
  Time_Init();//Start the timer
Serial_Init();//Open the serial port
while(1){<!-- -->
\t\t
//uint16_t count = 0;
if(Serial_GetRxFlag() == 1){<!-- -->
Delay_ms(1000);//Waiting for data transmission to complete
News = Serial_returnNews();
//count + + ;
//OLED_ShowNum(2,10, count, 4);
OLED_ShowString(3, 1, News);
if(Function_DateCheck(Serial_GetRFlagTime(), Serial_GetFlagDate()) == 0){<!-- -->
OLED_ShowString(4, 1, "ERROR");
}
else{<!-- -->
OLED_ShowString(4, 1, "RIGHT");
}
Serial_RESETI();//I to 0
free(News);//The released space must be released, otherwise the address will be disordered and the machine will be jammed directly.
}
Time_Show(time);
Time_Show_Date(date);
\t\t    
}
\t
}



The if loop in the main function of inputting data should be executed once, but it is executed twice

The reason is that although the flag bit is set to zero when it first enters the if loop, because the data has not been transmitted completely, the flag bit will be set to one later

Modify the code:

#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
#include "Time.h"
#include "Function.h"
#include <stdio.h>
#include <stdlib.h>


char *News = NULL;//Save data
unsigned char time[] = {<!-- -->22, 59, 30};
unsigned int date[] = {<!-- -->2023, 12, 31};
char month[] = {<!-- -->0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 

void TIM2_IRQHandler(void){<!-- -->//Timer 2
//Main application time update
if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET){<!-- -->
\t\t\t  
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);//Clear the flag bit
Time_Control(time, month, date);
}
\t\t  
}
 

 
int main(void){<!-- -->
\t 
OLED_Init();//Initialize OLED
  Time_Init();//Start the timer
Serial_Init();//Open the serial port
while(1){<!-- -->
\t\t
//uint16_t count = 0;
if(Serial_GetRxFlag() == 1){<!-- -->
Delay_ms(1000);//Waiting for data transmission to complete
News = Serial_returnNews();
//count + + ;
//OLED_ShowNum(2,10, count, 4);
OLED_ShowString(3, 1, News);
if(Function_DateCheck(Serial_GetRFlagTime(), Serial_GetFlagDate()) == 0){<!-- -->
OLED_ShowString(4, 1, "ERROR");
}
else{<!-- -->
OLED_ShowString(4, 1, "RIGHT");
}
Serial_RESETI();//I to 0
Serial_GetRxFlag(); // Otherwise the if loop will be executed twice
free(News);//The released space must be released, otherwise the address will be disordered and the machine will be jammed directly.
}
Time_Show(time);
Time_Show_Date(date);
\t\t    
}
\t
}



Rest of the code:

#include "stm32f10x.h" // Device header
#include <stdio.h>
#include <stdarg.h>
#include "Delay.h"
#include <stdlib.h>
#include "OLED.h"

uint8_t Serial_RxData;//Save data
uint8_t Serial_RxFlag;//Flag bit
GPIO_InitTypeDef GPIO_InitStructu;//GPIO
USART_InitTypeDef USART_InitStructure;//Serial port
NVIC_InitTypeDef NVIC_InitStructur; //Interrupt
char news[100] = "";//Save data
uint8_t I = 0;
uint8_t flagDate = 0;//Whether the flag is passed in date data
uint8_t flagTime = 0;//Whether the flag is passed in time data

//uint16_t cnt = 0;

void Serial_Init(void)
{<!-- -->
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
\t
\t 
GPIO_InitStructu.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructu.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructu.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, & amp;GPIO_InitStructu);
\t
GPIO_InitStructu.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructu.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructu.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, & amp;GPIO_InitStructu);
\t
\t
\t 
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, & amp;USART_InitStructure);
\t
\t
\t
\t 
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
\t
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//Channel
\t
\t 
NVIC_InitStructur.NVIC_IRQChannel = USART1_IRQn; //Interrupt channel
NVIC_InitStructur.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructur.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructur.NVIC_IRQChannelSubPriority = 1;
NVIC_Init( & amp;NVIC_InitStructur);
\t
USART_Cmd(USART1, ENABLE);
}


uint8_t Serial_GetRxFlag(void)//Automatically clear after reading the flag bit
{<!-- -->
if (Serial_RxFlag == 1)
{<!-- -->
Serial_RxFlag = 0;
return 1;
}
return 0;
}

uint8_t Serial_GetFlagDate(void)//Automatically clear after reading the flag bit
{<!-- -->
if (flagDate == 1)
{<!-- -->
flagDate = 0;
return 1;
}
return 0;
}

uint8_t Serial_GetRFlagTime(void)//Automatically clear after reading the flag bit
{<!-- -->
if (flagTime == 1)
{<!-- -->
flagTime = 0;
return 1;
}
return 0;
}

void Serial_GetRxFlag_SET(void){<!-- -->
Serial_RxFlag = 1;
}

char * Serial_returnNews(void){<!-- -->//Return an array
char*array;
//int j = I;
uint8_t i = 0;
array = (char *) malloc(sizeof(char) * 100);
while(i < I){<!-- -->
if(news[i] == '/') flagDate = 1;
if(news[i] == ':') flagTime = 1;
array[i] = news[i];
i + + ;
}
// OLED_ShowNum(2,1,j,3);
//OLED_ShowNum(2, 6, i, 3);
//cnt + + ;
//OLED_ShowNum(4, 1, cnt, 3);
return array;
}

void Serial_RESETI(void){<!-- -->//Initialization I
I = 0;
}

uint8_t Serial_GetI(void){<!-- -->//Return I
return I;
}
\t
void USART1_IRQHandler(void)//interrupt function
{<!-- -->
\t
if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
{<!-- -->
news[I] = USART_ReceiveData(USART1);//Read data
Serial_RxFlag = 1; //There is data in the flag bit
I++;
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
\t\t 
}
}


#ifndef __SERIAL_H
#define __SERIAL_H
#include "stm32f10x.h"
#include <stdio.h>

void Serial_Init(void);
uint8_t Serial_GetRxFlag(void);
uint8_t Serial_GetRxData(void);
void Serial_GetRxFlag_SET(void);
char * Serial_returnNews(void);
void Serial_RESETI(void);
uint8_t Serial_GetI(void);
uint8_t Serial_GetRFlagTime(void);
uint8_t Serial_GetFlagDate(void);

#endif