Skip to content

Matrix Orbital

Personal tools
You are here: Home » Application Notes » I2C » I2C using a BS2 BASIC Stamp

I2C using a BS2 BASIC Stamp

Document Actions
This is a very simple example of using I2C to talk to a Matrix Orbital Display, using a BS2 BASIC Stamp from Parallax.
In this example, a LCD2041 is communicated to by a BS2 BASIC Stamp and displays some text using I2C. A BS2 BASIC Stamp does not have I2C support built in - so you have to "bit bash" a solution. You can use the I2C subroutines for other I2C based projects as well.




'{$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
Created by Henry
Last modified 2007-11-02 09:11 AM
 

Powered by Plone

This site conforms to the following standards: