Reading info from BlinkM RGB LED
I am having trouble trying to read the I2C Address and firmware vesrion information from the BlinkM using a BS2. I have no problem sending commands - just reading. I have tried various combinations of instructions (based on code I use to access EEProms and a DS1307 real-time-clock).
Most of the examples I have found use the Arduino - the basic stamp examples I've found don't read data from the BlinkM.
Any help would be appreciated!
Most of the examples I have found use the Arduino - the basic stamp examples I've found don't read data from the BlinkM.
Any help would be appreciated!
' {$STAMP BS2p}
' {$PBASIC 2.5}
SDA PIN 0 ' I2C Serial Data Line
SCL PIN 1 ' I2C Serial Clock Line
DeviceAddr CON $09 '<< 1
Ack CON 0 ' acknowledge bit
Nak CON 1 ' no ack bit
I2C_data VAR Byte ' Work Byte For I2C I/O
I2C_ack VAR Bit ' Ack Bit From Device
BlinkM_cmd VAR Byte ' used by BlinkMSend & BlinkMSend3
BlinkM_dat1 VAR Byte ' used by BlinkMSend3
BlinkM_dat2 VAR Byte ' used by BlinkMSend3
BlinkM_dat3 VAR Byte ' used by BlinkMSend3
hueNum VAR Byte
scriptno VAR Byte
Main:
DEBUG "Welcome to BlinkMTest", CR
DEBUG DEC ? DeviceAddr, CR
BlinkM_cmd = "o" ' stop playing boot script, e.g. "{'o'}"
GOSUB BlinkMCmd
'GOSUB BlinkMVersion
GOSUB BlinkMAddress
BlinkM_cmd = "f" ' fade speed
BlinkM_dat1 = 240
GOSUB BlinkMCmd1
BlinkM_cmd = "t" ' time adjust
BlinkM_dat1 = 0
GOSUB BlinkMCmd1
BlinkM_cmd = "p" ' play black script, e.g. "{'p',2,0,0}"
BlinkM_dat1 = 9
BlinkM_dat2 = 0
BlinkM_dat3 = 0
GOSUB BlinkMCmd3
BlinkM_cmd = "p" ' play script to HSB command, e.g. "{'p', script#, repeat, line#}"
BlinkM_dat2 = 0 ' play forever
BlinkM_dat3 = 0 ' line 0
MainLoop:
'DEBUG CR,"Enter a hue number (1-9): "
'DEBUGIN DEC1 hueNum ' read from the serial debug input
'hueNum = hueNum * 28 ' scale to hue range of 0-255 (approximately)
'BlinkM_dat1 = hueNum ' set the hue
DEBUG CR,"Enter a script number (0-18): "
DEBUGIN DEC2 scriptno ' read from the serial debug input
BlinkM_dat1 = scriptno
GOSUB BlinkMCmd3
GOTO MainLoop
END
'^^^^^^^^^^^^^^ BLINKM SUBROUTINES ^^^^^^^^^^^^^^
BlinkMCmd:
'DEBUG ASC ? BlinkM_Cmd, CR
GOSUB I2C_Start
I2C_data = DeviceAddr << 1 ' shift left because address is upper 7 bits
GOSUB I2C_Send
I2C_data = BlinkM_cmd
GOSUB I2C_Send
GOSUB I2C_Stop
PAUSE 1
RETURN
BlinkMCmd1:
GOSUB I2C_Start
I2C_data = DeviceAddr << 1 ' shift left because address is upper 7 bits
GOSUB I2C_Send
I2C_data = BlinkM_cmd
GOSUB I2C_Send
I2C_data = BlinkM_dat1
GOSUB I2C_Send
GOSUB I2C_Stop
PAUSE 1
RETURN
BlinkMCmd3:
GOSUB I2C_Start
I2C_data = DeviceAddr << 1 ' shift left because address is upper 7 bits
GOSUB I2C_Send
I2C_data = BlinkM_cmd
GOSUB I2C_Send
I2C_data = BlinkM_dat1
GOSUB I2C_Send
I2C_data = BlinkM_dat2
GOSUB I2C_Send
I2C_data = BlinkM_dat3
GOSUB I2C_Send
GOSUB I2C_Stop
PAUSE 1
RETURN
BlinkMVersion: 'doesn't work
GOSUB I2C_Start
I2C_data = DeviceAddr << 1 ' shift left because address is upper 7 bits
GOSUB I2C_Send
I2C_data = "Z"
GOSUB I2C_Send
SHIFTIN SDA, SCL, MSBPRE, [noparse][[/noparse]I2C_data\8]
DEBUG "Version: ", DEC I2C_data, " - "
SHIFTIN SDA, SCL, LSBFIRST, [noparse][[/noparse]I2C_data\8]
DEBUG DEC I2C_data, CR
GOSUB I2C_Stop
PAUSE 1
RETURN
BlinkMAddress: 'doesn't work
GOSUB I2C_Start
I2C_data = DeviceAddr << 1 ' shift left because address is upper 7 bits
GOSUB I2C_Send
I2C_data = "a"
GOSUB I2C_Send
' GOSUB I2C_Stop
GOSUB I2C_Start
I2C_data = DeviceAddr << 1 ' shift left because address is upper 7 bits
GOSUB I2C_Send
IF (I2C_ack = Nak) THEN BlinkMAddress ' wait until not busy
I2C_ack = Nak
I2C_data = 0
' SHIFTIN SDA, SCL, LSBFIRST, [noparse][[/noparse]I2C_data\8]
SHIFTIN SDA, SCL, MSBPRE, [noparse][[/noparse]I2C_data\8]
SHIFTOUT SDA, SCL, LSBFIRST, [noparse][[/noparse]I2C_ack\1] ' send ack or nak
DEBUG "Address: ", DEC I2C_data, CR
GOSUB I2C_Stop
PAUSE 1
RETURN
'Send Address or Data Procedure
I2C_Send:
SHIFTOUT SDA, SCL, MSBFIRST, [noparse][[/noparse]I2C_data] '[noparse][[/noparse]I2C_data\8] 'Send Byte To Device
SHIFTIN SDA, SCL, MSBPRE, [noparse][[/noparse]I2C_ack\1] '[noparse][[/noparse]I2C_ack\1] ' Get Acknowledge Bit
RETURN
' -----[noparse][[/noparse] Low Level I2C Subroutines]----------------------------------------
I2C_Start: ' I2C Start Bit Sequence
INPUT SDA
INPUT SCL
LOW SDA ' SDA --> Low While SCL High
RETURN
I2C_Stop: ' I2C Stop Bit Sequence
LOW SDA
INPUT SCL
INPUT SDA ' SDA --> High While SCL High
RETURN

Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
The datasheet isn't much help since it caters to Arduino example code:
http://thingm.com/fileadmin/thingm/downloads/BlinkM_datasheet.pdf
·