在线时间159 小时
UID3302766
ST金币0
蝴蝶豆0
注册时间2016-8-2
金牌会员
- 最后登录
- 2020-12-7
|
发表于 2020-10-7 12:18:09
|
显示全部楼层
a0a.1 32b0c
以前学习I2C接口时做的液晶时钟
使用了一块STM32最小系统板、1602字符显示屏、I2C模块转串口模块、DS3231RTC模块,使用STM32作为主设备,RTC模块和将液晶屏作为从设备进行I2C通信。
将I2C模块转串口和RTC模块的SDA端子都连接到STM32的B6,然后将两个模块的SDA端子都连接到STM32的B7引脚,两个模块设置了I2C地址,使用这些地址来标识不同的模块。
代码如下:
- #include <LiquidCrystal_I2C.h>
- #include <DS3231_Simple.h>
-
- LiquidCrystal_I2C lcd(0x27,16,2);
- DS3231_Simple Clock;
- volatile int flag=0;
-
- void setup()
- {
- }
-
- void loop()
- {
- pinMode(PA0,INPUT_PULLUP);
- attachInterrupt(PA0, TimeADJ, CHANGE);
-
- Clock.begin();
- char *wk;
- lcd.init();
- lcd.backlight();
-
- int MyTemp;
-
- while(1)
- {
- DateTime MyTime = Clock.read();
- if (flag == 1){
- if (MyTime.Second >= 30) {
- ++MyTime.Minute;
- }
- MyTime.Second = 0;
- Clock.write(MyTime);
- flag = 0;
- }
- lcd.setCursor(0,0);
- lcd.print("20");
- lcd_Print(MyTime.Year);
- lcd.print("/");
- lcd_Print(MyTime.Month);
- lcd.print("/");
- lcd_Print(MyTime.Day);
- switch(MyTime.Dow){
- case 1:wk = "Mon"; break;
- case 2:wk = "Tue"; break;
- case 3:wk = "Wed"; break;
- case 4:wk = "Thu"; break;
- case 5:wk = "Fri"; break;
- case 6:wk = "Sat"; break;
- case 7:wk = "Sun";
- }
- lcd.setCursor(12,0);
- lcd.print(wk);
- lcd.setCursor(0,1);
- lcd_Print(MyTime.Hour);
- lcd.print(":");
- lcd_Print(MyTime.Minute);
- lcd.print(":");
- lcd_Print(MyTime.Second);
- MyTemp = Clock.getTemperatureFloat();
- lcd.setCursor(12,1);
- lcd.print(MyTemp);
- lcd.print("\xdf\x43");
- delay(100);
- }
- }
-
- void lcd_Print(int val)
- {
- if(val < 10){
- lcd.print("0");
- }
- lcd.print(val);
- }
-
- void TimeADJ() {
- flag=1;
- }
复制代码
|
|