Timers
The PIC24 processor in your Scamp has five 16-bit timers. Timers 1 and 2 are used by FlashForth. DO NOT CHANGE THEM. Modifying them in any way can cause erratic behaviour and may cause your Scamp to crash. Timers 3, 4 and 5 are available for your use. Timers 4 and 5 can be coupled together to make a single 32-bit timer.
Refer to Section 13 of the processor datasheet for information on configuring the timers, and the various options that are available.
Scamp3 provides words for working with each timer. These words simply place the address of the appropriate timer register on the stack, allowing you to write to it with ! (store) or read from it with @ (fetch).
Refer to Section 13 of the processor datasheet for information on configuring the timers, and the various options that are available.
Scamp3 provides words for working with each timer. These words simply place the address of the appropriate timer register on the stack, allowing you to write to it with ! (store) or read from it with @ (fetch).
- pr3 is the timer 3 period register.
- tmr3 is the timer 3 register.
- t3con is the timer 3 control register.
- pr4 is the timer 4 period register.
- tmr4 is the timer 4 register.
- t4con is the timer 4 control register.
- pr5 is the timer 5 period register.
- tmr5 is the timer 5 register.
- t5con is the timer 4 control register.
For example, to read the Timer 4 control register:
t4con @
To write the value $200 to the Timer 3 period register:
$200 pr3 !
Timer Interrupts
See the page on Interrupts to understand what interrupts are, and how they work on your Scamp.
For our example, let's use Timer 4 to flash the amber status LED periodically.
We'll start by defining the addresses of the interrupt control registers in the PIC24 that we need to use for Timer 4. See the Timer section of the PIC24 datasheet for reference.
We'll start by defining the addresses of the interrupt control registers in the PIC24 that we need to use for Timer 4. See the Timer section of the PIC24 datasheet for reference.
$0086 constant IFS1
$0096 constant IEC1
If we're toggling a LED, we need a flag to hold the current state of the LED. Using the stack isn't an option, since when the timer interrupt fires the stack will most likely be in use by some other word. So let's create a variable to hold the flag, and set it to 0.
variable myFlag
0 myFlag !
Now we want a word to toggle the LED. By defining this as a word in its own right, we can run it at the prompt to test it works.
: toggleLED
myFlag @
if
ledon
0 myFlag !
else
ledoff
1 myFlag !
then
;
Run the word a few times to test it. You will see the amber status LED changing each time you do.
Next we need an interrupt service routine (ISR) to service our Timer 4 interrupt. The word also must clear the Timer 4 interrupt flag in register IFS1.
Next we need an interrupt service routine (ISR) to service our Timer 4 interrupt. The word also must clear the Timer 4 interrupt flag in register IFS1.
: T4ISR \ Timer 4 ISR
[i
toggleLED \ what we want T4 to do
IFS1 @ $f7ff and IFS1 ! \ clear T4 int flag (bit 11)
i]
;i
Once we have the ISR, we allocate it to Timer 4's vector. See Table 8-2 of the PIC24 datasheet for reference. The vector for Timer 4 is #35.
' T4ISR \ get address of the ISR
#35 int! \ vector for T4 is #35 (table 8-2)
aivt \ switch to alternate vector table
Once we set up the timer, it should just all happen:
\ Set up timer 4
$8030 t4con ! \ Timer 4 on
\ TSYNC off
\ TCS internal
\ TECS = SOSC
\ select prescaler ratio 1:256
$2000 pr4 ! \ load PR4 with the period
\ ... and then we are GO...
IEC1 @ $0800 or IEC1 ! \ set T4IE int enable