Tinygo ESP8266 SoftI2C 调试RX8025T 不能读取到时间原因排查
先说排查出最终原因:ESP8266,貌似必须用 scl: D2 sda: D4引脚,如果用已定义好的D1,D2引脚 则会读取不到时间
以下代码是使用自带的i2csoft库读写Rx8025T,时钟芯片代码
以下读取到,逻辑分析仪情况
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/i2csoft"
"led/lg"
//"tinygo.org/x/drivers/delay"
)
const(
RX8025T_I2C_ADDR=0x32 //RX8025T I2C地址,读写地址为左移1位 <<1,读取地址为0x65,写地址为0x64
RX8025T_REG_SECOND=0x00 // BCD value 00 -> 59
RX8025T_REG_MINUTE=0x01 // BCD value 00 -> 59
RX8025T_REG_HOUR= 0x02 // BCD value 00 -> 23
RX8025T_REG_WEEKDAY=0x03 // bit 0->6: sunday->saturday
RX8025T_REG_DAY=0x04 // BCD value 01 -> 31
RX8025T_REG_MONTH=0x05 // BCD value 01 -> 12
RX8025T_REG_YEAR=0x06 // BCD value 00 -> 99
RX8025T_REG_RAM=0x07 // R/W accessible for any data in the range from 00 h to FF h
RX8025T_REG_ALARM_MIN=0x08 // BCD value 00 -> 59
RX8025T_REG_ALARM_HOUR=0x09 // BCD value 00 -> 23
RX8025T_REG_ALARM_W_OR_D=0x0A // WEEK: bit 0->6: sunday->saturday
RX8025T_REG_TIMER_COUNTER0=0x0B // Fixed-cycle timer control registers to set the preset countdown value
// for the fixed-cycle timer interrupt. (COUNTER1, COUNTER0) = 12bit number
RX8025T_REG_TIMER_COUNTER1=0x0C // When control register changes from 001h to 000h, the /INT pin goes to
RX8025T_REG_EXTEN =0x0D
RX8025T_EXTEN_TEST=0x80 // TEST bit. value should always be "0"
RX8025T_EXTEN_WADA= 0x40 // Week Alarm/Day Alarm bit. R/W to specify either WEEK or DAY
// as the target of the alarm interrupt function. 0:WEEK, 1:DAY
RX8025T_EXTEN_USEL=0x20 // Update Interrupt Select bit. R/W specify either "second"
// or "minute" as update interrupt, 0:second update, 1:minute update
RX8025T_EXTEN_TE=0x10 // Timer Enable bit. controls the start/stop setting for the fixed-cycle
// timer interrupt function. 1:start, 0:stop
RX8025T_EXTEN_FSEL1=0x08 // FOUT frequency Select bits to set the FOUT frequency. (FSEL1, FSEL0)
RX8025T_EXTEN_FSEL0=0x04 // 0,0:32.768KHz, 0,1:1024Hz, 1,0:1Hz, 1,1:32.768KHz
RX8025T_EXTEN_TSEL1=0x02 // Timer Select bits to set the countdown period (source clock) for the
// fixed-cycle timer interrupt (TSEL1, TSEL0)
RX8025T_EXTEN_TSEL0=0x01 // 0,0:4096Hz, 0,1:64Hz, 1,0:1Hz(per second), 1,1:1/60Hz(per minute)
RX8025T_RTC_STATUS= 0x0E
RX8025T_FLAG_UF= 0x20 // Update Flag. 0 -> 1 when a time update interrupt event has
// occurred. 1 is retained until a 0 is written
RX8025T_FLAG_TF=0x10 // Timer Flag. 0 -> 1 when a fixed-cycle timer interrupt
// event has occurred, 1 is retained until a 0 is written
RX8025T_FLAG_AF=0x08 // Alarm Flag. 0 -> 1 when an alarm interrupt event has occurred
// 1 is retained until a 0 is written
RX8025T_FLAG_VLF=0x02 // Voltage Low Flag. 0 -> 1 when data loss occurs, e.g. a supply
// voltage drop,
RX8025T_FLAG_VDET=0x01 // Voltage Detection Flag. 0 -> 1 when stop the temperature compensation
// such as due to a supply voltage drop, 1 is retained until a 0 is written
RX8025T_REG_CONTROL=0x0F
RX8025T_CONTR_CSEL1=0x80 // Compensation interval (CSEL1, CSEL0)
RX8025T_CONTR_CSEL0=0x40 // 0,0:0.5s, 0,1:2s(default), 1,0:10s, 1,1:30s
RX8025T_CONTR_UIE=0x20 // Update Interrupt Enable, 0:off, 1:on
RX8025T_CONTR_TIE=0x10 // Timer Interrupt Enable
RX8025T_CONTR_AIE=0x08 // Alarm Interrupt Enable
RX8025T_CONTR_RESET=0x01 // Writing a "1" to this bit stops the counter operation and
)
var tt=[]byte{0x30,0x59,0x12,0x02,0x09,0x08,0x22} //秒 分 时 周 天 月 年
var tt1=[]byte{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
var r=make([]byte, 7)
var w=make([]byte, 2)
var i2c=i2csoft.New(machine.D2, machine.D4)//!!!必须是D2,D4.。。。。
func main(){
i2c.Configure(i2csoft.I2CConfig{
Frequency: 400e3,//400e3,
})
i2c.SetBaudRate(115200)
RX8025T_Init()
RX8025T_SetTime()
for{
RX8025T_GetTime()
delay(1000)
}
}
func toBCD(x int) byte{
var n0,n1 byte
n0=byte(x%10)
n1=byte((x/10)%10)
return (n1 << 4) | n0
}
func RX8025T_Write(reg uint8,data []byte){
legacy.WriteRegister(i2c,0x32,reg,data)
}
// 初始化, 清除标志位, 关闭中断
func RX8025T_Init(){
RX8025T_Write(0x0E, []byte{0x00})
RX8025T_Write(0x0f, []byte{0x40})
RX8025T_Write(0x0D, []byte{0x42})
}
// 读取时间
func RX8025T_GetTime(){
i2c.Tx(0x32,[]byte{0x00},r)
//i2c.Tx(0x32,nil,r)
println(r[5])
//println(toBCD(2023))
}
// 设置时间
func RX8025T_SetTime(){
RX8025T_Write(0x00, tt)
RX8025T_Write(0x08, tt1)
}
func delay(t time.Duration){
time.Sleep(time.Millisecond * t)
}
以下为自己百度看了很多源码,根据C51代码移植来的
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。