Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
March 29, 2024, 03:57:11 15:57


Login with username, password and session length


Pages: [1]
Print
Author Topic: i2C MULTI_MASTER  (Read 6843 times)
0 Members and 1 Guest are viewing this topic.
o_cientista
Newbie
*
Offline Offline

Posts: 9

Thank You
-Given: 1
-Receive: 4


« on: June 07, 2012, 12:03:24 00:03 »

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

// 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);
        }
    }
}


//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);
        }
    }
}
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC