User manual
372
mikoPascal PRO for PIC32
MikroElektronika
Library Example
The example demonstrates working with the PIC32 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 congurations in this example are
made for the LV-32MX v6 board and PIC32MX460F512L.
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(); iv IVT_UART_2; ilevel 7; ics ICS_SRS;
begin
RS485Master_Receive(dat);
U2RXIF_bit := 0; // ensure interrupt not pending
end;
begin
cnt := 0;
CHECON := 0x32;
AD1PCFG := 0xFFFF;
PORTB := 0;
PORTD := 0;
TRISB := 0;
TRISD := 0;
UART2_Init(19200); // 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;
U2IP0_bit := 1; // set interrupt
U2IP1_bit := 1; // priority
U2IP2_bit := 1; // to 7
URXISEL1_U2STA_bit := 0; // 0x = Interrupt ag bit is set when a character is received
U2RXIF_bit := 0; // ensure interrupt not pending