e001046.pdf

(302 KB) Pobierz
138910685 UNPDF
MICROPROCESSORS
network (CAN) (5)
Part 5:
a CAN interface using BASIC537
If you are new to the
CAN bus, look for-
ward to some hard
work before seeing
the first usable
results. After all, at
least two microcon-
troller systems have
to be hooked up to
CAN bus controllers,
and a bus-based data link implemented based on the use of talk/listen
programs. Once you have data travelling over the bus, all further expan-
sion is really simple. This article attempts to make your first practical
experiments with the CAN bus as easy as possible.
Hardware & software design
by B. Kainka
The CAN bus interface described in
Elektor Electronics November 1999 may
be controlled using the BASIC537
higher programming language.
BASIC537 is an EPROM version of the
well-known Intel MCS51 BASIC, spe-
cially adapted and extended for the
80C537 microcontroller. Many of you
will be familiar with MCS51 BASIC
because it was the subject of several
articles in Elektor Electronics . Originally,
this BASIC interpreter was developed
for the (now obsolete) 8052AH-BASIC
microcontroller. When stored in an
external (E)PROM, however, it is also
great for other controllers from Intel’s
80xx series and second sources (see
also the 80C32 BASIC Control Com-
puter described in Elektor Electronics
February and March 1998). The 537
‘Lite’ Computer is described elsewhere
in this magazine — it too is capable of
running BASIC537. The ancestor of this
computer, a full-blown 80C357 micro-
controller system, was published in our
June 1997 magazine.
The new, smaller and cheaper 537
‘Lite’ Computer is employed here in
46
Elektor Electronics
1/2000
controller area
138910685.024.png
combination with the CAN bus inter-
face. The 537 ‘Lite’ is prominently fea-
tured on this month’s cover.
1
K5
K6
PFI
PFO
RESET
RESET
ALE
5V
INT4
INT3
INT6
INT5
INT2
T2EX
CLKO
T2
T1
T0
INT1
INT0
VAREFF
H ARDWARE
An adaptor board was developed to
implement a simple connection
between the 537 ‘Lite’ Computer (fitted
with a BASIC537 EPROM) and the
CAN interface board. The circuit dia-
gram of the adaptor is given in Fig-
ure 1 , the copper track layout and com-
ponent mounting plan, in Figure 2 . As
you can see on the photograph, there
are no wiring problems because the
537 ‘Lite’ board is plugged directly on
to the adaptor board. The link with the
CAN interface is then made using a
length of flatcable (see introductory
photograph).
To solve the power supply problem
in a simple way, a 5-volt voltage regu-
lator and supply reversal protection are
accommodated on the adaptor board.
In this way, the adaptor board can sup-
ply +5 V to the two other boards. The
upshot is that any low-cost wall adaptor
supplying 9-12 volts unregulated at
about 300 mA may be connected to
PCB connector K2. If you already have
a stabilized 5-volt line available, you
may omit IC1, D1, C1 and C2 from the
adaptor board, and
connect the 5-V supply
voltage directly to the
K1 terminals on the
adaptor board.
To keep cost down,
the adaptor board is
much smaller than the
537 ‘Lite’ Computer board to be
plugged on it. If you cut the adaptor
board in two along the line indicated
on the PCB overlay, and fit the two
sub-boards at the right distance above a
carrier (e.g. aluminium sheet), the 537
‘Lite’ Computer is easily plugged on to
this assembly. The only wire connec-
tion to make (if necessary) is INT2
between pin 12 (K3) and pin 32 (K6)
(see the photograph showing the 537
‘Lite’ Computer board with the two
adaptor sub-boards). The two adaptor
sub-boards have connecting pins for
the INT2 link, which is also shown as a
wire link on the component mounting
overlay (Figure 2).
5V
K4
K3
VOUT
VBATT
D7
1
2
D6
3
4
WR
RD
CS2
D5
5
6
D4
7
8
D3
9
10
ALE
INT2
D2
11
12
D1
13
14
RESET
D0
15
16
A7
A6
A5
A4
A3
A2
A1
A0
A15
A14
A13
A12
A11
A10
A9
A8
P47
P46
P45
P44
P43
K1
P42
+5V
P41
P40
P67
P66
P65
IC1
P63
D1
1N4001
P64
7805
5V
P60
K2
P50
P51
P52
P53
P54
P55
P56
P57
PSEN
WR
RD
CS0
CS1
CS2
+9V
C1
C2
C3
C4
10µ
16V
100n
10µ
16V
100n
990066 - 4 - 14
Figure 1. Circuit diagram of an
adaptor board that creates a
simple link between the 537
‘Lite’ Computer board and the
CAN bus interface.
The essential
settings are
supplied as
defaults by the
program. The
communication runs at 20 kbits/s. Mes-
sage are sent without the RTR bit — in
other words, no reply is requested. The
two systems should fulfil the following
functions:
noted between the termination resis-
tors being present or not.
T RANSMIT PROGRAM
AND TEST
The transmit program for Controller 1 is
given in Listing 1 . The CAN controller
SJA1000 is addressed by the 80537 sys-
tem via base address 0F000h. The
address range is defined in line 95
(BA=0F000h). If you use a different
system, all you have to do (initially) is
modify BA accordingly. The initialisa-
tion is carried out as described in the
article on the CAN bus interface hard-
ware. In lines 110 and 200, the results
of the register programming are
requested. The program then waits for
a register bit to go to a specific state. In
case the controller is not found on the
bus, or does not function correctly, the
program will ‘hang’ at this point. If
everything is successful, however, you
are greeted with these messages:
System 1 regularly sends messages
with Identifier 300 and containing
eight bytes. The data originates from
the first eight channels of the A-D con-
verter. The system continuously per-
forms measurements on eight ana-
logue inputs. The messages it transmits
may be received and processed by any
other system connected to the bus.
System 2 receives all messages on the
bus and copies them to the PC by way
of the RS232 interface. In fact, this is a
simple CAN monitor that allows you to
pick up and examine all data traffic on
the CAN bus.
C ONTROL IT ALL IN
BASIC
In essence, all you need to be able to
control the CAN interface board is a
program that looks after a bank of reg-
isters starting at address F000h in the
CAN controller SJA1000. The XBY
operator is employed for all access to
addresses in the external RAM area
and peripherals.
To make the introduction as easy
going as possible, the following para-
graphs describe the simplest case of a
data link between two 80C537 systems.
Reset OK
Init OK
A block diagram of the system config-
uration is given in Figure 3 . A special
cable is not required to link the two
systems. Our first experiments in the
lab indicated that a simple two-wire
link between pins 4 and 8 of the CAN
plugs is adequate if the total length
does not exceed about 1 metre. With
such a short cable, no difference was
To start with, it is sufficient to execute
the initialisation routine up to line 200.
An initial check may be made by look-
ing for a rectangular signal at the test
pin on the controller board. Whereas
this pin supplies 8 MHz before the ini-
tialisation, you will then find 2 MHz. If
this is okay so far, you may safely
Elektor Electronics
1/2000
47
138910685.025.png
K5
K4
K6
COMPONENTS LIST
H6
K3
Capacitors:
C1, C3 = 10 µ F 16V (radial)
C2, C4 = 100nF (ceramic)
Semiconductors:
D1 = 1N4001
IC1 = 7805
K1
Miscellaneous:
K1 = 2-way PCB terminal block,
raster 5mm
K2 = 2-way PCB terminal block,
raster 5mm
K3 = boxheader, straight, 16 pins
K4 = pin header, 1 row, 4 pins
K5, K6 = pin header, one row,
straight, 35 pins
C4
IC1
C3
C1
C2
K2
H8
Figure 2. Layout and com-
ponent mounting plan of
the adaptor board.
assume that the controller is driven
with the proper signals.
Now the complete program may be
loaded and executed. Experienced as
you are, you will no doubt have an
oscilloscope ready to observe data traf-
fic on the bus. Without a connection
having been made to a second system,
you will be able to find signals on the
data lines. After a hardware reset and
without an initialisation you will not be
able to find the ‘inactive’ level of 2.5 V
s long, which means that
the transmission rate is indeed
20 kBits/s. However, you will not fail to
see that there is a quasi-steady datas-
tream with 2-ms pauses, rather than
short data packets as would be
expected. Not to worry, however, this
is the normal behaviour of the con-
µ
Figure 3. Block diagram
showing how the CAN bus
is linked to the two 80537
systems programmed in
537 BASIC.
troller as long as it has not detected any
intelligence on the CAN bus. Yet, it is
not sufficient to connect the second
controller via the two-wire link,
because this, like system 1, has to be
initialised. By the way, the transmitting
station will continue to send a quasi-
continuous datastream even if the
BASIC program is terminated.
3
A T THE
RECEIVER SIDE
Now we are ready to ‘deploy’ the
receiver program as given in Listing 2 .
This program is for system number 2.
As you can see from the listing, the ini-
tialisation is the same as that used for
the transmitter. As soon as the initiali-
sation is done and the message “Init
OK” has appeared on the PC display,
the transmitting controller will com-
48
Elektor Electronics
1/2000
on the two wires. As soon as the trans-
mit program is started, data is easily
recognized as rectangular signals with
a level of 1 V. The shortest logic levels
are just 50
138910685.026.png 138910685.027.png 138910685.001.png 138910685.002.png 138910685.003.png 138910685.004.png 138910685.005.png 138910685.006.png 138910685.007.png 138910685.008.png 138910685.009.png 138910685.010.png 138910685.011.png 138910685.012.png 138910685.013.png 138910685.014.png 138910685.015.png 138910685.016.png 138910685.017.png 138910685.018.png 138910685.019.png
mence its normal operation. From then
on, short data packets with a length of
just over 5 ms will start to appear on
the CAN bus. Finally, the CAN bus
functions as you, the interested reader,
would like to see it: data packets being
sent back and forth over the bus with-
out any indication of their being read
anywhere at all!
The actual receiver program starts
at line 500 and waits for a message
which is announced by controller sta-
tus bit zero. As soon as a data packet
has arrived, the program may read ten
bytes from the controller. The first two
contain the message ID. It is recovered
from two bytes in line 570 and then
sent to the display. As expected, it is the
‘ID’, 300, which was arbitrarily defined
in the transmitter program.
Listing 1. Transmitter program CAN1.BAS.
90 REM Init CAN Controller
95 BA=0F000H
100 XBY(BA+00H)=01H : REM Reset Mode
110 IF (XBY(BA+00H).AND.1)<>1 THEN GOTO 110
111 PRINT ”Reset OK”
120 XBY(BA+1FH)=43H : REM CDR, 2 MHz
130 XBY(BA+04H)=0 : REM ACR
140 XBY(BA+05H)=0FFH : REM AMR, Acceptance Mask, all
150 XBY(BA+06H)=53H : REM BTR0, 20 Kbit/s*
160 XBY(BA+07H)=2FH : REM BTR1
170 XBY(BA+08H)=1AH : REM OCR;
180 XBY(BA+01H)=0EH : REM CMR, end sleep mode
190 XBY(BA+00H)=0 : REM CR, end reset mode
200 IF (XBY(BA+00H).AND.1)>0 THEN GOTO 200
201 PRINT ”init ok”
500 REM ************* Main Loop ***************
501 REM Send 8 Bytes of AD-Data in message 300
510 FOR N=0 TO 7
520 XBY(BA+0CH+N)=AD(N) : REM fill TB1..TB8
530 NEXT N
540 ID=300 : REM Message Identifier
550 DFL=8 : REM 8 Bytes
560 GOSUB 1000 : REM Send Massage
570 FOR T=1 TO 1000 : NEXT T
580 GOTO 500
1000 REM ************* Send CAN Telegram *************
1010 IF (XBY(BA+02H).AND.4)=0 THEN GOTO 1010 : REM SR
1020 XBY(BA+0AH)=INT(ID/8) : REM IDT1
1030 XBY(BA+0BH)=(ID-8*INT(ID/8))*32+DFL : REM IDT2
1040 XBY(BA+01H)=0DH : REM CMR, start transmission
1050 RETURN
Listing 2. Receiver program CAN2.BAS
The user data are read in a loop and
displayed in line 610. There , you
(finally…) get the measured values on
the eight analogue inputs of the first
controller system. Figure 4 shows the
received data in a terminal window.
90 REM Init CAN Controller
95 BA=0F000H
100 XBY(BA+00 H)=01H : REM Reset Mode
110 IF (XBY(BA+00H).AND.1)<>1 THEN GOTO 110
111 PRINT ”Reset OK”
120 XBY(BA+1FH)=43H : REM CDR, 2 MHz
130 XBY(BA+04H)=0 : REM ACR
140 XBY(BA+05H)=0FFH : REM AMR, Acceptance Mask, all
150 XBY(BA+06H)=53H : REM BTR0, 20 Kbit/s*
160 XBY(BA+07H)=2FH : REM BTR1
170 XBY(BA+08H)=1AH : REM OCR;
180 XBY(BA+01H)=0EH : REM CMR, end sleep mode
190 XBY(BA+00H)=0 : REM CR, end reset mode
200 IF (XBY(BA+00H).AND.1)>0 THEN GOTO 200
201 PRINT ”Init OK”
500 REM ******* Receiver Main Loop *************
510 SR=XBY(BA+02H) : REM Status Register
520 REM Error Detection and Clear Data Overrun
530 if (SR .AND. 2) = 2 then XBY(BA+01H)=8: :Goto 510
540 REM Get Receive Status
550 if (SR .AND. 1) =0 then goto 510
560 REM Read received message
570 ID=XBY(BA+14H)*8+INT(XBY(BA+15H)/32) : PRINT ID
580 DFL=XBY(BA+15H).AND.15 : rem Data Length
590 RTR=(XBY(0FE15H).AND.16)/16 : REM RTR not used
600 FOR N=0 To 7
610 PRINT N ,XBY(BA+16H+N)
620 NEXT N
630 XBY(BA+01H)=0CH : REM Release Receive Buffer
640 GOTO 510
F INALLY :
THREE ON THE BUS
Of course, the results up to now could
have been obtained with a rather sim-
pler RS232 interface. The CAN bus
however will typically not unleash its
power until more than two devices are
connected to the bus. To end the
‘lonely’ existence of the two systems
discussed so far, a third ‘CANable’
device should be added. The program
CAN3.BAS shown in Listing 3 (with-
out initialisation!) performs the follow-
ing functions:
Elektor Electronics
1/2000
49
138910685.020.png
Listing 3. Receiver & Transmitter program CAN3.BAS
without initialisation.
650 WRSFR 0E8H,PORT : REM Port 4 Output
660 XBY(BA+01H)=0CH : REM Release Receive Buffer
800 REM ******** Send AD-Data ***********
810 FOR N=0 TO 7
820 XBY(BA+0CH+N)=AD(N) : REM fill TB1..TB8
830 NEXT N
840 ID=500 : REM Message Identifier
850 DFL=8 : REM 8 Bytes
860 GOSUB 1000 : REM Send Message
870 FOR T=1 TO 1000 : NEXT T
880 GOTO 500
1000 REM ******* Send CAN Telegram *************
1010 IF (XBY(BA+02H).AND.4)=0 THEN GOTO 1010 : REM SR
1020 XBY(BA+0AH)=INT(ID/8) : REM IDT1
1030 XBY(BA+0BH)=(ID-8*INT(ID/8))*32+DFL : REM IDT2
1040 XBY(BA+01H)=0DH : REM CMR, Start Transmission
1050 RETURN
500 REM ************ Main Loop ***************
505 REM ************ Receiver ****************
510 SR=XBY(BA+02H) : REM Status Register
520 REM Error Detection and Clear Data Overrun
530 IF (SR.AND.2)=2 THEN XBY(BA+01H)=8 : GOTO 510
550 IF (SR.AND.1)=0 THEN GOTO 510
560 REM Read received message
570 ID=XBY(BA+14H)*8+INT(XBY(BA+15H)/32): Print ID
580 DFL=XBY(BA+15H).AND.15 : REM Data Length
590 RTR=(XBY(0FE15H).AND.16)/16 : REM RTR not used
600 IF ID<>300 THEN GOTO 660
610 PORT=0
620 IF XBY(BA+16H+0)>100 THEN PORT=PORT+1
630 IF XBY(BA+16H+1)>100 THEN PORT=PORT+2
640 IF XBY(BA+16H+2)>100 THEN PORT=PORT+4
Figure 4. Received
data in the terminal
window of BASIC537.
It receives all messages but only
processes the ones with the ID ‘300’.
The first three transmitted measure-
ment values are compared with certain
extreme values and switch on three
lines on Port 4 if a particular extreme is
exceeded.
After processing of the received
message, a message with the ID ‘500’ is
returned, where all A-D channels are
measured and transmitted again. As
soon as the third system is connected
to the bus, System 2 will also supply
data with ID ‘500’ to the terminal (see
Figure 5 ).
990066-4
Note: the three program listings dis-
cussed in this article are available for
downloading from the Elektor Elec-
tronics website at www.elektor-electron-
ics.co.uk
Article editing (German original): E. Krempelsauer
Design editing: K. Walraven
Figure 5. Reception of
messages with Identi-
fiers (IDs) ‘300’ and
‘500’.
50
Elektor Electronics
1/2000
138910685.021.png 138910685.022.png 138910685.023.png
Zgłoś jeśli naruszono regulamin