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 : Master and 2 Slaves

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







I2c : Master and 2 Slaves
PostPosted: Fri Aug 21, 2009 2:11 am     Reply with quote

Bonjour à Tous,

J'ai un projet qui consiste entre autre à une communication entre un Master et 2 Slaves.

Mon problème est le suivant: un seul slave uniquement qui réçoit les données du master ( confirmé par affichage sur LCD des données réçues et LED ). ( j'ai le schéma sur ISIS mais je sais comment le joindre au message!!!!)

Mon compilateur est CCS.

et en résultat je ne trouve que la donnée envoyée au slave_1, par contre si je permute l'ordre d'envoi( chez lE master) càd slave_2 avant slave_1 je récoit la donnée envoyée au slave_2.
merci pour toute intervention et bonne journée pour tous.
Voici les Codes :

TRANSLATION:

Hello All,
I have a project which consists amongst other things of a communication between i2c Master and 2 Slaves. My problem is the following: only one slave receives the data of the master (confirmed by posting on LCD of the received data and LED). (I have the diagram on ISIS but I know how to join it to the message!!!!). My compiler is CCS. In result I find only the data sent to the slave_1, on the other hand if I change the order of sending (at the master) to slave_2 before slave_1, I receive the data sent to the slave_2. Thank you for any intervention and good day to all. Here is the code:

1- Code Master :
Code:

#include <16F876A.h>
#device adc=8
#use delay(clock=4000000)
#fuses NOWDT,XT, NOPUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, NOWRT
#use i2c(Master,sda=PIN_C4,scl=PIN_C3,RESTART_WDT)
#include "D:\A_Projets Electroniques\I2C_Communications\PCW_Master_Communication.h"

 void main()
  {
   int8 sec,min,hour,day,date=19,month=11,year=2009;
   int16 jour_RTC;
   int16 N_jour=300,BPFA_N_jour,BPFO_N_jour,BPFA1_N_jour,BPFO1_N_jour;
   int8 heure=0;
   
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   set_tris_A(0b0000011);
   delay_ms(2000);

// Envoi de "jour_RTC" aux Slaves:------------------------------------------------------------------

    jour_RTC=(month*275/9)-((month+9)/12)*(1+ ((year-4* (year/4)+2)/3))+date-30;

    if (jour_RTC>255) {BPFA_N_jour=255;BPFO_N_jour=jour_RTC-256;}  // BPFA_jour_RTC= Les 8 premiers Bits de la variable Jour_RTC
                   else {BPFA_N_jour=jour_RTC;BPFO_N_jour=0;}
   i2c_start();
  [b] i2c_write(0xB0);        // adresse "Slave_1:B0" + mode écriture[/b]
   i2c_write(&BPFA_N_jour);  // adresse interne slave = Les premiers 8 bits
   i2c_write(BPFA_N_jour); //  donnée à écrire
   i2c_write(&BPFO_N_jour);  // adresse interne slave = Les Seconds  8 bits
   i2c_write(BPFO_N_jour); //  donnée à écrire
   i2c_stop();
 

   i2c_start();
  [b] i2c_write(0x02);  // adresse "Slave_2" + mode écriture[/b]     
i2c_write(&BPFA1_N_jour);  // adresse interne slave = Les premiers 8 bits
   i2c_write(255); //  donnée à écrire
   i2c_write(&BPFO1_N_jour);  // adresse interne slave = Les Seconds  8 bits
   i2c_write(40); //  donnée à écrire
   i2c_stop();

   delay_ms(50);

  }


Code Slave_1 :
Code:

#include <16F876A.h>
#device adc=8
#use delay(clock=4000000)
#fuses NOWDT,XT, NOPUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, NOWRT
#use i2c(Slave,sda=PIN_C4,scl=PIN_C3,address=0xB0,FORCE_HW,RESTART_WDT)
#include "D:\A_Projets Electroniques\I2C_Communications\PCW_Slave_Communication.h"
#define use_portb_lcd TRUE
#include "C:\Program Files\PICC\Drivers\i2c_isr_state.c"
#include <LCD.c>

int8 N_jour,jour_RTC,BPFA_N_jour,BPFO_N_jour;
 int8 heure;
BYTE incoming, state;
 int8 incoming1;

#int_SSP
void ssp_interupt (){

state = i2c_isr_state();

if(state <= 0x80)                     //Master is sending data
    {
    incoming = i2c_read();
    if(state == 0)
     {      }       
    if(state == 1)                    //First received byte is address
                     {
                      BPFA_N_jour=incoming;
                     }

    if(state == 2)                    //Second received byte is data
                  {  incoming = i2c_read();
                     BPFO_N_jour=incoming;

                      N_jour=BPFA_N_jour+BPFO_N_jour;
                      if (N_jour==322) output_high(PIN_A0);
lcd_gotoxy(1,1); printf(lcd_putc,"BPFA_N_jour:%u",BPFA_N_jour); lcd_gotoxy(1,2); printf(lcd_putc,"BPFO_N_jour:%u",BPFO_N_jour);
                  }

    }
if(state == 0x80)                     //Master is requesting data
    {
   // i2c_write(buffer[address]);
    }

}

void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);

   lcd_init();

    while (TRUE) {   }
                               

}


Code Slave_2:
Code:
#include <16F876A.h>
#device adc=8
#use delay(clock=4000000)
#fuses NOWDT,XT, NOPUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, NOWRT
#use i2c(Slave,sda=PIN_C4,scl=PIN_C3,address=0x02,FORCE_HW,RESTART_WDT)
#include "D:\A_Projets Electroniques\I2C_Communications\PCW_Slave_DUHR.h"
#define use_portb_lcd TRUE
#include "C:\Program Files\PICC\Drivers\i2c_isr_state.c"
#include <LCD.c>

