Shop OBEX P1 Docs P2 Docs Learn Events
In-depth question on experiment 30 (Real-time — Parallax Forums

In-depth question on experiment 30 (Real-time

ArchiverArchiver Posts: 46,084
edited 2001-09-23 09:47 in General Discussion
RP-

The code does some clever stuff to avoid some messy IFs:

GoBack:
IF (btns <= %1000) THEN DoSomeTask ' no update button pressed
rawTime = rawTime + (btnMin * 1439) ' subtract one minute
rawTime = rawTime + (btnHrs * 1380) ' subtract one hour
day = (day + (btnDay * 6)) // 7 ' previous day

UpdateClock: ' send updated value to
DS1302
rawTime = rawTime // 1440 ' clean-up time mods


The IF instruction at GoBack handles the case where btnBack is active
but none of btnMin, btnHrs, btnDay are also pressed.

The equation dealing with btnMin adds 1439 to rawTime if btnMin is
pressed. Note that btnMin can have values 0 or 1 depending on
whether the button is pressed. So the equation adds 0 or 1439 to
rawTime. Since there are 1440 minutes in a day, adding 1439 to a
current value and then adjusting to modulus 1440 in UpdateClock
effective subtracts one minute.

Similarly, the equation dealing with btnHrs adds 0 or 1380 to
rawTime. This adds 23 hours worth of minutes to rawTime and
UpdateClock again adjusts this to a day's worth of minutes. The
result is decrementing an hour.

Lastly, btnDay * 6 yields 0 or 6 which, when added to current day
and then fudged to modulus 7, subtracts 1 from the current day.

Work through some of the equations by hand with various values and
you'll see how they perform.


Regards,

Steve
Sign In or Register to comment.