User manual
470
mikoC PRO for dsPIC
MikroElektronika
URXISEL1_U2STA_bit = 0;
URXISEL1_U2STA_bit = 0;
NSTDIS_bit = 1; // no nesting of interrupts
U2RXIF_bit = 0; // ensure interrupt not pending
U2RXIE_bit = 1; // enable intterupt
while (1){
// upon completed valid message receiving
// data[4] is set to 255
cnt++;
if (dat[5]) { // if an error detected, signal it
PORTD = 0xAA; // by setting portd to 0xAA
}
if (dat[4]) { // if message received successfully
cnt = 0;
dat[4] = 0; // clear message received ag
j = dat[3];
for (i = 1; i <= dat[3]; i++) { // show data on PORTB
PORTB = dat[i-1];
} // increment received dat[0]
dat[0] = dat[0]+1; // send back to master
Delay_ms(1);
RS485Master_Send(dat,1,160);
}
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
}
}
// function to be properly linked.
}
RS485 Slave code:
Copy Code To Clipboard
sbit rs485_rxtx_pin at RF2_bit; // set transcieve pin
sbit rs485_rxtx_pin_direction at TRISF2_bit; // set transcieve pin direction
char dat[9]; // buffer for receving/sending messages
char i,j;
// Interrupt routine
void interrupt() org IVT_ADDR_U2RXINTERRUPT{
RS485Slave_Receive(dat);
U2RXIF_bit = 0; // ensure interrupt not pending
}