0% found this document useful (0 votes)
55 views2 pages

Implementing The I C Bus: New Instructions For MCS-51 BASIC

The document describes adding new instructions to the MCS-51 BASIC programming language to enable communication over an I2C bus. The author implemented routines for sending/receiving bytes and generating start/stop conditions as new BASIC instructions. A sample program is provided to demonstrate communicating with a PCF8574 I2C component by writing and reading back values from 0-255. The source code for the new I2C instructions is available from the author to extend the functionality of MCS-51 BASIC.

Uploaded by

Radun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views2 pages

Implementing The I C Bus: New Instructions For MCS-51 BASIC

The document describes adding new instructions to the MCS-51 BASIC programming language to enable communication over an I2C bus. The author implemented routines for sending/receiving bytes and generating start/stop conditions as new BASIC instructions. A sample program is provided to demonstrate communicating with a PCF8574 I2C component by writing and reading back values from 0-255. The source code for the new I2C instructions is available from the author to extend the functionality of MCS-51 BASIC.

Uploaded by

Radun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

COMPUTER

Implementing
2
the I C bus
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 I2C bus.
Fortunately, it’s easy to add home-made instructions to this version of BASIC.

More than twelve years ago, when Intel’s develop- 10 REM *****************************************
ment engineers made their BASIC interpreter 20 REM * I2C-Communication with MCS-51-BASIC *
(which was actually intended for internal, experi- 30 REM * over Ports 1.5 (SCL) and 1.6 (SDA) *
40 REM * *
mental use) available to the general public in the 50 REM * (C) H.-J. Boehling 07.29.99 *
form of the mask-programmed 8052-AH-BASIC 60 REM * www.germany.net/teilnehmer/101.107378 *
microcontroller, the development of I2C communi- 70 REM * www.isis.de/members/~boehling *
80 REM *****************************************
cations was still in its infancy. It’s thus no surprise 90 REM
that no thought was given to instructions for con- 100 CLKL=0DFH : CLKH=20H : DATL=0BFH : DATH=40H
trolling an I2C bus. Of course, it is possible to pro- 110 REM +++ I2C Test ++++++++++++++++++++++++++++
120 REM This test sends out value 0 to 255 to a PCF8574 and read it
gram an I2C interface using the regular instruc- back
tions, since there is no minimum clock frequency 130 ADDR=040H : REM I2C address
specified for I2C communications. If you want to 140 FOR BYTEOUT=0 TO 255
150 GOSUB 270
address only a few I2C components, you can easily 160 IF (ACK.OR.OUT)>0 THEN 220
incorporate the routines Send Byte, Get Byte, Start 170 GOSUB 350
and Stop, as shown in Listing 1, into your own pro- 180 IF (ACK.OR.OUT)>0 THEN 220
190 PRINT ”Read back:”,BYTEIN
grams. These routines use port P1.6 as the serial 200 NEXT
data line (SDA) and port P1.5 as the serial clock line 210 GOTO 130
(SCL). The circuitry external to the microcontroller 220 REM +++ I2C Transmission error ++++++++++++++
230 GOSUB 810 : REM Stop condition
is limited to four components, as shown in Fig- 240 IF ACK>0 THEN PRINT ”ACK failed!”
ure 1. However, this solution does not have much 250 IF OUT>0 THEN PRINT ”Time out!”
to offer in terms of speed. 260 GOTO 150
270 REM *** Send Data to I2C ********************
Since its first release, MCS-51 BASIC has been 280 GOSUB 730 : REM Start condition
freed from the rigid shell of the original special 290 BOUT=ADDR.AND.0FEH : REM Set write mode
300 GOSUB 420 : REM Send address out
microcontroller. It is now used quite often in a vari- 310 BOUT=BYTEOUT
ety of different versions, including those employed 320 GOSUB 420 : REM Send byte out
in Elektor projects. It is almost unique among high- 330 GOSUB 810 : REM Stop condition
340 RETURN
level languages in the ease with which it can be 350 REM *** Read Data from I2C ******************
extended with supplementary instructions. The 360 GOSUB 730 : REM Start condition
author has thus implemented the previously 370 BOUT=ADDR.OR.1 : REM Set read mode
380 GOSUB 420 : REM Send address out
described routines as MCS-51 BASIC instructions, 390 GOSUB 580 : REM read byte in
with which a clock rate of around 40 kHz can be 400 GOSUB 810 : REM Stop condition
achieved. Using these four new instructions 410 RETURN
420 REM === I2C Send Byte =======================
(I2COUT, I2CGET, I2CSTART and I2CSTOP) is

86 Elektor Electronics 7-8/2000


COMPUTER
430 BIT=80H : WERT=0.5 child’s play, as can be seen from the program ‘I2C
440 FOR I=1 TO 8 Communication Test’ shown in Listing 2. This pro-
450 SDA=(BOUT.AND.BIT)*WERT : REM Set data to bit of byte gram demonstrates communication with a
460 BIT=BIT/2 : WERT=WERT+WERT : REM Set pointer to next bit
470 GOSUB 860 : REM Make clock low PCF8574 at address 40H. It writes the values 0
480 PT1=PORT1.AND.DATL : PORT1=PT1.OR.SDA : REM Send bit out through 255 to the I/O IC and reads them back via
490 GOSUB 890 : REM Make clock high the I2C bus.
500 NEXT
510 REM —- Get ACK ——————————————- The source listing of the supplementary instruc-
520 GOSUB 860 : REM Make clock low tions is available on diskette from Readers Services
530 GOSUB 950 : REM Make data high (order number 006006-1) or from the download area
540 GOSUB 890 : REM Make clock high
550 ACK=PORT1.AND.DATH : REM If data is high ACK failed of the Elektor Internet site (www.elektor-electron-
560 GOSUB 860 : REM Make clock low ics.co.uk). It must be assembled into a hex file
570 RETURN using a suitable cross-assembler. This can then be
580 REM === I2C Get Byte ========================
590 BYTEIN=0 : WERT=0.5 burned into a 16 kB EPROM, along with the origi-
600 FOR I=1 TO 8 nal BASIC interpreter. A recommended cross-
610 GOSUB 860 : REM Make clock low
620 GOSUB 950 : REM Make data high
assembler is the freeware program ASEM-51 from
630 GOSUB 890 : REM Make clock high W. W. Heinz. The EPROM can also be burned ‘on
640 SDA=(PORT1.AND.DATH)/WERT : REM Read data... board’, if the hardware of your MCS-51-BASIC sys-
650 BYTEIN=BYTEIN.OR.SDA : REM ...to bit of byte
660 WERT=WERT+WERT : REM Set pointer to next bit
tem allows this. For this, all you need is the pro-
670 NEXT gram EPROMMER.LIS. Both of these programs are
680 REM —- Do ACK ——————————————- available free of charge from the 80C32-BASIC con-
690 GOSUB 860 : REM Make clock low
700 GOSUB 920 : REM Make data low to set ACK oK trol computer Internet site www.isis.de/mem-
710 GOSUB 890 : REM Make clock high bers/~boehling. If you have absolutely no interest
720 RETURN in burning your own EPROM, you can obtain a
730 REM === I2C Start condition ================
740 OUT=0 : REM Reset time out counter ready-programmed version of the extended BASIC
750 GOSUB 950 : REM Make data high interpreter (in a 27C128 or 27C256) from our Read-
760 GOSUB 890 : REM Make clock high ers Services under order number 006505-1.
770 IF OUT=3 THEN 800 : REM Wait 3 times for clock and data
high (000024-1)
780 IF (PORT1.AND.60H)<>60H THEN OUT=OUT+1 : GOTO 770 Text (German original): R. Gerstendorf
790 GOSUB 920 : REM Make data low (start condition)
800 RETURN
810 REM === I2C Stop condition ================= Literature: I2C Bus, Elektor Electronics (Publishing),
820 GOSUB 920 : REM Make data low ISBN 0 905705 47 5
830 GOSUB 890 : REM Make clock high
840 GOSUB 950 : REM Make data high
850 RETURN 5V
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
3k3

910 RETURN
920 REM —- Set Port 1.6 (SDA) to Low ————— 1N4148
930 PT1=PORT1.AND.DATL : PORT1=PT1
940 RETURN P1.5
950 REM —- Set Port 1.6 (SDA) to High ————- 330Ω
P1.6
960 PT1=PORT1.OR.DATH : PORT1=PT1
970 RETURN

1N4148
Listing 1. I2C with MCS-51-BASIC. 000024 - 11

Figure 1. The external components needed for


Listing 2. Application example using the four new commands. an I2C interface.

10 REM ******************************* 170 PRINT B


20 REM * I2C Communication Test * 180 I2CSTOP
30 REM * (C) H.-J. Boehling 08.29.99 * 190 NEXT
40 REM ******************************* 200 REM ===== Wait for key ============
50 ADDR=40H 210 K=GET : IF K>0 THEN 210
60 FOR I=0 TO 255 220 PRINT ”Continue?”
70 PRINT I, 230 K=GET : IF K=0 THEN 230
80 REM ===== I2C Write ===============
240 GOTO 60
90 I2CSTART
100 IF DBY(18H)=0 I2CPUT (ADDR) ELSE 260 250 REM ===== I2C Error ===============
110 IF DBY(18H)=0 I2CPUT (I) ELSE 260 260 STATUS=DBY(18H)
120 I2CSTOP 270 FOR J=1 TO 3 : I2CSTOP : NEXT
130 REM ===== I2C Read ================ 280 IF STATUS.AND.2=2 THEN PRINT ”Time out error!”
140 I2CSTART 290 IF STATUS.AND.4=4 THEN PRINT ”Busy error!”
150 IF DBY(18H)=0 I2CPUT (ADDR.OR.1) ELSE 260 300 IF STATUS.AND.8=8 THEN PRINT ”No acknowlege error!”
160 IF DBY(18H)=0 I2CGET B ELSE 260 310 GOTO 90

7-8/2000 Elektor Electronics 87

You might also like