COUNT Example

COUNT Pin, Duration, Variable

Function
Count the number of cycles (0-1-0 or 1-0-1) on the specified pin during the Duration time frame and store that number in Variable.

• Pin  is any SX I/O pin (RA.0 .. RA.7, RB.0 .. RB.7, RC.0 .. RC.7, RD.0 .. RD.7, RE.0 .. RE.7).
• Duration is a variable or constant (1 – 65535) specifying the time during which to count. The unit of time for Duration is expressed in milliseconds.
• Variable is a Byte or Word variable in which the count will be stored. Note that array elements are not allowed.

Quick Facts
 SX18 / SX20 / SX28 / SX48 / SX52 Units in Duration 1 ms Duration range 1 ms to 65.535 sec Minimum pulse width 50 / FREQ Maximum frequency(square wave) FREQ / 100

Explanation
The COUNT instruction makes the Pin an input, then for the specified Duration, counts cycles on that pin and stores the total in Variable. A cycle is a change in state from 1 to 0 to 1, or from 0 to 1 to 0. Each loop in the COUNT routine requires 40 clock cycles, so the incoming signal must remain high or low for at least 40 cycles to be measured accurately. The maximum frequency that COUNT can accurately handle is about 1% the clock frequency driving the SX. A 20 MHz clock source, for example, would allow the COUNT instruction to count transitions in a 200 kHz square wave (or period of 5 µsecs).

If you use COUNT on slowly changing analog waveforms like sine waves, you may find that the value returned is higher than expected. This is because the waveform may pass through the SX’s 1.4-volt logic threshold slowly enough that noise causes false counts. You can fix this by enabling the SCHMITT trigger configuration on the pin used for COUNT.

Related instructions: PULSIN