#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Private astream As AsyncStreams
Public wirevar As WireMaster 'I2C / TWI devices
Private timer1 As Timer
Private bc As ByteConverter
Dim seconds,minutes, hours,dayonweek,day,month,year As Byte
Dim bcd As Byte
Private const RTC_ADDRESS As Byte= 0x68
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
astream.Initialize(Serial1.Stream,"astream_NewData", "astream_Error")
wirevar.Initialize 'I2C / TWI connection with DS3231 RTC Sensor
Delay(100)
Log("AppStart")
'Set time (seconds,minutes,hours,dayOfWeek,dayOfMonth,month,year
'setDS3231time(00,58,10,1,30,8,16)
'Enable Timer to read Time on DS3231 RTC Sensor
timer1.Initialize("timer1_Tick",1000)
timer1.Enabled=True
End Sub
'Displays Time and Date in Log at Tick Event
Sub Timer1_Tick
Dim breadtime(7) As Byte
Dim DS3231value As Byte
Dim Datetimevalues(16) As Byte
breadtime=readDS3231time
For i=0 To 6
DS3231value=breadtime(i)
Datetimevalues(2*i+1)=Bit.Get(DS3231value,7)*8+Bit.Get(DS3231value,6)*4+Bit.Get(DS3231value,5)*2+Bit.Get(DS3231value,4)*1
Datetimevalues(2*i+2)=Bit.Get(DS3231value,3)*8+Bit.Get(DS3231value,2)*4+Bit.Get(DS3231value,1)*2+Bit.Get(DS3231value,0)*1
Next
'Log(hour-minute-second --- day-month-year)
Log(Datetimevalues(5),Datetimevalues(6),"-",Datetimevalues(3),Datetimevalues(4),"-",Datetimevalues(1),Datetimevalues(2),"---",Datetimevalues(9),Datetimevalues(10),"/",Datetimevalues(11),Datetimevalues(12),"/",Datetimevalues(13),Datetimevalues(14))
End Sub
' Sets Time and Date (seconds,minutes,hours,dayOfWeek,dayOfMonth,month,year) data To DS3231
Sub setDS3231time(second As Byte, minute As Byte, hour As Byte, dayofweek As Byte,dayOfMonth As Byte, months As Byte, years As Byte)
'First byte=0 --> Set Next input To start at the seconds register
'Next bytes (in BCD): second,minutes, hours, dayOfWeek,dayOfMonth,month,year
wirevar.WriteTo(RTC_ADDRESS,Array As Byte(0,decToBcd(second),decToBcd(minute),decToBcd(hour),decToBcd(dayofweek),decToBcd(dayOfMonth),decToBcd(month),decToBcd(year)))
End Sub
' Reads Time and Date data To DS3231
Sub readDS3231time As Byte()
'set DS3231 register pointer To 00h
wirevar.writeto(RTC_ADDRESS,Array As Byte(0)) 'set DS3231 register pointer To 00h
' request seven bytes of data from DS3231 starting from register 00h
' in BCD
Dim Datetime1() As Byte=wirevar.RequestFrom(RTC_ADDRESS, 7)
Return Datetime1
End Sub
'Sets data form astream buffer (in this case Serial, would be BLE, WIFI, etc..)
Sub astream_NewData (Buffer() As Byte)
If Buffer.Length <> 7 Then
Log("invalid data: Buffer Size not 7 --> ", Buffer.Length)
Else
hours = Buffer(0)
minutes = Buffer(1)
seconds = Buffer(2)
dayonweek=Buffer(3)
day = Buffer(4)
month = Buffer(5)
year = Buffer(6)
Log("B4R: Received time data!")
setDS3231time(seconds,minutes,hours,1,day,month,year)
End If
End Sub
Sub astream_Error
Log("error")
End Sub
' Convert normal decimal numbers To binary coded decimal
Sub decToBcd (val As Byte) As Byte
Dim bcd As Byte
Dim bcdUint As UInt
Dim numdec,numord As UInt
Dim bcduintmat(2) As UInt
Dim b(),b1(),b2(),b4() As Byte
If val>9 Then 'Convert to BCD
b=NumberFormat(val,2,0) 'Byte to String
'BCD=Dec number (4 bits High Nibble byte) + Ordinal number (4 bits Low Nibble Byte)
b1=bc.SubString(b,0) 'Dec number
b2=bc.SubString(b,1) 'Ordinal number
b2(0)=b2(0)-48 'substract ASCII value corresponding to number 0
b1(0)=b1(0)-48 'substract ASCII value corresponding to number 0
'Need to translate Byte to Uint to XOR the 2 numbers to build BCD number
numord=b2(0)
numdec=Bit.ShiftLeft(b1(0),4)
bcdUint=Bit.xor(numdec,numord)
'Pass Uint result to Array of Bytes to apply ByteConverter UintstoBytes to obtain Bytes
bcduintmat(0)=bcdUint
b4=bc.UIntsToBytes(bcduintmat)
bcd=b4(0)
Else 'If val <=9, bcdnumber=number
bcd=val
End If
'Log("bcd =" ,bcd)
Return bcd
End Sub
' Convert binary coded decimal To normal decimal numbers
Sub BcdToDec (value As Byte) As Byte ()
Dim bdec,bord,numbyte(2) As Byte
'Get BCD from byte (value)
'bdec= High Nibble, bord= Low Nibble
bdec=Bit.Get(value,7)*8+Bit.Get(value,6)*4+Bit.Get(value,5)*2+Bit.Get(value,4)*1
bord=Bit.Get(value,3)*8+Bit.Get(value,2)*4+Bit.Get(value,1)*2+Bit.Get(value,0)*1
Log ("val ",value, "bdec ",bdec," bord =",bord)
'Byte to Uint to XOR b1/b2 values (need to cast to UInt)
numbyte(1)=bdec
numbyte(0)=bord
Log(numbyte(1),numbyte(0))
Return numbyte
End Sub