Mikroelektronika MIKROE-442 데이터 시트
mikroBasic PRO for dsPIC30/33 and PIC24
MikroElektronika
445
Library Example
The example demonstrates working with the dsPIC as a Master node in RS-485 communication. Master sends message
to Slave with address 160 and waits for a response. After the response is received, the first byte of received data is
incremented and sent back to the Slave. The received data is displayed on PORTB while error on receiving (0xAA)
and number of consecutive unsuccessful retries are displayed on PORTD. Hardware configurations in this example are
made for the EasydsPIC4A board and dsPIC30F4013.
RS485 Master code:
Copy Code To Clipboard
program RS485_Master_Example
dim dat as byte[10] ‘ buffer for receving/sending messages
i, j as byte
cnt as longint
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
RS485Master_Receive(dat)
U2RXIF_bit = 0 ‘ ensure interrupt not pending
end sub
main:
cnt = 0
ADPCFG = 0xFFFF
PORTB = 0
PORTD = 0
TRISB = 0
TRISD = 0
UART2_Init(9600) ‘ initialize UART2 module
Delay_ms(100)
RS485Master_Init() ‘ initialize MCU as Master
dat[0] = 0xAA
dat[1] = 0xF0
dat[2] = 0x0F
dat[4] = 0 ‘ ensure that message received flag is 0
dat[5] = 0 ‘ ensure that error flag is 0
dat[6] = 0
RS485Master_Send(dat,1,160)
URXISEL1_U2STA_bit = 0
URXISEL1_U2STA_bit = 0
NSTDIS_bit = 1 ‘ no nesting of interrupts
U2RXIF_bit = 0 ‘ ensure interrupt not pending