Problem using TIMER3 and Program Slots (28x2)

BobMcNobby

Senior Member
I have a rather complex program (too complex to post here)
But basically if I set up the following code everything runs fine

#picaxe 28x2
SetFreq em64 ' set micro to run at 64 mhz !
adcsetup = %0000000000000001 ' set adc to pin 0
tmr3setup %00110011 ' timer3 set to 1/8 prescaler
main:
w27 = timer3 ' grab timer val
b0 = w27 >> 8 ' MSB
b1 = w27 & %11111111 ' LSB
PWMOUT C.1,64,b0 ' stuff MSB to scope
PWMOUT C.2,64,b1 ' stuff LSB to scope
goto main

HOWEVER... if I start using program slots to extend the length of my program, TIMER3 stops working !! :mad:(
My code works perfectly well jumping back and forth on the slots, its just the timer

Not sure if this is intentional, a bug or just me
Thanks in advance for any help

Bob
 

BobMcNobby

Senior Member
I have even tried disabling the timer before the jump then reenabling on 'the other side' - still no worky :mad:(
 

BobMcNobby

Senior Member
Cheers Pete, so I guess I need to..
1. save the relative timer value,
2. jump to slot & restart timer
3. update relative timer value
4. jump back to original slot & restart timer... etc

?? sort of ??
 

BobMcNobby

Senior Member
Done some maths and realised I am continually jumping around slots too fast to get resolution on my idea (above)
Therefore I guess I need to find an accurate solution so that I can time ticks of 30ms accurately whilst still maintaining my slots
 

inglewoodpete

Senior Member
Yep, that's about what you have to do. Fortunately for me, I had two completely different timer/supervision functions in the two slots.

If it's (almost) critical to keep the timer more-or-less accurate, then you could adjust the counter value before you restart it in the new slot. I imagine the time taken jumping between slots would not vary much.
 

inglewoodpete

Senior Member
Fortunately for me, I had two completely different timer/supervision functions in the two slots.
It might be the first sign of PICAXE madness but I'm now quoting myself!

What I did do was split my program into functional units, so that I could keep the routines that relied on the timer together with the running timer. Each program will have its own requirements but you may be able to split yours into the parts that run with the timer running and those that work when the timer is stopped or not important. I mapped out my entire program as subroutines (what calls what etc) and that made it easier to split. From memory, it was 7 or 8 levels deep so was quite complex.
 

BeanieBots

Moderator
It might be worth considering an external timer to generate pulses which cause an interrupt.
Then, just before exiting a slot, wait for the next tick.
That would give the next slot a full 30mS to setup whatever it has to do before the next tick is due.
 

BobMcNobby

Senior Member
I think I may have cracked it...

Slot 0 has my main program running and uses the TIMER3
before I jump to Slot1 I save the current timer (as a timer-value relative to timer3)
Slot1 is long but I only use one small part on each pass
When I return to Slot1 I restart/init the timer and make my previously saved timer-value relative to the reset one

.. I found it helped by watching Back to the Future Part I during this exercise !! lol

Cheers for your help pete, you provoked my brain in the right direction
 

BobMcNobby

Senior Member
Cheers Beenie, I did think about that but my prog is running at 100% @ 64mhz, there are no waits apart from a few us to set up data transmission
I basically need every ms I can get my hands on

Its not that I need to have things happen exactly at 30ms intervals, I just need to count how many 30ms intervals happen between to events

Its cured now thanks
 
Top