User manual

Table Of Contents
412
mikoC PRO for PIC32
MikroElektronika
RS485Slave_Receive(dat);
U2RXIF_bit = 0; // ensure interrupt not pending
}
void main() {
CHECON = 0x32;
AD1PCFG = 0xFFFF;
TRISB = 0;
TRISD = 0;
PORTB = 0;
PORTD = 0;
UART2_Init(19200); // initialize UART2 module
Delay_ms(100);
RS485Slave_Init(160); // Intialize MCU as slave, address 160
dat[0] = 0xAA;
dat[1] = 0xF0;
dat[2] = 0x0F;
dat[4] = 0; // ensure that message received ag is 0
dat[5] = 0; // ensure that error ag is 0
dat[6] = 0;
U2IP0_bit = 1; // set interrupt
U2IP1_bit = 1; // priority
U2IP2_bit = 1; // to 7
URXISEL1_U2STA_bit = 0; // 0x = Interrupt ag bit is set when a character is
received
U2RXIF_bit = 0; // ensure interrupt not pending
MVEC_bit = 1; // Interrupt controller congured for multi vectored mode
asm ei R0; // Enable all interrupts
U2RXIE_bit = 1; // enable intterupt
while (1) {
if (dat[5]) { // if an error detected, signal it by
PORTD = 0xAA; // setting portd to 0xAA
dat[5] = 0;
}
if (dat[4]) { // upon completed valid message receive
dat[4] = 0; // data[4] is set to 0xFF
j = dat[3];
for (i = 1; i <= dat[3];i++){
PORTB = dat[i-1];
}
dat[0] = dat[0]+1; // increment received dat[0]
Delay_ms(1);
RS485Slave_Send(dat,1); // and send it back to master
}
}
}