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 CCS Technical Support

i2c state problem

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



Joined: 13 Oct 2010
Posts: 8

View user's profile Send private message

i2c state problem
PostPosted: Wed Oct 13, 2010 7:57 am     Reply with quote

Hi all,

I have problems with I2C. I need to connect 3 PIC to one master but after six hour, I could only write to one slave. I tried every tutorial and code that I found but in proteus they don't work.

I can see that master passes the info to slave and slave executes
Code:
 i2c_write(buffer[address]);


but master reads this value as 0x80 which equal to the state value in that period.


Here is my master code
Code:

#include <16F877A.h>
#include "2401.c"
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#use i2c(MASTER, SDA=PIN_C4, SCL=PIN_C3)


int8 data=0;
int8 data1=0;
void main()
{
   init_ext_eeprom();    //Call before the other functions are used  ////
   
    delay_ms(100);
   data=read_ext_eeprom(2);  //Read the byte d from the address a     ////
   
   delay_ms(100);                                                              ////
   write_ext_eeprom(2, 15); // Write the byte d to the address a      ////
   
   delay_ms(100);
   data=read_ext_eeprom(2);  //Read the byte d from the address a     ////
                                                                  ////
   data1 = ext_eeprom_ready();  //Returns TRUE if the eeprom is ready    ////
                            //to receive opcodes                     ////
                                                                   ///
   
}


and slave

Code:
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0xA0)

BYTE address, buffer[0x10];
BYTE incoming, state;

#INT_SSP
void ssp_interupt ()
{
 
   
   state = i2c_isr_state();
   
   if(state <= 0x80)                     //Master is sending data
   {
      incoming = i2c_read();
      if(state == 1)                     //First received byte is address
         address = incoming;
      if(state == 2)                     //Second received byte is data
         buffer[address] = incoming;
   }
   if(state == 0x80)                     //Master is requesting data
   {
      i2c_write(buffer[address]);
   }
}


void main ()
{
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_SSP);
   buffer[0]=23;
   buffer[1]=24;
   buffer[2]=25;
   buffer[3]=26;
   buffer[4]=27;
   while (TRUE) {}
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 13, 2010 3:23 pm     Reply with quote

This thread has some example code for three i2c slave PICs.
This is a 2-page thread. Read both pages.
http://www.ccsinfo.com/forum/viewtopic.php?t=39242
amboleos



Joined: 13 Oct 2010
Posts: 8

View user's profile Send private message

PostPosted: Thu Oct 14, 2010 2:51 am     Reply with quote

Thank you for links, I tried the these codes that are supposed to be working.

Meanwhile my complier is PCWHD 4.112

Master

Code:
#include <18f4520.h>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)
   
#define SLAVE1_WRT_ADDR   0x14
#define SLAVE1_READ_ADDR  0x15

#define SLAVE2_WRT_ADDR   0x28
#define SLAVE2_READ_ADDR  0x29

#define SLAVE3_WRT_ADDR   0x42
#define SLAVE3_READ_ADDR  0x43

//====================================
void main()
{
int8 data1,data2,data3;

while(1)
  {
  output_high(pin_d4);
   delay_ms(60);
   output_low(pin_d4);
   i2c_start();
   i2c_write(SLAVE1_READ_ADDR);
   data1 = i2c_read(0);
   i2c_stop();
   delay_ms(10);
   i2c_start();
   i2c_write(SLAVE2_READ_ADDR);
   data2 = i2c_read(0);
   i2c_stop();
   delay_ms(10);
   i2c_start();
   i2c_write(SLAVE3_READ_ADDR);
   data3 = i2c_read(0);
   i2c_stop();
   printf(" [ %3U %3U %3U ] \r\n", data1,data2,data3);
   output_high(pin_d6);
   delay_ms(60);
   output_low(pin_d6);
  }

}


my slave (just one slave is connected to the bus)


Code:
#include <18F4520.h>
#device adc=8
#fuses HS,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=20000000)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x14)

int8 adc_result;


#INT_SSP
void ssp_interrupt()
{
int8 incoming, state;

state = i2c_isr_state();
   
if(state < 0x80)     // Master is sending data
  {
   incoming = i2c_read();
  }

if(state >= 0x80)   // Master is requesting data from slave
  {
   i2c_write(14);
  }

}


//======================================
void main ()
{
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
delay_us(20);
adc_result = read_adc();

enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
   
while(1)
  {
   adc_result = read_adc();
   delay_ms(500);
  }

}



My results

Code:
[14 255 255]
[255 255 255]
[255 255 255]
[255 255 255]
....


Just the first time, value is correctly readed. Confused
I also tried to clear all other slave codes and just let 1 slave and master communicate, but again it didnt work.

Problem is that slave never enters interrupt again after first one.
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Oct 14, 2010 5:39 am     Reply with quote

If this is a simulator (proteus) it may not be your code but the simulator!

I've never trusted any simulator in the 35 years I've been cutting code.

NONE will work 100% properly all of the time.

If real hardware, check the pullups for right values for your speed, board layout, etc.
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