Shorter period Timer on the 20M2

inglewoodpete

Senior Member
I have a project which uses a 20M2 on the AXE118 board. I need to implement a 40 to 50mS timer for regular checking of an ADC input.

My processor of choice would be a 20X2, due to its more flexible timer and interrupt configuration but I need to use multiple PWM channels. Hence the 20M2.

The 20M2 has a Timer word which increments every second (4 MHz and 16 MHz). I presume it would increment every 500mS at 8 or 32 MHz: still too slow for my needs.

I'm wondering if there is another, perhaps less visible byte or word that counts at a faster rate, causing the Timer variable to increment every second. My idea would be to read and count this system variable to create a non-blocking timer that increments every 40 to 50mS.


Note: This project is for client. I will receive a modest payment for the solution I provide.
 

AllyCat

Senior Member
Hi IP,

It's actually called the time system variable and increments once each second (provided that there are no significant "blocking" commands). Note that it cannot be reset to sub-second accuracy (i.e. it could increment just a few ms after a time = 0) and sadly, AFAIK, there is no access to its (PICaxe system software) "prescaler".

However, the prescaler's raw "tick" is caused by an overflow of Timer 1 every 20ms (i.e. the prescaler divides by 50) with Timer 1 (readable via two SFRs) actually counting from 45536 to 65535. So if you can poll those SFRs at least once every 20ms (to emulate the prescaler count) then you can read time to much higher resolution (theoretically to 1 microsecond).

It's the method that I used in #12 of this thread.

Cheers, Alan.
 

Goeytex

Senior Member
The 20M2 has four 8-bit timers and one 16 bit timer. Seems to me that one of them might be available via pokesfr. Guess this depends upon how many PWMs and which ones you are using. Otherwise you could throw down an 08M that triggers an external interrupt on the 20M2 every 50ms. Not an ideal solution but will certainly work.

Another thought was to use the the SR Latch feature to generate an interrupt every 50 ms, but the SRL uses one of the PWM pins.
 
Last edited:

AllyCat

Senior Member
Hi,

There are also the Capture-Compare latches (accessible by SFRs) but a problem is that they can't be used (directly) to generate an interrupt to the PICaxe interpreter. However, you could simply use a PWM output (if they're not all in use) to generate an interrupt (which might need further prescaling down to 50 ms).

But there is a "gotcha"; you can't generally "steer" any internal PIC signals (e.g. compare outputs or SR flip-flop) to a PICaxe interrupt input pin. That's because the interpreter "breaks" the hardware output path when it reads (polls) the pin (i.e. setting it back as an input). So you need an external hardware connection from the (e.g. PWM) output to the "interrupt" input pin.

Cheers, Alan.
 

hippy

Technical Support
Staff member
It might be possible but probably depends on exactly what PWM channels you are using; most of my ideas would be based on using an unused timer/counter but that won't work if they are all in use.

I haven't studied operation of the 'timer' variable but there may be some pre-scaler you could poke to change it's speed but perhaps not.

You could perhaps use HSEROUT to send a byte at a particular baud rate which could give a regular 'recieved byte' indication, you might be able to do the same with SPI, but I haven't check I/O pin conflicts etc.

You could probably piggy-back an 08M2 on the 20M2 and have that generate a clock at whatever rate you want and still have that programmable if required.

Or use an external RC circuit for timing if you don't want to use another chip.

I think we need to know exactly what resources are being used by the 20M2.
 

inglewoodpete

Senior Member
Many heads are better than one when thinking outside the square!

Alan mentioned the 20mS timer which got my brain cells churning.

The Servo commands generate a 20mS pulse with a clock speed of 4 & 16MHz (the pulse is possibly 10mS at 8 & 32 MHz, which gives even better granulation). If I loop a servo pulse train back from an output pin into an input with interrupts configured, I can get a 10mS or 20mS 'timer' interrupt. Almost as good as a 20X2!

The project is actually quite simple (with some fancy software) and doesn't need too many of the resources of the 20M2. The client wants to control the colours of (up to several hundred) RGB LEDs. The colour will be controlled by (many) Sharp GP2Y0A21YK0F Infrared range sensors, one PICAXE per sensor. Because of the large number of sensors and PICAXEs, hardware minimisation is essential.

Thanks for the suggestions. I'll report back on the success or otherwise in the coming days/weeks.
 

AllyCat

Senior Member
Hi,

Yes, Timer 1 is (also) used for servos, that's why it's kept running even after a "disabletime".

In principle, your interrupt routine could simply read the Timer1 High SFR, subtract about 117 (30,000 / 256) and write it back. That should extend the time/servo clock ticks to about 50 ms. There's generally no need to bother with the T1 Low byte because even T1 High always increments (perhaps by more than 1) between two consecutive PICaxe instructions.

Although the PICaxe interpreter only gets the "Time" (and servos) correct at 4 and 16 MHz, it's probably possible to "hack" the (hardware) prescaler via the SFRs to make the period correct (or as required) with other clock frequencies.

What has defined your choice of the 20M2, maximum output pins (versus price)?

Cheers, Alan.
 

inglewoodpete

Senior Member
As I mentioned, the X2s are my PICAXE of choice. I have used 08M2s and 14M2s in recent projects but, I even use the 20X2 SMD for 14-pin jobs (I designed the "Turbo 14" PCB before the 14M2 became available).

The client chose the 20M2, driven almost entirely by the 4 PWM channels & possibly the 2 8-bit ports. (The smallest X2 that has 4 x PWM is the 28X2.)

I'll try to use the 20M2 at 8 or 32 MHz to give 10mS interrupts. The IR will count by 5 and read the ADC pin every 5th interrupt. K.I.S.S.

Peter
 
Top