• 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);
    		    
    	}
    	
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    输入一次数据主函数中的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);
    		    
    	}
    	
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    其余代码:

    #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);
    		 
    	}
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    #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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
  • 相关阅读:
    软考高级之系统架构师之软件需求工程
    可达矩阵-邻接矩阵-以及有向图的python绘制
    凭着这份java面试一战到底,直接碾压面试官
    STM32——SDIO的学习(驱动SD卡)(实战篇)
    MacOS罗技鼠标定义的功能键经常失灵
    Git技法:.gitignore、移除暂存与撤销修改
    list的用法
    含文档+PPT+源码等]精品基于Uniapp+SSM实现的酒品移动电商平台app[包运行成功]计算机毕业设计Android项目源码
    Golang slice 通过growslice调用nextslicecap计算扩容
    Spring Boot:常见注解之@Override、@EntityListeners
  • 原文地址:https://blog.csdn.net/xyint/article/details/133863986