Here is a Pic SSP header file which allows a Pic with a MSSP to control it thru the Hardware I2C interface. In theory a software master I2C would be able to do the same, although not tested. Enjoy!
;***************************************************************
;This library is for the use of a Pic device as an I2C Slave
;thru the Synchronous Slave Port (SSP). This is a port of the
;Microchip AN734a to GCBasic.
;By Kent Schafer August 16,2007
;
;Pic is a tm of Microchip, I2C is a tm of Phillips/NXP
;***************************************************************
Sub InitSSP
SSPCON = 0x36 ;set for 7 bit address
SSPADD = PicSlave ;I2C address of slave
#script ;GCBasic treats AnyArray(1) as variable
RxBufferSize = BufferLen
if BufferLen = 1 then RxBufferSize = 2
'error Buffer Size BufferLen, Array Size RxBufferSize 'uncomment to see input and output of script in error listing
#endscript
;Setup Interrupt
PIR1 = 0
index = 0
Set SSPIE On ;Enable SSP peripheral interrupt
Set PEIE On ;Enable all peripheral interrupts
Set GIE On ;Enable global interrupts
End Sub
Sub Interrupt
FlagCheck:
If SSPIF Off Then goto FlagCheck
Set SSPIF Off ;Clear SSP flag
SlaveStatus ;Sub to check SSP status bits
End Sub
Sub SlaveStatus
SSPSTATtemp = SSPSTAT AND b'00101101'
Select Case SSPSTATtemp
'Case #1 Write operation, last byte was an address, buffer full
Case b'00001001'
For index = 1 to BufferLen
RxBuffer(index) = 0
Next
DummyRead = SSPBUF 'Dummy read of SSBUF
'Case #2 Write operation, last byte was data, buffer is full
Case b'00101001'
For index = 1 to BufferLen
RxBuffer(index) = SSPBUF
Next
'Case #3 Read Operation, last bytes was an address, buffer is empty
Case b'00001100'
index = 1
BufferTemp = RxBuffer(index)
WriteI2C BufferTemp
'Case #4 Read operation, last byte was data, buffer is empty
Case b'00101100'
For index = 2 to BufferLen
BufferTemp = RxBuffer(index)
WriteI2C BufferTemp
Next
'Case #5 A Nack was received when transmitting data from master
Case b'00101000'
;Set ERROR On
End Select
End Sub
Sub WriteI2C(BufferTemp)#NR
TestBuffer:
If BF = 1 Then goto TestBuffer ;Is buffer full?
Collision:
WCOL = 0 ;Clear the collision flag
SSPBUF = BufferTemp
If WCOL = 1 Then goto Collision ;Was there a collision?
CKP = 1 ;Release clock
End sub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here is a Pic SSP header file which allows a Pic with a MSSP to control it thru the Hardware I2C interface. In theory a software master I2C would be able to do the same, although not tested. Enjoy!
;***************************************************************
;This library is for the use of a Pic device as an I2C Slave
;thru the Synchronous Slave Port (SSP). This is a port of the
;Microchip AN734a to GCBasic.
;By Kent Schafer August 16,2007
;
;Pic is a tm of Microchip, I2C is a tm of Phillips/NXP
;***************************************************************
Sub InitSSP
SSPCON = 0x36 ;set for 7 bit address
SSPADD = PicSlave ;I2C address of slave
#script ;GCBasic treats AnyArray(1) as variable
RxBufferSize = BufferLen
if BufferLen = 1 then RxBufferSize = 2
'error Buffer Size BufferLen, Array Size RxBufferSize 'uncomment to see input and output of script in error listing
#endscript
;Setup Interrupt
PIR1 = 0
index = 0
Set SSPIE On ;Enable SSP peripheral interrupt
Set PEIE On ;Enable all peripheral interrupts
Set GIE On ;Enable global interrupts
End Sub
Sub Interrupt
FlagCheck:
If SSPIF Off Then goto FlagCheck
Set SSPIF Off ;Clear SSP flag
SlaveStatus ;Sub to check SSP status bits
End Sub
Sub SlaveStatus
SSPSTATtemp = SSPSTAT AND b'00101101'
Select Case SSPSTATtemp
'Case #1 Write operation, last byte was an address, buffer full
Case b'00001001'
For index = 1 to BufferLen
RxBuffer(index) = 0
Next
DummyRead = SSPBUF 'Dummy read of SSBUF
'Case #2 Write operation, last byte was data, buffer is full
Case b'00101001'
For index = 1 to BufferLen
RxBuffer(index) = SSPBUF
Next
'Case #3 Read Operation, last bytes was an address, buffer is empty
Case b'00001100'
index = 1
BufferTemp = RxBuffer(index)
WriteI2C BufferTemp
'Case #4 Read operation, last byte was data, buffer is empty
Case b'00101100'
For index = 2 to BufferLen
BufferTemp = RxBuffer(index)
WriteI2C BufferTemp
Next
'Case #5 A Nack was received when transmitting data from master
Case b'00101000'
;Set ERROR On
End Select
End Sub
Sub WriteI2C(BufferTemp)#NR
TestBuffer:
If BF = 1 Then goto TestBuffer ;Is buffer full?
Collision:
WCOL = 0 ;Clear the collision flag
SSPBUF = BufferTemp
If WCOL = 1 Then goto Collision ;Was there a collision?
CKP = 1 ;Release clock
End sub