beautypg.com

Remote Processing RPC-330 User Manual

Page 44

background image

MULTI-MODE COUNTER

SECTION 13

Page 13-2 RPC-330

short pulse is generated. The latch captures it captures
it. A latch is cleared using LINEB function.

500 A = LINEB(6,5) :REM clear counter 0 latch
510 A = LINEB(6,7) :REM clear counter 1 latch

Clear a latch befor e it is used as an inter rupt.

Program examples

This code resets counter 0 and enables its inputs. The
count is printed once a second. To see the count change,
momentarily bring " A0 IN" or "B0 IN" on P2 to ground.
When " B0 IN" is gr ounded, the count decrem ents.

10 LINEB 6,1,32
20 LINEB 6,1,64+8
30 ONTICK 1,500
40 GOTO 40
500 PRINT COUNT(0)
510 RETI

L i ne 2 0 c a n b e sh o r te n ed s om e w h at . 6 4 s e le c ts th e IC R
(Input contr ol register ) and 8 enab les inputs. 72 could
have been used.

The following program example returns a frequency.
I np u t s ig n al is at " A 0 I N " .

100 LINEB 6,1,32
110 LINEB 6,1,72 : REM enable inputs
120 ONTICK 1,500
130 IDLE
140 GOTO 130
500 A=COUNT(0) : REM get count
510 C=A-B : REM figure change from last time
520 PRINT "Frequency = ",A
530 B=A
530 RETI

The fir st frequen cy read will always be a bit off. This is
because of the time required to initialize ONTICK.
Subsequent readings are more accurate.

Accuracy is increased by stretching ONTICK to every
10 seconds. This is neces sary w hen higher accura cy is
needed.

Other factors affecting accurate readings in this program
include ser ial comm unications and ONIT R statem ent.

A problem w ith this routine is periodically, a large
negative number is returned. This is because the
multimode counter has rolled over. This is corrected by
periodica lly resetting the CN TR or transfer ring P R to
CN TR. Refer to the data sheet, Appendix A for counter
operating mo des.

The next program sets up the LS7166 to cause an
interr upt when a preset nu mber of counts (100 ) is
reached. H2[3-4] is jumpered to interrupt on a borrow.

10 LINEB 6,1,132
20 COUNT 0,100 : REM write to CNTR
30 LINEB 6,1,8 : REM transfer PR to CNTR
40 LINEB 6,1,72 : REM enable A/B counters
50 ONITR 0,500
100 PRINT COUNT(0) : REM print progress
110 GOTO 100

500 PRINT "In Interrupt"
510 RETI

Line 10 sets OCCR to divide by N. Line 50 enables
INT 0 interrupt. Line 100 prints the counter. W hen
pulses are applied to the A input, the count will go
down. W hen 100 pulses are detected at A input, the
message in line 500 is printed.

To use INT 1, jumper H3[3-4] and change the LINEB
address to 3. C hange references to count and INT to 1.

This next ex ample inte rfaces to a quadr ature en coder in
x1 mode. An interrupt is generated when the counter
crosses 0. Jum per H2[9-10] for IN T0 and H3[9-10] for
INT1. The program below is for counter 0. Set
variable B to 3 and C to 1 for counter 1 in line 5.

The cou nter is pr e-loaded to 15 0. W hen the enco der is
turned so the count goes down to 0, an interr upt is
generated.

5 B = 1 : C=0
10 LINEB6,B,32

: REM master counter reset

20 LINEB6,B,128

: REM set CYBW active

30 LINEB6,B,193

: REM enable quadrature x1 mode

40 LINEB6,B,72

: REM enable a/b counters

60 COUNTC,150

: REM load PR with 150

70 LINEB 6,B,8

: REM load PR into count reg

80 A=LINEB(6,B+4)

: REM reset interrupt latch

100 ONITR C,500

: REM set up interrupt

110 ONTICK .5,1000 : REM print out COUNT
120 GOTO 120

500 PRINT "Interrupt count",COUNT(C)
510 A = LINEB(6,B+4)

:REM Reset int. latch

520 RETI

1000 PRINT COUNT(C)
1010 RETI