PDA

View Full Version : I2C slave



Charles Linquis
- 16th March 2008, 00:44
I'm trying to make an I2C slave using some code I found in this forum originally
written for the 16F series.

I'm using an 18F8722 and as you can see, I'm trying a "loopback" technique where
I'm sending data using I2CWRITE and trying to receive it using the hardware I2C port.

PORTF.3 is connected to PORTC.4 and
PORTF.5 is connected to PORTC.3

Both have 4.7K pull-ups.

Some LEDs are connected to PortD. The code loops, but PIR1.3 is never set.

Does anyone know what I should try next? The code is below.


SDAS VAR PORTF.3
SCLS VAR PORTF.5


TRISC = %10111111
TRISD = %00000000
TRISF = %11111111

ADCON1 = 0 ; No analog


HSEROUT ["Alive",13,10]

PORTD = 0 ' Shut off all LEDs

SSP1STAT.7 = 1 ' Low speed mode
SSP1STAT.6 = 0 ' Disable SMBbus-specific inputs


' Define used register flags
SSPIF VAR PIR1.3 ' SSP (I2C) interrupt flag
BF VAR SSP1STAT.0 ' SSP (I2C) Buffer Full
R_W VAR SSP1STAT.2 ' SSP (I2C) Read/Write
D_A VAR SSP1STAT.5 ' SSP (I2C) Data/Address
CKP VAR SSP1CON1.4 ' SSP (I2C) SCK Release Control
SSPEN VAR SSP1CON1.5 ' SSP (I2C) Enable
SSPOV VAR SSP1CON1.6 ' SSP (I2C) Receive Overflow Indicator
WCOL VAR SSP1CON1.7 ' SSP (I2C) Write Collision Detect



' Define VARs and types

datain VAR BYTE ' Data in
counter1 VAR BYTE
I2Caddress VAR BYTE

' Initialize I2C slave mode

I2CAddress = $0

SSPADD = I2caddress ' Set our address
SSPCON1 = %00110110 ' I2C slave with 7-bit address
' PIE1.3 = 1 ' SSP (I2C) interrupt enable '- don't need Ints for this test
' INTCON.6 = 1 ' PEIE = Peripheral Interrupt Enable bit
' INTCON.7 = 1 ' GIE enable
SSPIF = 0
SSPSTAT = $00



mainloop:

HSEROUT ["."] ' Indicate we are doing something

IF SSPIF THEN
SSPIF = 0 ' clear the flag
datain = SSPBUF
SSPBUF = datain
CKP = 1
GOSUB Blink5 ' let me know that i2c receive occurred
ENDIF


I2CWRITE SDAS,SCLS,$0,$0,[$55] ; Send some data, any data
PAUSE 2000

GoTo mainloop ' Do it forever

Blink5:
For counter1 = 1 to 5
PORTD = $FF
PAUSE 100
PORTD = 0
PAUSE 100
NEXT Counter1
RETURN


END

Archangel
- 16th March 2008, 03:25
Hi Charles,
If I get this correctly you are sending out and receiving on the same PIC . . .
MeThinks that isn't going to work because by the time it gets around to receiving
the info the output has already sent it. . . Now I maybe totally wrong. Have you tried using 2 PICs?

Charles Linquis
- 16th March 2008, 03:32
I am sending and receiving on the same PIC. I'm sending using PBP bit-banging and I'm receiving using the hardware. Unless I'm mistaken, when I send a byte out using I2CWRITE, the hardware should grab it and set PIR1.3.

Anyway, I have done the same thing using SEROUT and HSERIN. That works.

BTW: I noticed that I had ADCON1 set to "0". I changed it to $FF (all digital) and it still doesn't work.

Archangel
- 16th March 2008, 03:46
Hi Charles,
I never thought that would work, ' have to give that a try.
Cheers
JS