The hardware timers - utilisation

GrahamGo

Senior Member
I believe that the Picaxe 08M2 is using the PIC12F1823. I see that it has three timers timer0, timer1, timer2, two 8 bits timers and one 16 bit. Searching the forum I see various threads which utilize timer1, via peekSFR and pokeSFR. I would like to try to use a timer to measure the time period of a pulse on an input pin, hopefully using an interrupt.

My question is this.

I am sure that the Picaxe firmware utilizes one or more of the timers, are any of the timers regarded as out of bounds, ie. 'dont touch' ? I cannot seem to find any documentation mentioning the timers and their usage and would appreciate a broad hint on what I can or cannot do.
 

AllyCat

Senior Member
Hi,

Yes it is possible to use Timer 1, but it is also used by the PICaxe operating system, particularly as a prescaler for the "time" variable and for Servo pulses. When it overflows after 65535 the operating system resets it to 45536 (i.e. -20000 or 20 ms) and AFAIK there is no way to prevent this happening (certainly DISABLETIME doesn't help). However, you can Reset Timer 1 to zero (using POKESFR commands) which will allow you to time periods up to 65 ms.

But why try to use an interrupt which is rather slow and unpredictable? The "Timer 1 Gate Control" hardware allows the timer to be started and stopped directly by selectable pulse polarity or period signals. Perhaps a good staring point would be this thread and post #10 in the thread linked from there in #1.

Cheers, Alan.
 
Last edited:

Goeytex

Senior Member
@pxgatpr

See Manual 2 Appendix 4 "Possible conflicting commands".

As I read this section it indicates that the interrupt processing used by Servo (or any other interrupt driven function/command ) can potentially corrupt the values returned by pulsin/pulse out. My experience is that it does. Servo is a resource hog and wants/needs exclusive access to timers 1 & 2. When the internal interrupts occur to service Servo timer1/timer2 this stops/delays other timing sensing commands like Pulsin/Pulsout until the internal interrupt is cleared and the program continues.

If your pulsin or pulseout command is timed to execute between the 20 ms servo pulses and as long as the pulsein\pulsout value is less than 20 ms you might could use them with servo active. However this is jumping through hoops and not very practical.
 

Goeytex

Senior Member
I believe that the Picaxe 08M2 is using the PIC12F1823. I see that it has three timers timer0, timer1, timer2, two 8 bits timers and one 16 bit. Searching the forum I see various threads which utilize timer1, via peekSFR and pokeSFR. I would like to try to use a timer to measure the time period of a pulse on an input pin, hopefully using an interrupt.
The Picaxe 08M2 is actually a Microchip 12F1840. IF we look at the description of pokesfr in manual2 it reads:

M2 parts:
As location can only take the value 0-255 the value for M2 locations taken from
the Microchip datasheet are created as follows:
Bit 7-5 Memory Bank $00-$07
Bit4-0 Addresses $0C to $1F on this bank
($00-$0B are invalid and cause instant reset)​

This basically says that we can access memory banks 0-7 and then only register addresses between 0x0C and 0x1F.

Now look at the PIC12F1840 Datasheet (Table 3-3)

The Timer1 registers are in bank 0 - Good
Timer1 Register addresses are $16,$17,$18 and $19 - Good
PIR1 Register is bank0 Address 0x11 - Good
PIE1 Register is bank1 Address 0x11 - Good


However, (here comes the bad news)

The INTCON register is in Bank0 / Address 0x0B and is therefore made out-of-bounds by the Picaxe firmware. Attempting to access will cause an instant chip reset. This is necessary since the Picaxe firmware needs to exclusively manage all hardware interrupts in order to work. So you cannot manually enable or disable global interrupts with the GIE bit.

This all means that with POKESFR and PEEKSFR you can access the timer, stop and start it and read the values and even read and clear the T1IF flag bit and for the looks of it even enable the TMR1IE Bit. But again, you CANNOT enable or disable the Global interrupts vial INTCON.

Even if this all works I am not sure how you would manage the TMR1 hardware interrupt. Then consider the Picaxe overhead of approx 100 us to execute one command and move to the next. This seems to me to be an exercise in futility.

The good new is that Pulsein works well and has good accuracy and good resolution if the Picaxe is operating at 32MHz.

Perhaps tell folks WHY you want to do this and you might get better responses. Are you possibly wanting to make high resolution measurements of very short duration pulses ?
 
Last edited:

pxgator

Senior Member
The good news is that Pulsein works well and has good accuracy and good resolution if the Picaxe is operating at 32MHz.
I agree, I've used pulsin many times at 32 mhz to decode holtek/manchester style bit streams and it works very well.

Cheers to All
 

AllyCat

Senior Member
Hi,

.. does the servo command conflict with pulsin/pulsout..??
Yes, I believe there is a conflict between PULSIN/OUT and the Servo pulses, but one has to be a little careful about generalising, beacuse there are lots of different PICaxes. What I believe is :

PULSIN and PULSOUT are "blocking" commands which execute a loop of carefully crafted "Machine Code" (Assembler) that takes exactly 10 PIC Instruction Cycles (i.e. 10 us with a 4 MHz clock) for each "count".

Many microcontrollers have "self-loading" counter/timers (after a roll-over) which can be arranged to divide by any desired number. But AFAIK the PIC Timer 1 cannot do this, so the preloading to 45536 (to give 20000 cycles = 20 ms servo pulses) must be done by a software interrupt. I would hope that it doesn't just force 45536 (in binary) into the timer but reads the timer, adds 45536 and then writes back (to take into account any interrupt latency), but I don't know. Some of the PWM outputs (which might be used as a substitute for Servo pulses) are generated directly by the "silicon" hardware, but I guess the Servo pulses result from an/the interrupt.

However, I recall reading recently that a "bug fix" on one of the PICaxe's (sorry I don't remember which) involved DISABLING interrupts during the PULSExx command, to give more accurate pulse timing. So my interpretation is that the "intended operation" is that if a PULSExx command lasts more than 20 ms (and maybe less) then there is a (high) risk that the servo pulses will stop/glitch.

