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.

[PIC] Rs485 digital i/o communication

Status
Not open for further replies.

banyas

Newbie level 3
Joined
Aug 10, 2014
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
65
Hi everyone,

I am able to turn-on the output LED's of Slave when I turn-on inputs of the Master.
However, I cannot do the reverse.
I want to turn-on the LED output of Master when I turn-on the input of Slave.
I want to turn-on D1 (output of master) when I press IN6 (input of slave). Is this possible?

I use the RS485 library of mikroc and PIC16f628a.
Attach is the code RS485-PROBLEM.png & schematic.


CODE of MASTER:

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
  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.f==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.f==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);
    }
}





CODE of SLAVE:

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
  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
 
       }
 
    }
 
 }

 
Last edited by a moderator:

I'm not sure exactly what functions like 'RS485Slave_Receive' and 'RS485Master_Receive' do, but in general it looks like the master doesn't do anything with dat when it's received from the slave, and for that matter the slave never sends anything. It looks like the master is set up to only send, and the slave only receives. So you would need to have them alternate, or set up a more elaborate communication protocol between them that would ensure both aren't transmitting at the same time. For example the simplest would probably be to have the slave always respond with it's button status immediately after receiving the button status data from the master. This way the master controls the timing, and you should eliminate collisions. (But the 10ms delay may not be long enough.) You would essentially need to duplicate the slave code into the master for setting the LEDs, and then have the slave send data in the same way the master does it after receiving a message from the master. It's hard to be more specific without seeing the entire code. (Sorry, I'm not familiar with the mikroC libraries.)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top