tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/pcf8563/time/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"machine"
     6  	"time"
     7  
     8  	"tinygo.org/x/drivers/pcf8563"
     9  )
    10  
    11  var (
    12  	i2c = machine.I2C0
    13  	rtc = pcf8563.New(i2c)
    14  )
    15  
    16  func main() {
    17  	i2c.Configure(machine.I2CConfig{Frequency: machine.TWI_FREQ_400KHZ})
    18  	rtc.Reset()
    19  
    20  	rtc.SetTime(time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC))
    21  
    22  	prev := -1
    23  
    24  	for {
    25  		for {
    26  			t, _ := rtc.ReadTime()
    27  			if prev != t.Second() {
    28  				fmt.Printf("%s\r\n", t.String())
    29  				prev = t.Second()
    30  				break
    31  			}
    32  			time.Sleep(time.Millisecond * 100)
    33  		}
    34  	}
    35  
    36  }