To use this file copy and paste this:
// #URL-lib "http://pin1.org/forthlib/flb/I2C/i2c.flb" into BV Terminal 3
or here to download.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// I2C interface
// Uses the built in I2C functionality of the LPC2xxx
// This is the generic I2C driver with ARM as the master
// To drive individual devices
// NOTE: This file only makes use of I2C channel 0, there is
// another channel but it is not implemented here
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// HISTORY:
// Aug 2007 *updated by adding a few extra words
// *added a section at the back
// Oct 2007 *error with P, should not use i2-valid (corrected)
// *added libaraies
// Dec 2007 *loader added
// *sid used in loader-1
// Feb 2008 *this file is now a library with
// *only the public words exposed
// Nov 2008 *Renamed the AA,SI,STO & STA constants to prevent
// clashes when using with sid=0
// *Increased number of re-tries on i2-start to cater for
// slow devices
// REQUIRES:
// #URL-lib "http://pin1.org/forthlib/flb/General/soft1.flb" sid=0
// #URL-lib "http://pin1.org/forthlib/flb/General/pinsel.flb" sid=101
// Register CONSTANTS:
// channel 0 - only channel 0 used, there is a channel 1, extend
// this file as required.
&E001C000 constant I2CONSET
&E001C004 constant I2STAT
&E001C008 constant I2DAT
&E001C00C constant I2ADR // not used
&E001C010 constant I2SCLH
&E001C014 constant I2SCLL
&E001C018 constant I2CONCLR
// easier to understand
4 constant I2_AA
8 constant I2_SI
16 constant I2_STO
32 constant I2_STA
Full Contents of File
&E001C000  constant  I2CONSET
&E001C004  constant  I2STAT
&E001C008  constant  I2DAT
&E001C00C  constant  I2ADR       
&E001C010  constant  I2SCLH
&E001C014  constant  I2SCLL
&E001C018  constant  I2CONCLR
4    constant  I2_AA
8    constant  I2_SI
16  constant  I2_STO
32  constant  I2_STA
50000  constant  i2cBusSpeed 
: i2c-init
        2  1  pinselect     
        3  1  pinselect     
        pclk  i2cBusSpeed 
        dup  I2SCLH  !  I2SCLL  !     
        &40  I2CONSET  !   
;
: i2-speed
        pclk  swap 
        I2SCLH  !  I2SCLL  !
;       
: i2-start
        0
        10000  for
                I2_STA  I2CONSET  ! 
                I2STAT  @  &08  =  if  drop  -1  leave  then
        next
;
: i2-valid
        100000 
        for 
                i2stat  @  &f8  <>  if  leave  then
        next
;               
: i2-stat
        i2stat  @  2dup  <>
        if 
                cr  ."  i2c  Error  -  Status  recieved  "  ph.  ."  Expecting  "  ph.
                abort
        else
                2drop
        then               
;
: i2-dat@i2dat  @  ; 
: i2-dat!i2dat  !  ; 
: i2c1I2_STA  I2_STO  +  I2_SI  +  i2conclr  !  i2-valid  ;             
: i2c2I2_STA  i2conset  !  I2_STO  I2_SI  +  i2conclr  !  i2-valid  ; 
: i2c3I2_STO  i2conset  !  I2_STA  I2_SI  +  i2conclr  !  i2-valid  ; 
: i2c4I2_STA  I2_STO  +  i2conset  !  I2_SI  i2conclr  !  i2-valid  ; 
: i2c5I2_STA  I2_STO  +  I2_SI  +  I2_AA  +  i2conclr  !  i2-valid  ;   
: i2c6I2_AA  i2conset  !  I2_STA  I2_STO  +  I2_SI  +  i2conclr  !  i2-valid  ; 
: i2-8i2dat  !  I2_STO  I2_SI  +  i2conclr  !  i2-valid  ; 
: i2-10i2dat  !  I2_STO  I2_SI  +  i2conclr  !  i2-valid  ; 
: (s)
        i2-start  0=  abort"  Error  I2C  cant  start"
        i2stat  @  &8  =    if  i2-8    escape  then
        i2stat  @  &10  =  if  i2-10  escape  then
        ."  Error  start  returns  "  i2stat  @  ph.
        ."  as  status  value"
;       
: <0>pI2_STO  i2conset  !  I2_STA  I2_SI  +  i2conclr  ! 
        -1
        100000 
        for 
                i2stat  @  &f8  =
                if
                        1+  leave
                then   
        next
        if
                ."  I2C  Stop  error"
        then       
;                       
: <0>s
       
        i2stat  @ 
        dup  &38  =  swap  dup  &48  =  swap  &58  =  or  or
        if  ."  Device  does  not  acknowledge"  p  then
;
: <0>rs     
        i2c2  i2stat  @  &10  =
        if
                i2-10 
        else
                ."  Repeated  start  error"
        then
;                       
         
: <0>sb
        i2dat  !
        i2c1
;       
       
: <0>rn
        i2c6  &50  i2-stat
        i2-dat@
;
: <0>rl
        i2c5  &58  i2-stat
        i2-dat@
; 
: <0>I2-init  i2c-init  ;
: <0>i2Speed  I2-speed  ;