// Usart test program // Jurgen Strydom 2007 09 05 // www.flatspike.com #include #include #include #pragma config WDT = OFF, OSC = INTIO2, LVP = OFF, MCLRE = OFF void main (void); //---------------------------------------------------------------------------- void main (void) { int k,h,i,j; h = 0; k = 0; i = 0; j = 0; // OSC settings OSCCONbits.IRCF2 = 1; //Set osc to be 8 MHZ OSCCONbits.IRCF1 = 1; //OSCCON<2:0> = 111; OSCCONbits.IRCF0 = 1; // // USART sttuff TRISB = 0b01010011; //Tris Settings RCSTAbits.SPEN = 1; // Usart bit ADCON1bits.PCFG6 = 1; //Configure as digital port ADCON1bits.PCFG5 = 1; //Configure as digital port ADCON1bits.PCFG4 = 1; //Configure as digital port OpenUSART( USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH , 25); PORTB = 0; //Clear PORTB TRISA = 0; PORTA = 0; i = 1; while (1){ while (DataRdyUSART() == 1) // Reading data with RS232 { h = ReadUSART(); if (h == 27) { if (i == 0) { i = 1; PORTAbits.RA0 = 1; } else { i = 0; PORTAbits.RA0 = 0; } k = 0; } else { WriteUSART(h); } h = 0; } if ((i == 1) & (BusyUSART() == 0) ) { WriteUSART(k); k++; if (j == 1) { j = 0;} else {j = 1; } if (k >= 255) {k = 0;} } else if (i == 0) { if (j == 1) { j = 0;} else {j = 1; } } PORTAbits.RA2 = j; } }