/** ds1302.cpp
2024-08-14
MPU:stm32f103c8t6
從DS1302取得日期時間後經由serial port以及SSD1306 Oled顯示
DS1302接腳 CE(RST):PB15 SCLK:PB13 IO(DAT):PB14 Gnd
SSD1306接腳 SCK:PB6 SDA:PB7 VCC:3.3v Gnd
library: https://github.com/Treboada/Ds1302
stm32duino:Adafruit_SSD1306_STM32.h
*/
//#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include <Ds1302.h>
#define en_oled //enable OLED SSD1306 on
#ifdef en_oled
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306_STM32.h> //C:\Users\halin\AppData\Local\Arduino15\packages\stm32duino\hardware\STM32F1\2022.9.26\libraries\Adafruit_SSD1306
//更改128*64 or 128*32 須由上面的路徑中更改Adafruit_SSD1306_STM32.h內定義
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#endif //endif en_oled
//#define en_debug ; //enable serial out
char kk[10];
String outstr = "";
// DS1302 RTC instance
Ds1302 rtc(PB15, PB13, PB14);// CE, SCLK, IO
//Ds1302 rtc(PB5, PB3, PB4);// CE, SCLK, IO
const static char* WeekDays[] =
{
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
/*
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun"
*/
};
uint8_t parseDigits(char* str, uint8_t count)
{
uint8_t val = 0;
while (count-- > 0) val = (val * 10) + (*str++ - '0');
return val;
}
void setup()
{
#ifdef en_debug
Serial.begin(9600);
#endif
pinMode(PC13, OUTPUT);
// initialize the RTC
rtc.init();
#ifdef en_oled
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
// Clear the buffer.
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
#endif
#ifdef en_debug
// test if clock is halted and set a date-time (see example 2) to start it
if (rtc.isHalted())
{
Serial.println("RTC is halted. Setting time...");
Ds1302::DateTime dt = {
.year = 24,
.month = Ds1302::MONTH_AUG,
.day = 14,
.hour = 19,
.minute = 46,
.second = 30,
.dow = Ds1302::DOW_TUE
};
rtc.setDateTime(&dt);
}
#endif
}
void loop()
{
digitalWrite(PC13, !digitalRead(PC13));
static char buffer[13];
static uint8_t char_idx = 0;
if (char_idx == 13)
{
// structure to manage date-time
Ds1302::DateTime dt;
dt.year = parseDigits(buffer, 2);
dt.month = parseDigits(buffer + 2, 2);
dt.day = parseDigits(buffer + 4, 2);
dt.dow = parseDigits(buffer + 6, 1);
dt.hour = parseDigits(buffer + 7, 2);
dt.minute = parseDigits(buffer + 9, 2);
dt.second = parseDigits(buffer + 11, 2);
// set the date and time
rtc.setDateTime(&dt);
char_idx = 0;
}
#ifdef en_debug
if (Serial.available())
{
buffer[char_idx++] = Serial.read();
}
#endif
if (char_idx == 0) {
// get the current time
Ds1302::DateTime now;
rtc.getDateTime(&now);
static uint8_t last_second = 0;
if (last_second != now.second)
{
last_second = now.second;
outstr = "";
sprintf(kk, "20%2.2d", now.year);
outstr = outstr + kk + '-';
sprintf(kk, "%2.2d", now.month);
outstr = outstr + kk + '-';
sprintf(kk, "%2.2d", now.day);
outstr = outstr + kk;
#ifdef en_debug
Serial.print(outstr);
Serial.print(' ');
Serial.print(WeekDays[now.dow - 1]);
Serial.print(' ');
#endif
#ifdef en_oled
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.println(outstr);
display.println(WeekDays[now.dow - 1]);
display.setTextSize(2);
#endif
outstr = "";
sprintf(kk, "%2.2d", now.hour);
outstr = outstr + kk + ':';
sprintf(kk, "%2.2d", now.minute);
outstr = outstr + kk + ':';
sprintf(kk, "%2.2d", now.second);
outstr = outstr + kk;
#ifdef en_debug
Serial.println(outstr);
#endif
#ifdef en_oled
display.println(outstr);
display.display();
#endif
/*
Serial.print("20");
Serial.print(now.year); // 00-99
Serial.print('-');
if (now.month < 10) Serial.print('0');
Serial.print(now.month); // 01-12
Serial.print('-');
if (now.day < 10) Serial.print('0');
Serial.print(now.day); // 01-31
Serial.print(' ');
Serial.print(WeekDays[now.dow - 1]); // 1-7
Serial.print(' ');
if (now.hour < 10) Serial.print('0');
Serial.print(now.hour); // 00-23
Serial.print(':');
if (now.minute < 10) Serial.print('0');
Serial.print(now.minute); // 00-59
Serial.print(':');
if (now.second < 10) Serial.print('0');
Serial.print(now.second); // 00-59
Serial.println();
*/
}
}
delay(100);
}