Carey Fisher
2018-02-27 20:24:47 UTC
I'm having a terrible time trying to get the I2C module on the 18F67K40 working. My test code is posted below. This code and the I2C module seem very simple so I must be missing something simple.
This code tries to read address 0000 of an attached 24LC512 EEPROM. I know the hardware is good because I can use Pic Basic Pro and read and write the EEPROM using I2C.
The code I'm using came off a forum on the Microchip Support website. It is "bullet proof". Ha.
I've put debugging statements in the code and what I've found is that the Interrupt flag, PIR3,SSP1IF, never gets asserted.
Any pointers would be greatly appreciated.
'i2ctest.pbp
'another try at reading I2C data from 24LC512
'Start Condition
'Control Byte for write
'High address byte
'Low address byte
'Start Condition
'Control Byte for read
'Read in data byte
'Return with NACK
'Stop Condition
asm
#define Memtop 0x10000 ;higest memory in the 18F67k40 processor
Startaddress = MemTop-800
SDA = 4
SCL = 3
ORG Startaddress
;***************************************************************
;
; 24LC256 I2C Single Byte Read Routines
;
;
;
_I2CTEST1
rcall I2CInit
;EE_RD
rcall I2CStrt ;send I2C START
movlw h'A6'
rcall I2CSend ;send device address (control byte)
movlw h'00'
rcall I2CSend ;send address hi byte
movlw h'00'
rcall I2CSend ;send address lo byte
rcall I2CStrt ;send I2C START
movlw h'A7'
rcall I2CSend ;send device address (control byte)
EE_R1
rcall I2CRcv ;receive mode (receive byte)
movwf _testbyte1
rcall I2CNack ;signal end of receive
rcall I2CStopp ;send STOP
return ;
;***************************************************************
;
; I2C primitives
;
; SSPADD = ((Fosc/I2Cclock)/4)-1 {formula from AN735}
;
I2CInit
bsf PIE3,SSP1IE ; enable the Interrupt flag, PIR3,SSP1IF
bsf TRISC,SCL ;
bsf TRISC,SDA ;
movlw b'00101000' ;SSPEN + 12C Master
movwf SSP1CON1 ;
movlw b'00000000' ;
movwf SSP1CON2 ;
movlw b'00000000' ;slew rate on for 400khz
movwf SSP1STAT ;
movlw d'13' ;(20000000/100000)-1/4
movwf SSP1ADD ;400khz using 20mhz xtal
return ;
I2CDebug
movf ssp1stat,0
movwf _testbyte1
movf ssp1con1,0
movwf _testbyte2
movf ssp1con2,0
movwf _testbyte3
movf ssp1con3,0
movwf _testbyte4
return
I2CWait
btfss PIR3,SSP1IF ;complete flag?
bra I2CWait ;no, wait
bcf PIR3,SSP1IF ;clear irq flag
return ;
I2CStrt
bsf SSP1CON2,SEN_SSP1CON2 ;send START condition
bra I2CWait ;
I2CStopp
bsf SSP1CON2,PEN ;send STOP condition
bra I2CWait ;
I2CSend
movwf SSP1BUF ;send byte
bra I2CWait ;
I2CRcv
btfsc SSP1STAT,R_W ;transmit complete?
bra I2CRcv ;no, wait
bsf SSP1CON2,RCEN ;receive enable
bcf PIR3,SSP1IF ;clear irq flag
rcall I2CWait ;
movf SSP1BUF,W ;
return ;
I2CAck
bcf SSP1CON2,ACKDT ;set ACK
bra I2CNack+2 ;
I2CNack
bsf SSP1CON2,ACKDT ;set NACK
bsf SSP1CON2,ACKEN ;send ACK or NACK
bra I2CWait ;
I2CRes
bsf SSP1CON2,RSEN ;send RESTART condition
bra I2CWait ;
endasm
Carey Fisher │ Director of Engineering
O +1 770-209-0012 x2031 │ F +1 770-209-0719
4295 Hamilton Mill Road │ Suite 100 │ Buford, GA 30518
www.omnimetrix.net
Tell us how we're doing! Click here to take our two-question survey.
This code tries to read address 0000 of an attached 24LC512 EEPROM. I know the hardware is good because I can use Pic Basic Pro and read and write the EEPROM using I2C.
The code I'm using came off a forum on the Microchip Support website. It is "bullet proof". Ha.
I've put debugging statements in the code and what I've found is that the Interrupt flag, PIR3,SSP1IF, never gets asserted.
Any pointers would be greatly appreciated.
'i2ctest.pbp
'another try at reading I2C data from 24LC512
'Start Condition
'Control Byte for write
'High address byte
'Low address byte
'Start Condition
'Control Byte for read
'Read in data byte
'Return with NACK
'Stop Condition
asm
#define Memtop 0x10000 ;higest memory in the 18F67k40 processor
Startaddress = MemTop-800
SDA = 4
SCL = 3
ORG Startaddress
;***************************************************************
;
; 24LC256 I2C Single Byte Read Routines
;
;
;
_I2CTEST1
rcall I2CInit
;EE_RD
rcall I2CStrt ;send I2C START
movlw h'A6'
rcall I2CSend ;send device address (control byte)
movlw h'00'
rcall I2CSend ;send address hi byte
movlw h'00'
rcall I2CSend ;send address lo byte
rcall I2CStrt ;send I2C START
movlw h'A7'
rcall I2CSend ;send device address (control byte)
EE_R1
rcall I2CRcv ;receive mode (receive byte)
movwf _testbyte1
rcall I2CNack ;signal end of receive
rcall I2CStopp ;send STOP
return ;
;***************************************************************
;
; I2C primitives
;
; SSPADD = ((Fosc/I2Cclock)/4)-1 {formula from AN735}
;
I2CInit
bsf PIE3,SSP1IE ; enable the Interrupt flag, PIR3,SSP1IF
bsf TRISC,SCL ;
bsf TRISC,SDA ;
movlw b'00101000' ;SSPEN + 12C Master
movwf SSP1CON1 ;
movlw b'00000000' ;
movwf SSP1CON2 ;
movlw b'00000000' ;slew rate on for 400khz
movwf SSP1STAT ;
movlw d'13' ;(20000000/100000)-1/4
movwf SSP1ADD ;400khz using 20mhz xtal
return ;
I2CDebug
movf ssp1stat,0
movwf _testbyte1
movf ssp1con1,0
movwf _testbyte2
movf ssp1con2,0
movwf _testbyte3
movf ssp1con3,0
movwf _testbyte4
return
I2CWait
btfss PIR3,SSP1IF ;complete flag?
bra I2CWait ;no, wait
bcf PIR3,SSP1IF ;clear irq flag
return ;
I2CStrt
bsf SSP1CON2,SEN_SSP1CON2 ;send START condition
bra I2CWait ;
I2CStopp
bsf SSP1CON2,PEN ;send STOP condition
bra I2CWait ;
I2CSend
movwf SSP1BUF ;send byte
bra I2CWait ;
I2CRcv
btfsc SSP1STAT,R_W ;transmit complete?
bra I2CRcv ;no, wait
bsf SSP1CON2,RCEN ;receive enable
bcf PIR3,SSP1IF ;clear irq flag
rcall I2CWait ;
movf SSP1BUF,W ;
return ;
I2CAck
bcf SSP1CON2,ACKDT ;set ACK
bra I2CNack+2 ;
I2CNack
bsf SSP1CON2,ACKDT ;set NACK
bsf SSP1CON2,ACKEN ;send ACK or NACK
bra I2CWait ;
I2CRes
bsf SSP1CON2,RSEN ;send RESTART condition
bra I2CWait ;
endasm
Carey Fisher │ Director of Engineering
O +1 770-209-0012 x2031 │ F +1 770-209-0719
4295 Hamilton Mill Road │ Suite 100 │ Buford, GA 30518
www.omnimetrix.net
Tell us how we're doing! Click here to take our two-question survey.
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailm
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailm