Name:
count
Syntax:

COUNT pin, period, wordvariable

Pin - is a variable/constant which specifies the input pin to use.

Period - is a variable/constant (1-65535ms at 4MHz).

Wordvariable - receives the result (0-65535).

Description:

Count pulses on an input pin.

Count checks the state of the input pin and counts the number of low to high transitions within the time 'period'. A word variable should be used for 'variable'. At 4MHz the input pin is checked every 20us, so the highest frequency of pulses that can be counted is 25kHz, presuming a 50% duty cycle (ie equal on-off time). Take care with mechanical switches, which may cause multiple 'hits' for each switch push as the metal contacts 'bounce' upon closure.

Effect of increased clock speed

For all PICAXE chips the minimum width of a clocking signal (total time of high and low added together) and that signal's maximum frequency will be as follows:

Clock Signal Signal Frequency Width Frequency
4MHz 40us 25kHz
8MHz 20us 50kHz
16MHz 10us 100kHz
32MHz 5us 200kHz
64MHz 2.5us 400kHz

The unit of time for the sampling period is also affected by the operating speed.

Clock Frequency Sample Period Time Unit
4MHz 1ms (1000 us)
8MHz 500 us
16MHz 250 us
32MHz 125 us
64MHz 62.5 us

Result Variable

The 'count' command can count up to 65535 pulses and a 16-bit word variable is required to store numbers that high.

An 8-bit byte variable can only hold a maximum value of 255 so, if more than 255 pulses are counted, the number stored in the byte variable will not accurately reflect the actual count of pulses.

For this reason it is recommend that a word variable is used to store the count of pulses.

If it is known there will never be more than 255 pulses during the period of the count, or it does not matter if the variable does not hold an accurate count when there are more than 255 pulses, then a byte variable may be used.

Applies To:
All
See Also:
Related Create:
Share:
Print:

Count input transitions

Count how many low to high transitions there are on input pin C.1 in a 5 second period and report the result

Code Example:
main:	count C.1, 5000, w1	; count pulses in 5secs (at 4MHz)
	debug			; display value
	goto main		; loop back to start
Copy Code Submit an Example

Submit Your Own Code!

You must be logged in to submit code examples. Login now.

Comments

  • Login to leave a comment.