B4R Question i dont use zs-042

rodmcm

Active Member
Licensed User
The ZS-042 has two sections to it, an EEprom and an RTC
As per another thread you have the information to use the RTC
The following uses the EEProm which is address 0x57
This was written for an ESP32, but should compile okay for a standard arduino

B4X:
Based on this article
'http://www.hobbytronics.co.uk/arduino-external-eeprom

#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 600
#End Region

Sub Process_Globals
    Dim bc As ByteConverter
    Private wire As WireMaster
    Dim I2Address As Byte = 0x57        ' 1010 A2 A1 A0             1010 111 = 57    ZS -042
    Dim StoreLocation As UInt =470
    Dim StoreInt As Int = 54
    Dim StoreValue As Byte = 0x38   ' Number 8
    Public Serial1 As Serial
End Sub


Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    RunNative("SetWireClock", 400000) '400khz
    wire.Initialize
    Delay(1000)

    For i= 32000 To 32050 Step 2
        WriteExtEEPROMInt(I2Address,i,i)
    Next
    
    For i= 32000 To 32050 Step 2
        Log(ReadExtEEPROMInt(I2Address,i))
    Next
    
    WriteExtEEPROMInt(I2Address,StoreLocation,StoreInt)

    Delay(1000)
    Log(ReadExtEEPROMInt(I2Address,StoreLocation))
    
    Log("Finish")
End Sub

#if C
void SetWireClock(B4R::Object* o) {
   Wire.setClock (o->toULong());
}
#end if

Sub WriteExtEEPROMInt (DeviceAddress As Int, EEAddress As UInt, Value As Int)
    Dim Data(4) As Byte
    Dim raf As RandomAccessFile
    raf.Initialize(Data, True)
    raf.WriteByte(Bit.ShiftRight(EEAddress, 8), raf.CurrentPosition)
    raf.WriteByte(Bit.And(EEAddress, 0xff), raf.CurrentPosition)
    raf.WriteInt16(Value,raf.CurrentPosition)
    wire.WriteTo(DeviceAddress, Data)
    Delay(5)
End Sub

Sub ReadExtEEPROMInt (DeviceAddress As Int, EEAddress As UInt) As Int
    Dim b(2) As Byte
    b(0) = ReadExtEEPROMByte (DeviceAddress ,EEAddress )
    b(1) = ReadExtEEPROMByte (DeviceAddress ,EEAddress +1)
    Dim x As Int = bc.IntsFromBytes(b)(0)
    Return x
End Sub


Sub WriteExtEEPROMByte (DeviceAddress As Int, EEAddress As UInt, Value As Byte)
    Dim Data(3) As Byte
    Dim raf As RandomAccessFile
    raf.Initialize(Data, True)
    raf.WriteByte(Bit.ShiftRight(EEAddress, 8), raf.CurrentPosition)
    raf.WriteByte(Bit.And(EEAddress, 0xff), raf.CurrentPosition)
    raf.WriteByte(Value, raf.CurrentPosition)
    wire.WriteTo(DeviceAddress, Data)
    Delay(5)
End Sub


Sub ReadExtEEPROMByte (DeviceAddress As Int, EEAddress As UInt) As Byte
    Dim Data(2) As Byte
    Dim raf As RandomAccessFile
    raf.Initialize(Data, True)
    raf.WriteByte(Bit.ShiftRight(EEAddress, 8), raf.CurrentPosition)
    raf.WriteByte(Bit.And(EEAddress, 0xff), raf.CurrentPosition)
    wire.WriteTo(DeviceAddress, Data)
    Delay(5)
    Dim Answer() As Byte = wire.RequestFrom(DeviceAddress,1)   
    If Answer.Length>0 Then
        Return Answer(0)
    End If
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…