Stm32_标准库_15_串口&蓝牙模块_手机与蓝牙模块通信_BUG修复

代码:

#include "stm32f10x.h"    // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
#include "Time.h"
#include "Function.h"
#include 
#include 


char *News = NULL;//存数据
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){//定时器2
	   //主要运用时间更新
	   if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET){
			  
			 TIM_ClearITPendingBit(TIM2, TIM_IT_Update);//清除标志位
			 Time_Control(time, month, date);
		 }
		  
}
 

 
int main(void){
	 
	OLED_Init();//初始化OLED
  Time_Init();//开启计时器
	Serial_Init();//开启串口
	while(1){
		
		//uint16_t count = 0;
		if(Serial_GetRxFlag() == 1){
			 Delay_ms(1000);//等待数据传输完
			 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至0
			 free(News);//释放空间必须释放否者发生地址紊乱,直接卡机
		}
		Time_Show(time);
		Time_Show_Date(date);
		    
	}
	
}



输入一次数据主函数中的if循环理应执行一次,但是却执行了两次

原因是在刚进入if循环的时候虽然标志位被制零了但是,由于数据没有传输完所以后续还会把标志位制一

修改代码:

#include "stm32f10x.h"    // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
#include "Time.h"
#include "Function.h"
#include 
#include 


char *News = NULL;//存数据
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){//定时器2
	   //主要运用时间更新
	   if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET){
			  
			 TIM_ClearITPendingBit(TIM2, TIM_IT_Update);//清除标志位
			 Time_Control(time, month, date);
		 }
		  
}
 

 
int main(void){
	 
	OLED_Init();//初始化OLED
  Time_Init();//开启计时器
	Serial_Init();//开启串口
	while(1){
		
		//uint16_t count = 0;
		if(Serial_GetRxFlag() == 1){
			 Delay_ms(1000);//等待数据传输完
			 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至0
			 Serial_GetRxFlag();//制零否者if循环将会被执行两次
			 free(News);//释放空间必须释放否者发生地址紊乱,直接卡机
		}
		Time_Show(time);
		Time_Show_Date(date);
		    
	}
	
}




其余代码:

#include "stm32f10x.h"                  // Device header
#include 
#include 
#include "Delay.h"
#include 
#include "OLED.h"

uint8_t Serial_RxData;//存数据
uint8_t Serial_RxFlag;//标志位
GPIO_InitTypeDef GPIO_InitStructu;//GPIO
USART_InitTypeDef USART_InitStructure;//串口
NVIC_InitTypeDef NVIC_InitStructur;//中断
char news[100] = "";//存数据
uint8_t I = 0;
uint8_t flagDate = 0;//标志是否传入日期数据
uint8_t flagTime = 0;//标志是否传入时间数据

//uint16_t cnt = 0;

void Serial_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	 
	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, &GPIO_InitStructu);
	
	GPIO_InitStructu.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructu.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructu.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructu);
	
	
	 
	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, &USART_InitStructure);
	
	
	
	 
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//通道
	
	 
	NVIC_InitStructur.NVIC_IRQChannel = USART1_IRQn;//中断通道
	NVIC_InitStructur.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructur.NVIC_IRQChannelPreemptionPriority = 3;
	NVIC_InitStructur.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructur);
	
	USART_Cmd(USART1, ENABLE);
}


uint8_t Serial_GetRxFlag(void)//读取标志位后自动青除
{
	if (Serial_RxFlag == 1)
	{
		Serial_RxFlag = 0;
		return 1;
	}
	return 0;
}

uint8_t Serial_GetFlagDate(void)//读取标志位后自动青除
{
	if (flagDate == 1)
	{
		flagDate = 0;
		return 1;
	}
	return 0;
}

uint8_t Serial_GetRFlagTime(void)//读取标志位后自动青除
{
	if (flagTime == 1)
	{
		flagTime = 0;
		return 1;
	}
	return 0;
}

void Serial_GetRxFlag_SET(void){
	   Serial_RxFlag = 1;
}

char * Serial_returnNews(void){//返还一个数组
	     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){//初始化I
	   I = 0;
}

uint8_t Serial_GetI(void){//返回I
	   return I;
}
	
void USART1_IRQHandler(void)//中断函数
{
	
	if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
	{
		news[I] = USART_ReceiveData(USART1);//读数据
		Serial_RxFlag = 1;//至标志位为有数据
		I ++;
		USART_ClearITPendingBit(USART1, USART_IT_RXNE);
		 
	}
}


#ifndef __SERIAL_H
#define __SERIAL_H
#include "stm32f10x.h"  
#include 

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

你可能感兴趣的:(stm32,智能手机,bug)