User manual
mikroBasic PRO for PIC32
MikroElektronika
383
MVEC_bit = 1 ‘ Interrupt controller congured for multi vectored mode
asm
ei R0 ‘ Enable all interrupts
end asm
U2RXIE_bit = 1 ‘ enable intterupt
RS485Master_Send(dat,1,160)
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() iv IVT_UART_2 ilevel 7 ics ICS_SRS
RS485Slave_Receive(dat)
U2RXIF_bit = 0 ‘ ensure interrupt not pending
end sub