PDA

View Full Version : Another I2C Slave Routine Problem



DanPBP
- 13th February 2009, 07:42
Hello Guys,

I spent a few days reading the forum, checking and double checking, and I hit a dead end...

I have two PICs 16F88, one master and one slave... I'm able to tell the slave to do something, but I'm not able to read a byte from the slave...

This is the master, pretty simple:



'-------------------------------------------------------------------------------
'-------------------------------------------------------------------------------
' Master
'-------------------------------------------------------------------------------
'-------------------------------------------------------------------------------

@ device pic16f88, intrc_osc_noclkout ' system clock options
@ device pic16f88, wdt_on ' watchdog timer
@ device pic16f88, pwrt_on ' power-on timer
@ device pic16f88, mclr_off ' master clear options (internal)
@ device pic16f88, protect_off ' code protect off

'-------------------------------------------------------------------------------

DEFINE OSC 8
OSCCON = %01110001 '8mhz

'-------------------------------------------------------------------------------

CMCON = 7
ADCON1 = 0 ' Disable A/D converter
ADCON0 = 0
ANSEL = 0 ' all analog pins to digital

'-------------------------------------------------------------------------------


DEFINE I2C_HOLD 1
DEFINE I2C_SLOW 1

scl VAR PORTB.4 ' i2c clock input
sda VAR PORTB.1 ' i2c data input

valor VAR BYTE ' value to read
LED VAR PORTA.6

'-------------------------------------------------------------------------------

main:
valor = 0

' I2CWrite sda, scl, $01, [31]
' High LED
' Pause 100
' I2CWrite sda, scl, $01, [30]
' Low LED
' Pause 100

I2CRead sda, scl, $01, [valor]

IF (valor = 24) Then
High LED
Pause 500
Low LED
EndIF

GoTo main

'-------------------------------------------------------------------------------

End

'-------------------------------------------------------------------------------



And, this is the slave:



'-------------------------------------------------------------------------------
'-------------------------------------------------------------------------------
' Slave
'-------------------------------------------------------------------------------
'-------------------------------------------------------------------------------

@ device pic16f88, intrc_osc_noclkout ' system clock options
@ device pic16f88, wdt_on ' watchdog timer
@ device pic16f88, pwrt_on ' power-on timer
@ device pic16f88, mclr_off ' master clear options (internal)
@ device pic16f88, protect_off ' code protect off

'-------------------------------------------------------------------------------

DEFINE OSC 8
OSCCON = %01110001 '8mhz

'-------------------------------------------------------------------------------

CMCON = 7
ADCON1 = 0 ' Disable A/D converter
ADCON0 = 0
ANSEL = 0 ' all analog pins to digital

'-------------------------------------------------------------------------------

DEFINE I2C_HOLD 1
DEFINE I2C_SLOW 1

'-------------------------------------------------------------------------------

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

'-------------------------------------------------------------------------------

I2Caddress CON 1 ' Make our address 1

'-------------------------------------------------------------------------------

SSPADD = I2Caddress ' Set our address
SSPCON = $36 ' Set to I2C slave with 7-bit address

'-------------------------------------------------------------------------------

LEDR VAR PORTA.3 ' LED 1
LEDL VAR PORTB.3 ' LED 2
BZR VAR PORTA.7 ' Buzzer
RL6 VAR PORTB.6 ' Relay 1
RL12 VAR PORTB.5 ' Relay 2

'-------------------------------------------------------------------------------

datain VAR BYTE ' Data in
dataout VAR BYTE ' Data out

'-------------------------------------------------------------------------------

GoTo boot

'-------------------------------------------------------------------------------

i2cslave: ' I2C slave subroutine
SSPIF = 0

IF R_W = 1 Then i2crd ' Read data from us

IF BF = 0 Then i2cexit ' Nothing in buffer so exit

IF D_A = 1 Then i2cwr ' Data for us (not address)

IF SSPBUF != I2Caddress Then i2cexit ' Clear the address from the buffer

GoTo i2cexit

'-------------------------------------------------------------------------------

i2cwr: ' I2C write data to us

datain = SSPBUF ' Put data into array

GoTo i2cexit

'-------------------------------------------------------------------------------

i2crd: ' I2C read data from us

SSPBUF = dataout ' Get data from array

CKP = 1 ' Release SCL line

GoTo i2cexit ' That's it

'-------------------------------------------------------------------------------

i2cexit:
SSPOV = 0

WCOL = 0

Return

'-------------------------------------------------------------------------------

boot:

Pause 500

High RL12

High RL6

'-------------------------------------------------------------------------------

main:
dataout = 24

IF SSPIF = 1 Then
GoSub i2cslave
Toggle LEDL
EndIF


IF datain = 121 Then ' on 12v
High RL12
EndIF

IF datain = 120 Then ' off 12v
Low RL12
EndIF

IF datain = 61 Then ' on 6v
High RL6
EndIF

IF datain = 60 Then ' off 6v
Low RL6
EndIF

IF datain = 31 Then ' on buzer
High BZR
EndIF

IF datain = 30 Then ' off buzzer
Low BZR
EndIF

GoTo main

'-------------------------------------------------------------------------------

End

'-------------------------------------------------------------------------------
'-------------------------------------------------------------------------------


Could someone please through some light over here... I really don't know what else to check...

Thanks a lot!

Daniel.

Acetronics2
- 13th February 2009, 09:15
Hi, Daniel

Did you had a look to the " I2cMaster " and " I2CSlave " example programs given on Melabs site ???

looks something is read from the slave chip.

Alain

DanPBP
- 14th February 2009, 04:10
Yes, all my code is based on those two files, and those two files don't work out of the box... I mean, the I2C routines don't work without adding a few more lines... And, I'm still in the dark... :(

DanPBP
- 15th February 2009, 11:05
Anyone willing to share ideas or a working code?

Thanks!

Daniel.

DanPBP
- 19th February 2009, 05:50
Problem solved!

Check here: http://www.picbasic.co.uk/forum/showthread.php?t=10141