创建一个带日历的温湿度计
#include "rtc.h"
ErrorStatus rest_rtc(void)
{
ErrorStatus err_rtc;
RTC_InitTypeDef RTC_InitStruct = {0};
RCC_LSE_Enable(RCC_LSE_MODE_OSC, RCC_LSE_AMP_NORMAL, RCC_LSE_DRIVER_NORMAL); // 选择LSE为RTC时钟
RTC_InitStruct.DateStruct.Day = 0x27; //日
RTC_InitStruct.DateStruct.Month = RTC_Month_May;//月
RTC_InitStruct.DateStruct.Week = RTC_Weekday_Monday;//星期
RTC_InitStruct.DateStruct.Year = 0x23; //年
RTC_InitStruct.TimeStruct.Hour = 0x11; //时
RTC_InitStruct.TimeStruct.Minute = 0x58;//分
RTC_InitStruct.TimeStruct.Second = 0x59;//秒
RTC_InitStruct.TimeStruct.AMPM = 0;
RTC_InitStruct.TimeStruct.H24 = 0; //采用12小时设置
RTC_InitStruct.RTC_ClockSource = RTC_RTCCLK_FROM_LSE;
err_rtc = RTC_Init(&RTC_InitStruct); // RTC模块初始化, 用户需选定需要使用的时钟源
if(err_rtc != SUCCESS)
{
return ERROR;
}
return SUCCESS;
}
rtc.h:
#ifndef __RTC_H__
#define __RTC_H__
#include "main.h"
ErrorStatus rest_rtc(void);
#endif
修改主函数内容如下:
#include "main.h"
#include "Lcd_Driver.h"
#include "LCD_calculate.h"
#include "dht11.h"
#include "rtc.h"
unsigned int counttime=0;
float temperature;
uint8_t humidity;
char buff_1[15];
char buff_2[15];
char buff_rtc[30];
void GPIO_Configuration(void);
void RCC_Configuration(void);
void BTIM_init(void);
int main()
{
RTC_InitTypeDef RTC_InitStruct = {0};
RCC_Configuration(); //系统时钟64M
GPIO_Configuration(); //LED初始化
BTIM_init(); //定时器初始化
Lcd_Init();
Lcd_Clear(GRAY0); //清屏
Redraw_Mainmenu();
while(DHT11_GPIO_Config()) //DHT11初始化
{
}
rest_rtc();
while(1)
{
if(counttime>500) //200毫秒采集一次数据并更新屏幕
{
counttime=0;
DHT11_Read_Data(&temperature,&humidity); //读取温湿度
sprintf(buff_1,"%0.1f",temperature);
sprintf(buff_2,"%d",humidity);
Gui_DrawFont_GBK16(90,25,BLUE,GRAY0,buff_1); //更新显示
Gui_DrawFont_GBK16(90,47,BLUE,GRAY0,buff_2);
RTC_GetDate(&RTC_InitStruct.DateStruct);// 取用当前日期,BCD格式
sprintf(buff_rtc,"20%02x-%02x-%02x",RTC_InitStruct.DateStruct.Year, RTC_InitStruct.DateStruct.Month, RTC_InitStruct.DateStruct.Day );
Gui_DrawFont_GBK16(10,80,BLUE,GRAY0,buff_rtc);
RTC_GetTime(&RTC_InitStruct.TimeStruct);// 获取当前时间,BCD格式
sprintf(buff_rtc,"%02x:%02x:%02x",RTC_InitStruct.TimeStruct.Hour, RTC_InitStruct.TimeStruct.Minute, RTC_InitStruct.TimeStruct.Second );
Gui_DrawFont_GBK16(10,100,BLUE,GRAY0,buff_rtc);
}
}
}
编译下载到开发板,运行效果如下:
全部0条评论
快来发表一下你的评论吧 !