External EEPROM can't read/write
I am using a 16F877 with a 24LC256 in an effort to display a logo on a graphic LCD. The following code should write the logo to the 24LC256 and read 10 of the written addresses from the 24LC256. However, the read retrieves FFH for each address.
The 24LC256 has pins 1, 2, 3, 4, and 7 at gnd. Pin 8 is 5V. Pin 5 to PORTC.4 with a 4.7k pull-up to 5V. Pin 6 to PORTC.3 with a 4.7k pull-up to 5V. I've tried multiple 24LC256's and other resistor values to no avail.
Any suggestions? Thank you.
Code:
@ DEVICE HS_OSC, WDT_OFF, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_OFF
DEFINE LOADER_USED 1
DEFINE OSC 20
ADCON1 = 7
sda var PORTC.4 'I2C SDA for ext eeprom
scl var PORTC.3 'I2C SCL for ext eeprom
ctl con 160 'EEPROM control code
addr var WORD 'eeprom word address
i var byte 'Index counter
edata var BYTE 'Data byte to be written and read
out_pin var PORTC.6 'TX pin of 16F877
ser_baud con 32 '19200
addr = 0
edata = 0
pause 100
serout2 out_pin,ser_baud,["Writing to EEPROM . . .",10,13]
for i = 0 to 249
lookup i,[_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$3F,$C0,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$0F,$FA,$60,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$7C,$04,$38,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$D2,$60,$C8,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$01,$D0,$22,$1C,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$03,$A2,$80,$0C,$00,$00,$00],edata
I2CWRITE sda,scl,ctl,addr.highbyte,addr.lowbyte,[edata]
pause 10
addr = addr + 1
next
. . .
for i = 0 to 249
lookup i,[_
$00,$00,$00,$01,$FE,$1F,$9F,$E0,$7C,$07,_
$FE,$3C,$1F,$03,$80,$3F,$C0,$3C,$E0,$00,_
$7F,$DF,$FC,$14,$97,$1D,$FD,$77,$00,$1C,_
$00,$00,$00,$03,$0F,$03,$E1,$F0,$FC,$0E,_
$1E,$38,$0E,$03,$80,$61,$E0,$FD,$E0,$00,_
$77,$CD,$FC,$80,$95,$8D,$EF,$7B,$80,$1C,_
$00,$00,$00,$00,$07,$01,$C0,$F0,$1C,$1C,_
$04,$38,$0E,$03,$80,$00,$E0,$1E,$00,$00,_
$7F,$DF,$6E,$2E,$BC,$CE,$FF,$BE,$80,$1C,_
$00,$00,$00,$00,$07,$01,$C0,$70,$1C,$18,_
$00,$38,$0E,$03,$80,$00,$E0,$1C,$00,$00,_
$7D,$4F,$FF,$81,$37,$3B,$FB,$F8,$80,$1C,_
$00,$00,$00,$00,$07,$01,$C0,$70,$1C,$38,_
$00,$38,$0E,$03,$80,$00,$E0,$1C,$00,$00,_
$5F,$E5,$BB,$CA,$2F,$FE,$FF,$F4,$80,$1E,_
$00,$00,$00,$00,$3F,$01,$C0,$70,$1C,$38,_
$00,$38,$0E,$03,$80,$07,$E0,$1C,$00,$00,_
$6F,$AF,$FF,$C1,$7E,$FE,$BF,$C7,$00,$0E,_
$00,$00,$00,$01,$E7,$01,$C0,$70,$1C,$38,_
$00,$38,$0E,$03,$80,$3C,$E0,$1C,$00,$00,_
$3D,$E3,$F7,$E0,$6B,$57,$F7,$C3,$00,$0F,_
$00,$00,$00,$43,$07,$01,$C0,$70,$1C,$38,_
$00,$38,$0E,$03,$80,$60,$E0,$1C,$00,$00,_
$2F,$EA,$DD,$FE,$D9,$BF,$BD,$F3,$00,$07,_ '1231 to 1240
$80,$00,$01,$CE,$07,$01,$C0,$70,$1C,$3C],edata
I2CWRITE sda,scl,ctl,addr.highbyte,addr.lowbyte,[edata]
pause 10
addr = addr + 1
if addr > 1230 and addr < 1241 then
serout2 out_pin,ser_baud,[dec addr,44,32,hex2 edata,72,10,13]
endif
next
. . .
for i = 0 to 169
lookup i,[_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$03,$C0,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
$00,$00,$00,$00,$00,$00,$00,$00,$00,$00],edata
I2CWRITE sda,scl,ctl,addr.highbyte,addr.lowbyte,[edata]
pause 10
addr = addr + 1
next
serout2 out_pin,ser_baud,["Reading EEPROM . . .",10,13]
for addr = 1231 to 1240
i2cread sda,scl,ctl,addr.highbyte,addr.lowbyte,[edata]
serout2 out_pin,ser_baud,[dec addr,44,32,hex2 edata,72,10,13]
next
serout2 out_pin,ser_baud,["Finished",10,13]
end
Huh, you don't know what ". . ." means?
I don't mean to be rude but the ". . ." means that I omitted code from the post or did you want three pages of the same thing over and over?
I also have no idea what colons have to do with anything.
Simple example; still doesn't work
I wrote a very simple program but with the same output result:
5, 57
5, 255
Code:
DEFINE LOADER_USED 1
DEFINE OSC 20
edata var BYTE
pause 100
i2cwrite portc.4,portc.3,160,5,[57]
pause 10
serout2 portc.6,32,[dec 5,44,32,dec 57,10,13]
i2cread portc.4,portc.3,160,5,[edata]
serout2 portc.6,32,[dec 5,44,32,dec edata,10,13]
end
I tried using portc.0 as SDA and portc.1 at SCL with the same result. I used a different 16F877. It just doesn't make any sense to me anymore. I've checked the datasheets and my circuit many, many times.
I'll try setting the registers
Darrel, I've tried 1k, 2k, 4.7k and 10k. I get a more well-defined square-wave with a 1 or 2k, but I still have the same result. I don't think the 24LC256 is being written to or read.
Jerson, I've looked at the 16F877 datasheet and found the first six bits of the SSPCON register and the first seven bits of the SSPCON2 register. Also, the requirement to set C.3 and C.4 as inputs with TRISC.
A lot of pages of the datasheet are devoted to I2C. Although PIC Basic Pro makes I2C a "no brainer", the data sheet suggests using assembler isn't so easy. I only have a small amount of assembler experience with an 8051.
I'll try configuring registers with PBP, but once I use the I2CWRITE and READ commands, won't PBP do it's own configuration, changing prior configurations?