Revision: 1799
Author: wattymage
Date: Mon Mar 15 10:55:51 2010
Log: initial alpha sample
http://code.google.com/p/jallib/source/detail?r=1799
Added:
/trunk/catpad/isr_250_tmr0.jal
=======================================
--- /dev/null
+++ /trunk/catpad/isr_250_tmr0.jal Mon Mar 15 10:55:51 2010
@@ -0,0 +1,93 @@
+-- Title: Real Time Clock running from ISR on TMR0
+-- Author: Eur van Andel, Copyright (c) 2003..2008, all rights reserved.
+-- Adapted-by:
+-- Compiler: >=2.4h
+--
+-- This file is part of jallib (http://jallib.googlecode.com)
+-- Released under the ZLIB license
(http://www.opensource.org/licenses/zlib-license.html)
+--
+-- Sources: http://www.romanblack.com/one_sec.htm,
http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
+--
+-- Description: This Interrupt Service Routine updates the variable
seconds about once per second.
+-- The precision is 0.2 ppm, the accuracy depends on the Xtal used.
+--
+-- Notes: This is the Bresenham Line Algorithm, invented at IBM in 1962,
which gets an accurate end result
+-- by summing the small errors resulting of taking discrete steps and
correcting when the error gets too large.
+-- This means that individual second lengths may show some jitter, but
that long-term timekeeping is accurate.
+-- Original assembler by Roman Black.
+--
+
+-- 20 MHz, so 200ns/clock, so 5 000 000 clocks/second
+-- the RTC has three bytes: bres_hi, bres_mid, bres_lo
+-- timer 0 runs on internal clock speed and interrupts on overflow
+-- every timer 0 interrupt decreases bres_mid by one.
+-- the 2 000 000 value is added when bres_hi en bres_mid are zero
+-- note that remainder is added to bres_lo, which cab overflow in bres_mid
+-- this keeps clock count accurate, although interrupts happen every 256
clock counts
+-- the ISR subtracts 1 from the mid byte. It first checks
+-- the mid byte for zero and borrows a bit from bres_hi if needed
+
+
+var volatile byte seconds -- updated by ISR
+const POLL_INTERVAL = 1368 -- 350ms
+const TICK_INIT = 12
+const USER_INIT = 100
+var volatile word userCount = USER_INIT -- debounce keys
+var volatile byte ticks = TICK_INIT
+var volatile word poll_count = POLL_INTERVAL -1
+var volatile word rtc_count = 3906 -- should be 3906.25
+-- RTC ISR constants:
+-- 0x4C 0x4B 0x40 5 000 000 20 MZz xtal
+-- 0x0F 0x42 0x40 1 000 000 4 MHz int osc
+-- 0x12 0x13 0xD0 1 250 000 20 MHz, prescaler = 4
+
+const hi = 0x4C -- 2 000 000 for 20 MHz xtal and prescaler = 4
+const mid = 0x4B
+const lo = 0x40 -- ideally modified in timeset too, to adjust clock
+
+var volatile byte bres_hi = hi
+var volatile byte bres_mid = mid
+var volatile byte bres_lo = 0 --lo
+
+-- every 21.333 us at 48MHz x 3 = 64us
+-- x12 = 256 us = RDS oversample
+-- radio poll is 350ms 1368 x 256 us = 350.208 ms
+
+
+procedure RTC() is
+ pragma interrupt
+ INTCON_TMR0IF = off -- clear the timer 0 interrupt flag
+ ticks = ticks -1 -- 256us counter
+ if (ticks < 1) then
+ ticks = TICK_INIT
+ CheckRDS = CheckRDS +1
+ poll_count = poll_count -1
+
+ if (poll_count<1) then
+ poll_count = POLL_INTERVAL
+ CheckRadio = CheckRadio +1
+ end if
+ userCount = userCount -1
+ if userCount < 1 then
+ userCount = USER_INIT
+ checkUser = CheckUser +1
+ end if
+ rtc_count = rtc_count -1
+ if rtc_count < 1 then
+ bres_lo = bres_lo + 1
+ if bres_lo == 4 then
+ bres_lo = 0
+ rtc_count = 3907
+ else
+ rtc_count = 3906
+ end if
+ seconds = seconds +1
+ checkTime = CheckTime +1
+ end if
+ end if
+
+end procedure -- end of ISR
+
+
+
+
--
You received this message because you are subscribed to the Google Groups
"jallib" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jallib?hl=en.