实现了手机发送信息给蓝牙模块,程序对数据进行分析拆解,并更新自身数据
main.c:
c
#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;//存数据
unsigned int 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};
unsigned int updateTime[] = {0, 0, 0};
unsigned int updateDate[] = {0, 0, 0};
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 flag = 0;
if(Serial_GetRxFlag() == 1){
Delay_ms(1000);//等待数据传输完
News = Serial_returnNews();
OLED_ShowString(3, 1, News);
if((flag = Function_DateCheck(Serial_GetRFlagTime(), Serial_GetFlagDate())) == 0){//至少得有标志符号
OLED_ShowString(4, 1, "error");
}
else{
if(flag == 1){
if(Function_TimeUpdate(updateTime, time, News) == 1) OLED_ShowString(4, 1, ":RIGHT");//写入
else OLED_ShowString(4, 1, "ERROR");
}
else{
OLED_ShowString(4, 1, "/RIGHT");
}
Function_ArrayReset(updateTime, updateDate);
}
flag = 0;
Serial_RESETI();//I至0
Serial_GetRxFlag();//制零否者if循环将会被执行两次
free(News);//释放空间必须释放否者发生地址紊乱,直接卡机
}
Time_Show(time);
//Time_Show_Date(date);
}
}
Function.c:
c
#include "stm32f10x.h"
#include "Serial.h"
#include "OLED.h"
#include "Delay.h"
//一些函数的实现
void Function_ArrayClear(char *News){//恢复数组初始化
uint16_t i = 0;
for(i = 0; i < 100; i ++) News[i] = '\0';
}
uint8_t Function_ArrayLength(char * a){//计算数组长度函数
uint8_t length = 0;
uint8_t i = 0;
while(a[i] != '\0'){
i ++;
length ++;
}
return length;
}
uint8_t Function_DateCheck(uint8_t flagTime, uint8_t flagDate){
if(flagDate == flagTime) return 0;
if(flagTime == 1) return 1;
return 2;
}
uint8_t Function_Numlength(uint16_t num){
uint8_t length = 0;
if(num == 0) return (uint8_t) 1;
while(num > 0){
num = num / 10;
length = length + 1;
}
return length;
}
uint8_t Function_TimeDateState(unsigned int *updateTime, char *News){
uint8_t cnt = 0;
uint8_t i = 0;
uint8_t end = Serial_GetI();
while(i < end){
if(News[i] == ':') {
cnt ++;
i ++;
if(cnt >= 3) return 0;
continue;
}
updateTime[cnt] = updateTime[cnt] * 10 + (News[i] - '0');
i ++;
}
//OLED_ShowNum(2,1,updateTime[2], 3);
if(cnt != 2) return 0;
//OLED_ShowString(2, 10, "here");
//OLED_ShowNum(2,1,, 3);
if(Function_Numlength(updateTime[0]) >= 3 || updateTime[0] > 23) return 0;
//OLED_ShowString(2, 10, "here");
if(Function_Numlength(updateTime[1]) >= 3 || updateTime[1] > 59) return 0;
if(Function_Numlength(updateTime[2]) >= 3 || updateTime[2] > 59) return 0;
//OLED_ShowString(2, 10, "here");
return 1;
}
uint8_t Function_TimeUpdate(unsigned int *updateTime, unsigned int *time, char *News){
if(Function_TimeDateState(updateTime, News) == 1){
uint8_t i = 0;
OLED_ShowString(2, 12, "here");
while(i < 3){
time[i] = updateTime[i];
i ++;
}
return 1;
}
return 0;
}
void Function_ArrayReset(unsigned int *updateTime, unsigned int *updateDate){
uint8_t i = 0;
while(i < 3){
updateDate[i] = 0;
updateTime[i] = 0;
i ++;
}
}
Function.h:
c
#ifndef __FUNCTION_H
#define __FUNCTION_H
#include "stm32f10x.h"
#include <stdio.h>
void Function_ArrayClear(char *News);
uint8_t Function_ArrayLength(char * a);
uint8_t Function_DateCheck(uint8_t flagDate, uint8_t flagTime);
uint8_t Function_Numlength(uint16_t num);
uint8_t Function_TimeDateState(unsigned int *updateTime, char *News);
uint8_t Function_TimeUpdate(unsigned int *updateTime, unsigned int *time, char *News);
void Function_ArrayReset(unsigned int *updateTime, unsigned int *updateDate);
#endif
Time.c:
c
#include "stm32f10x.h"
#include "OLED.h"
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/*初始化通用定时器TIM2*/
void Time_Init(void){
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//APB1外设开启
TIM_InternalClockConfig(TIM2);//选择内部时钟
/*初始化时基单元*/
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
TIM_TimeBaseInitStructure.TIM_Period = 10000 - 1;//ARR自动重装
TIM_TimeBaseInitStructure.TIM_Prescaler = 7200 - 1;//psc预分频器
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;//高级计时器内容直接给零
//记录1s
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);//刚初始化完就会进中断
TIM_ClearFlag(TIM2, TIM_FLAG_Update);//消除中断标志位
//使能更新中断
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/*配置中断*/
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//选择组2
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;//定时器2在NVIC内的通道
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM2, ENABLE);//启动定时器
}
void Time_Control(unsigned int *time, char *month, unsigned int *date){//更新时间
time[2] = time[2] + 1;
if(time[2] >= 60){
time[2] = 0;
time[1] = time[1] + 1;
if(time[1] >= 60){
time[1] = 0;
time[0] = time[0] + 1;
if(time[0] >= 24){
time[0] = 0;
date[2] = date[2] + 1;
if(date[2] >= month[date[1]] + 1){
date[2] = 1;
date[1] = date[1] + 1;
if(date[1] >= 13){
date[1] = 1;
date[0] = date[0] + 1;
if(date[0] >= 9999){
date[0] = 2023;
}
}
}
}
}
}
}
void Time_month2_Control(char *month,unsigned int *date){//判别闰平年
if((date[0] % 4 == 0 && date[0] % 100 != 0 )|| date[0] % 400 == 0) month[2] = 29;
else month[2] = 28;
}
void Time_Show(unsigned int *time){
OLED_ShowNum(1,1,time[0], 2);
OLED_ShowString(1, 3, ":");
OLED_ShowNum(1,4,time[1], 2);
OLED_ShowString(1, 6, ":");
OLED_ShowNum(1,7,time[2], 2);
}
void Time_Show_Date(unsigned int *date){
OLED_ShowNum(2,1,date[0], 4);
OLED_ShowString(2, 5, "/");
OLED_ShowNum(2,6,date[1], 2);
OLED_ShowString(2, 8, "/");
OLED_ShowNum(2,9,date[2], 2);
}
Time.h:
c
//显示时间&日期
#ifndef __TIME_H
#define __TIME_H
#include "stm32f10x.h"
#include <stdio.h>
void Time_Init(void);
void Time_Control(unsigned int *time, char *month, unsigned int *date);
void Time_month2_Control(char *month,unsigned int *date);
void Time_Show(unsigned int *time);
void Time_Show_Date(unsigned int *date);
#endif
Serial.c:
c
#include "stm32f10x.h" // Device header
#include <stdio.h>
#include <stdarg.h>
#include "Delay.h"
#include <stdlib.h>
#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;//标志是否传入时间数据
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 ++;
}
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);
}
}
Serial.h:
c
#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
效果:
时间 + 日期
main.c:
c
#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;//存数据
unsigned int 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};
unsigned int updateTime[] = {0, 0, 0};
unsigned int updateDate[] = {0, 0, 0};
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 flag = 0;
Time_month2_Control(month, date);
if(Serial_GetRxFlag() == 1){
Delay_ms(1000);//等待数据传输完
News = Serial_returnNews();
OLED_ShowString(3, 1, News);
if((flag = Function_DateCheck(Serial_GetRFlagTime(), Serial_GetFlagDate())) == 0){//至少得有标志符号
OLED_ShowString(4, 1, "error");
}
else{
if(flag == 1){
if(Function_TimeUpdate(updateTime, time, News) == 1) OLED_ShowString(4, 1, ":RIGHT");//写入
else OLED_ShowString(4, 1, "ERROR");
}
else{
if(Function_DateUpdate(updateDate,date,News) == 1)OLED_ShowString(4, 1, "/RIGHT");
else
OLED_ShowString(4, 1, "ERROr");
}
Function_ArrayReset(updateTime, updateDate);
}
Serial_RESETI();//I至0
Serial_GetRxFlag();//制零否者if循环将会被执行两次
free(News);//释放空间必须释放否者发生地址紊乱,直接卡机
}
Time_Show(time);
Time_Show_Date(date);
}
}
Function.c:
c
#include "stm32f10x.h"
#include "Serial.h"
#include "OLED.h"
#include "Delay.h"
//一些函数的实现
void Function_ArrayClear(char *News){//恢复数组初始化
uint16_t i = 0;
for(i = 0; i < 100; i ++) News[i] = '\0';
}
uint8_t Function_ArrayLength(char * a){//计算数组长度函数
uint8_t length = 0;
uint8_t i = 0;
while(a[i] != '\0'){
i ++;
length ++;
}
return length;
}
uint8_t Function_DateCheck(uint8_t flagTime, uint8_t flagDate){
if(flagDate == flagTime) return 0;
if(flagTime == 1) return 1;
return 2;
}
uint8_t Function_Numlength(uint16_t num){
uint8_t length = 0;
if(num == 0) return (uint8_t) 1;
while(num > 0){
num = num / 10;
length = length + 1;
}
return length;
}
uint8_t Function_TimeDateState(unsigned int *updateTime, char *News){
uint8_t cnt = 0;
uint8_t i = 0;
uint8_t end = Serial_GetI();
while(i < end){
if(News[i] == ':') {
cnt ++;
i ++;
if(cnt >= 3) return 0;
continue;
}
updateTime[cnt] = updateTime[cnt] * 10 + (News[i] - '0');
i ++;
}
if(cnt != 2) return 0;
if(Function_Numlength(updateTime[0]) >= 3 || updateTime[0] > 23) return 0;
if(Function_Numlength(updateTime[1]) >= 3 || updateTime[1] > 59) return 0;
if(Function_Numlength(updateTime[2]) >= 3 || updateTime[2] > 59) return 0;
return 1;
}
uint8_t Function_TimeUpdate(unsigned int *updateTime, unsigned int *time, char *News){
if(Function_TimeDateState(updateTime, News) == 1){
uint8_t i = 0;
//OLED_ShowString(2, 12, "here");
while(i < 3){
time[i] = updateTime[i];
i ++;
}
return 1;
}
return 0;
}
void Function_ArrayReset(unsigned int *updateTime, unsigned int *updateDate){
uint8_t i = 0;
while(i < 3){
updateDate[i] = 0;
updateTime[i] = 0;
i ++;
}
}
uint8_t Function_DateState(unsigned int *updateDate, char *News){
uint8_t cnt = 0;
uint8_t i = 0;
uint8_t end = Serial_GetI();
char month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
while(i < end){
if(News[i] == '/') {
cnt ++;
i ++;
if(cnt >= 3) {
return 0;
}
continue;
}
updateDate[cnt] = updateDate[cnt]* 10 + (News[i] - '0');
i ++;
}
if(cnt != 2) return 0;
if(Function_Numlength(updateDate[0]) >= 5 || updateDate[0] > 9999 || updateDate[0] == 0) return 0;
if((updateDate[0] % 4 == 0 && updateDate[0] % 100 != 0 )|| updateDate[0] % 400 == 0) month[2] = 29;
else month[2] = 28;
if(Function_Numlength(updateDate[1]) >= 3 || updateDate[1] > 12 || updateDate[1] == 0) return 0;
if(Function_Numlength(updateDate[2]) >= 3 || updateDate[2] > month[updateDate[1]]|| updateDate[2] == 0) return 0;
return 1;
}
uint8_t Function_DateUpdate(unsigned int *updateDate, unsigned int *date, char *News){
if(Function_DateState(updateDate, News) == 1){
uint8_t i = 0;
while(i < 3){
date[i] = updateDate[i];
i ++;
}
return 1;
}
return 0;
}
Function.h:
c
#ifndef __FUNCTION_H
#define __FUNCTION_H
#include "stm32f10x.h"
#include <stdio.h>
void Function_ArrayClear(char *News);
uint8_t Function_ArrayLength(char * a);
uint8_t Function_DateCheck(uint8_t flagDate, uint8_t flagTime);
uint8_t Function_Numlength(uint16_t num);
uint8_t Function_TimeDateState(unsigned int *updateTime, char *News);
uint8_t Function_TimeUpdate(unsigned int *updateTime, unsigned int *time, char *News);
void Function_ArrayReset(unsigned int *updateTime, unsigned int *updateDate);
uint8_t Function_DateState(unsigned int *updateDate, char *News);
uint8_t Function_DateUpdate(unsigned int *updateDate, unsigned int *date, char *News);
#endif
效果: