#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "dht11.h"
#include "led.h"
//#include "lcd.h"
#include "soft_iic_oled.h"
#include "soft_iic.h"
#include "rtc.h"
//#include "usmart.h"
#include "stdio.h" //printf函数头文件,将浮点数转换为字符串
//#include "sdio_sdcard.h"
//#include "text.h"
//#include "exfuns.h"
extern const uint8_t BMP1[];
uint8_t temperature;
uint8_t humidity;
char temp[6]={0}; //读出的温度暂存寄存器
char humi[6]={0}; //读出的湿度暂存寄存器
void dat_dis() //温湿度数据处理转为字符串
{
//humi[0]='R';
//humi[1]=':';
humi[0]=humidity/10+0x30;
humi[1]=humidity%10+0x30;
//humi[4]='%';
//humi[5]='\0'; //结束符,如果不要则后面会继续写数据
//temp[0]='T';
//temp[1]=':';
temp[0]=temperature/10+0x30;
temp[1]=temperature%10+0x30;
//temp[4]='C';
//temp[5]='\0';
}
int main(void)
{
uint8_t t;
/************************程序必备4项****************************/
HAL_Init(); /* 初始化HAL库 */
sys_stm32_clock_init(RCC_PLL_MUL9); /* 设置时钟, 72Mhz */
delay_init(72); /* 延时初始化 */
usart_init(115200); /* 串口初始化为115200 */
/***********************所有外设初始化***************************/
led_init();
iic_init();
oled_init(); //OLED初始化
//lcd_init();
OLED_ShowPicture(0,0,128,64,BMP1,1);
oled_refresh_gram(); /*更新显示到OLED,只有此语句才会有显示*/
rtc_init(); //TRC初始化
//exfuns_init(); /* 为fatfs相关变量申请内存,必须初始化否则无汉字显示 */
//f_mount(fs[0], "0:", 1); /* 挂载SD卡 */
//f_mount(fs[1], "1:", 1); /* 挂载FLASH,必须挂载否则无汉字显示 */
// while (fonts_init()) /* 检查字库,必须初始化字库否则无汉字显示 */
// {
// lcd_clear(WHITE); /* 清屏 */
// }
rtc_set_alarm(2025, 6, 11, 12, 0, 0); /* 设置一次闹钟12;0;0 */
rtc_set_time(2025,6,11,11,50,42); /* 2025-06-11; 11-50-42 */
while (DHT11_Init()) /* DHT11初始化 */
{
oled_show_string(10, 16, "DHT11 Error",16);
delay_ms(200);
}
delay_ms(1000); //延时
OLED_ColorTurn(0); //0正常显示,1 反色显示
OLED_DisplayTurn(0); //0正常显示 1 屏幕翻转显示
oled_clear(); //清屏
//OLCD显示时间
/**************************************************/
OLED_ShowChinese(45,0,9,16); //年,16X16字体显示
OLED_ShowChinese(70,0,10,16); //月
OLED_ShowChinese(96,0,2,16); //日
OLED_ShowChinese(0,48,11,16); //温
OLED_ShowChinese(13,48,13,16); //度
OLED_ShowChinese(69,48,12,16); //湿
OLED_ShowChinese(82,48,13,16); //度
OLED_ShowChinese(48,48,14,16); //℃
OLED_ShowChinese(116,48,15,16); //%
oled_refresh_gram(); /*更新显示到OLED,只有此语句才会有显示*/
/**************************************************/
//TFTLCD显示时间
/**************************************************/
// text_show_string(10+4*16, 30, 200, 24, "年", 24, 0, RED);//“年”字前面4个数字,若显示24大小,则像素为12X24
// text_show_string(10+6*16+32, 30, 200, 24, "月", 24, 0, RED);
// text_show_string(10+8*16+64, 30, 200, 24, "日", 24, 0, RED);
//
// text_show_string(10+4*16, 80, 200, 24, "时", 24, 0, RED);
// text_show_string(10+6*16+32, 80, 200, 24, "分", 24, 0, RED);
// text_show_string(10+8*16+64, 80, 200, 24, "秒", 24, 0, RED);
//
// text_show_string(10, 120, 200, 24, "温度: ℃", 24, 0, RED);
// text_show_string(10, 160, 200, 24, "湿度: %", 24, 0, RED);
/**************************************************/
while(1)
{
delay_ms(10);
DHT11_Read_Data(&temperature, &humidity); /*读取温湿度值 */
dat_dis();
//OLED显示温湿度
/**************************************************/
oled_show_string(32,51,temp,12);
oled_show_string(100,51,humi,12);
//oled_show_num(32,51,temp[0],1,12);
//oled_show_num(40,51,temp[1],1,12);
//oled_show_num(100,51,humi[0],1,12);
//oled_show_num(108,51,humi[1],1,12);
oled_refresh_gram(); /*更新显示到OLED,只有此语句才会有显示*/
/**************************************************/
//TFTLCD显示温湿度
/**************************************************/
// lcd_show_num(10+3*24,120,temperature,2,32,BLUE);
// lcd_show_num(10+3*24,160,humidity, 2,32,BLUE);
/**************************************************/
if(t!=calendar.sec)
{
t=calendar.sec;
rtc_get_time();
//OLED显示时间
/******************************************************************/
oled_show_num(12,3,calendar.year,4,12); //输出2025,小字体
oled_show_num(58,3,calendar.month,2,12); //输出月份
oled_show_num(83,3,calendar.date,2,12); //输出日期
OLED_DrawLine(10,16,117,16,1); //输出上横线
OLED_DrawLine(13,19,114,19,1);
OLED_DrawLine(10,16,10,48,1); //输出左竖线
OLED_DrawLine(13,19,13,45,1);
OLED_DrawLine(117,16,117,48,1); //输出右竖线℃
OLED_DrawLine(114,19,114,45,1);
OLED_DrawLine(10,48,117,48,1); //输出下竖线
OLED_DrawLine(13,45,114,45,1);
oled_refresh_gram(); /*更新显示到OLED,只有此语句才会有显示*/
oled_show_num(27,20,calendar.hour,2,24); // 输出时
oled_show_num(63,20,calendar.min, 2,24); // 输出分
oled_show_num(91,29,calendar.sec, 2,12); // 输出秒
oled_refresh_gram(); /*更新显示到OLED,只有此语句才会有显示*/
/*********************************************************************/
//TFTLCD显示时间
/*********************************************************************/
// lcd_show_num(10, 30,calendar.year, 4,24,BLUE);
// lcd_show_num(10+4*24, 30,calendar.month,2,24,BLUE);
// lcd_show_num(10+6*24+12, 30,calendar.date, 2,24,BLUE);
//
// lcd_show_num(10+1*24, 80,calendar.hour, 2,24,BLUE);
// lcd_show_num(10+4*24, 80,calendar.min, 2,24,BLUE);
// lcd_show_num(10+6*24+12, 80,calendar.sec, 2,24,BLUE);
/**********************************************************************/
delay_ms(500); //延时,避免刷新DHT11数据太快
}
}
}
原理图: 无
仿真: 无
代码:
Soft_IIC驱动4线OLED显示RTC时间.7z
(2.39 MB, 下载次数: 0)
|