Datasheet
if (dat[5]) { // if an error detected, signal it
PORTC = dat[5]; // by setting PORTC
}
if (dat[4]) { // if message received successfully
cnt = 0;
dat[4] = 0; // clear message received flag
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 Slave
Delay_ms(1);
RS485Master_Send(dat,1,160);
}
if (cnt > 100000) { // if in 100000 poll-cycles the answer
PORTA++; // was not detected, signal
cnt = 0; // failure of send-message
RS485Master_Send(dat,1,160);
if (PORTA > 10){ // if sending failed 10 times
PORTA = 0;
RS485Master_Send(dat,1,50); // send message on broad-
cast address
}
}
}
}
RS485 Slave code:
char dat[9]; // buffer for receving/sending messages
char i,j;
sbit RS485_rxtx_pin at PORTD.B2; // set transcieve pin
sbit RS485_rxtx_pin_direction at DDRD.B2; // set transcieve pin
direction
// Interrupt routine
void interrupt() org 0x16 {
RS485Slave_Receive(dat);
}
void main() {
PORTB = 0; // clear PORTB
PORTC = 0; // clear PORTC
DDRB = 0xFF; // set PORTB as output
DDRC = 0xFF; // set PORTB as output
398
MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Libraries
mikroC PRO for AVR
CHAPTER 6