Cheers, Alan.
 

hippy

Technical Support
Staff member
If one avoids all PICAXE commands which use the timers ( SERVO, SETTIMER, TMR3SETUP, PWMOUT, HPWM ) and avoids those commands which temporarily alter the operating frequency of the PICAXE ( if that would affect the rate of timer incrementing ), it is possible to utilise the 16-bit timers using direct access via SFR's.

There is also the caveat AllyCat mentions that on the M2's you need to use DISABLETIME but Timer 1 overflow will still generate an internal interrupt and cause the Timer 1 value to be reset to non-zero, and that behaviour cannot be disabled.

You cannot use interrupts - either PICAXE interrupts or internal firmware interrupts - but you can determine the state of the timers and whether they have overflowed by peeking the SFR's.

For accurately measuring a pulse width, the Gate Control mechanism is best, but it is also possible to detect when a timer is no longer incrementing and determine a pulse has ended but that will depend upon the interval between pulses being measured.

You will also need to handle overflows if the time period is longer than the overflow period. Because of the earlier caveat it will be harder to handle overflow on an M2.

I would recommend initial experiments with an X2 chip as that does not have the M2 'time' related earlier caveat. Unfortunately the 20X2 doesn't have gate control, but the 28X2 and 40X2 do. However that 'detecting timer not incrementing' trick can be used on a 20X2 -

http://www.picaxeforum.co.uk/showthread.php?14517-High-Accuracy-Timer

It might help to explain exactly what sort of pulses you are seeking to measure as that will allow best advice to be given. If it can be done using PULSIN it will save a lot of effort.
 

GrahamGo

Senior Member
It might help to explain exactly what sort of pulses you are seeking to measure as that will allow best advice to be given. If it can be done using PULSIN it will save a lot of effort.
Thanks all, for the excellent information. Rather than muddle this clean, informative thread. I will start new one. Why me, a novice Picaxe user asked the question.
 
Top