User manual

445
mikoPascal PRO for dsPIC30/33 and PIC24
MikroElektronika
while (TRUE) do
begin // upon completed valid message receiving
// data[4] is set to 255
Inc(cnt);
if (dat[5] <> 0) then // if an error detected, signal it
PORTD := 0xAA; // by setting portd to 0xAA
if (dat[4] <> 0) then // if message received successfully
begin
cnt := 0;
dat[4] := 0; // clear message received ag
j := dat[3];
for i := 1 to dat[3] do // show data on PORTB
PORTB := dat[i-1];
dat[0] := dat[0]+1; // send back to master
Delay_ms(1);
RS485Master_Send(dat,1,160);
end;
if (cnt > 100000) then // if in 100000 poll-cycles the answer
begin
Inc(PORTD); // was not detected, signal
cnt := 0; // failure of send-message
RS485Master_Send(dat,1,160);
if (PORTD > 10) then // if sending failed 10 times
begin
RS485Master_Send(dat,1,50); // send message on broadcast address
end;
end;
end;
end.
Copy Code To Clipboard
program RS485_Slave_Example;
var dat : array[20] of byte; // buffer for receving/sending
messages
i, j : byte;
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
RS485Slave_Receive(dat);
U2RXIF_bit := 0; // ensure interrupt not pending
end;
begin
ADPCFG := 0xFFFF;