RS485 Digital I/O communication

Here you can ask for specific code examples and discuss about it with other forum members.
Post Reply
Author
Message
alvinserna
Posts: 1
Joined: 06 Oct 2014 10:08

RS485 Digital I/O communication

#1 Post by alvinserna » 06 Oct 2014 10:22

Hi everyone,

I know this is a simple RS485 problem for expert but very important for us beginners.
I want to turn-on the output LED's of the master when the input switch of the slave in turn-on.
I want to turn-on D1 (output of master) when IN6 (input of slave) is on. Is this possible or not?

I am able to turn-on the output LED's of the slave when the input switch of the master in turn-on.
I use the RS485 library of MikroC.

This is the code and schematic of my problem.
Hope someone here can help. Thank you very much.

Master:

Code: Select all

/*
  MASTER - RS485 COMMUNICATION
  SIMPLE RS485 PROGRAM USING
  1. PIC16F628A - 4MHZ internal oscillator
  2. MAX485
*/
char dat[9];                          // buffer for receving/sending messages
sbit  rs485_rxtx_pin  at RB0_bit;               // set transcieve pin
sbit  rs485_rxtx_pin_direction at TRISB0_bit;   // set transcieve pin direction

// Interrupt routine
void interrupt() {
  RS485Master_Receive(dat);
}

void main(){
  CMCON  |= 0x07;        // Disable comparators
  TRISA = 0xff;          //all porta input
  TRISB = 0x07;
  PORTA = 0;
  PORTB = 0;
 /*
  PORTA.0 = INPUT 1
  PORTA.1 = INPUT 2
  PORTA.2 = INPUT 3
  PORTA.3 = INPUT 4
  PORTA.4 = INPUT 5
*/

/*
  PORTB.0 = RX/TCX
  PORTB.1 = RX
  PORTB.2 = TX
  PORTB.3 = OUTPUT 1
  PORTB.4 = OUTPUT 2
  PORTB.5 = OUTPUT 3
  PORTB.6 = OUTPUT 4
  PORTB.7 = OUTPUT 5
*/

  UART1_Init(9600);                    // initialize UART1 module
  Delay_ms(100);

  RS485Master_Init();                  // initialize MCU as Master
  dat[0] = 0;
  dat[1] = 0x00;
  dat[2] = 0x00;
  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,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;                         // enable all interrupts

  while (1){
//1
     while (porta.f4==1 && porta.f3==1 && porta.f2==1 && porta.f1==1 && porta.f0==0){
        delay_ms(10);
        dat[0]= 1;
        RS485Master_Send(dat,1,100);
     }
//2
     while (porta.f4==1 && porta.f3==1 && porta.f2==1 && porta.f1==0 && porta.f0==1){
        delay_ms(10);
        dat[0]= 2;
        RS485Master_Send(dat,1,100);
     }
//3
     while (porta.f4==1 && porta.f3==1 && porta.f2==1 && porta.f1==0 && porta.f0==0){
        delay_ms(10);
        dat[0]= 3;
        RS485Master_Send(dat,1,100);
     }
        dat[0]= 0;                  //send 0 if no input
        RS485Master_Send(dat,1,100);
    }


}

SLAVE:

Code: Select all

/*
  SLAVE - RS485 COMMUNICATION
  1. PIC16F628A - 4MHZ internal oscillator
  2. MAX485
*/
char dat[9];             // buffer for receving/sending messages

sbit  rs485_rxtx_pin at Rb0_bit;             // set transcieve pin
sbit  rs485_rxtx_pin_direction at TRISb0_bit;   // set transcieve pin direction

// Interrupt routine
void interrupt() {
 RS485Slave_Receive(dat);
}

void main() {
CMCON  |= 0x07;        // Disable comparators
  TRISA = 0x1F;        //INITIALIZE PORTA
  TRISB = 0x07;        //INITIALIZE PORTB
  PORTA = 0;
  PORTB = 0;
/*
  PORTA.0 = INPUT 1
  PORTA.1 = INPUT 2
  PORTA.2 = INPUT 3
  PORTA.3 = INPUT 4
  PORTA.4 = INPUT 5
*/

/*
  PORTB.0 = RX/TCX
  PORTB.1 = RX
  PORTB.2 = TX
  PORTB.3 = OUTPUT 1
  PORTB.4 = OUTPUT 2
  PORTB.5 = OUTPUT 3
  PORTB.6 = OUTPUT 4
  PORTB.7 = OUTPUT 5
*/

  UART1_Init(9600);                  // initialize UART1 module
  Delay_ms(100);
  RS485Slave_Init(100);              // Intialize MCU as slave, address 100

  dat[4] = 0;                        // ensure that message received flag is 0
  dat[5] = 0;                        // ensure that message received flag is 0
  dat[6] = 0;                        // ensure that error flag is 0

  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;                       // enable all interrupts

  while (1) {


//ALL MASTER INPUTS ARE OFF
       if (dat[0]==0) {
         portb=0x00 ;
       }
//1
       else if (dat[0]==1) {
         portb=0x08 ;         //TURN ON PORTB.F3 OF SLAVE

       }
//2
       else if(dat[0]==2){
         portb=0x10;          //TURN ON PORTB.F4 OF SLAVE

       }
//3
       else if(dat[0]==3){
        portb=0x18;          //TURN ON PORTB.F3 && PORTB.F4 OF SLAVE

       }

    }

 }
Attachments
RS485-PROBLEM.jpg
RS485-PROBLEM.jpg (219.4 KiB) Viewed 3661 times

Muphy
Posts: 318
Joined: 24 Feb 2008 14:05
Location: Stonehaven Scotland

Re: RS485 Digital I/O communication

#2 Post by Muphy » 08 Oct 2014 09:22

Is your issue with making the RS485 library work or simply getting your problem/project to work?

I would suggest that you could expect more help if you were to describe the symptoms, what you think the problem is and what you think might be the answer.

A post of "Here's my project, what's the answer?" is not going to get you much help I fear!

Good luck,

M

st5
Posts: 192
Joined: 04 Jan 2011 14:14
Location: 3090 Belgium

Re: RS485 Digital I/O communication

#3 Post by st5 » 12 Oct 2014 13:12

alvinserna wrote: This is the code and schematic of my problem.
Hope someone here can help. Thank you very much.
I can help. What's your budget for getting a working solution?
3x PicPlc16V6 1x EasypicV7

Going from programming on PC to embedded programming is like working in the dark with sunglasses on.

Post Reply

Return to “Code Requests”