freertos_demo.c


#include “headerfile.h”

/* 启动任务函数 */
#define START_TASK_PRIORITY 1
#define START_TASK_STACK_DEPTH 128
TaskHandle_t start_task_handler;
void Start_Task(void *pvParameters);

/* Task1 任务 配置 */
#define TASK1_PRIORITY 2
#define TASK1_STACK_DEPTH 128
TaskHandle_t task1_handler;
void Task1(void *pvParameters);

/* Task2 任务 配置 */
#define TASK2_PRIORITY 3
#define TASK2_STACK_DEPTH 128
TaskHandle_t task2_handler;
void Task2(void *pvParameters);

/* Task3 任务 配置 */
#define TASK3_PRIORITY 4
#define TASK3_STACK_DEPTH 128
TaskHandle_t task3_handler;
void Task3(void *pvParameters);

// 入口函数
QueueHandle_t queue1; /* 队列句柄 /
EventGroupHandle_t eventgroup_handle; /
事件标志组句柄 */

void FreeRTOS_Start(void)
{
/* 创建 queue1 队列 */
queue1 = xQueueCreate(64, sizeof(uint8_t));

/* 创建事件标志组 */

eventgroup_handle = xEventGroupCreate();

xTaskCreate((TaskFunction_t)Start_Task,
(char *)“Start_Task”,
(configSTACK_DEPTH_TYPE)START_TASK_STACK_DEPTH,
(void *)NULL,
(UBaseType_t)START_TASK_PRIORITY,
(TaskHandle_t *)&start_task_handler);
vTaskStartScheduler();
}

void Start_Task(void pvParameters)
{
taskENTER_CRITICAL(); /
进入临界区 */

	xTaskCreate((TaskFunction_t)Task1,
							(char *)"Task1",
							(configSTACK_DEPTH_TYPE)TASK1_STACK_DEPTH,
							(void *)NULL,
							(UBaseType_t)TASK1_PRIORITY,
							(TaskHandle_t *)&task1_handler);
	xTaskCreate((TaskFunction_t)Task2,
							(char *)"Task2",
							(configSTACK_DEPTH_TYPE)TASK2_STACK_DEPTH,
							(void *)NULL,
							(UBaseType_t)TASK2_PRIORITY,
							(TaskHandle_t *)&task2_handler);
	xTaskCreate((TaskFunction_t)Task3,
							(char *)"Task2",
							(configSTACK_DEPTH_TYPE)TASK3_STACK_DEPTH,
							(void *)NULL,
							(UBaseType_t)TASK3_PRIORITY,
							(TaskHandle_t *)&task3_handler);
							

							vTaskDelete(NULL);
							
							taskEXIT_CRITICAL(); /* 退出临界区 */

}

void Task1(void *pvParameters)
{

Task_UI_Beep();

}

void Task2(void *pvParameters)
{

Task_Key_Serial();

}

void Task3(void *pvParameters)
{

Task_Bluetooth_Send();

}

headerfile.h


#ifndef __headerfile_h
#define __headerfile_h

#include “main.h”
#include “stdio.h”
#include “string.h”
#include “stdint.h”

#include “gpio.h”

#include “freertos.h”
#include “task.h”
#include “cmsis_os2.h”
#include “queue.h”
#include “event_groups.h”

#include “tim.h”
#include “usart.h”
#include “i2c.h”
#include “rtc.h”

#include “OLED.h”
#include “fun.h”
#include “led.h”
#include “DHT11.h”
#include “myrtc.h”
#include “key.h”
#include “beep.h”
#include “UI.h”
#include “app_tasks.h”

#endif

key.c


#include “headerfile.h”
/// @brief 拷贝参数(Key初始化)
///
/// @param Key_Num :按键编号
///
/// @note

stKeyTdf Key_param[KEY_NUM];

void key_init(stKeyTdf *key_init_param ,emKeyTdf Key_Num)
{
memcpy(&Key_param[Key_Num],key_init_param,sizeof(stKeyTdf));
}

/// @brief key执行
///
/// @param Key_Num :按键编号
///
/// @note

void Key_Scan(emKeyTdf Key_Num)
{
Key_param[Key_Num].KeyValue = 0;
if(HAL_GPIO_ReadPin(Key_param[Key_Num].KeyGPIOx,Key_param[Key_Num].KeyGPIO_Pin) == GPIO_PIN_RESET)
{
vTaskDelay(100);
if(HAL_GPIO_ReadPin(Key_param[Key_Num].KeyGPIOx,Key_param[Key_Num].KeyGPIO_Pin) == GPIO_PIN_RESET)
{
Key_param[Key_Num].KeyValue = 1;
}
}
}

void Key_Excute(void)
{
Key_Scan(Key1);
Key_Scan(Key2);
Key_Scan(Key3);
Key_Scan(Key4);
}
/// @brief key获取键值
///
/// @param Key_Num :按键编号
///
/// @note

uint8_t Get_KeyNum(emKeyTdf Key_Num)

key.h


#ifndef _key_h
#define _key_h

#include “headerfile.h”

#define KEY_NUM 4

/// @brief key 按键数枚举
///
/// @note
typedef enum
{
Key1 = 0,
Key2,
Key3,
Key4,
}
emKeyTdf;

/// @brief key GPIO引角配置
///
/// @note

typedef struct
{
GPIO_TypeDef *KeyGPIOx;
uint16_t KeyGPIO_Pin;
uint8_t KeyValue;
}
stKeyTdf;

void Key_Scan(emKeyTdf Key_Num);
void key_init(stKeyTdf *key_init_param ,emKeyTdf Key_Num);
void Key_Excute(void);
uint8_t Get_KeyNum(emKeyTdf Key_Num);

#endif

DHT11.c


#include “headerfile.h”

stDht11ParamTdf Dht11Param[DHT11_NUM];

/// @brief 获取 Dht11 设备参数
///
/// @param Dht11_Num :设备号
///
/// @note

const stDht11ParamTdf *Get_Dht11Param(emDht11Tdf Dht11_Num)
{
return &Dht11Param[Dht11_Num];
}

/// @brief Dht11初始化
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_init(stDht11ParamTdf *Dht11_ParamInit,emDht11Tdf Dht11_Num)
{
memcpy(&Dht11Param[Dht11_Num].Dht11Cong,Dht11_ParamInit,sizeof(stDht11ConfigTdf) / sizeof(uint8_t));
}

/// @brief 将引角设为输入
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_pin_setInPut(emDht11Tdf Dht11_Num)
{
GPIO_InitTypeDef GPIO_InitStruct;

GPIO_InitStruct.Pin     = Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIO_Pin;
GPIO_InitStruct.Mode    = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull    = GPIO_NOPULL;
GPIO_InitStruct.Speed   = GPIO_SPEED_FREQ_MEDIUM;
HAL_GPIO_Init(Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIOx, &GPIO_InitStruct);

}

/// @brief 将引角设为输出
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_pin_setOutPut(emDht11Tdf Dht11_Num)
{
GPIO_InitTypeDef GPIO_InitStruct;

GPIO_InitStruct.Pin     = Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIO_Pin;
GPIO_InitStruct.Mode    = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull    = GPIO_NOPULL;
GPIO_InitStruct.Speed   = GPIO_SPEED_FREQ_MEDIUM;
HAL_GPIO_Init(Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIOx, &GPIO_InitStruct);

}

/// @brief 设置引角为高电平
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_pin_set(emDht11Tdf Dht11_Num)
{
HAL_GPIO_WritePin(Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIOx,Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIO_Pin,GPIO_PIN_SET);
}

/// @brief 设置引角为低电平
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_pin_reset(emDht11Tdf Dht11_Num)
{
HAL_GPIO_WritePin(Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIOx,Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIO_Pin,GPIO_PIN_RESET);
}

/// @brief 空闲状态
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_idle(emDht11Tdf Dht11_Num)
{
uint32_t TimeCount=210001000/Dht11Param[Dht11_Num].Dht11Cong.TimerPeriorUs;
stDht11WheelRuningTdf *Dht11WheelRuning = &Dht11Param[Dht11_Num].Dht11Wheelruning;
Dht11WheelRuning->wheelCount++;

if(Dht11WheelRuning->wheelCount >= TimeCount)
{
	Dht11WheelRuning->wheelCount = 0;
	Dht11WheelRuning->Dht11ReadState = Dht11_StartLow;
	dht11_pin_setOutPut(Dht11_Num);
	dht11_pin_reset(Dht11_Num);
}

}

/// @brief [开始低状态]执行
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_startlow(emDht11Tdf Dht11_Num)
{
uint32_t TimeCount=20*1000/Dht11Param[Dht11_Num].Dht11Cong.TimerPeriorUs;
stDht11WheelRuningTdf *Dht11WheelRuning = &Dht11Param[Dht11_Num].Dht11Wheelruning;
Dht11WheelRuning->wheelCount++;

if(Dht11WheelRuning->wheelCount >= TimeCount)
{
	Dht11WheelRuning->wheelCount = 0;
	Dht11WheelRuning->Dht11ReadState = Dht11_StartHigh;
	dht11_pin_setOutPut(Dht11_Num);
	dht11_pin_set(Dht11_Num);
}

}

/// @brief [开始高状态]执行
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_starthigh(emDht11Tdf Dht11_Num)
{
uint32_t TimeCount=15/Dht11Param[Dht11_Num].Dht11Cong.TimerPeriorUs;
stDht11WheelRuningTdf *Dht11WheelRuning = &Dht11Param[Dht11_Num].Dht11Wheelruning;
Dht11WheelRuning->wheelCount++;

if(Dht11WheelRuning->wheelCount >= TimeCount)
{
	Dht11WheelRuning->wheelCount = 0;
	Dht11WheelRuning->Dht11ReadState = Dht11_WaitAckLow;
	dht11_pin_setInPut(Dht11_Num);
}

}

/// @brief [等待应答低状态]执行
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_waitacklow(emDht11Tdf Dht11_Num)
{
uint32_t TimeCount=100/Dht11Param[Dht11_Num].Dht11Cong.TimerPeriorUs;
stDht11WheelRuningTdf *Dht11WheelRuning = &Dht11Param[Dht11_Num].Dht11Wheelruning;
Dht11WheelRuning->wheelCount++;

if(Dht11WheelRuning->wheelCount >= TimeCount)
{
	Dht11WheelRuning->wheelCount = 0;
	Dht11WheelRuning->Dht11ReadState = Dht11_Tdle;
	dht11_pin_setOutPut(Dht11_Num);
	dht11_pin_set(Dht11_Num);
}

if(HAL_GPIO_ReadPin(Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIOx,Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIO_Pin) == GPIO_PIN_RESET)
{
	Dht11WheelRuning->wheelCount = 0;
	Dht11WheelRuning->Dht11ReadState = Dht11_WaitAckHigh;
}

}

/// @brief [等待应答高状态]执行
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_waitackhigh(emDht11Tdf Dht11_Num)
{
uint32_t TimeCount=100/Dht11Param[Dht11_Num].Dht11Cong.TimerPeriorUs;
stDht11WheelRuningTdf *Dht11WheelRuning = &Dht11Param[Dht11_Num].Dht11Wheelruning;
Dht11WheelRuning->wheelCount++;

if(Dht11WheelRuning->wheelCount >= TimeCount)
{
	Dht11WheelRuning->wheelCount = 0;
	Dht11WheelRuning->Dht11ReadState = Dht11_Tdle;
	dht11_pin_setOutPut(Dht11_Num);
	dht11_pin_set(Dht11_Num);
}

if(HAL_GPIO_ReadPin(Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIOx,Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIO_Pin) == GPIO_PIN_SET)
{
	Dht11WheelRuning->wheelCount = 0;
	Dht11WheelRuning->Dht11ReadState = Dht11_WaitReadStart;
}

}

/// @brief [等待可开始读取]执行
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_waitreadstart(emDht11Tdf Dht11_Num)
{
uint32_t TimeCount=100/Dht11Param[Dht11_Num].Dht11Cong.TimerPeriorUs;
stDht11WheelRuningTdf *Dht11WheelRuning = &Dht11Param[Dht11_Num].Dht11Wheelruning;
Dht11WheelRuning->wheelCount++;

if(Dht11WheelRuning->wheelCount >= TimeCount)
{
	Dht11WheelRuning->wheelCount = 0;
	Dht11WheelRuning->Dht11ReadState = Dht11_Tdle;
	dht11_pin_setOutPut(Dht11_Num);
	dht11_pin_set(Dht11_Num);
}

if(HAL_GPIO_ReadPin(Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIOx,Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIO_Pin) == GPIO_PIN_RESET)
{
	Dht11WheelRuning->wheelCount = 0;
Dht11WheelRuning->LowCount = 0;
Dht11WheelRuning->HighCount = 0;
Dht11WheelRuning->BitCount = 0;
	Dht11WheelRuning->wheelCount = 0;
	Dht11WheelRuning->Dht11ReadState = Dht11_Reading;
}

}

/// @brief [读取]执行
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_reading(emDht11Tdf Dht11_Num)
{
uint32_t TimeCount=100/Dht11Param[Dht11_Num].Dht11Cong.TimerPeriorUs;
stDht11WheelRuningTdf *Dht11WheelRuning = &Dht11Param[Dht11_Num].Dht11Wheelruning;
Dht11WheelRuning->wheelCount++;

	if(Dht11WheelRuning->LowCount == 0)
{
		if((HAL_GPIO_ReadPin(Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIOx,Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIO_Pin) == GPIO_PIN_SET))
		{
			Dht11WheelRuning->LowCount = Dht11WheelRuning->wheelCount;
			Dht11WheelRuning->wheelCount = 0;
		}
			if(Dht11WheelRuning->wheelCount >= TimeCount)
	{
		Dht11WheelRuning->wheelCount = 0;
		Dht11WheelRuning->HighCount = 0;
		Dht11WheelRuning->LowCount = 0;
		Dht11WheelRuning->BitCount = 0;
		Dht11WheelRuning->wheelCount = 0;
		Dht11WheelRuning->Dht11ReadState = Dht11_Tdle;
		dht11_pin_setOutPut(Dht11_Num);
		dht11_pin_set(Dht11_Num);
	}
		
}
else
{
		if((HAL_GPIO_ReadPin(Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIOx,Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIO_Pin) == GPIO_PIN_RESET))
		{
			Dht11WheelRuning->HighCount = Dht11WheelRuning->wheelCount;
			
			if(Dht11WheelRuning->HighCount > Dht11WheelRuning->LowCount)
			{
				Dht11WheelRuning->TempCount[(Dht11WheelRuning->BitCount) >> 3] |= 0x01 << (7-(Dht11WheelRuning->BitCount)%8);
			}
			else
			{
				Dht11WheelRuning->TempCount[(Dht11WheelRuning->BitCount) >> 3] &= ~(0x01 << (7-(Dht11WheelRuning->BitCount)%8));
			}
			
		Dht11WheelRuning->wheelCount = 0;
		Dht11WheelRuning->HighCount = 0;
		Dht11WheelRuning->LowCount = 0;
		Dht11WheelRuning->BitCount++;
		}
			if(Dht11WheelRuning->wheelCount >= TimeCount)
	{
		Dht11WheelRuning->wheelCount = 0;
		Dht11WheelRuning->HighCount = 0;
		Dht11WheelRuning->LowCount = 0;
		Dht11WheelRuning->BitCount = 0;
		Dht11WheelRuning->Dht11ReadState = Dht11_Tdle;
		dht11_pin_setOutPut(Dht11_Num);
		dht11_pin_set(Dht11_Num);
	}	
}

if(Dht11WheelRuning->BitCount >= 40)
{
	Dht11WheelRuning->BitCount = 0;
	
	if(Dht11WheelRuning->TempCount[4] == Dht11WheelRuning->TempCount[0]+
		                                   Dht11WheelRuning->TempCount[1]+
																			 Dht11WheelRuning->TempCount[2]+
	                                     Dht11WheelRuning->TempCount[3])
	{
		
		Dht11Param[Dht11_Num].Dht11Runing.Humidity[0] = Dht11WheelRuning->TempCount[0];
		Dht11Param[Dht11_Num].Dht11Runing.Humidity[1] = Dht11WheelRuning->TempCount[1];
		Dht11Param[Dht11_Num].Dht11Runing.Temperature[0] = Dht11WheelRuning->TempCount[2];
		Dht11Param[Dht11_Num].Dht11Runing.Temperature[1] = Dht11WheelRuning->TempCount[3];

	}
		Dht11WheelRuning->Dht11ReadState = Dht11_Tdle;
		dht11_pin_setOutPut(Dht11_Num);
	  dht11_pin_set(Dht11_Num);
				
}

}

/// @brief 状态机回调函数
///
/// @param Dht11_Num :设备号
///
/// @note

void Dht11_Callback(emDht11Tdf Dht11_Num)
{
stDht11WheelRuningTdf *Dht11WheelRuning = &Dht11Param[Dht11_Num].Dht11Wheelruning;

switch(Dht11WheelRuning->Dht11ReadState)
{
	case Dht11_Tdle:
	{
		dht11_idle(Dht11_Num);break;
	}
	case Dht11_StartLow:
	{
		dht11_startlow(Dht11_Num);break;
	}
	case Dht11_StartHigh:
	{
		dht11_starthigh(Dht11_Num);break;
	}
	case Dht11_WaitAckLow:
	{
		dht11_waitacklow(Dht11_Num);break;
	}
	case Dht11_WaitAckHigh:
	{
		dht11_waitackhigh(Dht11_Num);break;
	}
	case Dht11_WaitReadStart:
	{
		dht11_waitreadstart(Dht11_Num);break;
	}
	case Dht11_Reading:
	{
		dht11_reading(Dht11_Num);break;
	}
	default:
	{
		break;
	}
}

}

DHT11.h


#include “headerfile.h”

stDht11ParamTdf Dht11Param[DHT11_NUM];

/// @brief 获取 Dht11 设备参数
///
/// @param Dht11_Num :设备号
///
/// @note

const stDht11ParamTdf *Get_Dht11Param(emDht11Tdf Dht11_Num)
{
return &Dht11Param[Dht11_Num];
}

/// @brief Dht11初始化
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_init(stDht11ParamTdf *Dht11_ParamInit,emDht11Tdf Dht11_Num)
{
memcpy(&Dht11Param[Dht11_Num].Dht11Cong,Dht11_ParamInit,sizeof(stDht11ConfigTdf) / sizeof(uint8_t));
}

/// @brief 将引角设为输入
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_pin_setInPut(emDht11Tdf Dht11_Num)
{
GPIO_InitTypeDef GPIO_InitStruct;

GPIO_InitStruct.Pin     = Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIO_Pin;
GPIO_InitStruct.Mode    = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull    = GPIO_NOPULL;
GPIO_InitStruct.Speed   = GPIO_SPEED_FREQ_MEDIUM;
HAL_GPIO_Init(Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIOx, &GPIO_InitStruct);

}

/// @brief 将引角设为输出
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_pin_setOutPut(emDht11Tdf Dht11_Num)
{
GPIO_InitTypeDef GPIO_InitStruct;

GPIO_InitStruct.Pin     = Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIO_Pin;
GPIO_InitStruct.Mode    = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull    = GPIO_NOPULL;
GPIO_InitStruct.Speed   = GPIO_SPEED_FREQ_MEDIUM;
HAL_GPIO_Init(Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIOx, &GPIO_InitStruct);

}

/// @brief 设置引角为高电平
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_pin_set(emDht11Tdf Dht11_Num)
{
HAL_GPIO_WritePin(Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIOx,Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIO_Pin,GPIO_PIN_SET);
}

/// @brief 设置引角为低电平
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_pin_reset(emDht11Tdf Dht11_Num)
{
HAL_GPIO_WritePin(Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIOx,Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIO_Pin,GPIO_PIN_RESET);
}

/// @brief 空闲状态
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_idle(emDht11Tdf Dht11_Num)
{
uint32_t TimeCount=210001000/Dht11Param[Dht11_Num].Dht11Cong.TimerPeriorUs;
stDht11WheelRuningTdf *Dht11WheelRuning = &Dht11Param[Dht11_Num].Dht11Wheelruning;
Dht11WheelRuning->wheelCount++;

if(Dht11WheelRuning->wheelCount >= TimeCount)
{
	Dht11WheelRuning->wheelCount = 0;
	Dht11WheelRuning->Dht11ReadState = Dht11_StartLow;
	dht11_pin_setOutPut(Dht11_Num);
	dht11_pin_reset(Dht11_Num);
}

}

/// @brief [开始低状态]执行
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_startlow(emDht11Tdf Dht11_Num)
{
uint32_t TimeCount=20*1000/Dht11Param[Dht11_Num].Dht11Cong.TimerPeriorUs;
stDht11WheelRuningTdf *Dht11WheelRuning = &Dht11Param[Dht11_Num].Dht11Wheelruning;
Dht11WheelRuning->wheelCount++;

if(Dht11WheelRuning->wheelCount >= TimeCount)
{
	Dht11WheelRuning->wheelCount = 0;
	Dht11WheelRuning->Dht11ReadState = Dht11_StartHigh;
	dht11_pin_setOutPut(Dht11_Num);
	dht11_pin_set(Dht11_Num);
}

}

/// @brief [开始高状态]执行
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_starthigh(emDht11Tdf Dht11_Num)
{
uint32_t TimeCount=15/Dht11Param[Dht11_Num].Dht11Cong.TimerPeriorUs;
stDht11WheelRuningTdf *Dht11WheelRuning = &Dht11Param[Dht11_Num].Dht11Wheelruning;
Dht11WheelRuning->wheelCount++;

if(Dht11WheelRuning->wheelCount >= TimeCount)
{
	Dht11WheelRuning->wheelCount = 0;
	Dht11WheelRuning->Dht11ReadState = Dht11_WaitAckLow;
	dht11_pin_setInPut(Dht11_Num);
}

}

/// @brief [等待应答低状态]执行
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_waitacklow(emDht11Tdf Dht11_Num)
{
uint32_t TimeCount=100/Dht11Param[Dht11_Num].Dht11Cong.TimerPeriorUs;
stDht11WheelRuningTdf *Dht11WheelRuning = &Dht11Param[Dht11_Num].Dht11Wheelruning;
Dht11WheelRuning->wheelCount++;

if(Dht11WheelRuning->wheelCount >= TimeCount)
{
	Dht11WheelRuning->wheelCount = 0;
	Dht11WheelRuning->Dht11ReadState = Dht11_Tdle;
	dht11_pin_setOutPut(Dht11_Num);
	dht11_pin_set(Dht11_Num);
}

if(HAL_GPIO_ReadPin(Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIOx,Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIO_Pin) == GPIO_PIN_RESET)
{
	Dht11WheelRuning->wheelCount = 0;
	Dht11WheelRuning->Dht11ReadState = Dht11_WaitAckHigh;
}

}

/// @brief [等待应答高状态]执行
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_waitackhigh(emDht11Tdf Dht11_Num)
{
uint32_t TimeCount=100/Dht11Param[Dht11_Num].Dht11Cong.TimerPeriorUs;
stDht11WheelRuningTdf *Dht11WheelRuning = &Dht11Param[Dht11_Num].Dht11Wheelruning;
Dht11WheelRuning->wheelCount++;

if(Dht11WheelRuning->wheelCount >= TimeCount)
{
	Dht11WheelRuning->wheelCount = 0;
	Dht11WheelRuning->Dht11ReadState = Dht11_Tdle;
	dht11_pin_setOutPut(Dht11_Num);
	dht11_pin_set(Dht11_Num);
}

if(HAL_GPIO_ReadPin(Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIOx,Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIO_Pin) == GPIO_PIN_SET)
{
	Dht11WheelRuning->wheelCount = 0;
	Dht11WheelRuning->Dht11ReadState = Dht11_WaitReadStart;
}

}

/// @brief [等待可开始读取]执行
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_waitreadstart(emDht11Tdf Dht11_Num)
{
uint32_t TimeCount=100/Dht11Param[Dht11_Num].Dht11Cong.TimerPeriorUs;
stDht11WheelRuningTdf *Dht11WheelRuning = &Dht11Param[Dht11_Num].Dht11Wheelruning;
Dht11WheelRuning->wheelCount++;

if(Dht11WheelRuning->wheelCount >= TimeCount)
{
	Dht11WheelRuning->wheelCount = 0;
	Dht11WheelRuning->Dht11ReadState = Dht11_Tdle;
	dht11_pin_setOutPut(Dht11_Num);
	dht11_pin_set(Dht11_Num);
}

if(HAL_GPIO_ReadPin(Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIOx,Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIO_Pin) == GPIO_PIN_RESET)
{
	Dht11WheelRuning->wheelCount = 0;
Dht11WheelRuning->LowCount = 0;
Dht11WheelRuning->HighCount = 0;
Dht11WheelRuning->BitCount = 0;
	Dht11WheelRuning->wheelCount = 0;
	Dht11WheelRuning->Dht11ReadState = Dht11_Reading;
}

}

/// @brief [读取]执行
///
/// @param Dht11_Num :设备号
///
/// @note

void dht11_reading(emDht11Tdf Dht11_Num)
{
uint32_t TimeCount=100/Dht11Param[Dht11_Num].Dht11Cong.TimerPeriorUs;
stDht11WheelRuningTdf *Dht11WheelRuning = &Dht11Param[Dht11_Num].Dht11Wheelruning;
Dht11WheelRuning->wheelCount++;

	if(Dht11WheelRuning->LowCount == 0)
{
		if((HAL_GPIO_ReadPin(Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIOx,Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIO_Pin) == GPIO_PIN_SET))
		{
			Dht11WheelRuning->LowCount = Dht11WheelRuning->wheelCount;
			Dht11WheelRuning->wheelCount = 0;
		}
			if(Dht11WheelRuning->wheelCount >= TimeCount)
	{
		Dht11WheelRuning->wheelCount = 0;
		Dht11WheelRuning->HighCount = 0;
		Dht11WheelRuning->LowCount = 0;
		Dht11WheelRuning->BitCount = 0;
		Dht11WheelRuning->wheelCount = 0;
		Dht11WheelRuning->Dht11ReadState = Dht11_Tdle;
		dht11_pin_setOutPut(Dht11_Num);
		dht11_pin_set(Dht11_Num);
	}
		
}
else
{
		if((HAL_GPIO_ReadPin(Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIOx,Dht11Param[Dht11_Num].Dht11Cong.Dht11GPIO_Pin) == GPIO_PIN_RESET))
		{
			Dht11WheelRuning->HighCount = Dht11WheelRuning->wheelCount;
			
			if(Dht11WheelRuning->HighCount > Dht11WheelRuning->LowCount)
			{
				Dht11WheelRuning->TempCount[(Dht11WheelRuning->BitCount) >> 3] |= 0x01 << (7-(Dht11WheelRuning->BitCount)%8);
			}
			else
			{
				Dht11WheelRuning->TempCount[(Dht11WheelRuning->BitCount) >> 3] &= ~(0x01 << (7-(Dht11WheelRuning->BitCount)%8));
			}
			
		Dht11WheelRuning->wheelCount = 0;
		Dht11WheelRuning->HighCount = 0;
		Dht11WheelRuning->LowCount = 0;
		Dht11WheelRuning->BitCount++;
		}
			if(Dht11WheelRuning->wheelCount >= TimeCount)
	{
		Dht11WheelRuning->wheelCount = 0;
		Dht11WheelRuning->HighCount = 0;
		Dht11WheelRuning->LowCount = 0;
		Dht11WheelRuning->BitCount = 0;
		Dht11WheelRuning->Dht11ReadState = Dht11_Tdle;
		dht11_pin_setOutPut(Dht11_Num);
		dht11_pin_set(Dht11_Num);
	}	
}

if(Dht11WheelRuning->BitCount >= 40)
{
	Dht11WheelRuning->BitCount = 0;
	
	if(Dht11WheelRuning->TempCount[4] == Dht11WheelRuning->TempCount[0]+
		                                   Dht11WheelRuning->TempCount[1]+
																			 Dht11WheelRuning->TempCount[2]+
	                                     Dht11WheelRuning->TempCount[3])
	{
		
		Dht11Param[Dht11_Num].Dht11Runing.Humidity[0] = Dht11WheelRuning->TempCount[0];
		Dht11Param[Dht11_Num].Dht11Runing.Humidity[1] = Dht11WheelRuning->TempCount[1];
		Dht11Param[Dht11_Num].Dht11Runing.Temperature[0] = Dht11WheelRuning->TempCount[2];
		Dht11Param[Dht11_Num].Dht11Runing.Temperature[1] = Dht11WheelRuning->TempCount[3];

	}
		Dht11WheelRuning->Dht11ReadState = Dht11_Tdle;
		dht11_pin_setOutPut(Dht11_Num);
	  dht11_pin_set(Dht11_Num);
				
}

}

/// @brief 状态机回调函数
///
/// @param Dht11_Num :设备号
///
/// @note

void Dht11_Callback(emDht11Tdf Dht11_Num)
{
stDht11WheelRuningTdf *Dht11WheelRuning = &Dht11Param[Dht11_Num].Dht11Wheelruning;

switch(Dht11WheelRuning->Dht11ReadState)
{
	case Dht11_Tdle:
	{
		dht11_idle(Dht11_Num);break;
	}
	case Dht11_StartLow:
	{
		dht11_startlow(Dht11_Num);break;
	}
	case Dht11_StartHigh:
	{
		dht11_starthigh(Dht11_Num);break;
	}
	case Dht11_WaitAckLow:
	{
		dht11_waitacklow(Dht11_Num);break;
	}
	case Dht11_WaitAckHigh:
	{
		dht11_waitackhigh(Dht11_Num);break;
	}
	case Dht11_WaitReadStart:
	{
		dht11_waitreadstart(Dht11_Num);break;
	}
	case Dht11_Reading:
	{
		dht11_reading(Dht11_Num);break;
	}
	default:
	{
		break;
	}
}

}

beep.c


#include “headerfile.h”

/// @brief Beep设置状态
///
/// @param Beep_State :UI界面号
///
/// @note
void Beep_Set(stBEEPStateTdf Beep_State)
{
if(Beep_State == Beep_On)
{
BEEP_RESET;
}
else
{
BEEP_SET;
}
}
void Beep_Init(void)
{
Beep_Set(Beep_Off);
}

beep.h


#ifndef _beep_h
#define _beep_h

#include “headerfile.h”

/// @brief BEEP状态
///
/// @note

typedef enum
{
Beep_Off = 0,
Beep_On,
}
stBEEPStateTdf;

#define BEEP_GPIO GPIOA
#define BEEP_GPIO_PIN GPIO_PIN_0

#define BEEP_SET HAL_GPIO_WritePin(BEEP_GPIO,BEEP_GPIO_PIN,GPIO_PIN_SET)
#define BEEP_RESET HAL_GPIO_WritePin(BEEP_GPIO,BEEP_GPIO_PIN,GPIO_PIN_RESET)

void Beep_Set(stBEEPStateTdf Beep_State);
void Beep_Init(void);

#endif

myrtc.c


#include “headerfile.h”

void bkup_allData(RTC_TimeTypeDef sTime, RTC_DateTypeDef sDate)
{
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, sTime.Hours);
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR2, sTime.Minutes);
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR3, sTime.Seconds);
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR4, sDate.WeekDay);
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR5, sDate.Month);
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR6, sDate.Date);
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR7, sDate.Year);
}

void MyRTC_Init(void)
{
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef DateToUpdate = {0};
RTC_AlarmTypeDef sAlarm = {0};

sTime.Hours   = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1);
sTime.Minutes = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR2);
sTime.Seconds = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR3);
DateToUpdate.WeekDay = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR4);
DateToUpdate.Month   = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR5);
DateToUpdate.Date    = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR6);
DateToUpdate.Year    = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR7);

// 如果有无效数据(如全部为0),可设置默认值,但您原来的代码未做判断,保持原样
// 若您之前有判断,请保留

if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
    Error_Handler();

if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BIN) != HAL_OK)
    Error_Handler();

// 设置闹钟(固定为 00:00:00,用于唤醒或触发,但不影响备份)
sAlarm.AlarmTime.Hours   = 0;
sAlarm.AlarmTime.Minutes = 0;
sAlarm.AlarmTime.Seconds = 0;
sAlarm.Alarm = RTC_ALARM_A;
if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK)
    Error_Handler();

}

myrtc.h


#ifndef _myrtc_h
#define _myrtc_h

#include “headerfile.h”

void MyRTC_Init(void);
void bkup_allData(RTC_TimeTypeDef sTime, RTC_DateTypeDef sDate);

#endif

OLED.c


#include “headerfile.h”
#include “OLED_Font.h”

///引脚配置/
void OLED_W_SCL(uint8_t x)
{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,(GPIO_PinState)x);
}

void OLED_W_SDA(uint8_t x)
{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,(GPIO_PinState)x);
}
/引脚初始化/
void OLED_I2C_Init(void)
{

OLED_W_SCL(1);
OLED_W_SDA(1);

}

///**
// * @brief I2C开始
// * @param 无
// * @retval 无
// */
//void OLED_I2C_Start(void)
//{
// OLED_W_SDA(1);
// OLED_W_SCL(1);
// OLED_W_SDA(0);
// OLED_W_SCL(0);
//}

///**
// * @brief I2C停止
// * @param 无
// * @retval 无
// */
//void OLED_I2C_Stop(void)
//{
// OLED_W_SDA(0);
// OLED_W_SCL(1);
// OLED_W_SDA(1);
//}

/**

  • @brief I2C发送一个字节
  • @param Byte 要发送的一个字节
  • @retval 无
    */
    //void OLED_I2C_SendByte(uint8_t Byte)
    //{
    // uint8_t i;
    // for (i = 0; i < 8; i++)
    // {
    // OLED_W_SDA(!!(Byte & (0x80 >> i)));
    // OLED_W_SCL(1);
    // OLED_W_SCL(0);
    // }
    // OLED_W_SCL(1); //额外的一个时钟,不处理应答信号
    // OLED_W_SCL(0);
    //}

//void OLED_I2C_SendByte(uint8_t Byte)
//{
// HAL_I2C_Master_Transmit(&hi2c1,0x78, &Byte, 1, HAL_MAX_DELAY);
//}

/**

  • @brief OLED写命令
  • @param Command 要写入的命令
  • @retval 无
    */
    void OLED_WriteCommand(uint8_t Command)
    {
    // OLED_I2C_Start();
    // OLED_I2C_SendByte(0x78); //从机地址
    // OLED_I2C_SendByte(0x00); //写命令
    // OLED_I2C_SendByte(Command);
    // OLED_I2C_Stop();
    uint8_t Byte[]={0x00,0};
    Byte[1]=Command;
    HAL_I2C_Master_Transmit(&hi2c1,0x78, Byte, 2, HAL_MAX_DELAY);
    }

/**

  • @brief OLED写数据
  • @param Data 要写入的数据
  • @retval 无
    */
    void OLED_WriteData(uint8_t Data)
    {
    // OLED_I2C_Start();
    // OLED_I2C_SendByte(0x78); //从机地址
    // OLED_I2C_SendByte(0x40); //写数据
    // OLED_I2C_SendByte(Data);
    // OLED_I2C_Stop();
    uint8_t Byte[]={0x40,0};
    Byte[1]=Data;
    HAL_I2C_Master_Transmit(&hi2c1,0x78, Byte, 2, HAL_MAX_DELAY);
    }

/**

  • @brief OLED设置光标位置
  • @param Y 以左上角为原点,向下方向的坐标,范围:0~7
  • @param X 以左上角为原点,向右方向的坐标,范围:0~127
  • @retval 无
    */
    void OLED_SetCursor(uint8_t Y, uint8_t X)
    {
    OLED_WriteCommand(0xB0 | Y); //设置Y位置
    OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //设置X位置高4位
    OLED_WriteCommand(0x00 | (X & 0x0F)); //设置X位置低4位
    }

/**

  • @brief OLED清屏
  • @param 无
  • @retval 无
    */
    void OLED_Clear(void)
    {
    uint8_t i, j;
    for (j = 0; j < 8; j++)
    {
    OLED_SetCursor(j, 0);
    for(i = 0; i < 128; i++)
    {
    OLED_WriteData(0x00);
    }
    }
    }

/**

  • @brief OLED显示一个字符
  • @param Line 行位置,范围:1~4
  • @param Column 列位置,范围:1~16
  • @param Char 要显示的一个字符,范围:ASCII可见字符
  • @retval 无
    */
    void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char)
    {
    uint8_t i;
    OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8); //设置光标位置在上半部分
    for (i = 0; i < 8; i++)
    {
    OLED_WriteData(OLED_F8x16[Char - ’ '][i]); //显示上半部分内容
    }
    OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8); //设置光标位置在下半部分
    for (i = 0; i < 8; i++)
    {
    OLED_WriteData(OLED_F8x16[Char - ’ '][i + 8]); //显示下半部分内容
    }
    }

/**

  • @brief OLED显示字符串
  • @param Line 起始行位置,范围:1~4
  • @param Column 起始列位置,范围:1~16
  • @param String 要显示的字符串,范围:ASCII可见字符
  • @retval 无
    */
    void OLED_ShowString(uint8_t Line, uint8_t Column, char *String)
    {
    uint8_t i;
    for (i = 0; String[i] != ‘\0’; i++)
    {
    OLED_ShowChar(Line, Column + i, String[i]);
    }
    }

/**

  • @brief OLED次方函数
  • @retval 返回值等于X的Y次方
    */
    uint32_t OLED_Pow(uint32_t X, uint32_t Y)
    {
    uint32_t Result = 1;
    while (Y–)
    {
    Result *= X;
    }
    return Result;
    }

/**

  • @brief OLED显示数字(十进制,正数)
  • @param Line 起始行位置,范围:1~4
  • @param Column 起始列位置,范围:1~16
  • @param Number 要显示的数字,范围:0~4294967295
  • @param Length 要显示数字的长度,范围:1~10
  • @retval 无
    */
    void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
    {
    uint8_t i;
    for (i = 0; i < Length; i++)
    {
    OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + ‘0’);
    }
    }

/**

  • @brief OLED显示数字(十进制,带符号数)
  • @param Line 起始行位置,范围:1~4
  • @param Column 起始列位置,范围:1~16
  • @param Number 要显示的数字,范围:-2147483648~2147483647
  • @param Length 要显示数字的长度,范围:1~10
  • @retval 无
    */
    void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length)
    {
    uint8_t i;
    uint32_t Number1;
    if (Number >= 0)
    {
    OLED_ShowChar(Line, Column, ‘+’);
    Number1 = Number;
    }
    else
    {
    OLED_ShowChar(Line, Column, ‘-’);
    Number1 = -Number;
    }
    for (i = 0; i < Length; i++)
    {
    OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + ‘0’);
    }
    }

/**

  • @brief OLED显示数字(十六进制,正数)
  • @param Line 起始行位置,范围:1~4
  • @param Column 起始列位置,范围:1~16
  • @param Number 要显示的数字,范围:0~0xFFFFFFFF
  • @param Length 要显示数字的长度,范围:1~8
  • @retval 无
    */
    void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
    {
    uint8_t i, SingleNumber;
    for (i = 0; i < Length; i++)
    {
    SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
    if (SingleNumber < 10)
    {
    OLED_ShowChar(Line, Column + i, SingleNumber + ‘0’);
    }
    else
    {
    OLED_ShowChar(Line, Column + i, SingleNumber - 10 + ‘A’);
    }
    }
    }

/**

  • @brief OLED显示数字(二进制,正数)
  • @param Line 起始行位置,范围:1~4
  • @param Column 起始列位置,范围:1~16
  • @param Number 要显示的数字,范围:0~1111 1111 1111 1111
  • @param Length 要显示数字的长度,范围:1~16
  • @retval 无
    */
    void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
    {
    uint8_t i;
    for (i = 0; i < Length; i++)
    {
    OLED_ShowChar(Line, Column + i, Number / OLED_Pow(2, Length - i - 1) % 2 + ‘0’);
    }
    }

/**

  • @brief OLED初始化

  • @param 无

  • @retval 无
    */
    void OLED_Init(void)
    {
    uint32_t i, j;

    for (i = 0; i < 1000; i++) //上电延时
    {
    for (j = 0; j < 1000; j++);
    }

    OLED_I2C_Init(); //端口初始化

    OLED_WriteCommand(0xAE); //关闭显示

    OLED_WriteCommand(0xD5); //设置显示时钟分频比/振荡器频率
    OLED_WriteCommand(0x80);

    OLED_WriteCommand(0xA8); //设置多路复用率
    OLED_WriteCommand(0x3F);

    OLED_WriteCommand(0xD3); //设置显示偏移
    OLED_WriteCommand(0x00);

    OLED_WriteCommand(0x40); //设置显示开始行

    OLED_WriteCommand(0xA1); //设置左右方向,0xA1正常 0xA0左右反置

    OLED_WriteCommand(0xC8); //设置上下方向,0xC8正常 0xC0上下反置

    OLED_WriteCommand(0xDA); //设置COM引脚硬件配置
    OLED_WriteCommand(0x12);

    OLED_WriteCommand(0x81); //设置对比度控制
    OLED_WriteCommand(0xCF);

    OLED_WriteCommand(0xD9); //设置预充电周期
    OLED_WriteCommand(0xF1);

    OLED_WriteCommand(0xDB); //设置VCOMH取消选择级别
    OLED_WriteCommand(0x30);

    OLED_WriteCommand(0xA4); //设置整个显示打开/关闭

    OLED_WriteCommand(0xA6); //设置正常/倒转显示

    OLED_WriteCommand(0x8D); //设置充电泵
    OLED_WriteCommand(0x14);

    OLED_WriteCommand(0xAF); //开启显示

    OLED_Clear(); //OLED清屏
    }

OLED.h


#ifndef __OLED_H
#define __OLED_H
#include “headerfile.h”

void OLED_Init(void);
void OLED_Clear(void);
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);

#endif

OLED_Font.h


#ifndef __OLED_FONT_H
#define __OLED_FONT_H
#include “headerfile.h”
/OLED字模库,宽8像素,高16像素/
const uint8_t OLED_F8x16[][16]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0

0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1

0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2

0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,
0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3

0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,
0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4

0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,
0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5

0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,
0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6

0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7

0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,
0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8

0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,
0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9

0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,
0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10

0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14

0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,
0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15

0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16

0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17

0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,
0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18

0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,
0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19

0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,
0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20

0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,
0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21

0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,
0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22

0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,
0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23

0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,
0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24

0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25

0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,
0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26

0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27

0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,
0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28

0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,
0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29

0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,
0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30

0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,
0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31

0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,
0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32

0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,
0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33

0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,
0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34

0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,
0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35

0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,
0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36

0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37

0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38

0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,
0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39

0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40

0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41

0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,
0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42

0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,
0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43

0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,
0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44

0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,
0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45

0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,
0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46

0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47

0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,
0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48

0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49

0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,
0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50

0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,
0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51

0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,
0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52

0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53

0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,
0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54

0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,
0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55

0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,
0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56

0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,
0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57

0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,
0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58

0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,
0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59

0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60

0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,
0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61

0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63

0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64

0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65

0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,
0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66

0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,
0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67

0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,
0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68

0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69

0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70

0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71

0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,
0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72

0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73

0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,
0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74

0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,
0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75

0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76

0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77

0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,
0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78

0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79

0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,
0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80

0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,
0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81

0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82

0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83

0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,
0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84

0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,
0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85

0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86

0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,
0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87

0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88

0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89

0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90

0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,
0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91

0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92

0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,
0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93

0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94

};

#endif

UI.c


#include “headerfile.h”
#include “myrtc.h”

emUiScreenTdf g_CurrentScreen = UI_MAIN;
uint8_t g_SelectedField = 0;
stTimeSettingTdf g_TimeSet;
stAlarmSettingTdf g_AlarmSet;
stThresholdTdf g_Threshold;

static void ApplyTimeToRTC(void)
{
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
sTime.Hours = g_TimeSet.Hour;
sTime.Minutes = g_TimeSet.Minute;
sTime.Seconds = g_TimeSet.Second;
sDate.Year = g_TimeSet.Year;
sDate.Month = g_TimeSet.Month;
sDate.Date = g_TimeSet.Day;
sDate.WeekDay = 1; // 忽略
HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN);

}

// ---------- 绘制界面(与之前相同) ----------
static void DrawMainScreen(void)
{
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);

bkup_allData(sTime,sDate);

const stDht11ParamTdf *pDht = Get_Dht11Param(Dht11_1);

char line[16];
sprintf(line, "Date:%04d-%02d-%02d", 2000 + sDate.Year, sDate.Month, sDate.Date);
OLED_ShowString(1, 1, line);
sprintf(line, "Time: %02d:%02d:%02d", sTime.Hours, sTime.Minutes, sTime.Seconds);
OLED_ShowString(2, 1, line);
sprintf(line, "Temp: %2d C", pDht->Dht11Runing.Temperature[0]);
OLED_ShowString(3, 1, line);
sprintf(line, "Hum : %2d %%", pDht->Dht11Runing.Humidity[0]);
OLED_ShowString(4, 1, line);

}

static void DrawTimeSetScreen(void)
{
char line[16];
OLED_ShowString(1, 1, “Set Time”);
sprintf(line, “Year: 20%02d”, g_TimeSet.Year);
OLED_ShowString(2, 1, line);
sprintf(line, “Mon :%02d Day:%02d”, g_TimeSet.Month, g_TimeSet.Day);
OLED_ShowString(3, 1, line);
sprintf(line, “Hour:%02d Min:%02d”, g_TimeSet.Hour, g_TimeSet.Minute);
OLED_ShowString(4, 1, line);
}

static void DrawAlarmSetScreen(void)
{
char line[16];
OLED_ShowString(1, 1, “Set Alarm”);
sprintf(line, “Hour: %02d”, g_AlarmSet.Hour);
OLED_ShowString(2, 1, line);
sprintf(line, “Min : %02d”, g_AlarmSet.Minute);
OLED_ShowString(3, 1, line);
sprintf(line, “Enable: %s”, g_AlarmSet.Enable ? "ON " : “OFF”);
OLED_ShowString(4, 1, line);
}

static void DrawThresholdSetScreen(void)
{
char line[16];
OLED_ShowString(1, 1, “Threshold”);
sprintf(line, “TempMax: %2d C”, g_Threshold.TempMax);
OLED_ShowString(2, 1, line);
sprintf(line, “HumMax : %2d %%”, g_Threshold.HumMax);
OLED_ShowString(3, 1, line);
}

// ---------- 公共接口 ----------
void UI_UpdateScreen(void)
{
switch (g_CurrentScreen)
{
case UI_MAIN: DrawMainScreen(); break;
case UI_TIME_SET: DrawTimeSetScreen(); break;
case UI_ALARM_SET: DrawAlarmSetScreen(); break;
case UI_THRESHOLD_SET: DrawThresholdSetScreen(); break;
default: break;
}
}

void UI_ScreenNext(void)
{
if (g_CurrentScreen == UI_TIME_SET)
ApplyTimeToRTC(); // 只有时间被保存

// 闹钟和阈值不保存到BKP,仅内存有效

OLED_Clear();
g_CurrentScreen = (emUiScreenTdf)((g_CurrentScreen + 1) % UI_MAX);
g_SelectedField = 0;

// 如果切换到时间设置界面,从RTC加载最新时间
if (g_CurrentScreen == UI_TIME_SET) {
    RTC_TimeTypeDef sTime;
    RTC_DateTypeDef sDate;
    HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
    HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
    g_TimeSet.Year  = sDate.Year;
    g_TimeSet.Month = sDate.Month;
    g_TimeSet.Day   = sDate.Date;
    g_TimeSet.Hour  = sTime.Hours;
    g_TimeSet.Minute= sTime.Minutes;
    g_TimeSet.Second= sTime.Seconds;
}

UI_UpdateScreen();

}

void UI_FieldNext(void)
{
if (g_CurrentScreen == UI_TIME_SET)
g_SelectedField = (g_SelectedField + 1) % 6;
else if (g_CurrentScreen == UI_ALARM_SET)
g_SelectedField = (g_SelectedField + 1) % 3;
else if (g_CurrentScreen == UI_THRESHOLD_SET)
g_SelectedField = (g_SelectedField + 1) % 2;
UI_UpdateScreen();
}

void UI_ValueInc(void)
{
if (g_CurrentScreen == UI_TIME_SET) {
switch (g_SelectedField) {
case 0: g_TimeSet.Year = (g_TimeSet.Year + 1) % 100; break;
case 1: g_TimeSet.Month = (g_TimeSet.Month % 12) + 1; break;
case 2: g_TimeSet.Day = (g_TimeSet.Day % 31) + 1; break;
case 3: g_TimeSet.Hour = (g_TimeSet.Hour + 1) % 24; break;
case 4: g_TimeSet.Minute = (g_TimeSet.Minute + 1) % 60; break;
case 5: g_TimeSet.Second = (g_TimeSet.Second + 1) % 60; break;
}
} else if (g_CurrentScreen == UI_ALARM_SET) {
switch (g_SelectedField) {
case 0: g_AlarmSet.Hour = (g_AlarmSet.Hour + 1) % 24; break;
case 1: g_AlarmSet.Minute = (g_AlarmSet.Minute + 1) % 60; break;
case 2: g_AlarmSet.Enable = !g_AlarmSet.Enable; break;
}
} else if (g_CurrentScreen == UI_THRESHOLD_SET) {
if (g_SelectedField == 0) g_Threshold.TempMax++;
else g_Threshold.HumMax++;
}
UI_UpdateScreen();
}

void UI_ValueDec(void)
{
if (g_CurrentScreen == UI_TIME_SET) {
switch (g_SelectedField) {
case 0: g_TimeSet.Year = (g_TimeSet.Year + 99) % 100; break;
case 1: g_TimeSet.Month = (g_TimeSet.Month + 10) % 12 + 1; break;
case 2: g_TimeSet.Day = (g_TimeSet.Day + 29) % 31 + 1; break;
case 3: g_TimeSet.Hour = (g_TimeSet.Hour + 23) % 24; break;
case 4: g_TimeSet.Minute = (g_TimeSet.Minute + 59) % 60; break;
case 5: g_TimeSet.Second = (g_TimeSet.Second + 59) % 60; break;
}
} else if (g_CurrentScreen == UI_ALARM_SET) {
switch (g_SelectedField) {
case 0: g_AlarmSet.Hour = (g_AlarmSet.Hour + 23) % 24; break;
case 1: g_AlarmSet.Minute = (g_AlarmSet.Minute + 59) % 60; break;
case 2: g_AlarmSet.Enable = !g_AlarmSet.Enable; break;
}
} else if (g_CurrentScreen == UI_THRESHOLD_SET) {
if (g_SelectedField == 0) g_Threshold.TempMax–;
else g_Threshold.HumMax–;
}
UI_UpdateScreen();
}

void UI_Init(void)
{
// 设置默认值(闹钟和阈值不备份,上电恢复为默认)
g_TimeSet.Year = 0;
g_TimeSet.Month = 0;
g_TimeSet.Day = 0;
g_TimeSet.Hour = 0;
g_TimeSet.Minute= 0;
g_TimeSet.Second= 0;

g_AlarmSet.Hour   = 0;
g_AlarmSet.Minute = 0;
g_AlarmSet.Enable = 0;
g_Threshold.TempMax = 40;
g_Threshold.HumMax  = 80;

OLED_Clear();
g_CurrentScreen = UI_MAIN;
g_SelectedField = 0;
UI_UpdateScreen();

}

UI.h


#ifndef UI_H
#define UI_H

#include “headerfile.h”

typedef enum
{
UI_MAIN = 0,
UI_SET,
UI_TIME_SET,
UI_ALARM_SET,
UI_THRESHOLD_SET,
UI_MAX
} emUiScreenTdf;

typedef struct
{
uint8_t Year; // 后两位
uint8_t Month;
uint8_t Day;
uint8_t Hour;
uint8_t Minute;
uint8_t Second;
} stTimeSettingTdf;

typedef struct
{
uint8_t Hour;
uint8_t Minute;
uint8_t Enable; // 1启用,0禁用
} stAlarmSettingTdf;

typedef struct
{
int8_t TempMax;
uint8_t HumMax;
} stThresholdTdf;

// 全局变量(外部可访问)
extern emUiScreenTdf g_CurrentScreen;
extern uint8_t g_SelectedField;
extern stTimeSettingTdf g_TimeSet;
extern stAlarmSettingTdf g_AlarmSet;
extern stThresholdTdf g_Threshold;

// 函数声明
void UI_Init(void);
void UI_UpdateScreen(void);
void UI_ScreenNext(void);
void UI_FieldNext(void);
void UI_ValueInc(void);
void UI_ValueDec(void);

// 您需要实现的加载/保存函数(在 user_settings.c 中)
void LoadSettings(void);
void SaveSettings(void);

#endif

fun.c


#include “headerfile.h”
//void test()
//{
// HAL_GPIO_WritePin();
//}

extern osEventFlagsId_t xEventGroupCreateHandle;
extern osMessageQueueId_t xUartRxQueueHandle;

void Led_Init(void)
{
stLedParamTdf led_init_param;

led_init_param.LedParam.LedGPIOx = GPIOA;
led_init_param.LedParam.LedGPIO_Pin = GPIO_PIN_0;
led_init_param.LedParam.LedOnState = led_on_high;
led_init(&led_init_param,led1);

led_init_param.LedParam.LedGPIOx = GPIOA;
led_init_param.LedParam.LedGPIO_Pin = GPIO_PIN_1;
led_init_param.LedParam.LedOnState = led_on_high;
led_init(&led_init_param,led2);

led_init_param.LedParam.LedGPIOx = GPIOA;
led_init_param.LedParam.LedGPIO_Pin = GPIO_PIN_2;
led_init_param.LedParam.LedOnState = led_on_high;
led_init(&led_init_param,led3);

}

void Led_Excute(void)
{
led_on(led1);
led_on(led3);
HAL_Delay(400);
led_off(led1);
led_off(led3);
HAL_Delay(400);
}
void Dht11_Init(void)
{
stDht11ParamTdf Dht11_ParamInit;
Dht11_ParamInit.Dht11Cong.Dht11GPIOx = GPIOB;
Dht11_ParamInit.Dht11Cong.Dht11GPIO_Pin = GPIO_PIN_13;
Dht11_ParamInit.Dht11Cong.TimerPeriorUs = 5;
dht11_init(&Dht11_ParamInit,Dht11_1);
}
void Oled_Excute(void)
{
// OLED_Clear();
// const stDht11ParamTdf *Dht11_Data = Get_Dht11Param(Dht11_1);
//
// OLED_ShowNum(1,1,Dht11_Data->Dht11Runing.Humidity[0],8);
// OLED_ShowNum(3,1,Dht11_Data->Dht11Runing.Temperature[0],8);
// OLED_ShowNum(2,1,(Dht11_Data->Dht11Runing.Humidity[0]-32)/1.8,8);
OLED_ShowNum(1,1,Get_KeyNum(Key1),1);
OLED_ShowNum(1,3,Get_KeyNum(Key2),1);
OLED_ShowNum(1,5,Get_KeyNum(Key3),1);
OLED_ShowNum(1,7,Get_KeyNum(Key4),1);

}

