手表的选择
之前买了小米手环4,但是充电方式很鸡肋,不能很方便的充电。忘拿配套充电座的时候没法充,充电还得把表带摘下来。于是有了自己造一块手表的想法。
元器件的购买
0.91寸OLED屏,IIC
pro mini,另外这块板子没有自带USB口,所以需要USB转UART的模块,我这里用到CP2102。
DS1302,这个是核心零件,至于图片这里不放了。我用的是SOP8封装的贴片,配了SOP8封装的PCB实验板。
32.768KHz的无源晶振,DS1302上必须用。
锂电池200毫安时,3.7v,配一个可充电的过充过放保护板。
以上零件均可在淘宝买到
代码部分
用到了olikraus的u8g2, Makuna的RTC第三方库文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#include <ThreeWire.h> #include <RtcDS1302.h> #include <U8x8lib.h>
ThreeWire myWire(4,3,5); RtcDS1302<ThreeWire> Rtc(myWire); U8X8_SSD1306_128X32_UNIVISION_HW_I2C u8x8( U8X8_PIN_NONE, SCL, SDA);
void setup() {
Serial.begin(57600); Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); if (!Rtc.IsDateTimeValid()) { Rtc.SetDateTime(compiled); }
if (Rtc.GetIsWriteProtected()) { Rtc.SetIsWriteProtected(false); }
if (!Rtc.GetIsRunning()) { Rtc.SetIsRunning(true); }
RtcDateTime now = Rtc.GetDateTime(); if (now < compiled) { Rtc.SetDateTime(compiled); } u8x8.begin(); u8x8.setPowerSave(0); }
void loop() { RtcDateTime now = Rtc.GetDateTime(); u8x8.setFont(u8x8_font_chroma48medium8_r); printDateTime(now); }
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt) { char datestring[9]; char timestring[11];
snprintf_P(datestring, countof(datestring), PSTR("%02u:%02u:%02u"), dt.Hour(), dt.Minute(), dt.Second() ); u8x8.drawString(0,0,datestring); snprintf_P(timestring, countof(timestring), PSTR("%04u/%02u/%02u"), dt.Year(), dt.Month(), dt.Day() ); u8x8.drawString(0,1,timestring); u8x8.refreshDisplay(); delay(1000); }
|