AN203

Product: PicStic 1-4

 

Using the Hardware Counter on PA4.

 

Date: 8/14/00

Introduction: This application note demonstrates how to use the counter located on PA4 of the PicStic 1, 2, 3, and 4.

Background: Using PicBasic makes it easier to create programs for the PicStic, however PicBasic doesn't have a direct command to access the timer/counter on PA4. Using the POKE command the PicStic's registers can be set up to enable the counter. Using the PEEK command the timer/counter register can be read.

The two registers that are written to are the OPTION_REG register and the TMR0 register. Both registers are eight bits wide. Each bit in the OPTION_REG register has a specific function.

OPTION_REG Register (Address 81h)

bit 7: *RBPU: PORTB Pull-up Enable bit

1 = PORTB pull-ups are disabled

0 = PORTB pull-ups are enabled (by individual port latch values)

bit 6: INTEDG: Interrupt Edge Select bit

1 = Interrupt on rising edge of PB0/INT pin

0 = Interrupt on falling edge of PB0/INT pin

bit 5: T0CS: TMR0 Clock Source Select bit

1 = Transition on PA4/T0CKI pin

0 = Internal instruction cycle clock (CLKOUT)

bit 4: TOSE: TMR0 Source Edge Select bit

1 = Increment on high-to-low transition on PA4/T0CKI pin

0 = Increment on low-to-high transition on PA4/T0CKI pin

bit 3 PSA: Prescaler Assignment bit

1 = Prescaler assigned to the Watchdog Timer

0 = Prescaler assigned to TMR0

bit 2-0: PS2:PS0: Prescaler Rate Select bits

Please refer to the PIC16F84's data sheet.

The TMR0 register is where the counters count is held. The only time it is written to is to clear the count.

 

How it works: The PicBasic program listed below sets up the counter and transmits the count out of PB7. The push button on PA4 increments the counter by 1 every time it is pressed.

Program Listing:

' This program demonstrates how to use the timer/counter

' on the PicStic 1,2,3, and 4's PA4 pin.

'Define the registers

Symbol OPTION=$81 'Define options register

Symbol TMR=$01 'Define timer/counter register

'Set up the counter using POKE

Poke OPTION, %11101111 'Clear bit 4 to select rising edge

Clear: Poke TMR,$00 'Set the counter to 0

Begin: Peek TMR,B0 'Read the counter

Pause 1000

serout 7,N9600,(#B0,13,10) 'Print the count out PB7

If B0>=100 Then Clear 'Clear the counter if it reaches 100

Goto Begin 'Start the procedure over again