PDA

View Full Version : Please help with i2cslave i2c slave



cycle_girl
- 28th November 2005, 23:06
I have searched through posts and read application notes, especially AN734 and
http://www.melabs.com/resources/samples/pbp/i2cslave.bas

However, I am still unable to get my 16F690 to operate in slave mode. In the code below, if SSPIF ever gets set, I'm supposed to get 5 blinks of LED1. I send READ and WRITE commands to i2c address 0, but never get the 5 blinks.

I'm also monitoring SCL and SDA and the PIC is not generating an ACK (not pulling down SDA after the 8th clock cycle of the control byte). Can anyone help? cycle_girl

' Alias pins
scl VAR PORTB.6 ' I2C clock input
sda VAR PORTB.4 ' I2C data input

' Define used register flags
SSPIF VAR PIR1.3 ' SSP (I2C) interrupt flag
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 SSPCON.4 ' SSP (I2C) SCK Release Control
SSPEN VAR SSPCON.5 ' SSP (I2C) Enable
SSPOV VAR SSPCON.6 ' SSP (I2C) Receive Overflow Indicator
WCOL VAR SSPCON.7 ' SSP (I2C) Write Collision Detect

' Define constants
I2Caddress CON $00

' Allocate RAM
datain VAR BYTE ' Data in
counter1 VAR BYTE

' Initialize I2C slave mode
TRISC = %00000000 ' set PORTC to output ffor LEDs
TRISB = %11111111 ' set PORTB to all input for i2c

SSPADD = I2caddress ' Set our address
SSPCON = $36 ' 00110110 Set to I2C slave with 7-bit address
PIE1.3 = 1 ' SSP (I2C) interrupt enable
INTCON.6 = 1 ' PEIE = Peripheral Interrupt Enable bit
INTCON.7 = 1 ' GIE enable
SSPIF = 0
SSPSTAT = $00

mainloop:

PORTC = $0A ' turns on LED1 and LED3 so I can see that it's running

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

GoTo mainloop ' Do it forever

Blink5:
For counter1 = 1 to 5
PORTC = $02
PAUSE 100
PORTC = $00
PAUSE 100
NEXT
RETURN

end

cycle_girl
- 29th November 2005, 19:00
I found the problem myself...

ANSELH = $00

I needed to set my SDA line to digital. Apparently, it defaults to analog.

mister_e
- 1st December 2005, 13:55
<img src=http://www.picbasic.co.uk/forum/attachment.php?attachmentid=639&stc=1&d=1133445289>