I2C using a BS2 BASIC Stamp with "Bit Bash" Method
*BASIC Stamp 2p models have native I2C support, but this example still can be used.
BASIC Stamp: Any BASIC Stamp 2 models
Communication Type: I2C
Display Used: Any Matrix Orbital display that supports I2C such as the MOI or LK/VK/PK series.
In this example we are talking to a display at address 50 hex over I2C communication protocol.
'{$STAMP BS2}
'I2C test program.
27/4/2003 Ben Lennard
'Bit Bashing I2C on a Parallax BS2
'Write to a Matrix Orbital LCD via I2C on a BS2
'STAMP EEPROM
DATA
Companyname
DATA "Lennard Electronics "
URL DATA "www.lennard.net.nz "
'CONSTANTS
SDA CON
1 'I2C Data line
SCL CON
0 'I2C Clock line
'VARIABLES
ack VAR
BIT 'Acknowledge from the I2C bus
I2C_data
VAR BYTE 'Data to/from the I2C bus
i VAR BYTE 'just a for loop variable
LCDcharacter
VAR BYTE
DIRS=%0011111111111111
Main:
PAUSE 1000 'Give the MO Display
time to initalise it's self - 1 seconds
GOSUB StartI2C
I2C_data = %01010000 'MO Display address
for WRITING to
GOSUB Write_data
I2C_data = %11111110 '254 = command
GOSUB Write_data
I2C_data = %01011000 '88 = clear display
GOSUB Write_data
for i = Companyname to Companyname + 39 '
this for loop writes the display message
READ i,
LCDcharacter
I2C_data = LCDcharacter
GOSUB Write_data
next
GOSUB StopI2C
end
'^^^^^^^^^^^^^^
I2C SUBROUTINES ^^^^^^^^^^^^^^
'Start Procedure. Set start condition on bus: SDA Hi-Lo while
SCL Hi
StartI2C:
HIGH SDA
HIGH SCL
LOW SDA
LOW SCL
RETURN
'Stop Procedure.
Set stop condition on bus: SDA Lo-Hi while SCL Hi
StopI2C:
LOW SDA
HIGH SCL
HIGH SDA
RETURN
'Write Address/Data
Procedure
Write_data:
shiftout SDA, SCL, msbfirst, [I2C_data]
'Check for Receiver Acknowledge
RX_ACK:
HIGH SDA
DIRS=%0011111011111011
HIGH SCL
ack = IN2
LOW SCL
DIRS=%0011111111111111
'if ack
= 1 then indicate_NO_ACK
'debug "Acknowledged", 13
'RETURN
'indicate_NO_ACK:
' debug "NO Acknowledge", 13
RETURN
'^^^^^^^^^^^^^^
I2C SUBROUTINES ^^^^^^^^^^^^^^
A special thanks to Ben Lennard for this great example, please visit his website for more electronics examples. For technical discussions and questions, please visit our forums
