User manual

mikroPascal PRO for dsPIC30/33 and PIC24
MikroElektronika
446
PORTB := 0;
PORTD := 0;
TRISB := 0;
TRISD := 0;
UART2_Init(9600); // initialize UART2 module
Delay_ms(100);
RS485Slave_Init(160); // Intialize MCU as slave, address 160
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;
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
while (TRUE) do
begin
if (dat[5] <> 0) then // if an error detected, signal it by
begin
PORTD := 0xAA; // setting portd to 0xAA
dat[5] := 0;
end;
if (dat[4] <> 0) then // upon completed valid message receive
begin
dat[4] := 0; // data[4] is set to 0xFF
j := dat[3];
for i := 1 to dat[3] do // show data on PORTB
PORTB := dat[i-1];
dat[0] := dat[0]+1; // increment received dat[0]
Delay_ms(1);
RS485Slave_Send(dat,1); // and send it back to master
end;
end;
end.