hour - 1 =255?

martinn

Member
I am summarising data collected over an hour, each hour the data is written to an eeprom. To record the hour the data was collected for, I simply use the current RTC reading (converted to decimal) -1. This works fine for all hours except midnight (hour 24) where it returns 255 instead of 23.

I could write the hour to a variable at the start of each hour, but I am curious as to what is happening.

Any suggestions?

Martin
 

SD2100

New Member
If the clock hits midnight and is zero then you subtract 1 from your variable which is 0 then that variable will now equal 255, if a byte variable is decremented below 0 or incremented above 255 then it will overflow

EG:
b0 = 0
b0 = b0 - 1
b0 now equals 255


Edited by - Phil75 on 21/10/2006 13:34:36
 

BeanieBots

Moderator
If you want to continue taking readings beyond 23, there is a fairly simple solution.
Read in the hour and store it in a variable, say b0.
Then, periodically, in a polling loop, read the time again into another variable say b1.
If b0=b1, then you are still within the same hour, if they are different, an hour has elapsed. Then make b0=b1 and start polling again. Each time they are found to be different you can increment another variable to count the total elapsed hours.
 
Top