#define RX_BUF_LEN 60

char rx_buf[RX_BUF_LEN];

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
if (huart->Instance == USART1)
{
if (xUartRxQueueHandle != NULL)
{
char *msg = pvPortMalloc(Size + 1);
if (msg)
{
memcpy(msg, rx_buf, Size);
msg[Size] = ‘\0’;
xQueueSendFromISR(xUartRxQueueHandle, &msg, NULL);
}
}

    HAL_UARTEx_ReceiveToIdle_IT(&huart1, (uint8_t *)rx_buf, RX_BUF_LEN);
}

}

void Key_Init(void)
{
stKeyTdf key_init_param;

	key_init_param.KeyGPIOx = GPIOB;
key_init_param.KeyGPIO_Pin = GPIO_PIN_11;
key_init(&key_init_param ,Key1);

		key_init_param.KeyGPIOx = GPIOB;
key_init_param.KeyGPIO_Pin = GPIO_PIN_1;
key_init(&key_init_param ,Key2);

	key_init_param.KeyGPIOx = GPIOA;
key_init_param.KeyGPIO_Pin = GPIO_PIN_7;
key_init(&key_init_param ,Key3);

	key_init_param.KeyGPIOx = GPIOA;
key_init_param.KeyGPIO_Pin = GPIO_PIN_5;
key_init(&key_init_param ,Key4);

}

void Text11(void)
{

// EventBits_t bits = xEventGroupGetBits(xEventGroupCreateHandle);

OLED_ShowNum(1,1,g_CurrentScreen,6);

}

fun.h


#ifndef __FUN_H
#define __FUN_H
#include “headerfile.h”

void Led_Init(void);
void Led_Excute(void);
void Oled_Excute(void);
void Key_Init(void);
void UI_Init(void);
void Text11(void);
#endif

app_task.c


#include “headerfile.h”
#include “myrtc.h”

extern EventGroupHandle_t xEventGroupCreateHandle;
extern QueueHandle_t xUartRxQueueHandle;

#define EVENT_UPDATE_UI (1 << 0)
#define EVENT_ALARM_TRIGGER (1 << 1)
#define EVENT_SETTINGS_CHANGED (1 << 2)

void Task_UI_Beep(void)
{
TickType_t xLastWakeTime = xTaskGetTickCount();
while (1)
{
vTaskDelayUntil(&xLastWakeTime, 100);

    EventBits_t bits = xEventGroupGetBits(xEventGroupCreateHandle);
    if (bits & EVENT_UPDATE_UI)
    {
        UI_UpdateScreen();
        xEventGroupClearBits(xEventGroupCreateHandle, EVENT_UPDATE_UI);
    }
    if (bits & EVENT_SETTINGS_CHANGED)
    {
        xEventGroupClearBits(xEventGroupCreateHandle, EVENT_SETTINGS_CHANGED);
    }

    const stDht11ParamTdf *pDht = Get_Dht11Param(Dht11_1);
    RTC_TimeTypeDef sTime;
    HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);

    // 闹钟检测
    if (g_AlarmSet.Enable &&
        sTime.Hours == g_AlarmSet.Hour &&
        sTime.Minutes == g_AlarmSet.Minute &&
        sTime.Seconds == 0)
    {
        xEventGroupSetBits(xEventGroupCreateHandle, EVENT_ALARM_TRIGGER);
    }

    // 温湿度阈值检测
    if (pDht->Dht11Runing.Temperature[0] > g_Threshold.TempMax ||
        pDht->Dht11Runing.Humidity[0] > g_Threshold.HumMax)
    {
        xEventGroupSetBits(xEventGroupCreateHandle, EVENT_ALARM_TRIGGER);
    }

    if (bits & EVENT_ALARM_TRIGGER)
    {
        Beep_Set(Beep_On);
        vTaskDelay(pdMS_TO_TICKS(500));
        Beep_Set(Beep_Off);
        xEventGroupClearBits(xEventGroupCreateHandle, EVENT_ALARM_TRIGGER);
    }

    if (g_CurrentScreen == UI_MAIN)
    {
        UI_UpdateScreen();
    }
}

}

void Task_Key_Serial(void)
{
char *rxMsg;
BaseType_t res;

while (1)
{
    Key_Excute();

    if (Get_KeyNum(Key1)) UI_ScreenNext();
    if (Get_KeyNum(Key2)) UI_FieldNext();
    if (Get_KeyNum(Key3)) UI_ValueInc();
    if (Get_KeyNum(Key4)) UI_ValueDec();

    res = xQueueReceive(xUartRxQueueHandle, &rxMsg, 0);
    if (res == pdPASS)
    {
        if (strncmp(rxMsg, "SETTIME:", 8) == 0)
        {
            int y, m, d, hh, min, sec;
            sscanf(rxMsg + 8, "%d-%d-%d %d:%d:%d", &y, &m, &d, &hh, &min, &sec);
            RTC_TimeTypeDef sTime = {0};
            RTC_DateTypeDef sDate = {0};
            sDate.Year = y - 2000;
            sDate.Month = m;
            sDate.Date = d;
            sDate.WeekDay = 1;
            sTime.Hours = hh;
            sTime.Minutes = min;
            sTime.Seconds = sec;
            HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
            HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
            xEventGroupSetBits(xEventGroupCreateHandle, EVENT_UPDATE_UI);
        }
        else if (strncmp(rxMsg, "SETTH:", 6) == 0)
        {
            int temp, hum;
            sscanf(rxMsg + 6, "%d,%d", &temp, &hum);
            g_Threshold.TempMax = temp;
            g_Threshold.HumMax = hum;
            // 不备份阈值
            xEventGroupSetBits(xEventGroupCreateHandle, EVENT_UPDATE_UI);
        }
        vPortFree(rxMsg);
    }
    vTaskDelay(pdMS_TO_TICKS(20));
}

}

void Task_Bluetooth_Send(void)
{
TickType_t xLastWakeTime = xTaskGetTickCount();
const TickType_t xPeriod = pdMS_TO_TICKS(3000);

char sendBuf[80];
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
const stDht11ParamTdf *pDht;

while (1)
{
    vTaskDelayUntil(&xLastWakeTime, xPeriod);

    HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
    HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
    pDht = Get_Dht11Param(Dht11_1);

    sprintf(sendBuf, "T:%d.%d.%d.%d.%d.%d/r/nTE:%d/r/nHU:%d/r/n",
            2000 + sDate.Year, sDate.Month, sDate.Date,
            sTime.Hours, sTime.Minutes, sTime.Seconds,
            pDht->Dht11Runing.Temperature[0],
            pDht->Dht11Runing.Humidity[0]);

    HAL_UART_Transmit(&huart1, (uint8_t*)sendBuf, strlen(sendBuf), 100);
}

}

app_task.h


#ifndef APP_TASKS_H
#define APP_TASKS_H

#include “FreeRTOS.h”
#include “event_groups.h”
#include “queue.h”

//extern EventGroupHandle_t xEventGroupCreateHandle;
//extern QueueHandle_t xUartRxQueueHandle;

// 事件位定义
#define EVENT_UPDATE_UI (1 << 0)
#define EVENT_ALARM_TRIGGER (1 << 1)
#define EVENT_SETTINGS_CHANGED (1 << 2)

// 任务函数(带 FreeRTOS 标准参数)
void Task_UI_Beep(void);
void Task_Key_Serial(void);
void Task_Bluetooth_Send(void);

#endif

Logo

openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构

更多推荐