Mikroelektronika MIKROE-738 Datenbogen

Seite von 682
410
mikoC PRO for PIC32
MikroElektronika
Library Example
This is a simple demonstration of RS485 Library routines usage.
Master sends message to Slave with address 160 and waits for a response. The Slave accepts data, increments it and 
sends it back to the Master. Master then does the same and sends incremented data back to Slave, etc.
Master displays received data on PORTB, while error on receive (0xAA) and number of consecutive unsuccessful 
retries are displayed on PORTD. Slave displays received data on PORTB, while error on receive (0xAA) is displayed 
on PORTD. Hardware configurations in this example are made for the LV-32MX v6 board and PIC32MX460F512L.
RS485 Master code:
Copy Code To Clipboard 
char dat[
10
];              
 // buffer for receving/sending messages
char i,j;
sbit  RS485_rxtx_pin  at RF2_bit;               
// set transcieve pin
sbit  RS485_rxtx_pin_direction at TRISF2_bit;   
// set transcieve pin direction
// Interrupt routine
void interrupt() iv IVT_UART_2 ilevel 7 ics ICS_SRS {
  RS485Master_Receive(dat);
  U2RXIF_bit = 
0
;           
// ensure interrupt not pending
}
void main(){
  long cnt = 
0
;
  CHECON  = 
0x32
;
  AD1PCFG = 
0xFFFF
;
  PORTB = 
0
;
  PORTD = 
0
;
  TRISB = 
0
;
  TRISD = 
0
;
  UART2_Init(
19200
);        
// 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
;
  
  U2IP0_bit = 
1
;            
// set interrupt
  U2IP1_bit = 
1
;            
// priority 
  
U2IP2_bit = 
1
;
            // to 7