Hi, Jinx wrote: > Does anyone have simple, repeat simple, code to show > setup and operation of said module to read / write bytes > to an EEPROM ? Examples I've found so far are convoluted > over-blown state machines for multi-device systems that are > way OTT for my needs. So much use of collision and error Hmm... what I have are pretty simple, however as it is an general routine intended to be used be used inside the isr one might call them overblown. But it's easy to rip out or simplify by omitting as desired. But in case you stand by your request that an state machine is out of the question I'll refrain from posting the complete code unless you perhaps have a second thought :)=20 > routines I found it hard to pick out the basics. Normally I'd > nut it out myself but I've read that some examples (MC in > particular) aren't worth the screen they're printed on, so I'd > rather not waste time on published code that doesn't work > > TIA Agreed, but mine is actually based on an m-chip example but polished until is actually have some uses :)=20 BTW it's two 'modules' one for the i2c master (isr) functions and one for the eechip functions. State =3D 0 -> local i2c idle=20 State =3D 1-3 -> i2c read State =3D 4-8 -> i2c write Uses BuffBankFsr (defined to any fsrX ) to read from any memory location within an dedicated buffer bank (only low adress used) when writing. Uses FSR0 for reding to any memory location when reading. Here's the main i2c state machine handler ( all of it ): ; *********************************************************************** ; ; I2C_INT_HANDLER - Handles read/writes to/from i2c bus devices ; Uses an state variable i2c_State to preserv state between calls ; I2C_INT_HANDLER GLOBAL I2C_INT_HANDLER BCF _i2c_timetick ; clear timeout detect bit MOVF i2c_State,W,A TABLE_JUMP_ISR_GTO I2C_JumpWrite I2C_JumpWrite ; address were jump table branch occurs, this addr also used in fill=20 ; jump to processing for each state =3D i2cState value for each state GOTO I2C_WrtStart ; write start sequence =3D 0 GOTO I2C_SendWrtAddr ; write address, R/W=3D1 =3D 1 GOTO I2C_WrtAckTest ; test ack,write data =3D 2 GOTO I2C_WrtStop ; do stop if done =3D 3 I2C_JumpRead GOTO I2C_ReadStart ; read start sequence =3D 4 GOTO I2C_SendReadAddr ; read address, R/W=3D0 =3D 5 GOTO I2C_ReadAckTest ; test acknowledge after address =3D 6 GOTO I2C_ReadData ; read more data =3D 7 GOTO I2C_ReadStop ; generate stop sequence =3D 8 I2C_JumpEnd =20 Fill (RETURN), (I2C_JumpWrite-I2C_JumpEnd) + ( i2c_SizeMask*4 ) ;---------------------------------------------------------------------- ; ********************* Write data to Slave ********************* ;---------------------------------------------------------------------- ; Generate I2C bus start condition [ I2C STATE -> 0 ] I2C_WrtStart INCF i2c_State,F,A ; update I2C state variable BSF SSPCON2,SEN,A ; initiate I2C bus start condition MOVLW LOW(I2C_Buffer) ; setup buffer pointer to start of i2c buffer MOVWF i2c_write_ptr,A ; RETURN ;=20 ; Generate I2C address write (R/W=3D0) [ I2C STATE -> 1 ] I2C_SendWrtAddr MOVF i2c_temp_address,W,A ; get 7-bit address INCF i2c_State,F,A ; update I2C state variable MOVWF SSPBUF,A ; initiate I2C bus write condition RETURN ;=20 ; Test acknowledge after address and data write [ I2C STATE -> 2 ] I2C_WrtAckTest BTFSS SSPCON2,ACKSTAT,A ; test for acknowledge from slave GOTO I2C_WrtData ; go to write data module BSF _i2c_ackerror ; set acknowledge error CLRF i2c_State,A ; reset I2C state variable BSF SSPCON2,PEN,A ; initiate I2C bus stop condition RETURN ; ; Generate I2C write data condition I2C_WrtData ; first check if write count is zero MOVF i2c_write_count,F,A ; reload BTFSC STATUS,Z GOTO I2C_no_error ; no bytes to write MOVF i2c_write_ptr,W,A ; retrieve ptr address MOVWF BuffBankFSR ; initialize FSR for indirect access INCF i2c_write_ptr,F,A ; inc. pointer for next byte MOVF BuffBankINDF,W,A ; retrieve byte into w DECFSZ i2c_write_count,F,A ; test if all done with writes GOTO I2C_send_byte ; not end of string INCF i2c_State,F,A ; update I2C state variable I2C_send_byte MOVWF SSPBUF,A ; initiate I2C bus write condition RETURN ;=20 ; Generate I2C bus stop condition [ I2C STATE -> 3 ] I2C_WrtStop BTFSS SSPCON2,ACKSTAT,A ; test for acknowledge from slave GOTO I2C_no_error ; bypass setting error flag BSF _i2c_ackerror ; set acknowledge error CLRF i2c_State,A ; reset I2C state variable GOTO I2C_stop I2C_no_error CLRF i2c_State,A ; reset I2C state variable ( to idle ) BSF _i2c_writedone ; set write done flag I2C_stop BSF SSPCON2,PEN,A ; initiate I2C bus stop condition RETURN ; ;---------------------------------------------------------------------- ; ********************* Read data from Slave ********************* ;---------------------------------------------------------------------- ; Generate I2C start condition [ I2C STATE -> 4 ] I2C_ReadStart INCF i2c_State,f ; update I2C state variable BSF SSPCON2,SEN ; initiate I2C bus start condition RETURN ;=20 ; Generate I2C address write (R/W=3D1) [ I2C STATE -> 5 ] I2C_SendReadAddr=20 MOVF i2c_temp_address,W,A ; get 7-bit address INCF i2c_State,F,A ; update I2C state variable MOVWF SSPBUF,A ; initiate I2C bus write condition RETURN ;=20 ; Test acknowledge after address write [ I2C STATE -> 6 ] I2C_ReadAckTest BTFSS SSPCON2,ACKSTAT,A ; test for acknowledge from slave GOTO I2C_StartReadData ; good ack, go issue bus read BSF _i2c_ackerror ; set acknowledge error CLRF i2c_State,A ; reset I2C state variable BSF SSPCON2,PEN,A ; initiate I2C bus stop condition RETURN ; I2C_StartReadData BSF SSPCON2,RCEN, ; generate receive condition INCF i2c_State,F,A ; update I2C state variable RETURN ; Read slave I2C [ I2C STATE -> 7 ] I2C_ReadData MOVF i2c_read_ptrH,W,A ; retrieve high ptr address MOVWF FSR0H ; initialize FSR0 high reg for indirect access MOVF i2c_read_ptrL,W,A ; retrieve low ptr address MOVWF FSR0L ; initialize FSR0 low reg for indirect access INCF i2c_read_ptrL,F,A ; inc. pointer for next byte MOVFF SSPBUF,INDF0 ; save recieved byte=20 DECFSZ i2c_read_count,F,A ; test if all done with reads GOTO I2C_SendReadAck ; not end of string so send ACK ; End of data Send Not Acknowledge I2C_SendReadNack INCF i2c_State,F,A ; update I2C state variable ( nxt is stop ) BSF SSPCON2,ACKDT,A ; acknowledge bit state to send (not ack) BSF SSPCON2,ACKEN,A ; initiate acknowledge sequence RETURN ; Send Acknowledge I2C_SendReadAck BCF SSPCON2,ACKDT,A ; acknowledge bit state to send BSF SSPCON2,ACKEN,A ; initiate acknowledge sequence I2C_SendReadAck_Wait BTFSC SSPCON2,ACKEN,A ; ack cycle complete? GOTO I2C_SendReadAck_Wait ; no, so loop again NOP BCF PIR1,SSPIF,A ; clear SSP H/W flag for ack NOP BSF SSPCON2,RCEN,A ; generate receive condition RETURN ;=20 ; Generate I2C stop condition [ I2C STATE -> 8 ] I2C_ReadStop BSF SSPCON2,PEN,A ; initiate I2C bus stop condition CLRF i2c_State,A ; reset I2C state variable BSF _i2c_readdone ; set read done flag RETURN=20 -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.