User manual

mikroPascal PRO for dsPIC30/33 and PIC24
MikroElektronika
444
Library Example
The example demonstrates working with the dsPIC as a Master node in RS-485 communication. Master sends message
to Slave with address 160 and waits for a response. After the response is received, the rst byte of received data is
incremented and sent back to the Slave. The received data is displayed on PORTB while error on receiving (0xAA)
and number of consecutive unsuccessful retries are displayed on PORTD. Hardware congurations in this example are
made for the EasydsPIC4A board and dsPIC30F4013.
Copy Code To Clipboard
program RS485_Master_Example;
var dat : array[10] of byte; // buffer for receving/sending messages
i, j : byte;
cnt : longint;
var rs485_rxtx_pin : sbit at RF2_bit; // set transcieve pin
rs485_rxtx_pin_direction : sbit at TRISF2_bit; // set transcieve pin direction
// Interrupt routine
procedure interrupt(); org IVT_ADDR_U2RXINTERRUPT;
begin
RS485Master_Receive(dat);
U2RXIF_bit := 0; // ensure interrupt not pending
end;
begin
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);
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