Mikroelektronika MIKROE-350 データシート
if (dat[4] <> 0) then
' if message received successfully
cnt = 0
dat[4] = 0 ' clear message received flag
j = dat[3]
for i = 1 to dat[3] ' show data on PORTB
for i = 1 to dat[3] ' show data on PORTB
PORTB = dat[i-1]
next i
dat[0] = dat[0]+1 ' increment received dat[0]
Delay_ms(1) ' send back to slave
dat[0] = dat[0]+1 ' increment received dat[0]
Delay_ms(1) ' send back to slave
RS485Master_Send(dat,1,160)
end if
if (cnt > 100000) then
' if in 100000 poll-cycles the answer
Inc(PORTA) ' was not detected, signal
cnt = 0 ' failure of send-message
RS485Master_Send(dat,1,160)
if (PORTA > 10) then
' if sending failed 10 times
PORTA = 0
RS485Master_Send(dat,1,50) ' send message on broadcast
address
end if
end if
wend
end.
RS485 Slave code:
program RS485_Slave_Example
dim dat as byte[20] ' buffer for receving/sending
messages
messages
i, j
as byte
dim rs485_rxtx_pin as sbit at PORTD.B2 ' set transcieve pin
rs485_rxtx_pin_direction
as sbit at DDRD.B2 ' set transcieve
pin direction
' Interrupt routine
sub procedure interrupt() org 0x16
sub procedure interrupt() org 0x16
RS485Slave_Receive(dat)
end sub
main:
PORTB = 0 ' clear PORTB
PORTC = 0 ' clear PORTC
DDRB = 0xFF ' set PORTB as output
DDRC = 0xFF ' set PORTB as output
334
MIKROELEKTRONIKA
- SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Libraries
mikroBasic PRO for AVR
CHAPTER 6