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 MULTI_MASTER

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



Joined: 14 May 2012
Posts: 5

View user's profile Send private message

i2C MULTI_MASTER
PostPosted: Wed Jun 06, 2012 4:54 pm     Reply with quote

Good day.      Two months ago I'm trying to do two PICs act as master and as slave in another time and work as a slave and master, but so far failed. I therefore do not know what else to do. Someone could help me. Here is the code of the master and slave.
Code:

// MASTER

#include <16F877A.h>
#device adc=10

#FUSES NOWDT                     //No Watch Dog Timer
#FUSES HS                        //High speed Osc (> 4mhz)
#FUSES PUT                       //Power Up Timer
#FUSES PROTECT                   //Code protected from reads
#FUSES NODEBUG                   //No Debug mode for ICD
#FUSES BROWNOUT                  //Brownout reset
#FUSES NOLVP                     //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                     //No EE protection
#FUSES NOWRT                     //Program memory not write protected

#use delay(clock=20M)

//NOTE: Must declare MASTER before SLAVE, i2c_isr_state() returns 0
//      when MASTER is the most recent #use i2c
#use i2c(MASTER, sda=PIN_C1, scl=PIN_C0, stream=I2CM)
#use i2c(SLAVE, sda=PIN_C4, scl=PIN_C3, address=0xA0, force_hw, stream=I2CS)
#use rs232(baud=57600, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8, stream=COM1)

#use fast_io(D)

int rcv_buf[0x10];       
int wrt_buf[0x10];       
int address;           

#int_SSP // interrupt on i2c activity
void  i2c_isr(void)
{
    int state, incoming;
    state = i2c_isr_state(I2CS);
   
    if(state < 0x80)
    {
        incoming = i2c_read(I2CS);
        if (state == 1)
        {
            address = incoming;
        }
       
        else if (state >= 2)
        {
            rcv_buf[address] = incoming;
        }
    }
    else if(state == 0x80)
    {
        i2c_write(I2CS,wrt_buf[address]);
    }
}

void main()
{
   
    enable_interrupts(INT_SSP);
    enable_interrupts(GLOBAL);
   
    printf("\n\rbegin");
   
    while (TRUE)
    {
        if (!input(PIN_B4))
        {
        i2c_start(I2CM);
        i2c_write(I2CM,0xB0);      //i2c address of a slave device
        delay_ms(10);
        i2c_write(I2CM,0x00);      //1st byte to slave
        delay_ms(10);
        i2c_write(I2CM,0x22);      //2nd byte to slave
        delay_ms(10);
        i2c_stop(I2CM);
        }
    }
}

Code:

//SLAVE

#include <16F877A.h>
#device adc=10

#FUSES NOWDT                     //No Watch Dog Timer
#FUSES HS                        //High speed Osc (> 4mhz)
#FUSES PUT                       //Power Up Timer
#FUSES PROTECT                   //Code protected from reads
#FUSES NODEBUG                   //No Debug mode for ICD
#FUSES BROWNOUT                  //Brownout reset
#FUSES NOLVP                     //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                     //No EE protection
#FUSES NOWRT                     //Program memory not write protected

#use delay(clock=20M)

//NOTE: Must declare MASTER before SLAVE, i2c_isr_state() returns 0
//      when MASTER is the most recent #use i2c
#use i2c(MASTER, sda=PIN_C1, scl=PIN_C0, stream=I2CM)
#use i2c(SLAVE, sda=PIN_C4, scl=PIN_C3, address=0xB0, force_hw, stream=I2CS)
#use rs232(baud=57600, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8, stream=COM1)

#use fast_io(D)

int rcv_buf[0x10];       
int wrt_buf[0x10];       
int address;           

#int_SSP // interrupt on i2c activity
void  i2c_isr(void)
{
    int state, incoming;
    state = i2c_isr_state(I2CS);
   
    if(state < 0x80)
    {
        incoming = i2c_read(I2CS);
        if (state == 1)
        {
            address = incoming;
        }
       
        else if (state >= 2)
        {
            rcv_buf[address] = incoming;
        }
    }
    else if(state == 0x80)
    {
        i2c_write(I2CS,wrt_buf[address]);
    }
}

void main()
{
   
    enable_interrupts(INT_SSP);
    enable_interrupts(GLOBAL);
   
    printf("\n\rbegin");
   
    while (TRUE)
    {
        if (!input(PIN_B4))
        {
        i2c_start(I2CM);
        i2c_write(I2CM,0xa0);      //i2c address of a slave device
        delay_ms(10);
        i2c_write(I2CM,0x00);      //1st byte to slave
        delay_ms(10);
        i2c_write(I2CM,0x22);      //2nd byte to slave
        delay_ms(10);
        i2c_stop(I2CM);
        }
    }
}
temtronic



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

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 7:25 pm     Reply with quote

Step one. Program one PIC as a master only, the other PIC as a slave only. Then confirm your programs and PIC function 100% ! Be sure to use the correct pullups on the I2C bus lines for your Vss and speeds though.

Step two. Why do you want 'multimaster' mode with ONLY 2 PICs ??? It's not needed and complicates the whole communications between PICs.
o cientista



Joined: 14 May 2012
Posts: 5

View user's profile Send private message

PostPosted: Thu Jun 07, 2012 8:10 am     Reply with quote

good day!     I really need communication MULTI_NASTER because I use several PIC in my project. In the example just put two to get easier for people to help me. I'm trying to do this for over two months and still could not. I'm desperate. Please someone help me!
Ttelmah



Joined: 11 Mar 2010
Posts: 19540

View user's profile Send private message

PostPosted: Thu Jun 07, 2012 8:55 am     Reply with quote

The point is though that you need to move one step at a time. Your ISR code immediately has a fault that will stop it working (state 0x80, _must_ read the character before writing the output one), which will prevent it working, and you might have found if you had simplified, and got simple one to one I2C communication working _first_. Key thing throughout development and debugging is to always move from a known working starting point, adding the smallest amount possible, to move to the next level.

Best Wishes
o cientista



Joined: 14 May 2012
Posts: 5

View user's profile Send private message

PostPosted: Fri Jun 08, 2012 12:36 pm     Reply with quote

Good morning!   
  I'm from Brazil and speak Portuguese. Sorry for my english. I did what you said but still did not work. The problems that this program line: #use i2c(MASTER, sda=PIN_C1, scl=PIN_C0, stream=I2CM) If I comment this line pragrama it receives the ID, ADDRESS and DATA, but this way you receive the PIC will not be able to function as the master. Here, the attached photo of operating with errors. http://www.4shared.com/photo/36VTMkVt/i2c_DEBUG.html
http://www.4shared.com/photo/vWV6RBYK/I2C.html?
temtronic



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

View user's profile Send private message

PostPosted: Fri Jun 08, 2012 1:41 pm     Reply with quote

One picture looks like Proteus.
Please tell us you're using REAL hardware and NOT a simulation !!
o cientista



Joined: 14 May 2012
Posts: 5

View user's profile Send private message

PostPosted: Sat Jun 09, 2012 3:36 pm     Reply with quote

I'm using the Proteus Simulation. But when I comment this line: #use i2c(MASTER, sda=PIN_C1, scl=PIN_C0, stream=I2CM) it gets.
temtronic



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

View user's profile Send private message

PostPosted: Sat Jun 09, 2012 5:13 pm     Reply with quote

Sorry, but using Proteus is NOT the real world !

Nobody here will spend ANY time on this 'project' as Proteus is FULL of bugs, errors and faulty DRCs.
It is beyond any sane person to dive into such a crummy 'simulation' and rewrite their code (if allowed) to correct for the hundreds of errors easily spotted. It's actually far easier to buy PICs, breadboard them, cut code, download, test and debug...all in one day.....odds are real good you'll never get Proteus to 'function' correctly.

When you get real hardware up and running, we'll be happy to help out as we can easily test real hardware.
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