int8 N_jour,jour_RTC,BPFA_N_jour,BPFO_N_jour,BPFA1_N_jour,BPFO1_N_jour;
 int8 heure,adress_slave;
BYTE incoming, state;

#int_SSP
void ssp_interupt (){

state = i2c_isr_state();

if(state <= 0x80)                     //Master is sending data
    {
   
    incoming = i2c_read();
   
    if(state ==0 )                    //First received byte is address
                     {
                        }
   
    if(state ==1 )                    //First received byte is address
                     {
                      BPFA1_N_jour=incoming;
                      }

    if(state == 2)                    //Second received byte is data
                  {  incoming = i2c_read();
                     BPFO1_N_jour=incoming;
                      N_jour=BPFA1_N_jour+BPFO1_N_jour;
                      if (N_jour==295) output_high(PIN_A1);
 lcd_gotoxy(1,1); printf(lcd_putc,"adress_slave:%u",adress_slave);
 lcd_gotoxy(1,2); printf(lcd_putc,"BPFO1_N_jour:%u",BPFO1_N_jour);
                  }

            }
if(state == 0x80)                     //Master is requesting data
    {
   // i2c_write(buffer[address]);
    }


}

void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);

   lcd_init();
   
   while (TRUE) {  }
                                       
}

NB/ j'attire votre attention aussi que lorsque j'ai fait un test sur le changement de niveau de la broche C3 (SCL) du slave qui n'est pas entrain de récevoir les données : résultat : pas de changement ( càd dans la routine d'interruption INT_SSP : si (Input(PIN_C3) != 1) OUTPUT_HIGH (LED) : le LED ne s'allume pas donc si comme si pas d'interruption dans le 2 ème Slave.)

Voulez vous m'assister svp car j'ai passé pas mal des nuits à résoudre ce problème et aucun résultat satisfaisant.

Merci our Tous et Bonne journée.
zied74
Guest







PostPosted: Fri Aug 21, 2009 5:12 am     Reply with quote

Thanks for any help.

Good day.
Salenko



Joined: 08 Sep 2008
Posts: 84

View user's profile Send private message

Re: I2c : Master and 2 Slaves
PostPosted: Fri Aug 21, 2009 8:57 am     Reply with quote

zied74 wrote:
(I have the diagram on ISIS but I know how to join it to the message!!!!).


Hi,

you can take a screen capture of your schematic then upload it with http://imageshack.us/


could you translate this paragraph ?

zied74 wrote:
Bonjour à Tous,

NB/ j'attire votre attention aussi que lorsque j'ai fait un test sur le changement de niveau de la broche C3 (SCL) du slave qui n'est pas entrain de récevoir les données : résultat : pas de changement ( càd dans la routine d'interruption INT_SSP : si (Input(PIN_C3) != 1) OUTPUT_HIGH (LED) : le LED ne s'allume pas donc si comme si pas d'interruption dans le 2 ème Slave.)

Voulez vous m'assister svp car j'ai passé pas mal des nuits à résoudre ce problème et aucun résultat satisfaisant.

Merci our Tous et Bonne journée.



This code worked for me http://www.ccsinfo.com/forum/viewtopic.php?t=36695 though it is incomplete (to understand why, see the latest messages http://www.ccsinfo.com/forum/viewtopic.php?t=35696)

this may help you.
Guest








PostPosted: Fri Aug 21, 2009 10:02 am     Reply with quote

thanks Salenko,

Quote:
NB/ j'attire votre attention aussi que lorsque j'ai fait un test sur le changement de niveau de la broche C3 (SCL) du slave qui n'est pas entrain de récevoir les données : résultat : pas de changement ( càd dans la routine d'interruption INT_SSP : si (Input(PIN_C3) != 1) OUTPUT_HIGH (LED) : le LED ne s'allume pas donc si comme si pas d'interruption dans le 2 ème Slave.)

Voulez vous m'assister svp car j'ai passé pas mal des nuits à résoudre ce problème et aucun résultat satisfaisant.

Merci our Tous et Bonne journée.



Translation :

I draw your attention as well as when I made a test on the level change of the Pin C3 ( SCL) of the Slave which has not receiving the data: result: no change (: in the routine of interruption INT_SSP: if (Input ( PIN_C3)! = 1) OUTPUT_HIGH (LED): the LED does not ignite if as if no interruption in 2nd Slave.)
Want to assist me, because I spent a lot of nights to resolve this problem and no satisfactory result.

Thank you our All and Good day.

I have not find in this forum the button to download the schematic.(GIF)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 21, 2009 10:36 am     Reply with quote

Quote:
#use i2c(Slave,sda=PIN_C4,scl=PIN_C3,address=0x02,FORCE_HW,RESTART_WDT)


Quote:

i2c_start();
i2c_write(0x02); // adresse "Slave_2" + mode écriture
i2c_write(&BPFA1_N_jour); // adresse interne slave = Les premiers 8 bits
i2c_write(255); // donnée à écrire
i2c_write(&BPFO1_N_jour); // adresse interne slave = Les Seconds 8 bits
i2c_write(40); // donnée à écrire
i2c_stop();

delay_ms(50);


A slave address of 0x02 is not allowed. See these posts:
http://www.ccsinfo.com/forum/viewtopic.php?t=36558&start=6
http://www.ccsinfo.com/forum/viewtopic.php?t=33966&start=3
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