【WCH】CH32F203基于内部RTC+I2C SSD1306 OLED时钟和温度显示
- ✨主要是在其基础 上增加温度显示,温度数据来源于DS18B20,更换了OLED驱动显示字体相关内容。
- 🔰仅作为功能演示,内部RTC精度不高,长时间运行需要依赖第三方数据校准才行。
- 🔖代码是从STM32工程基础上修改来的,修改相关内容后完全可以适配CH32F203上运行。
⛳相关驱动注意事项
- 软件I2C驱动SSD1306需要注意,驱动引脚需要配置为推挽输出模式,而不是像硬件I2C那样引脚配置使用开漏输出。
- 采用内部RTC时钟源,其时钟频率为40KHz,作为时钟源分频系数:
39999
📑引脚定义说明
- 🎉引脚可以根据个人使用需求切换到任意其他可用引脚上。
CH32F203 ------ I2C SSD1306 OLED
PA5 ------- SCL
PA7 ------- SDA
CH32F203 ------ DS18B20
PA15 ------ DATA
📓驱动代码部分
u8 RTC_Init(void)
{
u8 temp = 0;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
BKP_DeInit();
RCC_LSICmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET && temp < 250)
{
temp++;
delay_ms(10);
}
if(temp >= 250)return 1;
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForLastTask();
RTC_WaitForSynchro();
RTC_ITConfig(RTC_IT_SEC, ENABLE);
RTC_WaitForLastTask();
RTC_EnterConfigMode();
RTC_SetPrescaler(40000-1);
RTC_WaitForLastTask();
RTC_Set(2023, 4, 25, 18, 0, 35);
RTC_ExitConfigMode();
BKP_WriteBackupRegister(BKP_DR1, 0XA1A1);
RTC_NVIC_Config();
RTC_Get();
return 0;
}
#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "oled.h"
#include "rtc.h"
#include "ds18b20.h"
u8 year_buf[4];
u8 month_buf[2];
u8 day_buf[2];
u8 temp_buf[4];
int main(void)
{
u16 temp;
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
OLED_Init();
RTC_Init();
DS18B20_Init();
delay_ms(200);
OLED_CLS();
OLED_P16x16Ch(40, 0, 36);
OLED_P16x16Ch(72, 0, 37);
OLED_P16x16Ch(106, 0, 38);
OLED_P16x16Ch(0, 6, 39);
OLED_P16x16Ch(16, 6, 41);
OLED_P16x16Ch(32, 2, 34);
OLED_P16x16Ch(32, 4, 35);
OLED_P16x16Ch(80, 2, 34);
OLED_P16x16Ch(80, 4, 35);
while(1)
{
sprintf((char*)year_buf, "%04d", calendar.w_year);
OLED_P8x16Str(8, 0, year_buf);
sprintf((char*)month_buf, "%02d", calendar.w_month);
OLED_P8x16Str(58, 0, month_buf);
sprintf((char*)day_buf, "%02d", calendar.w_date);
OLED_P8x16Str(90, 0, day_buf);
OLED_P16x16Ch(0, 2, calendar.hour / 10 * 2);
OLED_P16x16Ch(0, 4, calendar.hour / 10 * 2 + 1);
OLED_P16x16Ch(16, 2, calendar.hour % 10 * 2);
OLED_P16x16Ch(16, 4, calendar.hour % 10 * 2 + 1);
OLED_P16x16Ch(48, 2, calendar.min / 10 * 2);
OLED_P16x16Ch(48, 4, calendar.min / 10 * 2 + 1);
OLED_P16x16Ch(64, 2, calendar.min % 10 * 2);
OLED_P16x16Ch(64, 4, calendar.min % 10 * 2 + 1);
OLED_P16x16Ch(95, 2, calendar.sec / 10 * 2);
OLED_P16x16Ch(95, 4, calendar.sec / 10 * 2 + 1);
OLED_P16x16Ch(111, 2, calendar.sec % 10 * 2);
OLED_P16x16Ch(111, 4, calendar.sec % 10 * 2 + 1);
OLED_P16x16Ch(32, 6, calendar.week + 20);
temp = DS18B20_Get_Temp();
OLED_P16x16str(64, 6, 11);
OLED_P16x16str(80, 6, 12);
OLED_P16x16str(96, 6, temp % 1000 / 100);
OLED_P16x16str(112, 6, temp % 100 / 10);
}
}
📚工程源码
- ✨申明:本文章仅发表在CSDN网站,任何其他网站,未注明来源,见此内容均为盗链和爬取。
- 🍁对于文中所提供的相关资源链接将作不定期更换。
链接: https:
提取码: 1adv