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
}
}
}