Jump to content
Sign in to follow this  
qwerty860

Slave I2c With Pic16f88

Recommended Posts

Hello,

 

I am trying to use my PIC16F88 as a slave i2c device. I am using BoostC.

 

I have a status led that starts out on, and as soon as an interrupt is detected it will turn off. When I connect the PIC to the master I2C, the led never turns off. I'm sure the master is working correctly and the address is correct.

 

Any ideas?

 

My code is as follows:

 

 

#include <system.h>

#pragma CLOCK_FREQ 8000000
#pragma DATA 0x2007, 0b0001111100010000

volatile bit led1	@ PORTB.3;

void init_i2c_slave();
void interrupt();

void main()
{
//*********************//
// configure I/O ports //
//*********************//
portb   = 00000000b;
trisb   = 11110111b; //0 = Output, 1 = Input
porta   = 00000000b;	 
trisa   = 11111111b; //0 = Output, 1 = Input
osccon  = 01110000b; //internal oscillator @ 8MHz
cmcon   = 00000111b; //comparators off
ansel   = 00000001b; //AN0 on (RA0)

init_i2c_slave();

led1 = 1;

while(1);
}

void interrupt()
{
led1 = 0;
  if (sspstat&0x04)
 {
   //I2C Slave Transmission
   sspbuf=0;
   sspcon.4 = 1;// CKP, SSPBUFF must be write before
   pir1.3 = 0;//reset the interrupt flag
 }
  else
 {
	//I2C Slave Reception
	pir1.3 = 0;//reset the interrupt flag
 }
}

void init_i2c_slave()
{
//SSPSTAT
sspstat.7 = 0;  // this bit must be maintained clear
sspstat.6 = 0;// this bit must be maintained clear
//SCL and SDA as input
trisb.1 = 1; //SDA
trisb.4 = 1; //SCL
//SSPCON
sspcon.7 = 0;// WCOL Write Collision Detect bit
sspcon.6 = 0;// SSPOV Receive Overflow Indicator bit
sspcon.5 = 1;// Synchronous Serial Port Enable
sspcon.4 = 1;// CKP
sspcon.3 = 0;//0110: I2C slave mode: 7 bits address
sspcon.2 = 1;
sspcon.1 = 1;
sspcon.0 = 0;
sspadd=0x86;
//SSP IE
pie1.3 = 1;
pir1.3 = 0;
//Interrupt Control
intcon.7=1;	 //enable global interrupt
intcon.6=1;	 //enable global interrupt
}

 

Thanks,

qwerty860

Share this post


Link to post
Share on other sites

Howdy, firstly I would enable the SSP (sspcon.5) last, after everything else, or all together in a single write e.g. sspcon = 0x36, also do you have pullup resistors connected. Try that and post back if it don't work.

Also in your interupt routine you should test for 'pir1.3' flag, not 'sspstat&0x04', you seem to be testing if a start bit was detected last.

Share this post


Link to post
Share on other sites

Hi Chris,

 

Thanks for your suggestions. I made the changes to the code you suggested and I am still having no luck. (I was using pull-ups the first time). Any other ideas?

 

Thanks again

Share this post


Link to post
Share on other sites
Hi Chris,

 

Thanks for your suggestions. I made the changes to the code you suggested and I am still having no luck. (I was using pull-ups the first time). Any other ideas?

 

Thanks again

 

 

Hi qwerty860, please post your current code.

Share this post


Link to post
Share on other sites

Here is my most recent code. It is changed somewhat from above, most notable I've switched to a 20 MHz clock, my master is "fast i2c" so it runs at 400 KHz. I also changed some of the ordering of the initialization from some one else's suggestions. Still, no luck.

 

Thanks in advance!

 

#include <system.h>

#pragma CLOCK_FREQ 20000000
#pragma DATA 0x2007, 0b0001111100000010
//					 0b00* * = 0 for code protect

volatile bit led1	@ PORTB.3;

void init_i2c_slave();
void interrupt();

void main()
{
//*********************//
// configure I/O ports //
//*********************//
portb   = 00000000b;
trisb   = 11110111b; //0 = Output, 1 = Input
porta   = 00000000b;	
trisa   = 11111111b; //0 = Output, 1 = Input
osccon  = 01110000b; 
cmcon   = 00000111b; //comparators off
ansel   = 00000001b; //AN0 on (RA0)

init_i2c_slave();

led1 = 1;

while(1) nop();
}

void interrupt()
{
led1 = 0;
}

void init_i2c_slave()
{
trisb.1 = 1; //SDA
trisb.4 = 1; //SCL
sspcon = 0x36;

sspadd = 0x50;
sspstat = 0;
pie1.3 = 1;  //sspie
pir1.3 = 0; //sspif
pie1 = 1;
intcon.6 = 1; //PEIE
intcon.7 = 1; //Global Interrupt Enable
}

Share this post


Link to post
Share on other sites
Here is my most recent code. It is changed somewhat from above, most notable I've switched to a 20 MHz clock, my master is "fast i2c" so it runs at 400 KHz. I also changed some of the ordering of the initialization from some one else's suggestions. Still, no luck.

 

Thanks in advance!

 

#include <system.h>

#pragma CLOCK_FREQ 20000000
#pragma DATA 0x2007, 0b0001111100000010
//					 0b00* * = 0 for code protect

volatile bit led1	@ PORTB.3;

void init_i2c_slave();
void interrupt();

void main()
'{
//*********************//
// configure I/O ports //
//*********************//
portb   = 00000000b;
trisb   = 11110111b; //0 = Output, 1 = Input
porta   = 00000000b;	
trisa   = 11111111b; //0 = Output, 1 = Input
osccon  = 01110000b; 
cmcon   = 00000111b; //comparators off
ansel   = 00000001b; //AN0 on (RA0)

init_i2c_slave();

led1 = 1;

while(1) nop();
}

void interrupt()
{
led1 = 0;
}

void init_i2c_slave()
{
trisb.1 = 1; //SDA
trisb.4 = 1; //SCL
sspcon = 0x36;

sspadd = 0x50;
sspstat = 0;
pie1.3 = 1;  //sspie
pir1.3 = 0; //sspif
pie1 = 1;
intcon.6 = 1; //PEIE
intcon.7 = 1; //Global Interrupt Enable
}

 

As I said before, I would initialise the sspadd register, clear sspstat and then initialise sspcon (in that order), also I noticed that you have enabled the interupt 'pie1.3 = 1; //sspie' and then disabled it 'pie1 = 1'. Delete the line 'pie1 = 1;'. Make sure you have pullups connected and in your interupt routine test for the pir1 sspif flag being set and if so then clear it. Hopefully that should get your led to blink, if not then maybe check your master is sending the right address.

Share this post


Link to post
Share on other sites

Hi all,

 

Bit of an old thread, but...

 

Have you ever made this Slave code work? I am busy porting my 16F887 Slave code to 16F88 and scanning the forums for any issues...

 

Please - let me know.

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...