| Home |
Last Modified: 21 November 2000 |
6172 CAN Micro Controller Module AN010
This application example is not yet completely tested
4 x 4 keyboard matrix
This application example enables the user to input 16 keys (switches) in a 4 x 4 matrix.
This example can be combined with AN009 to create a keyboard + LCD display module for CANopen.
header "4 x 4 keyboard matrix AN010"
keybrd: equ 0 ;keyboard matrix memory location (2 bytes)
row1: equ 0 ;row 1 select bit (OUT9)
row2: equ 1 ;row 2 select bit (OUT10)
row3: equ 2 ;row 3 select bit (OUT11)
row4: equ 3 ;row 4 select bit (OUT12)
temp: equ 63 ;scratchpad memory
;initialization sequence
sequence seq_0
ldwc 0 ;clear all 4 row outputs
stwm PORTB
srdy ;READY output active
enasq 0 ;enable sequences
endsq
;read all 4 keyboard rows to memory
sequence seq_3,5 ;execute this sequence every 50 mS
maskc %00001111 ;read only lower 4 bits of port C
setb row1 ;activate keyboard row 1
delay 5 ;some delay before reading it...
inpc temp ;read keyboard row 1 to temp
resb row1
setb row2 ;now activate keyboard row 2
delay 5 ;some delay before reading it...
ldwm PORTC ;read keyboard row 2 in W
resb row2
setb row3 ;now activate keyboard row 3
shlc 4 ;row 2 into upper 4 bits
orwm temp ;add row 1
stwm keybrd ;result to memory
inpc temp ;read keyboard row 3 to temp
resb row3
setb row4 ;now activate keyboard row 4
delay 5 ;some delay before reading it...
ldwm PORTC ;read keyboard row 4
resb row4
shlc 4 ;row 4 into upper 4 bits
orwm temp ;add row 3
stwm keybrd+1 ;result to memory
endsq
;this sequence executes in about 230 µS
end
Assuming that all 6172 objects are in their default state, the following objects need to be adjusted (if a PDO is used):
|
1 |
Subindex |
Value |
Description |
|---|---|---|---|
|
PDO mapping of transmit PDO 1 |
|||
|
1A00h |
0 |
2 |
2 objects in transmit PDO 1 |
|
1A00h |
1 |
60000108h |
memory location 0 |
|
1A00 |
2 |
60000208h |
memory location 2 |
|
Enable PDO transmission on any change of memory location 0-1 |
|||
|
6006 |
1 |
255 |
memory location 0 |
|
6006 |
2 |
255 |
memory location 1 |
The keyboard now can be read by:
Copy and Paste this part direct after the declarations in the
canappl.bas program
this program can be found on the 6197 designers kit diskette or
ordered separately as service part together with one of our CAN products.
APPLICATION:
'This simple program is made for one 6172 and a 6390 as CAN bus interface
'the following assumptions are done:
'- 6390 is connected to COM2
'- 6172 ID = 1, CANbus speed = 50 kBAUD
'- assembled sequence file is "AN010.seq"
'The keyboard is read using the PDO, if the PDO isn't received
'within 5 seconds, it is requested.
'The results are displayed as one line with a 16 bit binary string
'this continues until the user presses enter
CLS
PRINT "CD Systems 6172 AN010 DEMO program"
PRINT
Activate6390 2, 19200 '6390 works at COM2, 19200 BAUD
SetCANBUSSpeed 50 'CAN BUS at 50 kBAUD
C% = CheckSequence%(1, "AN005.seq") 'download sequence if not already done
C% = PutU8% (1, &h1A00, 0, 2) '2 objects in transmit PDO1
C% = PutU32% (1, &h1A00, 1, &h60000108) 'transmit PDO1 from memory 0
C% = PutU32% (1, &h1A00, 2, &h60000208) 'transmit PDO1 from memory 1
C% = PutU8% (1, &h6006, 1, 255) 'enable transmit PDO on any change
C% = PutU8% (1, &h6006, 2, 255)
SendNMTCommand (1, 1) 'node in operational state
BT = TIMER - 6 'force immediate request
PDOCOB% = (1 + 384) * 32 'tx PDO1 COB ID shifted left 5 bits
DO
' send a transmit PDO1 remote request if nothing received after 5 seconds
IF (BT + 5) < TIMER THEN
SendToCAN (CANIDS$ (PDOCOB% + &h10))
BT = TIMER
END IF
' check if the transmit PDO is received
R$ = ReceiveFromCAN$
IF LEN (R$) > 1 THEN 'anything received?
IF (CANIDR% (R$) AND &hFFE0) = PDOCOB% THEN 'PDO1 tx received?
LOCATE 10,1
FOR I% = 3 TO 4: M% = 1 '2 bytes in PDO
FOR B% = 1 TO 8 '8 keys in a byte
IF (ASC(MID$(R$,I%)) AND M%) THEN PRINT "1"; ELSE PRINT "0";
M% = M% * 2
NEXT B%
NEXT I%
BT = TIMER
END IF
END IF
LOOP UNTIL INKEY$ = CHR$(13)
SendNMTCommand (1, 2) 'stop node
DO 'clear & ignore any received message
R$ = ReceiveFromCAN$
LOOP UNTIL R$ = ""
END