Revision: 1897
Author: [email protected]
Date: Thu Apr 8 20:07:06 2010
Log: timer3_rtc update
http://code.google.com/p/jallib/source/detail?r=1897
Modified:
/trunk/sample/18f452_timer3_rtc.jal
=======================================
--- /trunk/sample/18f452_timer3_rtc.jal Wed Apr 7 21:00:56 2010
+++ /trunk/sample/18f452_timer3_rtc.jal Thu Apr 8 20:07:06 2010
@@ -1,4 +1,4 @@
--- Title: second counter using timer3
+-- Title: Basic usage of timer3 module
-- Author: Matthew Schinkel - borntechi.com, copyright (c) 2009, all
rights reserved.
-- Adapted-by:
-- Compiler: >=2.4m
@@ -6,7 +6,8 @@
-- This file is part of jallib (http://jallib.googlecode.com)
-- Released under the BSD license
(http://www.opensource.org/licenses/bsd-license.php)
--
--- Description: counts seconds with timer3
+-- Description: This example shows basic usage of timer3 to output a 1khz
+-- square wave using timer3, while blinking a led
--
-- Sources:
--
@@ -62,42 +63,24 @@
pir2_tmr3if = off -- clear overflow to start timer
-- set timer3 clock prescaler
-t3con_t3ckps = 1 -- set prscal of 1:2
-
--- define a rollover of 1 second
--- holds 1 second: 1.00000000000 or 0x174876E800
-var byte*6 one_sec
-var byte one_sec_array[6] at one_sec
-one_sec_array[5] = 0x00
-one_sec_array[4] = 0x17
-one_sec_array[3] = 0x48
-one_sec_array[2] = 0x76
-one_sec_array[1] = 0xE8
-one_sec_array[0] = 0x00
-
--- counter will roll over 38.14697265625 times in one second for 20mhz
clock
--- (1sec / ( (timer freq ) * clock increments ) / prescaller 1:2 =
interrupts per sec
--- (1 / ( (1/20_000_000*4) * 65536 ) / 2 =
38.14697265625
--- holds 0.14697265625 or 36C0679D9 (the remainder)
-var byte*6 num_add
-var byte num_add_array[6] at num_add
-num_add_array[5] = 0x00
-num_add_array[4] = 0x03
-num_add_array[3] = 0x6c
-num_add_array[2] = 0x06
-num_add_array[1] = 0x79
-num_add_array[0] = 0xD9
-
--- counts the remainder up to one second
+-- each increment slows clock by a multiple of 2
+t3con_t3ckps = 1 -- set prscal of 1:1
+
+-- holds value of one second 1.0000000
+var dword one_sec = 10000000
+
+-- interrupt will occur every 0.0262144 seconds for 20mhz clock
+var dword interrupt_occur_time = 262144
+
+-- counts the remainder up to one second (up to 10000000)
-- after count is > 1 second, we will minus 1 second within interrupt.
-var byte*6 remainder_count = 0
-var byte remainder_count_array[6] at remainder_count
-
--- seconds counter, allow it to roll over to 0
+var byte*6 time_count = 0
+var byte time_count_array[6] at time_count
+
+-- second counter, allow it to roll over to 0
var byte second_count = 255
-----------
-var byte count1 = 0 -- counts up to 38 (interupt every 38.14697265625 sec)
procedure timer_isr() is
pragma interrupt -- interupt procedure
@@ -107,35 +90,30 @@
PIR2_TMR3IF = off -- clear timer overflow
- count1 = count1 + 1 -- count the number of times an interrupt
occurs
- if count1 == 38 then -- if timer is at 1 sec
-
- -- if remainder_count is > 1 sec
- if remainder_count > one_sec then
-
- -- remove one second from remainder_count
- remainder_count = remainder_count - one_sec
-
- -- remove one second from actual timing
- count1 = count1 + 1
- else
- -- add 0.14697265625 (the remainder) to remainder_count
- remainder_count = remainder_count + num_add
- end if
-
- second_count = second_count + 1 -- increment the seconds
- if second_count == 60 then -- reset seconds to 0 each min
+ -- if time_count is > 1 sec
+ if time_count > one_sec then
+ -- remove one second from time_count since it went over 1 sec
+ time_count = time_count - one_sec
+
+ -- add one second to displayed time
+ second_count = second_count + 1
+
+ -- reset seconds to 0 each min
+ if second_count >= 60 then
second_count = 0
end if
-
+
-- send seconds to serial port
print_byte_dec (serial_hw_data,second_count)
serial_hw_data = 0x0D
serial_hw_data = 0x0A
-
+
led = !led -- switch led on/off each second
- count1 = 0
+ else
+ -- add 0.0262144 seconds
+ time_count = time_count + interrupt_occur_time
end if
+
end procedure
-- main program here
--
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.