Understanding RTC communication
Erlend
Posts: 612
I am working my way through Kwabena's RTC engine. Plan is to mod it to be a reader only, which returns the timedate data packed into one LONG variable - into bits 1-5(sec), 6-10(min), 11-14(hr), 15-17(dow), 18-21(day), 22-25(month), 26-29(year-2000). I want it to contiously poll the time, package it, and write it to the parent cmpTime variable. So, it has to run in it's own cog.
Below is the core of the code. I do not understand what is going on. Confusingly many returns, bolean operations, and bitshifts. Would anyone care to explain me?
Erlend
Below is the core of the code. I do not understand what is going on. Confusingly many returns, bolean operations, and bitshifts. Would anyone care to explain me?
PUB readTime '' 11 Stack Longs 'Reads the RTC data and converts to time-data, and puts it into time[] return getRam(0) PRI getRAM(index) ' 8 Stack Longs startDataTransfer result := transmitPacket(constant(104 << 1)) result and= transmitPacket(index) stopDataTransfer startDataTransfer result and= transmitPacket(constant((104 << 1) | 1)) if(index) result &= receivePacket(false) else bytefill(@time, 0, 7) if(result) repeat 7 timeTemporary := receivePacket(6 <> index) time[index++] := (((timeTemporary >> 4) * 10) + (timeTemporary & $F)) stopDataTransfer PRI transmitPacket(value) ' 4 Stack Longs value := ((!value) >< 8) repeat 8 dira[dataPin] := value dira[clockPin] := false dira[clockPin] := true value >>= 1 dira[dataPin] := false dira[clockPin] := false result := not(ina[dataPin]) dira[clockPin] := true dira[dataPin] := true PRI receivePacket(aknowledge) ' 4 Stack Longs dira[dataPin] := false ' repeat 8 result <<= 1 ' dira[clockPin] := false result |= ina[dataPin] ' dira[clockPin] := true dira[dataPin] := (not(not(aknowledge))) dira[clockPin] := false dira[clockPin] := true dira[dataPin] := true PRI startDataTransfer ' 3 Stack Longs outa[dataPin] := false outa[clockPin] := false dira[dataPin] := true dira[clockPin] := true PRI stopDataTransfer ' 3 Stack Longs dira[clockPin] := false dira[dataPin] := false
Erlend
Comments
The getRam function just gets the bytes of time data from the RTC. You should read the DS1307 RTC data sheet for more information about what that is doing. The constant(104) thing is the RTC address. Its shifted 1 because bit 0 specifies a read when the it is 1 and a write when it is 0.
Just try to get a good grasp on the I2C bus and then read the data sheet for the DS1307 and you'll understand.
value := ((!value) >< 8)
repeat 8
dira[dataPin] := value
dira[clockPin] := false
dira[clockPin] := true value >>= 1
I do know the whole thing is just about picking one bit at the time and put them together - and vice versa.
Erlend