Implementing The I C Bus: New Instructions For MCS-51 BASIC
Implementing The I C Bus: New Instructions For MCS-51 BASIC
Implementing
2
the I C bus
New instructions for MCS-51 BASIC
Design by H.-J. Böhling
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
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