CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

I2C Slave problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
MathewF



Joined: 07 Dec 2006
Posts: 1

View user's profile Send private message

I2C Slave problem
PostPosted: Thu Dec 21, 2006 8:45 am     Reply with quote

i2c Slave Problem

I am using:

CCS Compiler PCM 4.013

Device PIC 16F819 as SLAVE

Device PIC 18F252 as MASTER


I am having trouble with the i2c i can not get the 819 to read or write anything to the master, I have the code working on some 18F2431.

I have pared the code down completly so it will just read or write one byte in response to 252 and stil get nothing.

On the 819, the poll command still works, the write comand does not write and the read command causes the clock pin to go low and stay low.

Can Anyone Help?


Thanks


Code:


/*****************************************************************************************************
******                           SPEED INPUT PIC V0-3                              ******
******                                 21-12-06                                 ******
******                              Matthew Finch                                 ******
******                                Rosmore Ltd                                 ******
*****************************************************************************************************/
#include <16f819.h>         //Header file for the device
#ZERO_RAM               //Zeros all RAM at startup
#use Delay(Clock=20000000)   //sets up clock for any delay instructions
#Fuses HS,NOWDT,PUT,BROWNOUT,NOLVP,NOWRT,NOCPD,NOPROTECT,NODEBUG,CCPB3      //Sets up Configeration bits

//Constant definitions
#define SLAVE_ADDRESS 120      //I2C address for this chip

//file definitions
#include <Main Delay.c>      //Delay for Program Loop
#include <I2c(5).h>         //I2C Interrupt
   
//Variable definitions

void main()
{
output_low(PIN_B6);
output_float(PIN_B1);   //float i2c pins   
output_float(PIN_B4);

Set_up_main_loop_timer();
Init_Setup_I2C();
enable_Interrupts(GLOBAL);

output_low(PIN_B2);      //low pins used for looking at program
output_low(PIN_B6);
output_low(PIN_B7);
while(TRUE)
   {
   output_toggle(PIN_B5);
   Main_loop_timer();
   }

}


/*******************************************************************************
      I2C    V5-0    21/12/06    Matthew Finch    Rosmore Ltd

Read/Write Commands down I2C lines 
********************************************************************************/
#USE I2C(SLAVE,SDA=PIN_B1,SCL=PIN_B4,ADDRESS=SLAVE_ADDRESS,FORCE_HW,FAST)

//Constant definitions
#BYTE SSPSTAT = 0x94
#BYTE SSPBUF = 0x13
#BYTE SSPCON = 0x14

//function definitions
void Init_Setup_I2C();

//Variable definitions

/*******************************************************************************
put in main section of program at the beginning (before program loop)

Setups up the I2C interupt
********************************************************************************/
void Init_Setup_I2C()
{   
enable_interrupts(INT_SSP);   
}


/*******************************************************************************
I2C Interupt
********************************************************************************/
#INT_SSP
void i2c_interupt()
{

int a;

if(i2c_poll()==TRUE)         //reads in byte if one there
{   
   a=i2c_read();   
   //output_float(PIN_B4);
}   
else
{
   i2c_write(7);
   output_toggle(PIN_B6);
}

}
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Dec 21, 2006 10:27 am     Reply with quote

First, don't do a poll() inside an ISR. If you have entered the ISR then something has happened to cause the interrupt. Instead of doing a poll(), check the SSPSTAT register and see what condition exists. Is the Master simply addressing the Slave? Is the Master trying to write a command or data to the Slave? Or, is the Master trying to read data out of the it? You need to find out what the Master is trying to do before the Slave can know how to respond. Next, don't do something like calling another function from an ISR. ISR's should be short and simple. If you want to read the ADC then set a bit and when the ISR exits have the main() loop read that bit and do your ADC stuff there. Lastly, that I saw, you don't need 'fast' in your #use_i2c fuse. The Master controls the speed of the clock.

Search the forum here. There is a boat load of posts regarding I2C communications and how to set things up.

Ronald
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Dec 21, 2006 11:52 am     Reply with quote

CCS has a function to do this. Look at I2C_ISR_STATE( ) in the manual.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group