ds3231: basic support for DS3231 RTC device

This commit is contained in:
Daniel Esteban
2019-03-30 16:54:27 +01:00
committed by Ayke van Laethem
parent d9b426b90b
commit f6bdc9734f
3 changed files with 259 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
// Connects to an MAG3110 I2C magnetometer.
package main
import (
"machine"
"time"
"fmt"
"github.com/tinygo-org/drivers/ds3231"
)
func main() {
machine.I2C0.Configure(machine.I2CConfig{})
rtc := ds3231.New(machine.I2C0)
rtc.Configure()
valid := rtc.IsTimeValid()
if !valid {
date := time.Date(2019, 12, 05, 20, 34, 12, 0, time.UTC)
rtc.SetTime(date)
}
running := rtc.IsRunning()
if !running {
err := rtc.SetRunning(true)
if err != nil {
fmt.Println("Error configuring RTC")
}
}
for {
dt, err := rtc.ReadTime()
if err != nil {
fmt.Println("Error reading date:", err)
} else {
fmt.Printf("Date: %d/%s/%02d %02d:%02d:%02d \r\n", dt.Year(), dt.Month(), dt.Day(), dt.Hour(), dt.Minute(), dt.Second())
}
temp, _ := rtc.ReadTemperature()
fmt.Printf("Temperature: %.2f ºC \r\n", float32(temp)/1000)
time.Sleep(time.Second * 1)
}
}