Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Master and multi Slaves problem RS485

Status
Not open for further replies.

hitachicm615

Newbie level 5
Joined
Feb 16, 2012
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,361
Hello,

I' using MikroC example of code for RS485 communication between one Master and one Slave. It's work well.
However, i'm trying now with two Slaves, One Master and i have problem.

After serveral secondes (between 2seconds and 10seconds), i enter in my error if() on Master.
Code:
 if (dat[5])  { 
    UART_Set_Active(&UART2_Read, &UART2_Write, &UART2_Data_Ready, &UART2_Tx_Idle);             // if an error detected, signal it
      Uart2_write(0x12);                 // Send 0x12 to computer via Uart1
    }

My slave code is wrote like Slave code example from MikroC. I change only adress value in init. 159 for one Slave and 160 for second one.
In Master code, i'm sending to slave(159) and slave(160) but i have the problem described above (Detector error).

Maybe my master code to send one time to Slave159 after Slave160 is wrong ?

Thanks,

Code:
char dat[10];                        // buffer for receving/sending messages
char i,j;
int slave = 0;
int cnt;
int error = 0;

sbit  rs485_rxtx_pin  at LATC2_bit;               // set transcieve pin
sbit  rs485_rxtx_pin_direction at TRISC2_bit;   // set transcieve pin direction

// Interrupt routine
void interrupt() {
     if(slave==0)
     {
        RS485Master_Receive(dat);
        slave=1;
     }
     else if(slave==1)
     {
        RS485Master_Receive(dat);
        slave=0;
     }
}


void main() {

 UART2_Init(9615);
 Delay_ms(100);
 UART_Set_Active(&UART2_Read, &UART2_Write, &UART2_Data_Ready, &UART2_Tx_Idle);

  ANSELB = 0;                                        // Configure AN pins as digital I/O
  ANSELC = 0;
  ANSELD = 0;

  LATB   = 0;
  LATD   = 0;
  TRISB  = 0;
  TRISD  = 0;
  TRISC = 0;


  UART1_Init(9615);                                  // initialize UART1 module
  Delay_ms(100);
  UART_Set_Active(&UART1_Read, &UART1_Write, &UART1_Data_Ready, &UART1_Tx_Idle);


  RS485Master_Init();                                // initialize MCU as Master
  dat[0] = 0xAA;
  dat[1] = 0xF0;
  dat[2] = 0x0F;
  dat[4] = 0;                                        // ensure that message received flag is 0
  dat[5] = 0;                                        // ensure that error flag is 0
  dat[6] = 0;

  //RS485Master_Send(dat,1,159);
  //RS485Master_Send(dat,1,160);
  delay_ms(100);


  RCIE_bit = 1;                                      // enable interrupt on UART1 receive
  TXIE_bit = 0;                                      // disable interrupt on UART1 transmit
  PEIE_bit = 1;                                      // enable peripheral interrupts
  GIE_bit = 1;

while(1){
Delay_ms(500);
cnt++;
  if (slave==0)
  {
  RS485Master_Send(dat,1,159);
  }
  
  else if (slave==1)
  {
  RS485Master_Send(dat,1,160);
  }
  
    if (dat[5])  { 
    UART_Set_Active(&UART2_Read, &UART2_Write, &UART2_Data_Ready, &UART2_Tx_Idle);                    // if an error detected, signal it
      Uart2_write(0x12);                 // Send 0x11 to computer via Uart1
    }

  if (dat[4]) {                      // if message received successfully
      cnt = 0;
      dat[4] = 0;                      // clear message received flag
      j = dat[3];
      UART_Set_Active(&UART2_Read, &UART2_Write, &UART2_Data_Ready, &UART2_Tx_Idle);
      delay_ms(200);
      Uart2_Write(dat[0]);
      UART_Set_Active(&UART1_Read, &UART1_Write, &UART1_Data_Ready, &UART1_Tx_Idle);                               // increment received 
      Delay_ms(1);

    }
   if (cnt > 100000) {
      error ++;
      cnt = 0;
      UART_Set_Active(&UART1_Read, &UART1_Write, &UART1_Data_Ready, &UART1_Tx_Idle);
      RS485Master_Send(dat,1,160);
      if (error > 10)                  // if sending failed 10 times
        RS485Master_Send(dat,1,50);    //   send message on broadcast address
      }

    }
}
 

Hi! hitachicm615
Sorry I'm not good at English because I'm Vietnamese. I have a problem.I want to connect pic to pic use rs485.The master is pic 16f887 and 2 slave is 16f887
anyone have sample code for my reference
tks
 

You select the slaves by making SS pin high and then send data using UART to the RS485 communication chip.
Hi! jayanth.devarayanadurga
you can send me the sample code consists of two slave and one master? I'm doing a thesis on the fire alarm communication address
tks
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top