May sound silly to some, but I just can't get my head around this.
Implementing RTC in BeagleBone Black and to set the time I have to convert decimal
Year (20) , Month Day Hours Min & secs into Hex. eg. year 20 = 20 Hex
The year is a good example.
I need to put into a uByte buffer 20 Hex to represent 20 decimal.
Also had to convert from 20 Hex to 20 decimal, but that was easy.
All the Date / Time bits are valued as per:
b7--b6--b5--b4--b3--b2--b1--b0
x----40--20--10--8----4----2----1
Code: Select all
Sub i2cGetTime
With i2c
.pBuffer = callocate(7) '7 bytes for SD2405 time data.
'-----i2c Tests-------
.Result = iolib_init() 'initialize the library iobb.
Print "iolib_init = ";.Result
configure_i2c_pins(19,20)
.PortNbr = 2: .Addr = &H32 'P9-19 & P9-20 the pins to use (no feedback)
.Handle = i2c_open(.PortNbr,.Addr) 'Open the port and get a handle.
Print "i2c Handle = ;"; .Handle
'--------------------------------
.Result = i2c_read(.Handle, .pBuffer,7) 'attempt a read of 7 bytes using Handle
If .Result > -1 Then 'prints error if -1
Dim as uByte Xq,xChr
For Xq = 0 to 6 'for 6 registers
xChr = *(.pBuffer + Xq) 'get uByte from pBuffer + Xq
Dim as uByte upper,lower,Masked
Upper = xChr shr 4
Lower = xChr And &B00001111
Print "Upper = ";Xq,Upper
Print "Lower = ";Xq,Lower
Select case Xq
Case 0
.Secs = (Upper * 10) + Lower
Case 1
.Mins = (Upper * 10) + Lower
Case 2
Masked = Upper And &B00000011 'masked for 12/24 Hr
.Hours = (Masked * 10) + Lower
Case 3
.wDay = Lower
Case 4
Masked = Upper And &B00111111
.Days = (Masked * 10) + Lower
Case 5
.Mnths = 0
Masked = Upper And &B00011111
.Mnths = (Masked * 10) + Lower
Case 6
.Years = ((Upper * 10) + Lower) + 2000
End Select
Next
Print .Years;"-";.Mnths;"-";.Days;" ";.Hours;":";.Mins;":";.Secs
EndIf
deallocate(.pBuffer) 'finished with Buffer
'----------------------------
.Result = i2c_close(.Handle)
Print "Close Result = ";.Result
iolib_free() 'finished with Library
End With
End Sub
Regards