Hi, Jinx wrote: >Tony, why do some of your instructions include the ,A ? well I noticed you already got an reply, but to recap,=20 it's an special ram bank than can always be accessed on the=20 18xxx chip without needing to set the ram bank selection bits. Sort of similar to the 16F8xx series 'mirrored ram'. Quite handy for isr routines or to pass variables between routines. Supposedly one should be able to omitt the ',A' when using reloctable code and the linker, but that just didn't work in the early versions.=20 This snippet was form that 'era' :) Anyway some minor points to the state handler: Only allows one tx/rx at a time, i.e. no i2c 'stack'. - before statring the i2c write, the following should be performed: - check that state=3D0 and that there are no activity on the bus - setup i2c slave adress (if not done before) - fill i2c buffer with relevant data, count=3Dno.of.bytes - call isr state handler *outside* the isr when everything is setup it will initiate the start condition and set state=3D1, form hereon everything will be handled inside the isr. - before statring the i2c read, the following should be performed: - check that state=3D0 and that there are no activity on the bus - setup i2c slave adress (if not done before) - setup dest adress pointers (where the data will be stored) - setup number of bytes to read - setup state=3D4 and call isr state handler *outside* the isr=20 when everything is setup it will initiate the start condition and set state=3D5, form hereon everything will be handled inside the isr. Some example snippets: *example isr handler* INT_TEST_I2C_IRQ =09 ; TEST FOR COMPLETION OF VALID I2C EVENT=20 BTFSS PIE1,SSPIE,A ; test if interrupt is enabled GOTO INT_TEST_ANOTHER_FLAG BTFSS PIR1,SSPIF,A ; test for SSP H/W flag GOTO INT_TEST_ANOTHER_FLAG ; optionally test for bus collision (if multi master) BCF PIR1,SSPIF,A ; clear SSP H/W flag TSTFSZ i2c_State,A ; if non zero then continue i2c seq. CALL I2C_INT_HANDLER ; service valid I2C event =09 *wait for idle* ( potential dangerous code, can loop forever, I also use an timer to detect timeout conditions so this is circumvented ) ;+++++ ; I2C_WAIT_IDLE Holds execution until there is no activity on the i2c bus ; Either local or remote I2C_WAIT_IDLE GLOBAL I2C_WAIT_IDLE ; check that there are no local activity ( all rx/tx's are done ) I2C_WAIT_IDLE_BUSY TSTFSZ i2c_State,A GOTO I2C_WAIT_IDLE_BUSY I2C_WAIT_IDLE_FLUSH ; test for i2c bus idle state BTFSC SSPSTAT,R_W,A ; test if transmit is progress=20 GOTO I2C_WAIT_IDLE_FLUSH ; module busy so wait I2C_WAIT_IDLE_BUS MOVF SSPCON2,W,A ; get copy of SSPCON2 for status bits ANDLW 0x1F ; mask out non-status bits BTFSS STATUS,Z ; test for zero state, if Z set, bus is idle GOTO I2C_WAIT_IDLE_BUS ; bus is busy so test again RETURN *start write* (can be outside isr) ;+++++ ; I2C_START_TX Starts an transmission with data in i2c buffer ; and current adress I2C_START_TX GLOBAL I2C_START_TX CLRF i2c_eventflags,A CALL I2C_INT_HANDLER RETURN *start read* (can be outside isr) #define I2C_RXSTART_STATE 0x04 ; i2c state for starting an rx sequence ;+++++ ; I2C_START_RX Starts an transmission with number of byte in w ; and current adress, data will be stored in i2c_buffer I2C_START_RX GLOBAL I2C_START_RX MOVWF i2c_read_count,A; save byte counter CLRF i2c_eventflags,A MOVLW I2C_RXSTART_STATE ; setup i2c state variable MOVWF i2c_State,A CALL I2C_INT_HANDLER ; and issue the start condition RETURN /Tony -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body