

_____________________________________________________________________________


          An  I2C  Interface  for  the  HP48 Calculator



                             A.  R.  Duell
______________________________________________________________________________



1) Introduction


At the Aniversary Conference 5 years ago, Wlodek Mier-Jedrzejowicz
mentioned the lack of HPIL on the HP48 family, and thus the inab-
ility to link this machine to the real world (`The HPCC44T Project
- Where Next', page 60).

    Many early machines had this capability. On the rear of Hewlett-
Packard's first calculator, the HP9100A, was an edge connector that was
intended to link up standard peripherals, like a printer or a card reader.
The interface was, however documented by HP (I am looking for a copy of
this specification if anyone has it), and some users soon realised that
this desktop calculator could be used to control real-world systems. 

    A similar interface, added specifically for real-world control, was
present  on  a  variant  of  the  HP-97  printing  calculator  called  the
HP-97S, while the later HP-41, HP-71 and HP-75 families all sup-
ported the HP-IL standard, for which Hewlett-Packard manufac-
tured a pair of general-purpose parallel interfaces - the HP82165
for stand-alone applications and the HP82166 for building-in to cus-
tom products.  Again, these machines were used for simple control
tasks.

    Unfortunately,  no similar interface exists for the current top-
of-the-line calculator, the HP48 family.  This machine only has a
standard 3-line (Transmit Data, Receive Data and Ground) RS232
interface, which although useful for connecting the calculator to a
desktop (or larger!)  computer and transferring programs or data,
is of little use for real-world control.

    The solution comes in the form of a rather different serial in-
terface.   In  order  to  simplify  the  wiring  of  consumer  electronics
products such as television sets and video cassette recorders, Philips
have designed a family of chips that communicate using a 3-wire
(Data, Clock and Ground) interface known as I2C - Inter IC Com-
munication.  Although many of these devices are extremely spe-
cialised, there are also general-purpose devices, including an 8-bit
parallel  I/O  port  (the  PCF8574),  a  4-channel  8-bit  analogue  to
digital converter (the PCF8591), an 8-channel 6-bit digital to ana-
logue converter (the TDA8444), and even a complete video display
system.

    Therefore, an interface between the HP48 and the I2C bus would
allow this calculator to control all these IC's, and thus provide a
replacement for the HP-IL interface.

    In  a  short  document  such  as  this,  it  is  only  possible  to  give
a brief overview of the design,  and people who are interested in
constructing one should obtain the full manual which contains full
schematics of the interface and sample I2C devices, a source listing
for the PIC microcontroller program, and examples of its use. This
document is available from HPCC, and also should soon be available
on the Internet.

2)  An  overview  of  the  system

A block diagram of the interface is shown in figure 1.  The HP48's
RS232 interface is linked using the standard cable to the new inter-
face unit while the `other side' of this unit is connected to the I2C
devices.
                                  ____________           _________________
                       RS232      |          |  I2C      |               |
             _____________________|          |___________|               |
             |                    |          |           |               |
             |                    |          |           |               |
_____________|_______________     |          |           |               |
|                           |     |    I2C   |           |   I2C         |
|  ______________________   |     |          |           |               |
|  |                    |   |     | Interface|           | Devices       |
|  |                    |   |     |          |           |               |
|  |                    |   |     |          |           |               |
|  |                    |   |     |          |           |               |
|  |                    |   |     |          |           |               |
|  |                    |   |     |          |           |               |
|  |____________________|   |     |__________|           |_______________|
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|        HP-48              |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|__________________________ |


             Figure 1: System Block Diagram

    The I2C interface is electrically a bus, with all the devices being
connected in parallel to the same 3 wires.  This is in contrast to
the HP-IL system, where the devices were linked in a loop with the
output of a device feeding the input of the next.  Physically, each
device would typically be fitted with an `in' and an `out' connector,
wired in parallel, so that several devices could be plugged together
and connected to the HP48 interface.

    The HP48 sends simple ASCII-coded commands to the inter-
face unit, which then performs the appropriate actions on the I2C
devices and returns ASCII-coded results to the HP48. These return
values indicate the success or failure of the operation, and are also
used to send data back to the HP48 when a device on the I2C bus
is read.

    There are several ways of producing suitable I2C devices to con- nect
to this interface.  There are a number of commercial devices that use this
interface, for example the Velleman K8000 (Available in the UK from
Maplin).  This board provides 16 opto-isolated digital I/O lines, 4 8-bit
analogue inputs, a single 8-bit analogue output and 8 6-bit analogue
outputs. A simple cable is all that's needed to link it to the HP48
interface. 

    Secondly, some magazines, in particular Elektor, have published
projects that are controlled by the I2C bus.  Again, these can be
linked up to this interface unit using nothing more than a cable.

    The full documentation for this project describes a number of
I2C projects, including demonstration boards for the 8-bit I/O port
and  the  4-channel  analogue  to  digital  converter  and  a  2-channel
mains on/off switch. These can be used as a starting-point for more
complex designs, or regarded as the replacement for the HP82166
GPIO interface and built into other systems.

    Finally, it is possible to incorporate the standard Philips chips
into a more complex project and thus build a custom device that
is controlled by the I2C bus. These chips are very easy to use, and
in many cases this is the simplest way to make an I2C device.

3) A  description  of  the  hardware


