# interfacing eeprom with pic16f767

Status
Not open for further replies.

#### frubo

##### New Member
Im trying to interface 2 of my 24AA1025 eeproms to my pic16f767. Ive never done anything like this and I am confused at where to begin. I read through the datasheet on the pic16f767 where it describes how to transmit data using built in USART Synchronous Master Mode. It says it works for serial eeproms so I assumed thats what I wanted (maybe not). The problem is when I look at the data sheet for the eeprom it goes into things like start/stop, acknowledging, and clock cycles. These things arent described in the pic datasheet. What do I do?

#### Pommie

##### Well-Known Member
Your eeprom is an I²C device. You need a pic that has hardware I²C or bitbang it. I have posted code to access serial eeproms somewhere on here. Have a search and see if it helps.

Mike.

#### frubo

##### New Member
I did some research and put bits and pieces of code together but I am having a difficuilt time debugging it. It simply writes a single byte (0x05) to eeprom and reads it back displaying it on PORTA leds. Despite writing 0x05 to the eeprom I always get 0x01 back. I am not sure whether it is my read or write. Also I have pull up resistors on SCL and SDA (5k ohm).

Code:
	list             p=16f767
#include	<P16F767.inc>

d1		equ		20h
d2		equ		21h
d3		equ		22h
;**********************************************************************
org		0x0000

; init
bcf	STATUS, RP1
bcf	STATUS, RP0    ;bank 0

clrf	PORTA

bsf	STATUS, RP0      ;bank 1

movlw	62h   	; 4mhz clk
movwf	OSCCON

movlw 	0Fh 	; Configure all pins

movlw 	00h 	; all output on portA
movwf 	TRISA

bsf	TRISC,3		; set as input (SCL)
bsf	TRISC,4		; set as input (SDA)

bcf 	STATUS, RP0 	; bank 0
movlw	28h
movwf	SSPCON      ; enable MSSP and stuff

bsf	STATUS, RP0 ; bank 1

movlw	09h

bcf 	STATUS, RP0 	; bank 0

; write byte
call	START
movlw	b'10100000'       ; control bits (write mode)
call	SENDBYTE
call	SENDBYTE
call	SENDBYTE
movlw	b'00000101'        ; send 0x05
call	SENDBYTE
call	STOP

; buncha delays just in case, let the eeprom do its thing
movlw	0ffh
call	DELAY
movlw	0ffh
call	DELAY
movlw	0ffh
call	DELAY
movlw	0ffh
call	DELAY
movlw	0ffh
call	DELAY

clrw

call	START
call	SENDBYTE
movlw	00h
call	SENDBYTE
movlw	00h
call	SENDBYTE
call	RESTART
movlw	b'10100001'            ; control bits (read mode)
call	SENDBYTE
call	READBYTE                      ; byte put into w register
movwf	PORTA                   ; display output

bsf SSPCON2, ACKDT                ; no acknowledge
bsf SSPCON2, ACKEN

call	STOP

LOOP_FOREVER
goto	LOOP_FOREVER

;---------------------------------
;	START
;---------------------------------

START
bsf SSPCON2,SEN
call	WAIT
return

;---------------------------------
;	WAITSSP
;---------------------------------

WAIT
bsf	STATUS, RP0 ;bank 1
btfsc	SSPSTAT,2
goto	WAIT
bcf 	STATUS, RP0 	; Bank0
return

;---------------------------------
;	STOP
;---------------------------------

STOP
bsf SSPCON2,PEN
call	WAIT
return

;---------------------------------
;	RESTART
;---------------------------------

RESTART
bsf SSPCON2,RSEN
call	WAIT
return

;---------------------------------
;	SENDBYTE
;---------------------------------

SENDBYTE
movwf	SSPBUF
call	WAIT
btfsc	SSPCON2, ACKSTAT		; wait for acknowledge
goto	$-1 return ;--------------------------------- ; READBYTE ;--------------------------------- READBYTE bsf SSPCON2,RCEN call WAIT movf SSPBUF,0 return ;--------------------------------- ; DELAY ROUTINE delays n milliseconds ;--------------------------------- DELAY movwf d3 DELAY_1 ;993 cycles movlw 0xC6 movwf d1 movlw 0x01 movwf d2 DELAY_0 decfsz d1, f goto$+2
decfsz	d2, f
goto	DELAY_0

decfsz	d3, f
goto	DELAY_1

return
END

#### Pommie

##### Well-Known Member
You can't just test bit 2 of SSPSTAT. You need to test the individual bits or the SSP interrupt flag.

This is how I do it in my code,
Code:
I2cSendStart	movlw	1<<SEN
goto	I2cSendBit

I2cSendStop	movlw	1<<PEN
goto	I2cSendBit

I2cSendReStart	movlw	1<<RSEN

I2cSendBit	bsf	STATUS,RP0
iorwf	SSPCON2,F
bcf	STATUS,RP0
I2cWaitSSP	btfss	PIR1,SSPIF
goto	I2cWaitSSP
bcf	PIR1,SSPIF
return
HTH

Mike.

#### frubo

##### New Member
yay I got it working. I had already tried what you just posted and it didnt work but that was because my banks were screwed up. I fixed both of those things though. Thanks for the help. Ive gotta start checking those MPLAB messages 'register not in bank 0' more often, that usually seems to be my problem =/