The data is stored on the IC in register. You can imagine them as those oldfashioned filingcabinets. Each drawer has its number and contains some info:
04H Date The H tell us that these are hexadecimal figures.
08H to 3FH Ram
If we want to read or store some data we'll first have to tell the device in which drawer we want to be. We can do this by writing the hex code for that drawer to the device. The device then will grant us acces to that drawer. After you write or read something from or to this register the device will automatically jump to the next one. So there is no need to send the location every time
Now lets put this in code:
For this code you will need to dim hours as byte, minutes as byte and seconds as byte.
First, we will set the clock:
Seconds = Makebcd(Seconds) We convert our variables into BCD format
Minutes = Makebcd(Minutes)
Hours = Makebcd(Hours)
reset hours.6 We reset bit 6 of the hoursbyte to make sure that
our Clock runs in 24h modus. If bit 6 is 1 then the
clock runs in 12h modus and bit 5 will then
contain the AM/PM data.
I2cwbyte &HD0 We tell the device that we want to write a byte
2cwbyte &H00 We start at the register for seconds hex 00
I2cwbyte Seconds Adding seconds
I2cwbyte Minutes Adding minutes
I2cwbyte Hours Adding hours
Now our clock is set! Lets read from it now.
I2cwbyte &HD0 We tell the device that we want to write a byte.
I2cwbyte &H00 We ask the device to go to the seconds register.
I2cwbyte &HD1 We tell the device that we want to read bytes.
I2crbyte Seconds , Ack We read the data and acknowledge that we want
to read the next byte too.
I2crbyte Minutes , Ack
I2crbyte Hours , Nack We don't ackowledge here so the device knows
that we are done reading.
Hours = Hours And &B00111111 We remove bits 6 and 7 as they contain other
data. If you are in 12h modus, then you need to
remove bit 5 too
Hours = Makedec(Hours) We convert back to decimal format.
Minutes = Makedec(minutes)
Seconds = Makedec(seconds)
Now we know what time it is.
In the next step we will take a closer look at the DS18B20.
Enter the email associated with your account and we will send you your username and a temporary password.
That's a pro feature!
Want to go pro?
First step is to sign up.
Already a member?" Login »