A  full  schematic  diagram  for  this  interface  is  shown  in  figure  2.
Apart  from  the  simple  5V  power  supply,  there  are  only  2  chips
in the design, a MAX232 and a PIC16C84.  The former converts
between the RS232 voltage levels used by the HP48's serial port
and the TTL levels used by the rest of the system.



                  78L05                      +5V
               +--------------+              --- 
               |              |               |
  +9Vo---+-----|In         Out|----+----------+
         |     |     Com      |    |
        ---    +--------------+   ---100nF 
        ---100nF      |           --- 
         |           ---           |
         |            ///          --- 
   0Vo---+                        ///
         |
        ---
        ///
                MAX232
             +--------------+
             |              |
         +---|C1+         V+|--+
         |   |              |  |
4 0ff   ===  |              | ===  +5V
 2u2    ---  |              | ---  ---
         |   |              |  |    |
         +---|C1-           |  +----+                     +5V
             |              |                             ---
         +---|C2+         V-|--+                           |
         |   |              |  |                           +--+
        ===  |              | ---                          |  |
        ---  |              | ===                          \  \
         |   |              |  |                        3k3/  /3k3
         +---|C2-           | ---     PIC16C84             \  \
             |              | ///   +------------          /  /
             |              |       |           |   300R   |  |
TxD o--------|T1out     T1in|-------|PA0     PB1|---/\/\---)--+-o SCL
             |              |       |           |          |
Rxd o--------|R1in     R1out|-------|PA1     PB0|---/\/\---+----o SDA  
             |              |       |           |   300R
Gnd o--+     |              |       |           |          +----o Gnd 
       |     +--------------+       |           |          |
       |                            |           |         ---  
      ---          +5V              |           |         ///
      ///          ---              |           |
+5V                 |               |           |   4MHz Xtal
---                 /       +-------|Mclr   OscI|-------+--------+
 | 33k            |<        |       |           |       |        | 
 +-/\/\-+--/\/\/--|  2N     |       |           |      ---      --- 15pf
        |   10k   |\ 3906   |       |           |     =====     ---
        |           |       |       |           |      ---       |
        |           +-------|       |           |       |        +------+
       ---          |               |           |       |        |      |  
  3v3   ^|          /               |           |       |       ---    ---
        |           \39k            |           |       |       ---    ///
        |           /               |           |       |        | 15pf
       ---          \               |       OscO|-------+--------+
       ///          |               |           |   
                    |               +-----------+
                   ---
                   ///


         Figure 2: HP48 to I2C interface schematic diagram

    The  PIC16C84  is  a  microcontroller  chip  that  has  been  pro-
grammed  to  accept  commands  from  the  HP48,  perform  the  ap-
propriate actions on the I2C bus and then send results back to the
HP48.  A commented source listing of this program is included in
the interface manual, and is also included on the Conference Disk.

    In order to write data to an I2C device, the `W' command is
used.   This  has  the  syntax  `W<address><data><CR>',  where
<address> is the physical address of the required I2C device and
<data> is one or more data bytes to send.  Both of these values
are expressed in hexadecimal notation. The interface then responds
with either `OK<CR>' if the operation succeeded, or an error code
if it did not.

    Data can be read back from an I2C device using the `R' com-
mand. This has a similar syntax, `R<number of bytes><address><CR>'.
Again, all values are 2-digit hexadecimal numbers, and the interface
will return either the bytes read, followed by <CR> if the operation
was successful, or an error code if it was not.

    For example, to set the port lines of the first PCF8574 I/O port
(at address 40 hex) to the value 85 (also in hexadecimal), the string
`W4085<CR>' should be sent to the interface, which will respond
with `OK<CR>' if the device exists.  Similarly, to read back the
state of these lines, the string `R0141<CR>' should be send, and
a 2 digit hexadecimal number will then be sent to the HP48 by the
interface, followed by a <CR>.

    Unlike many commercial data logger units for the HP48,  the
number of I/O lines on this interface can be extended by simply
adding  another  parallel  port  chip  or  ADC  chip.   It  is  therefore
possible to link this device to fairly complex systems.

4)  Some  simple  RPL  driver  programs

This interface can be controlled using reasonably simple user-RPL
programs(It is also possible to use System-RPL or machine code if 
desired).

    Firstly, it is necessary to define some useful subroutines. CLRBUF
clears the RS232 receiver buffer on the HP48 :

\<<  BUFLEN  DROP  SRECV  DROP  DROP  \>>


NIB\->CHR converts a number between 0 and 15 to the equivalent
hexadecimal character :

\<<  "0123456789ABCDEF"  SWAP  1  +  DUP  SUB  \>>

And finally RDIF reads the return code from the interface (until
a <CR> is received) and returns it on top of the stack :


\<<  ""
   DO  BUFLEN  DROP  SRECV  DROP  +
   UNTIL  DUP  13  CHR  POS  0  >
   END
\>>

4.1)  Talking  to  the  PCF8574  parallel  port

The program OUTPORT takes a decimal number from 0 to 255
from the top of the stack and sends it to the first parallel port chip
at address 40 hex on the I2C bus. It can be used to drive the LEDs
on the PCF8574 demonstration board.  The return code from the
interface is left on top of the HP48's stack

\<<  CLRBUF  256  MOD  255  SWAP  -  DUP  16  /  IP
NIB\->CHR  SWAP  16  MOD  NIB\->CHR  +  "W40"
SWAP  +  13  CHR  +  XMIT  DROP  RDIF
\>>

    The status of these port lines can be read using the INPORT
program,  which leaves this value as a decimal number on top of
the stack.  Provided none of the port lines have been set `on', this
program will read the status of the DIP switch on the PCF8574
demonstration board.

\<<  CLRBUF  "R0141"  13  CHR  +  XMIT  DROP  RDIF
1  2  SUB  "#"  SWAP  +  "h"  +  OBJ\->  B\->R  255
SWAP  -
\>>

4.2) Reading  the  PCF8591  Analogue  to  Digital Converter

The program ADC takes a channel number (between 0 and 3) from
the top of the stack and reads that channel of the first PCF8591
ADC chip at address 90 hex on the I2C bus. The value is returned
as a decimal number from 0 to 255 on top of the stack.

\<<  CLRBUF  "W904"  SWAP  4  MOD  NIB\->CHR  +  13
CHR  +  XMIT  DROP  RDIF  DROP  "R0291"  13  CHR
+  XMIT  DROP  RDIF  3  4  SUB  "#"  SWAP  +  "h"
+  OBJ\->  B\->R
\>>


    It is easy to use the HP48 to convert this value into a real-world
quantity.  As an example, the program VOLTS uses the ADC program to read a
given ADC channel which is then converted to a unit object giving the
actual voltage at that input of the converter chip (This assumes a 5V
reference, as used by the PCF8591 demonstration board).  Similar programs
can be used to scale the output if other real-world quantities are
measured using this device. 

\<<  ADC  256  /  5_V  *  \>>

5) Conclusions

This interface enables the HP48 to monitor and control real-world
systems, and is therefore a suitable replacement for the HP-IL mod-
ule and HP82165 GPIO interface for the HP-71B handheld com-
puter.
