Modified 24LC128 example code to write 12 bits but it doesn't work???
wmtell1
Posts: 14
I modified the example code for the 24LC128 to write a 16 bit word and it correctly wrote the 16 bits. Since I'm now using a 12 bit LT1298 I don't need to write 16 bits and·I've modified the code to write 12 bits. The program·displays·the value of NIB2 using the DEBUG window and it shows the correct value but a [noparse][[/noparse]0] is incorrectly written. Does anyone have a clue as to what's happening?
' {$STAMP BS2sx}
' {$PBASIC 2.5}
' {$PORT COM5}
' Purpose...Write a 12 bit word to a 24LC128 EEPROM
'
' =========================================================================
'
'
' Program Description
'
'
'WRITE a 12 bit word via a subroutine to a 24LC128 EEPROM address,
'starting at 1, incrementing by 2 and ending at address 4095.
'Flash an LED ON/OFF to confirm each WRITE cycle.
'
'
'
'
[noparse][[/noparse] I/O Definitions ]
LED PIN 4
SDA PIN 5 ' I2C Serial Data Line
SCL PIN 6 ' I2C Serial Clock Line
'
[noparse][[/noparse] Constants ]
Address CON $A0 ' Address Of 24LC128
Ack CON 0 ' Acknowledge Bit
'
[noparse][[/noparse] Variables ]
temp VAR Word ' Variable Used For Work
i2cWork VAR Word ' Work Byte For I2C I/O
i2cWork1 VAR Word ' Work Byte For I2C I/O
i2cWork2 VAR Word ' Work Byte For I2C I/O
i2cAck VAR Bit ' Ack Bit From Device
eeprom_add VAR Word ' EEPROM Address
J···· VAR········ Nib
'
[noparse][[/noparse] Main Routine ]
FOR temp = 1 TO 4095 STEP 2
· eeprom_add = temp
· GOSUB WriteEE
NEXT 'temp
END
'
[noparse][[/noparse] Subroutines ]
· ' Writing Section
WriteEE:
FOR J = 1 TO 2
· GOSUB I2C_Start
· GOSUB Control_Byte_Write
· GOSUB Addrs
· GOSUB I2C_Write
· GOSUB I2C_Stop
· PAUSE 100
NEXT 'J
RETURN
I2C_Stop: ' I2C Stop Bit Sequence
LOW SDA
INPUT SCL
INPUT SDA ' SDA --> High While SCL High
RETURN
I2C_Start: ' I2C Start Bit Sequence
INPUT SDA
INPUT SCL
LOW SDA ' SDA --> Low While SCL High
RETURN
Control_Byte_Write: ' I2C Control Write Byte Sequence
i2cWork = Address
i2cWork.BIT0 = 0 ' Sets Unit To Write
SHIFTOUT SDA, SCL, MSBFIRST, [noparse][[/noparse]i2cWork\8] ' Send Byte To Device
SHIFTIN SDA, SCL, MSBPRE, [noparse][[/noparse]i2cAck\1] ' Get Acknowledge Bit
RETURN
Addrs: ' I2C Address Byte Sequence
i2cWork = eeprom_add· + J - 1
SHIFTOUT SDA, SCL, MSBFIRST, [noparse][[/noparse]i2cWork.HIGHBYTE\8] ' Send Byte To Device
SHIFTIN SDA, SCL, MSBPRE, [noparse][[/noparse]i2cAck\1] ' Get Acknowledge Bit
SHIFTOUT SDA, SCL, MSBFIRST, [noparse][[/noparse]i2cWork.LOWBYTE\8] ' Send Byte To Device
SHIFTIN SDA, SCL, MSBPRE, [noparse][[/noparse]i2cAck\1] ' Get Acknowledge Bit
RETURN
I2C_Write: ' I2C Write 2 Byte Sequence
IF J =1 THEN
· i2cWork = temp.LOWBYTE
· HIGH LED
SHIFTOUT SDA, SCL, MSBFIRST, [noparse][[/noparse]i2cWork\8] 'Send 8 bits (1 byte) To Device
ELSE
· i2cWork = temp.NIB2
· LOW LED
DEBUG "temp.NIB2= ",temp.NIB2,CR
SHIFTOUT SDA, SCL, MSBFIRST, [noparse][[/noparse]i2cWork\4] 'Send 4 bits To Device
ENDIF
SHIFTIN SDA, SCL, MSBPRE, [noparse][[/noparse]i2cAck\1] ' Get Acknowledge Bit
RETURN
Control_Byte_Read: ' I2C Control Read Byte Sequence
i2cWork = Address
i2cWork.BIT0 = 1 ' Sets Device To Read
SHIFTOUT SDA, SCL, MSBFIRST, [noparse][[/noparse]i2cWork\8] ' Send Byte To Device
SHIFTIN SDA, SCL, MSBPRE, [noparse][[/noparse]i2cAck\1] ' Get Acknowledge Bit
RETURN
' {$STAMP BS2sx}
' {$PBASIC 2.5}
' {$PORT COM5}
' Purpose...Write a 12 bit word to a 24LC128 EEPROM
'
' =========================================================================
'
'
' Program Description
'
'
'WRITE a 12 bit word via a subroutine to a 24LC128 EEPROM address,
'starting at 1, incrementing by 2 and ending at address 4095.
'Flash an LED ON/OFF to confirm each WRITE cycle.
'
'
'
'
[noparse][[/noparse] I/O Definitions ]
LED PIN 4
SDA PIN 5 ' I2C Serial Data Line
SCL PIN 6 ' I2C Serial Clock Line
'
[noparse][[/noparse] Constants ]
Address CON $A0 ' Address Of 24LC128
Ack CON 0 ' Acknowledge Bit
'
[noparse][[/noparse] Variables ]
temp VAR Word ' Variable Used For Work
i2cWork VAR Word ' Work Byte For I2C I/O
i2cWork1 VAR Word ' Work Byte For I2C I/O
i2cWork2 VAR Word ' Work Byte For I2C I/O
i2cAck VAR Bit ' Ack Bit From Device
eeprom_add VAR Word ' EEPROM Address
J···· VAR········ Nib
'
[noparse][[/noparse] Main Routine ]
FOR temp = 1 TO 4095 STEP 2
· eeprom_add = temp
· GOSUB WriteEE
NEXT 'temp
END
'
[noparse][[/noparse] Subroutines ]
· ' Writing Section
WriteEE:
FOR J = 1 TO 2
· GOSUB I2C_Start
· GOSUB Control_Byte_Write
· GOSUB Addrs
· GOSUB I2C_Write
· GOSUB I2C_Stop
· PAUSE 100
NEXT 'J
RETURN
I2C_Stop: ' I2C Stop Bit Sequence
LOW SDA
INPUT SCL
INPUT SDA ' SDA --> High While SCL High
RETURN
I2C_Start: ' I2C Start Bit Sequence
INPUT SDA
INPUT SCL
LOW SDA ' SDA --> Low While SCL High
RETURN
Control_Byte_Write: ' I2C Control Write Byte Sequence
i2cWork = Address
i2cWork.BIT0 = 0 ' Sets Unit To Write
SHIFTOUT SDA, SCL, MSBFIRST, [noparse][[/noparse]i2cWork\8] ' Send Byte To Device
SHIFTIN SDA, SCL, MSBPRE, [noparse][[/noparse]i2cAck\1] ' Get Acknowledge Bit
RETURN
Addrs: ' I2C Address Byte Sequence
i2cWork = eeprom_add· + J - 1
SHIFTOUT SDA, SCL, MSBFIRST, [noparse][[/noparse]i2cWork.HIGHBYTE\8] ' Send Byte To Device
SHIFTIN SDA, SCL, MSBPRE, [noparse][[/noparse]i2cAck\1] ' Get Acknowledge Bit
SHIFTOUT SDA, SCL, MSBFIRST, [noparse][[/noparse]i2cWork.LOWBYTE\8] ' Send Byte To Device
SHIFTIN SDA, SCL, MSBPRE, [noparse][[/noparse]i2cAck\1] ' Get Acknowledge Bit
RETURN
I2C_Write: ' I2C Write 2 Byte Sequence
IF J =1 THEN
· i2cWork = temp.LOWBYTE
· HIGH LED
SHIFTOUT SDA, SCL, MSBFIRST, [noparse][[/noparse]i2cWork\8] 'Send 8 bits (1 byte) To Device
ELSE
· i2cWork = temp.NIB2
· LOW LED
DEBUG "temp.NIB2= ",temp.NIB2,CR
SHIFTOUT SDA, SCL, MSBFIRST, [noparse][[/noparse]i2cWork\4] 'Send 4 bits To Device
ENDIF
SHIFTIN SDA, SCL, MSBPRE, [noparse][[/noparse]i2cAck\1] ' Get Acknowledge Bit
RETURN
Control_Byte_Read: ' I2C Control Read Byte Sequence
i2cWork = Address
i2cWork.BIT0 = 1 ' Sets Device To Read
SHIFTOUT SDA, SCL, MSBFIRST, [noparse][[/noparse]i2cWork\8] ' Send Byte To Device
SHIFTIN SDA, SCL, MSBPRE, [noparse][[/noparse]i2cAck\1] ' Get Acknowledge Bit
RETURN
Comments