User manual

446
mikoBasic PRO for dsPIC30/33 and PIC24
MikroElektronika
U2RXIE_bit = 1 ‘ enable intterupt
while (TRUE)
‘ 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
end if
if (dat[4] <> 0) then ‘ if message received successfully
cnt = 0
dat[4] = 0 ‘ clear message received ag
j = dat[3]
for i = 1 to dat[3] ‘ show data on PORTB
PORTB = dat[i-1]
next i
dat[0] = dat[0]+1 ‘ send back to master
Delay_ms(1)
RS485Master_Send(dat,1,160)
end if
if (cnt > 100000) then ‘ if in 100000 poll-cycles the answer
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
RS485Master_Send(dat,1,50) ‘ send message on broadcast address
end if
end if
wend
end.
Copy Code To Clipboard
program RS485_Slave_Example
dim dat as byte[20] ‘ buffer for receving/sending messages
i, j as byte
dim rs485_rxtx_pin as sbit at RF2_bit ‘ set transcieve pin
rs485_rxtx_pin_direction as sbit at TRISF2_bit ‘ set transcieve pin direction
‘ Interrupt routine
sub procedure interrupt() org IVT_ADDR_U2RXINTERRUPT
RS485Slave_Receive(dat)
U2RXIF_bit = 0 ‘ ensure interrupt not pending
end sub
main:
ADPCFG = 0xFFFF