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

PIC18F4550 I2C Slave transmits garbage

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



Joined: 30 Jul 2010
Posts: 1

View user's profile Send private message

PIC18F4550 I2C Slave transmits garbage
PostPosted: Fri Jul 30, 2010 6:04 pm     Reply with quote

Hello.

I am developing a circuit in which I have two PIC18F4550's communicating via I2C. I have successful communication from master to slave but when I read from the slave I always receive the same thing, decimal 106.

In my slave code, even if I comment out the I2C_write command, I STILL receive the 106 from the slave. I also attached an LED as an indicator that the slave code is operating properly. The LED indicates that the correct section of code operates when I ask for data from the slave.

The craziest part is that I have written very similar slave code for the PIC16F88 and it works perfectly in both directions with the 4550 master.

Can anybody PLEASE help me shed some light on this issue? I have spent many hours on it now and I'm stumped...and frustrated.

Thanks!

Slave Code:
Code:

#include <18F4550.h>
#fuses HSPLL, NOWDT, NOPROTECT, NOLVP, USBDIV, PLL5, CPUDIV3, VREGEN, PUT
#use delay (clock=24000000)
#use i2c(Slave,sda=PIN_B0,scl=PIN_B1,address=0xB4,force_hw)


//  pin defines

#define RED_LED PIN_B2

BYTE adress, buffer[0x10];

#INT_SSP
void SSP_isr()
{

   BYTE incoming, state, clear;
   state = i2c_isr_state();

   if (state <= 0x80){    //The code comes here when the master is sending data
   incoming = i2c_read();
 
   if (state ==1){
   adress = incoming;
   output_high(RED_LED);
   }
 
   if (state == 2){
   buffer[adress] = incoming;
   }
 
   }
 
   if (state == 0x80){   //This is where the code comes when the master asks for data
 
   i2c_write(buffer[adress]);
 
   //output_high(RED_LED);
   }
 
}


Master Code:
Code:

#include <18F4550.h>
#fuses HSPLL, NOWDT, NOPROTECT, NOLVP, USBDIV, PLL5, CPUDIV3, VREGEN, PUT
#use delay (clock=24000000)
#use i2c(Master,sda=PIN_B0,scl=PIN_B1)

void Write_Slave(int Slave_Address, int Buffer_Address, Write_Byte)
{
   i2c_start();
   i2c_write(Slave_Address);     // Device address
   i2c_write(Buffer_Address);  // address of the buffer that you are writing to 'buffer[address]'
   i2c_write(Write_Byte);  //data sent to the buffer(address) above
   i2c_stop();
}
void Read_Slave(int Slave_Address, int Buffer_Address)
{
   i2c_start();
   i2c_write(Slave_Address);
   i2c_write(Buffer_Address);
   //i2c_stop();
   delay_us(1);
   i2c_start();
   i2c_write(Slave_Address+1);
   delay_us(2);
   data = i2c_read(0);

   i2c_stop();


}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 30, 2010 7:06 pm     Reply with quote

What's the frequency of the crystal or external oscillator on each PIC ?
(Also, specify if it is a crystal, or an external oscillator).

What's your compiler version ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19346

View user's profile Send private message

PostPosted: Sat Jul 31, 2010 2:02 am     Reply with quote

Try manually setting CKP.
So:
Code:

#bit CKP=0xFC6.4

   if (state == 0x80){   //This is where the code comes when the master asks for data
   i2c_write(buffer[adress]);
   CKP=1;
   //output_high(RED_LED);
   }

I seem to remember having problems on some chips/compiler versions with the compiler not doing this.

Best Wishes
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