Chapter 14 multi-mode counter – Remote Processing RPC-2350 User Manual

Page 60

Advertising
background image

CHAPTER 14

MULTI-MODE COUNTER

14-5

inputs to count
130 on tick 0,1 gosub 1000
140 c = 1

:'initialize loop counter

200 bit 0,0,0
210 bit 0,0,1

:'rising edge increments

count
220 a=count(8)
230 if a <> c then print "Count error"
: end
240 inc c
250 goto 200
1000 print count(8)

:'show current

count
1010 return

The following example returns a frequency. Input signal
is at J2-9 (A inpu t)

10 OUT &F1,32

:’RESET COUNTER

20 OUT &F1,72

:’ENABLE INPUTS

30 ON TICK 0,1 GOSUB 1000

40 GOTO 40

:’HANG OUT HERE

1000 A = COUNT(8)

:’GET COUNT

1010 C = A-B

:’FIGURE CHANGE FROM

LAST TIME
1020 PRINT “Frequency = “;a
1030 B = A
1040 RETURN

The first frequency read w ill be off, due to initialization.
Accuracy is increased by stretching reading to every 10
seconds. Other factors affecting accuracy include serial
communications and other interrupt processing.

The counter will not miss counts. Due to interrupt
latency, some co unts will be lar ger than o thers. It is
several counts off at about 8 kHz. If you average the
counts it will be accurate.

Another problem w ith this routine is periodically, a large
negative number is returned. This is because the counter
has rolled o ver. This is cor rected b y periodic ally
resetting the CNTR.

Program C NTR2. BAS sets up the LS7166 to cause an
interrupt when a preset number of counts is reached.
W7[2-3] is jumpered to interrupt on a borrow. To
reload the count, bring the LDCTR line (J10-6) low.
When the count is 0 again, another in terru pt is
generated. Y ou can also count up provided you bring
counter line ‘B’ (J10-8) low while coun ter line ‘A ’ is
high.

CNT R3.BAS interfaces to a quadratur e encoder in x1
mode. The counter is pre-loaded to 100.

NOTE: See CAMBASIC resolution limit below.

CAM BASIC resolution lim it
CAM BASIC stores num bers to 7 dig its + exponent.
The counter outputs numbers to 8 digits. This means
that when the counter counts down from 0 to 1677215,
CAM BASIC will store it as 1 .67 721E+ 7. T he last digit
is dropped.

You can compensate for this easily by introducing an
offset. Preload the counter to some number, say
100,000. This becomes the zero point. W hen the count
is below this number, the counter is in “negative”
territory. See CNTR4.BAS

Program CNT R5.BAS r eads the counter in Basic (not
using CO UN T(8) and prints the va lue in hex for mat.
This routine can be useful when an external device
triggers the LCT R line (J10-6) to transfer the count to a
latch. The count at that time can be read.

MEASURING PULSE WIDTH

You can measure pulse widths with 217 nano-Second
precision. W idths can be as long as 3.64 Seconds using
the counter input at J10.

There are lim itations to measuring pulse widths. Below
lists the major ones.

The pulse repetition r ate must be slower than the time it
takes CAM BASIC to respond to it. As a guide, the
pulse repetition rate should be less than 100 Hz.
Meas uring a 50 micro -second signa l every se cond is
easy. Measu ring a 500 micro -second signa l every m illi-
second is difficult, if not impossible.

Only logic low pulses ar e measur ed. If a high pu lse
width is desired, invert the input signal. See figure
below.

Advertising