目的
创建一个带日历的温湿度计
步骤
- 新建rtc.c/rtc.h,并把两个文件添加到user下面:
- rtc.c 代码如下:
#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);
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;
RTC_InitStruct.RTC_ClockSource = RTC_RTCCLK_FROM_LSE;
err_rtc = RTC_Init(&RTC_InitStruct);
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();
GPIO_Configuration();
BTIM_init();
Lcd_Init();
Lcd_Clear(GRAY0);
Redraw_Mainmenu();
while(DHT11_GPIO_Config())
{
}
rest_rtc();
while(1)
{
if(counttime>500)
{
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);
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);
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);
}
}
}
编译下载到开发板,运行效果如下: