PIC AXE 28X1 Timer interrupt

martini

New Member
I've been struggling to get the timer interrupt working on the 28-x1 chip.

This is the code I have used:
settimer t1s_4
setintflags %10000000,%10000000 ‘ enable timer interrupt

main: pause 1000 ‘ wait 1 seconds
high 0 ‘ Turn LED on
pause 1000 ‘ Wait 1 second
low 0 ‘ Turn LED off
inc b1 ‘ Turn LED on
debug ‘ display variables in debug window
goto main ‘ loop

Interrupt: inc b2 ‘ increment b2 each time the timer overflows
setintflags %10000000,%10000000 ‘ re-activate interrupt
return ‘ return from sub

The main program loop works. The debug statement shows the b1 register incrementing each loop.
The timer variable increments each time the timer overfloes but a timer interrupt does not seem to be generated and the b2 register never gets incremented.

Am I doing something wrong? Any hints as to how to the the rimer interrupt working would be appricated.

May thanks,
Martin
 

Technical

Technical Support
Staff member
You have misunderstood the interrupt. It occurs when timer overflows from 65535 to 0, not every time timer 'ticks'.

Try preloading timer to a high value (e.g. timer = 65530) at the start of your program to see the effect.
 

hippy

Ex-Staff (retired)
I had to go and read the SETTIMER description a few times to get to grips with this. I hadn't really understood it either, or more correctly had forgotten ! This thread may also cast some light on things - <A href='http://www.rev-ed.co.uk/picaxe/forum/Topic.asp?topic_id=6955&amp;forum_id=31&amp;Topic_Title=Timer%2Bmaths%2B28X1&amp;forum_title=PICAXE+Forum' Target=_Blank>External Web Link</a>

SETTIMER sets how long it takes between 'timer' variable increments, so &quot;SETTIMER T1S_4&quot; is right; 'timer' increments every second.

An interrupt doesn't occur though except on 'timer' variable overflow, so the code would only interrupt after 65536 timer increments, every 65536 seconds, ~18 hours.

To get an interrupt every second, you need to increment 'timer' once a second, but also ensure that the 'timer' overflows when that increment occurs. This can be done by setting the 'timer' variable with a high value so it more quickly overflows, with -<code><pre><font size=2 face='Courier'> SetTimer T1S_4
timer = $FFFF </font></pre></code> The 'timer' will also need to be reset in the interrupt routine using, as I understand it -<code><pre><font size=2 face='Courier'> toFlag = 0
timer = $FFFF </font></pre></code> You do not want to use SETTIMER in the interrupt because that would disrupt the hardware timer ( although your Debug in the code will do that as noted in the manual ).
 
Top