Positron Compiler Documentation

Counter

Source: Positron8 Compiler User Manual, PDF page 133

Syntax

Variable = Counter Pin, Period

Overview

Count the number of pulses that appear on pin during period, and store the result in variable.

Parameters

Variable is a user-defined variable. Pin is a Port.Pin constant declaration i.e. PORTA.0. Period may be a constant, variable, or expression.

Example

' Count the pulses that occur on PORTA.0 within a 100ms time period
' and displays the results.
  Device = 18F26K40
                                ' Select the device to compile for
  Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
  Declare Hserial_Baud = 9600 ' Set the Baud rate for HRsoutLn
  Dim MyWord as Word
                                ' Create a word size variable
  Dim MyPin as PORTA.0
                                ' Assign the input pin to PORTA.0
  Cls
  Do
    MyWord = Counter MyPin, 100 ' Variable MyWord now contains the Count
    HRsoutLn Dec MyWord, " "
                                ' Display the decimal result on a serial terminal
  Loop
                                ' Do it indefinitely

Notes

The resolution of period is in milliseconds (ms). It obtains its scaling from the oscillator declaration, Declare Xtal.

Counter checks the state of the pin in a loop, and counts the rising edge of a transition (low to high).

With a 4MHz oscillator, the pin is checked every 20us, and every 4us with a 20MHz oscillator. From this we can determine that the highest frequency of pulses that may be counted is: -

25KHz using a 4MHz oscillator. 125KHz using a 20MHz oscillator.

See also : PulseIn, Rcin.