mamech
Full Member level 3

Hello
I intend to make a master / slave rs485 communication between 2 pic micros. I found a example about how to make this in mikroE compiler help, then I edited few lines in it to suit my application. The problem that the micros do not perform the supposed logic of the code.
Master code:
char dat[10]; // buffer for receving/sending messages
char i,j;
sbit rs485_rxtx_pin at RC2_bit; // set transcieve pin
sbit rs485_rxtx_pin_direction at TRISC2_bit; // set transcieve pin direction
// Interrupt routine
void interrupt() {
RS485Master_Receive(dat);
}
void main(){
long cnt = 0;
PORTB = 0;
PORTD = 0;
PORTE = 0;
TRISB = 0;
TRISD = 0;
TRISE = 0;
UART1_Init(9600); // initialize UART1 module
Delay_ms(100);
RS485Master_Init(); // initialize MCU as Master
dat[0] = 0x02;
dat[1] = 0x0;
dat[2] = 0x0;
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,160);
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){
// upon completed valid message receiving
// data[4] is set to 255
cnt++;
if (dat[5]) { // if an error detected, signal it
PORTD = 0xff; // by setting portd to 0xAA
}
if (dat[4]) { // if message received successfully
cnt = 0;
dat[4] = 0; // clear message received flag
if(dat[0]=='k')
{
PORTE = ~ PORTE;
Delay_ms(5000);
dat[0] = 0x02; // send back to master
Delay_ms(1);
RS485Master_Send(dat,1,160);
Delay_ms(100);
}
}
if (cnt > 100000) {
PORTD ++;
cnt = 0;
RS485Master_Send(dat,1,160);
if (PORTD > 10) // if sending failed 10 times
RS485Master_Send(dat,1,50); // send message on broadcast address
}
}
}
Slave Code:
char dat[9]; // buffer for receving/sending messages
char i,j;
sbit rs485_rxtx_pin at RC2_bit; // set transcieve pin
sbit rs485_rxtx_pin_direction at TRISC2_bit; // set transcieve pin direction
// Interrupt routine
void interrupt() {
RS485Slave_Receive(dat);
}
void main() {
PORTB = 0;
PORTD = 0;
PORTE= 0;
TRISB = 0;
TRISD = 0;
TRISE = 0;
UART1_Init(9600); // initialize UART1 module
Delay_ms(100);
RS485Slave_Init(160); // Intialize MCU as slave, address 160
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) {
if (dat[5]) { // if an error detected, signal it by
PORTD = 0xAA; // setting portd to 0xAA
dat[5] = 0;
}
if (dat[4]) { // upon completed valid message receive
dat[4] = 0; // data[4] is set to 0xFF
if(dat[0]==0x02)
{
PORTE = ~ PORTE;
Delay_ms(5000);
dat[0] = 'k'; // send back to master
Delay_ms(1);
RS485Slave_Send(dat,1);
Delay_ms(100);
}
}
}
}
Can anyone tell me what is wrong in this code? It is almost the same as rs485 example of microE but all what it does is the slave sets portA then nothing after this step.
Did anyone here used rs485 library of mikroE? Is it good one or it is buggy?
I intend to make a master / slave rs485 communication between 2 pic micros. I found a example about how to make this in mikroE compiler help, then I edited few lines in it to suit my application. The problem that the micros do not perform the supposed logic of the code.
Master code:
char dat[10]; // buffer for receving/sending messages
char i,j;
sbit rs485_rxtx_pin at RC2_bit; // set transcieve pin
sbit rs485_rxtx_pin_direction at TRISC2_bit; // set transcieve pin direction
// Interrupt routine
void interrupt() {
RS485Master_Receive(dat);
}
void main(){
long cnt = 0;
PORTB = 0;
PORTD = 0;
PORTE = 0;
TRISB = 0;
TRISD = 0;
TRISE = 0;
UART1_Init(9600); // initialize UART1 module
Delay_ms(100);
RS485Master_Init(); // initialize MCU as Master
dat[0] = 0x02;
dat[1] = 0x0;
dat[2] = 0x0;
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,160);
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){
// upon completed valid message receiving
// data[4] is set to 255
cnt++;
if (dat[5]) { // if an error detected, signal it
PORTD = 0xff; // by setting portd to 0xAA
}
if (dat[4]) { // if message received successfully
cnt = 0;
dat[4] = 0; // clear message received flag
if(dat[0]=='k')
{
PORTE = ~ PORTE;
Delay_ms(5000);
dat[0] = 0x02; // send back to master
Delay_ms(1);
RS485Master_Send(dat,1,160);
Delay_ms(100);
}
}
if (cnt > 100000) {
PORTD ++;
cnt = 0;
RS485Master_Send(dat,1,160);
if (PORTD > 10) // if sending failed 10 times
RS485Master_Send(dat,1,50); // send message on broadcast address
}
}
}
Slave Code:
char dat[9]; // buffer for receving/sending messages
char i,j;
sbit rs485_rxtx_pin at RC2_bit; // set transcieve pin
sbit rs485_rxtx_pin_direction at TRISC2_bit; // set transcieve pin direction
// Interrupt routine
void interrupt() {
RS485Slave_Receive(dat);
}
void main() {
PORTB = 0;
PORTD = 0;
PORTE= 0;
TRISB = 0;
TRISD = 0;
TRISE = 0;
UART1_Init(9600); // initialize UART1 module
Delay_ms(100);
RS485Slave_Init(160); // Intialize MCU as slave, address 160
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) {
if (dat[5]) { // if an error detected, signal it by
PORTD = 0xAA; // setting portd to 0xAA
dat[5] = 0;
}
if (dat[4]) { // upon completed valid message receive
dat[4] = 0; // data[4] is set to 0xFF
if(dat[0]==0x02)
{
PORTE = ~ PORTE;
Delay_ms(5000);
dat[0] = 'k'; // send back to master
Delay_ms(1);
RS485Slave_Send(dat,1);
Delay_ms(100);
}
}
}
}
Can anyone tell me what is wrong in this code? It is almost the same as rs485 example of microE but all what it does is the slave sets portA then nothing after this step.
Did anyone here used rs485 library of mikroE? Is it good one or it is buggy?