E007S86.PDF

(60 KB) Pobierz
000024-UK I2C implement.
COMP UTER
Implementing
New instructions for MCS-51 BASIC
Design by H.-J. Böhling
The popular but already somewhat outdated programming language MCS-
51 BASIC does not have any instructions for controlling an I 2 C bus.
Fortunately, it’s easy to add home-made instructions to this version of BASIC.
More than twelve years ago, when Intel’s develop-
ment engineers made their BASIC interpreter
(which was actually intended for internal, experi-
mental use) available to the general public in the
form of the mask-programmed 8052-AH-BASIC
microcontroller, the development of I 2 C communi-
cations was still in its infancy. It’s thus no surprise
that no thought was given to instructions for con-
trolling an I 2 C bus. Of course, it is possible to pro-
gram an I 2 C interface using the regular instruc-
tions, since there is no minimum clock frequency
specified for I 2 C communications. If you want to
address only a few I 2 C components, you can easily
incorporate the routines Send Byte, Get Byte, Start
and Stop, as shown in Listing 1, into your own pro-
grams. These routines use port P1.6 as the serial
data line (SDA) and port P1.5 as the serial clock line
(SCL). The circuitry external to the microcontroller
is limited to four components, as shown in Fig-
ure 1. However, this solution does not have much
to offer in terms of speed.
Since its first release, MCS-51 BASIC has been
freed from the rigid shell of the original special
microcontroller. It is now used quite often in a vari-
ety of different versions, including those employed
in Elektor projects. It is almost unique among high-
level languages in the ease with which it can be
extended with supplementary instructions. The
author has thus implemented the previously
described routines as MCS-51 BASIC instructions,
with which a clock rate of around 40 kHz can be
achieved. Using these four new instructions
(I2COUT, I2CGET, I2CSTART and I2CSTOP) is
10 REM *****************************************
20 REM * I2C-Communication with MCS-51-BASIC *
30 REM * over Ports 1.5 (SCL) and 1.6 (SDA) *
40 REM * *
50 REM * (C) H.-J. Boehling 07.29.99 *
60 REM * www.germany.net/teilnehmer/101.107378 *
70 REM * www.isis.de/members/~boehling *
80 REM *****************************************
90 REM
100 CLKL=0DFH : CLKH=20H : DATL=0BFH : DATH=40H
110 REM +++ I2C Test ++++++++++++++++++++++++++++
120 REM This test sends out value 0 to 255 to a PCF8574 and read it
back
130 ADDR=040H : REM I2C address
140 FOR BYTEOUT=0 TO 255
150 GOSUB 270
160 IF (ACK.OR.OUT)>0 THEN 220
170 GOSUB 350
180 IF (ACK.OR.OUT)>0 THEN 220
190 PRINT ”Read back:”,BYTEIN
200 NEXT
210 GOTO 130
220 REM +++ I2C Transmission error ++++++++++++++
230 GOSUB 810 : REM Stop condition
240 IF ACK>0 THEN PRINT ”ACK failed!”
250 IF OUT>0 THEN PRINT ”Time out!”
260 GOTO 150
270 REM *** Send Data to I2C ********************
280 GOSUB 730 : REM Start condition
290 BOUT=ADDR.AND.0FEH : REM Set write mode
300 GOSUB 420 : REM Send address out
310 BOUT=BYTEOUT
320 GOSUB 420 : REM Send byte out
330 GOSUB 810 : REM Stop condition
340 RETURN
350 REM *** Read Data from I2C ******************
360 GOSUB 730 : REM Start condition
370 BOUT=ADDR.OR.1 : REM Set read mode
380 GOSUB 420 : REM Send address out
390 GOSUB 580 : REM read byte in
400 GOSUB 810 : REM Stop condition
410 RETURN
420 REM === I2C Send Byte =======================
86
Elektor Electronics
7-8/2000
the I 2 C bus
138910399.001.png
COMP UTER
430 BIT=80H : WERT=0.5
440 FOR I=1 TO 8
450 SDA=(BOUT.AND.BIT)*WERT : REM Set data to bit of byte
460 BIT=BIT/2 : WERT=WERT+WERT : REM Set pointer to next bit
470 GOSUB 860 : REM Make clock low
480 PT1=PORT1.AND.DATL : PORT1=PT1.OR.SDA : REM Send bit out
490 GOSUB 890 : REM Make clock high
500 NEXT
510 REM —- Get ACK ——————————————-
520 GOSUB 860 : REM Make clock low
530 GOSUB 950 : REM Make data high
540 GOSUB 890 : REM Make clock high
550 ACK=PORT1.AND.DATH : REM If data is high ACK failed
560 GOSUB 860 : REM Make clock low
570 RETURN
580 REM === I2C Get Byte ========================
590 BYTEIN=0 : WERT=0.5
600 FOR I=1 TO 8
610 GOSUB 860 : REM Make clock low
620 GOSUB 950 : REM Make data high
630 GOSUB 890 : REM Make clock high
640 SDA=(PORT1.AND.DATH)/WERT : REM Read data...
650 BYTEIN=BYTEIN.OR.SDA : REM ...to bit of byte
660 WERT=WERT+WERT : REM Set pointer to next bit
670 NEXT
680 REM —- Do ACK ——————————————-
690 GOSUB 860 : REM Make clock low
700 GOSUB 920 : REM Make data low to set ACK oK
710 GOSUB 890 : REM Make clock high
720 RETURN
730 REM === I2C Start condition ================
740 OUT=0 : REM Reset time out counter
750 GOSUB 950 : REM Make data high
760 GOSUB 890 : REM Make clock high
770 IF OUT=3 THEN 800 : REM Wait 3 times for clock and data
high
780 IF (PORT1.AND.60H)<>60H THEN OUT=OUT+1 : GOTO 770
790 GOSUB 920 : REM Make data low (start condition)
800 RETURN
810 REM === I2C Stop condition =================
820 GOSUB 920 : REM Make data low
830 GOSUB 890 : REM Make clock high
840 GOSUB 950 : REM Make data high
850 RETURN
860 REM —- Set Port 1.5 (SCL) to Low —————
870 PT1=PORT1.AND.CLKL : PORT1=PT1
880 RETURN
890 REM —- Set Port 1.5 (SCL) to High ————-
900 PT1=PORT1.OR.CLKH : PORT1=PT1
910 RETURN
920 REM —- Set Port 1.6 (SDA) to Low —————
930 PT1=PORT1.AND.DATL : PORT1=PT1
940 RETURN
950 REM —- Set Port 1.6 (SDA) to High ————-
960 PT1=PORT1.OR.DATH : PORT1=PT1
970 RETURN
child’s play, as can be seen from the program ‘I2C
Communication Test’ shown in Listing 2. This pro-
gram demonstrates communication with a
PCF8574 at address 40H. It writes the values 0
through 255 to the I/O IC and reads them back via
the I 2 C bus.
The source listing of the supplementary instruc-
tions is available on diskette from Readers Services
(order number 006006-1) or from the download area
of the Elektor Internet site (www.elektor-electron-
ics.co.uk). It must be assembled into a hex file
using a suitable cross-assembler. This can then be
burned into a 16 kB EPROM, along with the origi-
nal BASIC interpreter. A recommended cross-
assembler is the freeware program ASEM-51 from
W. W. Heinz. The EPROM can also be burned ‘on
board’, if the hardware of your MCS-51-BASIC sys-
tem allows this. For this, all you need is the pro-
gram EPROMMER.LIS. Both of these programs are
available free of charge from the 80C32-BASIC con-
trol computer Internet site www.isis.de/mem-
bers/~boehling . If you have absolutely no interest
in burning your own EPROM, you can obtain a
ready-programmed version of the extended BASIC
interpreter (in a 27C128 or 27C256) from our Read-
ers Services under order number 006505-1.
(000024-1)
Text (German original): R. Gerstendorf
Literature: I 2 C Bus, Elektor Electronics (Publishing),
ISBN 0 905705 47 5
5V
1N4148
P1.5
P1.6
330
1N4148
Listing 1. I 2 C with MCS-51-BASIC.
000024 - 11
Listing 2. Application example using the four new commands.
Figure 1. The external components needed for
an I 2 C interface.
10 REM *******************************
20 REM * I2C Communication Test *
30 REM * (C) H.-J. Boehling 08.29.99 *
40 REM *******************************
50 ADDR=40H
60 FOR I=0 TO 255
70 PRINT I,
80 REM ===== I2C Write ===============
90 I2CSTART
100 IF DBY(18H)=0 I2CPUT (ADDR) ELSE 260
110 IF DBY(18H)=0 I2CPUT (I) ELSE 260
120 I2CSTOP
130 REM ===== I2C Read ================
140 I2CSTART
150 IF DBY(18H)=0 I2CPUT (ADDR.OR.1) ELSE 260
160 IF DBY(18H)=0 I2CGET B ELSE 260
170 PRINT B
180 I2CSTOP
190 NEXT
200 REM ===== Wait for key ============
210 K=GET : IF K>0 THEN 210
220 PRINT ”Continue?”
230 K=GET : IF K=0 THEN 230
240 GOTO 60
250 REM ===== I2C Error ===============
260 STATUS=DBY(18H)
270 FOR J=1 TO 3 : I2CSTOP : NEXT
280 IF STATUS.AND.2=2 THEN PRINT ”Time out error!”
290 IF STATUS.AND.4=4 THEN PRINT ”Busy error!”
300 IF STATUS.AND.8=8 THEN PRINT ”No acknowlege error!”
310 GOTO 90
7-8/2000
Elektor Electronics
87
138910399.002.png
 
Zgłoś jeśli naruszono regulamin