hitachicm615
Newbie level 5
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.
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,
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
}
}
}