#include "p24fj64ga002.h"
#define XTFREQ 7372800 //On-board Crystal frequency
#define PLLMODE 2 //On-chip PLL setting
#define FCY XTFREQ*PLLMODE //Instruction Cycle Frequency
#define BAUDRATE 9600
#define BRGVAL ((FCY/BAUDRATE)/16)-1
int main(void)
{
CLKDIVbits.RCDIV = 0;
RPINR18bits.U1RXR = 9; // Make Pin RP9 U1RX
RPOR4bits.RP8R = 3; // Make Pin RP8 U1TX
PADCFG1 = 0xFF; // Make analog pins digital
LATB = 0x0;
TRISB = 0x0FFF; // Configure LED pins as output
TMR1 = 0; // Clear timer 1
PR1 = 0x3D09; // Interrupt every 250ms
IFS0bits.T1IF = 0; // Clear interrupt flag
IEC0bits.T1IE = 1; // Set interrupt enable bit
T1CON = 0x8030; // Fosc/4, 1:256 prescale, start TMR1
TRISB = 0x0300;
U1BRG = BRGVAL;
U1MODE = 0x8000; // Reset UART to 8-n-1, alt pins, and enable
U1STA = 0x0440; // Reset status register and enable TX & RX
_U1RXIF=0; // Clear UART RX Interrupt Flag
while(1)
{
int a;
while (_U1RXIF==0); // Wait and Receive One Character
a = U1RXREG;
while(!U1STAbits.TRMT);
U1TXREG = '"';
while(!U1STAbits.TRMT); // Echo Back Received Character with quotes
U1TXREG = a;
while(!U1STAbits.TRMT);
U1TXREG = '"';
while(!U1STAbits.TRMT);
U1TXREG = ' ';
_U1RXIF=0; // Clear UART RX Interrupt Flag
}
return 0;
}
void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void)
{
IFS0bits.T1IF = 0; // clear interrupt flag
LATB ^= 0xF000; //Toggle LED's
}