Revision: 1659
Author: [email protected]
Date: Fri Feb 12 21:31:21 2010
Log: added sample for timer3 on 18f452
http://code.google.com/p/jallib/source/detail?r=1659
Added:
/trunk/sample/18f452_timer3.jal
=======================================
--- /dev/null
+++ /trunk/sample/18f452_timer3.jal Fri Feb 12 21:31:21 2010
@@ -0,0 +1,87 @@
+-- Title: Basic usage of timer3 module
+-- Author: Matthew Schinkel - borntechi.com, copyright (c) 2009, all
rights reserved.
+-- Adapted-by:
+-- Compiler: >=2.4m
+--
+-- This file is part of jallib (http://jallib.googlecode.com)
+-- Released under the BSD license
(http://www.opensource.org/licenses/bsd-license.php)
+--
+-- Description: This example shows basic usage of timer3 to output a 1khz
+-- square wave using timer3, while blinking a led
+--
+-- Sources:
+--
+-- notes:
+--
+
+-- include chip
+include 18F452 -- target picmicro
+
+-- This program assumes a 20 MHz resonator or crystal
+-- is connected to pins OSC1 and OSC2.
+pragma target clock 20_000_000 -- oscillator frequency
+-- configuration memory settings (fuses)
+pragma target OSC HS -- HS crystal or resonator
+pragma target WDT disabled -- no watchdog
+pragma target LVP disabled -- no Low Voltage Programming
+
+-- timer out def
+alias timer_out is pin_a1
+alias timer_out_direction is pin_a1_direction
+--
+timer_out_direction = output
+
+-- led def
+alias led is pin_a0
+alias led_direction is pin_a0_direction
+--
+led_direction = output
+
+-- set all IO as digital
+enable_digital_io()
+
+-- init pic interrupt settings
+intcon_gie = on ; enables all unmasked interrupts
+intcon_peie = on ; enables all unmasked peripheral interrupts
+
+-- Set this to desired interval. timer3 will count up.
+-- Interupt will occur when timer_interval rolls
+-- over from 65535 to 0
+var word timer_interval = 60535 -- 1khz sign wave at 20mhz
+-- this value can be calculated:
+-- 65535 - ( (1/1khz) / ( (1/20_000_000)*4 ) ) = 60535
+-- timermax - ( (1/HZ ) / ( (1/OSC FREQ )*4 ) ) = timer_interval
+
+-- timer3 setup
+tmr3 = timer_interval -- timer interval
+t3con_tmr3cs = 0 -- use internal clock
+pie2_tmr3ie = 1 -- enable the timer3 interrupt bit
+t3con_tmr3on = 1 -- 1 = enables timer 3
+pir2_tmr3if = off -- clear overflow to start timer
+
+-- set timer3 clock prescaler
+-- each increment slows clock by a multiple of 2
+t3con_t3ckps = 0 -- set prscal of 1:1
+
+-----------
+procedure timer_isr() is
+ pragma interrupt -- interupt procedure
+
+ if !PIR2_TMR3IF then -- check if this is a timer3 interupt
+ return -- exit interupt if it is not
+ end if
+
+ timer_out = !timer_out -- change led to oposite value
+
+ TMR3 = timer_interval -- set the interval
+ PIR2_TMR3IF = off -- clear overflow to start timer
+end procedure
+
+-- main program here
+forever loop
+ _usec_delay (250_000)
+ led = on
+ _usec_delay (250_000)
+ led = off
+end loop
+
--
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.