pcf8563: add support for pcf8563 real time clock

This commit is contained in:
sago35
2021-05-09 13:44:05 +09:00
committed by Ron Evans
parent 91dadd5535
commit 761bcfc4db
8 changed files with 453 additions and 2 deletions
+45
View File
@@ -0,0 +1,45 @@
package main
import (
"fmt"
"machine"
"time"
"tinygo.org/x/drivers/pcf8563"
)
var (
i2c = machine.I2C0
rtc = pcf8563.New(i2c)
)
func main() {
i2c.Configure(machine.I2CConfig{Frequency: machine.TWI_FREQ_400KHZ})
rtc.Reset()
time.Sleep(3 * time.Second)
rtc.SetTime(time.Date(2006, 1, 2, 15, 4, 50, 0, time.UTC))
rtc.SetAlarm(time.Date(2006, 1, 2, 15, 5, 0, 0, time.UTC))
rtc.EnableAlarmInterrupt()
prev := -1
for {
for {
t, _ := rtc.ReadTime()
if prev != t.Second() {
fmt.Printf("%s\r\n", t.String())
prev = t.Second()
if rtc.AlarmTriggered() {
fmt.Printf("alarm triggered\r\n")
rtc.ClearAlarm()
}
break
}
time.Sleep(time.Millisecond * 100)
}
}
}
+31
View File
@@ -0,0 +1,31 @@
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/pcf8563"
)
var (
i2c = machine.I2C0
rtc = pcf8563.New(i2c)
)
func main() {
i2c.Configure(machine.I2CConfig{Frequency: machine.TWI_FREQ_400KHZ})
rtc.Reset()
for {
rtc.SetOscillatorFrequency(pcf8563.RTC_COT_1HZ)
time.Sleep(3 * time.Second)
rtc.SetOscillatorFrequency(pcf8563.RTC_COT_32HZ)
time.Sleep(3 * time.Second)
rtc.SetOscillatorFrequency(pcf8563.RTC_COT_1KHZ)
time.Sleep(3 * time.Second)
rtc.SetOscillatorFrequency(pcf8563.RTC_COT_32KHZ)
time.Sleep(3 * time.Second)
rtc.SetOscillatorFrequency(pcf8563.RTC_COT_DISABLE)
time.Sleep(3 * time.Second)
}
}
+36
View File
@@ -0,0 +1,36 @@
package main
import (
"fmt"
"machine"
"time"
"tinygo.org/x/drivers/pcf8563"
)
var (
i2c = machine.I2C0
rtc = pcf8563.New(i2c)
)
func main() {
i2c.Configure(machine.I2CConfig{Frequency: machine.TWI_FREQ_400KHZ})
rtc.Reset()
rtc.SetTime(time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC))
prev := -1
for {
for {
t, _ := rtc.ReadTime()
if prev != t.Second() {
fmt.Printf("%s\r\n", t.String())
prev = t.Second()
break
}
time.Sleep(time.Millisecond * 100)
}
}
}
+46
View File
@@ -0,0 +1,46 @@
package main
import (
"fmt"
"machine"
"time"
"tinygo.org/x/drivers/pcf8563"
)
var (
i2c = machine.I2C0
rtc = pcf8563.New(i2c)
)
func main() {
i2c.Configure(machine.I2CConfig{Frequency: machine.TWI_FREQ_400KHZ})
rtc.Reset()
time.Sleep(3 * time.Second)
rtc.SetTime(time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC))
rtc.SetTimer(15 * time.Second)
rtc.EnableTimerInterrupt()
prev := -1
for {
for {
t, _ := rtc.ReadTime()
if prev != t.Second() {
fmt.Printf("%s\r\n", t.String())
prev = t.Second()
if rtc.TimerTriggered() {
fmt.Printf("timer triggered\r\n")
rtc.ClearTimer()
rtc.SetTimer(10 * time.Second)
}
break
}
time.Sleep(time.Millisecond * 100)
}
}
}