Try this code, I cleaned it up a lot. Slave address is 2.
1) From the master, try to read 12 from the slave
2) Try to send one byte (zero) to the slave and do something.
I don't have MPASM so, it could be some small errors, check them.
Code:'Oscillator Initialization DEFINE OSC 32 ' Osc. in MHz OSCCON = %01110000 OSCTUNE.6 = 1 ADCON0.0 = 0 ' Turns off A-to-D module ADCON1 = $0F ' Makes 100% port A digital TRISA = %00000000 CMCON = 7 ' Comparators Off DEFINE I2C_HOLD 1 ' Define used register flags SSPIF VAR PIR1.3 ' SSP (I2C) interrupt flag SSPIE VAR PIE1.3 ' SSP Int Enable BF VAR SSPSTAT.0 ' SSP (I2C) Buffer Full R_W VAR SSPSTAT.2 ' SSP (I2C) Read/Write D_A VAR SSPSTAT.5 ' SSP (I2C) Data/Address CKP VAR SSPCON1.4 ' SSP (I2C) SCK Release Control SSPEN VAR SSPCON1.5 ' SSP (I2C) Enable SSPOV VAR SSPCON1.6 ' SSP (I2C) Receive Overflow Indicator WCOL VAR SSPCON1.7 ' SSP (I2C) Write Collision Detect 'STRETCH VAR SSPCON2.0 ' SSP (I2C) Clock stretch '---------------- Alias pins ----------------------------------- TRISC.3 = 1 ' input TRISC.4 = 1 ' input '--------------- Define used register flags ------------------- STAT_BF VAR SSPSTAT.0 ' SSP (I2C) Buffer Full STAT_RW VAR SSPSTAT.2 ' SSP (I2C) Read/Write STAT_DA VAR SSPSTAT.5 ' SSP (I2C) Data/Address CKE VAR SSPSTAT.6 ' SSP (I2C) Data/Address '------------------- Rx Buffer definition -------------------- RxBufferLEN CON 1 RxBuffer VAR BYTE[Rxbufferlen] RxBufferIndex VAR BYTE '------------------ Tx Buffer definition ---------------------- TxBufferLEN CON 1 TxBuffer VAR BYTE[txbufferlen] TxBufferIndex VAR BYTE '------------------ Define constants ------------------------ I2Caddress CON $2 ' Make address = 2 ' Define constants datain VAR BYTE readcnt VAR BYTE WRData VAR BYTE 'SSPADD = My_I2C_address ' Set our address SSPADD = I2Caddress ' Set our address SSPCON2.7 = 0 ' General call address disabled SSPCON1 = $36 ' Set to I2C slave with 7-bit address SSPSTAT = 0 SSPIE = 1 SSPIF = 0 RxBufferIndex = 0 TxBufferIndex = 0 bf = 0 SSPOV = 0 'Port C harware UART settings DEFINE HSER_RCSTA 90h DEFINE HSER_TXSTA 20h DEFINE HSER_BAUD 9600 DEFINE HSER_CLROERR 1 '----------------- Initialization Done! ----------------------------- GoTo main '------------------------------------------------------------------------------- I2C_Slave: ' I2C slave subroutine sspif = 0 ' clear interrupt flag IF r_w = 1 Then I2C_Read ' read data from us IF bf = 0 Then I2C_Exit ' nothing in buffer so exit IF d_a = 1 Then I2C_Write ' data for us (not address) IF sspbuf != i2caddress Then I2C_Exit ' clear the address from the buffer readcnt = 0 ' mark as first read GoTo I2C_Exit '------------------------------------------------------------------------------- I2C_Write: ' I2C write data to us datain = sspbuf ' put buffer data into array rxbuffer[rxbufferindex] = datain rxbufferindex=rxbufferindex + 1 IF rxbufferindex = rxbufferlen Then ' end of buffer transfer wrdata = 1 rxbufferindex = 0 EndIF GoTo i2c_exit '------------------------------------------------------------------------------- I2C_Read: ' I2C read data from us IF d_a = 0 Then txbufferindex = 0 EndIF While stat_bf : Wend ' koop while buffer is full wcol = 0 ' clear collision flag sspbuf = txbuffer[txbufferindex] While wcol wcol = 0 sspbuf = txbuffer[txbufferindex] Wend ckp = 1 ' release clock, allowing read by master txbufferindex = txbufferindex + 1 ' increment index IF txbufferindex = txbufferlen Then ' all bytes have been tx txbufferindex = 0 ' reset index EndIF GoTo i2c_exit '------------------------------------------------------------------------------- I2C_Exit: sspov = 0 wcol = 0 wrdata = 0 Return '------------------------------------------------------------------------------- '------------------------------------------------------------------------------- main: txbuffer[0] = 12 ' test value rxbuffer[0] = 0 IF sspif = 1 Then GoSub i2c_slave EndIF Select Case rxbuffer[0] Case 0 ' do something here, turn on LED for example. End Select GoTo main End




Bookmarks