Use of Timer0 on a PICAXE-08M2

copyright, Peter H. Anderson, Balimore, MD, Jan, 2012

In Progress.

This discussion illustrates how to TIMER0 on a PIC12F1823 (PICAXE-08M2) to count event occuring on T0CKI (terminal 5). Once setup, this operation is "background". That is, the PICAXE may be executing the program you wrote to do amazing things and and all the time be counting events on terminal 5 in the background.

Applications that come to mind are to count the number of "tips" of a tipping bucket in implementing a rain guage, counting the number of milk bottles traveling along a belt or counting the number of people passing past a point over the course of an hour. In all of these cases, the number of events is counted with no strain on the firmware and the timing of the beginning and the end is not critcal. For example, counting the tips of a tipping bucket might be daily and it really doesn't matter if that is exactly a day or 23 hours and 37 minutes.

Note that all PIC12F / 16F182x devices have this capability and thus, it is possible this capability may be used with any of the M2 PICAXEs. However, note that this is a capability which is not standard and I cannot be sure this will work in every application of the PICAXE-08M2. For example, if you execute a "touch" command, the configuration of the PIC may be changed such that the TMR0 capability is lost.

The best advice is to fiddle and rejoice with successes and don't weep with failure.

Figure 1 is a block diagram of the Timer0 module. Bit t0xcs = 0 selects the event source as input T0CKI. The state of bit tmr0se determines whether TMR0 increments on the rise or fall of the pulse. Bit tmr0cs is set to a logic 1 to select T0CKI rather than the system clock. Bit psa equal to one bypasses the prescaler. In summary, the OPTION_REG at location $095 is set to a value of %00111xxx or $38. This would usually be done once and forever thereafter TMR0 at location $015 may be cleared or read.


There are a number of points.

SFRs for the 12/16F182 series are values are in the range of $000 - $7ff which is eight banks. Only the lower five bits of each bank are used. Thus, the Rev-Ed designers very cleverly accomodated this into a byte by using the high three bits for the bank followed by the five bits within the bank. Thus, the OPTION_REG at location 0x095 is represented as bank 1, location $15 or $35 (%001 10101). TIMER0 is at bank 0, location $15 or $15 (%00010101).

Symbol CountVal = B0

   PokeSFR $35, $38  ' set OPTION_REG (0x95) to 0x38 (%0011 1000)
   PokeSFR $15, $00  ' clear TMR0
       Pause 10000
       PeekSFR $15, CountVal ' Read TMR0 
       PokeSFR $15, $00
       SerTxD (#CountVal, CR, LF) ' Display the value
       'Pause 10000