Timers and Interrupts
This applies to version 2.02 +
Timers
There are 5 timers available in the PIC32 but timers 4 and 5 are used by the system and so timers 1 through 3 are available for user use. In addition to this a pseudo timer, timer 0 is available. This is timer 1 but is fed by the slow oscillator and so longer periods can be obtained.
The general syntax for setting a timer is:
settimer <0-3> <period in ms>
As an example to set timer 0 for a 5 second period:
settimer 0 5000
This will set the period and start the timer, a timer can be stopped by:
stoptimer <0-3>
To start the timer again use settimer.
Timer 0 & Timer 1
Timer 0 and timer 1 use the same hardware (timer 1) and so cannot be used at the same time. Timer 0 is fed from the slow oscillator at 32768Hz and timer 1 is fed from the system oscillator. The possible range of the timers is as follows:
Timer 0 1ms to 509000ms (509
seconds)
Timer 1 1ms to 415ms
Timer 2 1ms to 415ms
Timer 3 1ms to 415ms
Once a timer has been set it will count up to the value set by the settimer above and then roll over back to zero. It will continue to do this until stopped.
Using the Timers
To see the values the registers must be read directly with peeki() and to set timer values less than 1ms can also be achieved by setting the registers directly with pokei(). Below is an example:
constant TMR1 0xbf800610
constant PR1 0xbf800620
function a
print peeki(TMR1)
endf
function b
print peeki(PR1)
endf
If timer 0 is set to a period of 5000 (5 seconds) then PR1 will be set to 20480, there is also a pre-device counter that settimer will calculate and set, this is part of the timer configuration register. Function 'a' will show the timer value at any instant so calling it several times will show a different value. The timer register (TMR1) will count up until it reaches 20480 and then it will reset back to 0 and continue counting up again.
These or similar functions can be used in user programs. Timers can also be made to interrupt the processor when the rollover (from 20480 to 0 in this case) occurs. This can be captured and dealt with, details of this are in the next section.
Interrupts
There are over 60 possible sources of interrupt on the PIC32 and these can be accessed via the registers using peeki() and pokei(). However to make programming similar, eight sources of interrupt have been provided that have a simple basic interface these are:
T0 Timer 0 (timer 0 is timer 1)
T1 Timer 1
T2 Timer 2
T3 Timer 3
E0 External Interrupt 0
RF6 JP4-13 (BV513)
E1 External Interrupt 1
RD8 JP4-8 (BV513)
E2 External Interrupt 2
RD18 JP4-5 (BV513)
E3 External Interrupt 3
RD10 JP4-6 (BV513)
There are just two keywords required for using these 'on' and 'di', the syntax is:
on <source> <function>
and
di <source>
Where <source> is any one of the above 'T0', 'E0' etc. Case is important 't0' will not be recognised and the '0' is a zero not capital O. The function must exist.
function test1
print "This is test 1 "
endf
As an example using the above function, to make this print out every 10 seconds then the following would be required:
settimer 0 10000 // sets timer to 10 seconds
on T0 test1
Similarly test1 can be called whenever an external pin is taken from high to low, for example port RD8 would be:
on E1 test1
NOTES:
- Any error will stop the timer and interrupt.
- An external interrupt will occur when the input is taken from high to low
- For external interrupts the corresponding port line is set to an input
- If T0 is used the low frequency oscillator is started