User manual

mikroC PRO for dsPIC
MikroElektronika
469
Library Example
This is a simple demonstration of RS485 Library routines usage.
Master sends message to Slave with address 160 and waits for a response. The Slave accepts data, increments it and
sends it back to the Master. Master then does the same and sends incremented data back to Slave, etc.
Master displays received data on PORTB, while error on receive (0xAA) and number of consecutive unsuccessful
retries are displayed on PORTD. Slave displays received data on PORTB, while error on receive (0xAA) is displayed
on PORTD. Hardware congurations in this example are made for the EasydsPIC4A board and 30f4013.
RS485 Master 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[10]; // buffer for receving/sending messages
char i,j;
// Interrupt routine
void interrupt() org IVT_ADDR_U2RXINTERRUPT {
RS485Master_Receive(dat);
U2RXIF_bit = 0; // ensure interrupt not pending
}
void main(){
long cnt = 0;
ADPCFG = 0xFFFF;
PORTB = 0;
PORTD = 0;
TRISB = 0;
TRISD = 0;
UART2_Init(9600); // initialize UART2 module
Delay_ms(100);
RS485Master_Init(); // initialize MCU as Master
dat[0] = 0xAA;
dat[1] = 0xF0;
dat[2] = 0x0F;
dat[4] = 0; // ensure that message received ag is 0
dat[5] = 0; // ensure that error ag is 0
dat[6] = 0;
RS485Master_Send(dat,1,160);