Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

I2c EEPROM Read byte

Status
Not open for further replies.

FarshadN

New Member
Hi all,

I've used a PIC16f876a and could successfully write 128Byte (as LCD CHR codes) into 24C512 Eeprom, the problem rose when I try to read bytes back and display onto LCD.

when doing random read I can successfully read one byte from any where in the memory page, but after the first byte when i send ACK to read more, all i get is 0xff on all byte and LCD shows █. no matter what. :(

I'd appreciate any help.

Farshad
 

Sceadwian

Banned
Are you sure you're using the right command to read the next byte?
 

Pommie

Well-Known Member
Most Helpful Member
Post your code. You must be doing something wrong but I can't think what it might be.

Mike.
 

FarshadN

New Member
here is my code:
as I mentioned the first read is fine but when it goes to the loop for next bytes it's wrong data display on LCD.


setup

bcf STATUS, RP0
clrf PORTB
clrf PORTC
bsf STATUS, RP0 ;bank1
movlw b'00011100'
movwf TRISC ; PORTC <4:3> as input
bcf STATUS, RP0 ;bank0

call INITLCD

clrf SSPCON
movlw b'00101000' ;init I2C with FOsc
movwf SSPCON

bcf PIR1, SSPIF ;init XT flag bit

bsf STATUS, RP0 ;bank1
clrf SSPCON2 ;init SSPCON2
clrf SSPSTAT
bsf SSPSTAT, SMP ;slew disable for 100KHz
bcf SSPSTAT, CKE ;SMBus disabled
movlw h'9'
movwf SSPADD ; 100Khz baud rate
bcf STATUS, RP0 ;bank0
movlw d'16'
movwf data_count

read_mode
bsf STATUS, RP0 ;bank1
bsf SSPCON2, SEN ; send Start
btfss SSPSTAT, S
goto $-1
bcf STATUS, RP0 ;bank0
btfss PIR1, SSPIF ;wait till Start completed
goto $-1
bcf PIR1, SSPIF ; clear TXflag bit
movlw b'10100000'
movwf SSPBUF ;set device address<0>write
bsf STATUS, RP0 ;bank1
btfsc SSPSTAT, BF ; check if buffer is empty
goto $-1
btfsc SSPCON2, ACKSTAT ; check if ACK received
goto $-1
bcf STATUS, RP0 ;bank0
btfss PIR1, SSPIF ;wait till Start completed
goto $-1
bcf PIR1, SSPIF ; clear TXflag bit
movlw d'0'
movwf SSPBUF ; first word address
bsf STATUS, RP0 ;bank1
btfsc SSPSTAT, BF ; check if buffer is empty
goto $-1
btfsc SSPCON2, ACKSTAT ; check if ACK received
goto $-1
bcf STATUS, RP0 ;bank0
btfss PIR1, SSPIF ;wait till Start completed
goto $-1
bcf PIR1, SSPIF ; clear TXflag bit
movlw d'5'
movwf SSPBUF ;second word address page0=0, page1=128
bsf STATUS, RP0 ;bank1
btfsc SSPSTAT, BF ; check if buffer is empty
goto $-1
btfsc SSPCON2, ACKSTAT ; check if ACK received
goto $-1
bcf STATUS, RP0 ;bank0
btfss PIR1, SSPIF ;wait till Start completed
goto $-1
bcf PIR1, SSPIF ; clear TXflag bit
bsf STATUS, RP0 ;bank1
bsf SSPCON2, RSEN ;send Restart
btfss SSPSTAT, S
goto $-1
bcf STATUS, RP0 ;bank0
btfss PIR1, SSPIF ;wait till Start completed
goto $-1
bcf PIR1, SSPIF ; clear TXflag bit
movlw b'10100001'
movwf SSPBUF ;set device address<1>read
bsf STATUS, RP0 ;bank1
btfsc SSPSTAT, BF ; check if buffer is empty
goto $-1
btfsc SSPCON2, ACKSTAT ; check if ACK received
goto $-1
bcf STATUS, RP0 ;bank0
btfss PIR1, SSPIF ; wait XT complete
goto $-1
bcf PIR1, SSPIF ; clear TXflag bit
data_read
bsf STATUS, RP0 ;bank1
bsf SSPCON2, RCEN ;I2C receive enabled
btfss SSPSTAT, BF ; check if buffer is full
goto $-1
bcf STATUS, RP0 ;bank0
btfss PIR1, SSPIF ;wait till Start completed
goto $-1
bcf PIR1, SSPIF ; clear TXflag bit
bcf STATUS, RP0
movf SSPBUF, w
call LCDWRITE ;data to LCD
clrf SSPBUF
bsf STATUS, RP0
bcf SSPCON2, ACKDT ;ACK send
bsf SSPCON2, ACKEN ;init Sequel ACK
bcf STATUS, RP0 ;bank0
btfss PIR1, SSPIF ;wait till Start completed
goto $-1
bcf PIR1, SSPIF ; clear TXflag bit
decfsz data_count, f
goto data_read
return



read_end
bsf STATUS, RP0 ;bank1
bsf SSPCON2, ACKDT ;NO acknowlege bit
bsf SSPCON2, ACKEN ;init Sequel ACK
bcf STATUS, RP0 ;bank0
btfss PIR1, SSPIF ;wait till Start completed
goto $-1
bcf PIR1, SSPIF ; clear TXflag bit
bsf SSPCON2, PEN ;send Stop bit
btfss SSPSTAT, P
goto $-1
bcf STATUS, RP0 ;bank0
btfss PIR1, SSPIF ; wait XT complete
goto $-1
bcf PIR1, SSPIF ; clear TXflag bit
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
Too late for me now. If nobody answers before tomorrow I'll take a look.

Mike.
 

FarshadN

New Member
Hi Mike!

have you had time to look at my code. I also added a "btfsc SSPSTAT, R-W" check after the RCEN was set to slow down the loop, but still same problem all I get is 0xff on all bytes.
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top