\ \\\\\\\\\\\\\\\\\\\\\ REAL TIME CLOCK FUNCTIONS \\\\\\\\\\\\\\\\\\\\\\\\ HEX \ All numerical values are in hexidecimal format 2VARIABLE TICK_VAL \ Storage for real time clock ticker VARIABLE TCI2_VEC \ Storage for original TCI2 vector VARIABLE PRE_RTC_IMR \ Storage for IMR value prior to RTC setup 2710 CONSTANT TICK_RATE \ 10000 counts, 5 msec rate for RTXEB ICLK @ 4 MHz \ Process the real time clock interrupt service routine : RTC-ISR ( -- ) SELCPR \ Work in multi-page environment CR@ DUP 0010 OR CR! \ Save carries and block interrupts TICK_VAL 2@ \ Load the 32-bit ticker 1 S>D D+ \ Increment the 32-bit ticker by 1 TICK_VAL 2! \ Save the 32-bit ticker CR! \ Restore the interrupts and carries ; : RTC-INIT ( -- ) IMR@ DUP DUP \ Load the interrupt mask register and duplicate PRE_RTC_IMR ! \ Save a copy of the IMR TIMER2 OR IMR! \ Mask the Timer 2 interrupt (TCI2), just in case 0 S>D TICK_VAL 2! \ Initialize the real time clock TICK_RATE TC2! \ Set up the Timer 2 interrupt rate (0.005 sec) 9 @INTERRUPT TCI2_VEC ! \ Save the original Timer 2 interrupt vector ['] RTC-ISR 9 !INTERRUPT \ Setup the Timer 2 interrupt vector TIMER2 NOT AND IMR! \ Unmask the Timer 2 interrupt (TCI2) ; : RTC-HALT ( -- ) IMR@ TIMER2 OR IMR! \ Mask the Timer 2 interrupt (TCI2) TCI2_VEC @ 9 !INTERRUPT \ Restore the original Timer 2 interrupt vector PRE_RTC_IMR @ IMR! \ Restore the interrupt mask register ; : TICK-N-PASSED? ( d -- flag ) TICK_VAL 2@ \ Read the current value of the ticker D< \ Determine if N is less than current tick value ; : WAIT-FOR-TICK-N ( d -- ) BEGIN \ Enter loop to wait for ticker values to match 2DUP \ Duplicate the future ticker value TICK-N-PASSED? \ Compare to current ticker value UNTIL 2DROP \ Exit the loop if the ticker is >= N ; : WAIT-FOR-NEXT-TICK ( -- ) TICK_VAL 2@ \ Read the current value of the ticker 1 S>D D+ \ Increment 32-bit value by one WAIT-FOR-TICK-N \ Wait until the next tick occurs ; : WAIT-N-TICKS ( d -- ) TICK_VAL 2@ \ Load the 32-bit ticker D+ \ Increment the value by the number of ticks WAIT-FOR-TICK-N \ Wait for the ticks to transpire ; \ \\\\\\\\\\\\\\\\\\\\ END OF REAL TIME CLOCK FUNCTIONS \\\\\\\\\\\\\\\\\\\\