// ******************************************************************************************* // // Include file for PIC24FJ64GA002 microcontroller. This include file defines // MACROS for special function registers (SFR) and control bits within those // registers. #include "p24fj64ga002.h" #include #include "lcd.h" // ******************************************************************************************* // // Configuration bits for CONFIG1 settings. // // Make sure "Configuration Bits set in code." option is checked in MPLAB. // This option can be set by selecting "Configuration Bits..." under the Configure // menu in MPLAB. // // These settings are appropriate for debugging the PIC microcontroller. If you need to // program the PIC for standalone operation, change the COE_ON option to COE_OFF. _CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & BKBUG_ON & COE_ON & ICS_PGx1 & FWDTEN_OFF & WINDIS_OFF & FWPSA_PR128 & WDTPS_PS32768 ) // ******************************************************************************************* // // Configuration bits for CONFIG2 settings. // Make sure "Configuration Bits set in code." option is checked in MPLAB. // This option can be set by selecting "Configuration Bits..." under the Configure // menu in MPLAB. _CONFIG2( IESO_OFF & SOSCSEL_SOSC & WUTSEL_LEG & FNOSC_PRIPLL & FCKSM_CSDCMD & OSCIOFNC_OFF & IOL1WAY_OFF & I2C1SEL_PRI & POSCMOD_XT ) // ******************************************************************************************* // // Defines to simply UART's baud rate generator (BRG) regiser // given the osicllator freqeuncy and PLLMODE. #define XTFREQ 7372800 // On-board Crystal frequency #define PLLMODE 4 // On-chip PLL setting (Fosc) #define FCY (XTFREQ*PLLMODE)/2 // Instruction Cycle Frequency (Fosc/2) // Baud rate calculation for UART1 (BRGH=0) #define BAUDRATE1 115200 #define BRGVAL1 ((FCY/BAUDRATE1)/16)-1 // Baud rate calculation for UART2 (BRGH=1) #define BAUDRATE2 4800 #define BRGVAL2 ((FCY/BAUDRATE2)/16)-1 // ******************************************************************************************* // #define CMD_LENGTH 6 #define MSG_BUFFER_LENGTH 256 // ******************************************************************************************* // const char* GPRMCCommandString = "$GPRMC"; // ******************************************************************************************* // volatile char cmdBuffer[CMD_LENGTH]; volatile char msgBuffer[MSG_BUFFER_LENGTH+1]; volatile unsigned msgIndex; volatile unsigned msgUpdate; // ******************************************************************************************* // int main(void) { // ******************************************************************************* // // UART1 is used to re-transmit data received from GPS module to host PC // ******************************************************************************* // // RPINR18 is a regsiter for selectable input mapping (see Table 10-2) for // for UART1. U1RXR is 8 bit value used to specifiy connection to which // RP pin. RP9 is used for this configuration. Physical Pin 18. RPINR18bits.U1RXR = 9; // RPOR4 is a register for selctable ouput mapping (see Regsiter 1019) for // pins RP9 and RP8. The register for RP8 is assigned to 3 to connect // the U1TX output for UART1 (see table 10-3). Physical Pin 17. RPOR4bits.RP8R = 3; // Set UART1's baud rate generator register (U1BRG) to the value calculated above. U1BRG = BRGVAL1; // Set UART1's mode register to 8-bit data, no parity, 1 stop bit, enabled. // UARTEN = 1 (enable UART) // PDSEL1:PDSEL0 = 00 (8-bit data, no parity) // STSEL = 0 (1 stop bit) U1MODE = 0x8000; // Set UART2's status and control register // UTXISEL1:UTXISEL0 = 00 (U1TXIF set when character // written to trasmit buffer) // UTXEN = 1 (trasnmit enabled) // URXISEL1:URXISEL0 = 01 (U1RXIF set when any character // is received in receive buffer) // RIDLE = 0 (Reciver is active) U1STA = 0x0440; // Reset status register and enable TX & RX // ******************************************************************************* // // UART2 used to collect receive data from GPS Module // PolStart PMB-GPS Module uses a 4800 buad rate, assuming 8-bits, 1 stop bit. // // NOTE: The TX output for UART2 is not connected. // ******************************************************************************* // // RPINR19 is a regsiter for selectable input mapping (see Table 10-2) for // for UART2. U2RXR is 8 bit value used to specifiy connection to which // RP pin. RP11 is used for this configuration. Physical Pin 22. // Pin 22 is the IO10 connection on the 16-bit 28-pin Starter Board RPINR19bits.U2RXR = 11; // Set UART2's baud rate generator register (U2BRG) to the value calculated above. U2BRG = BRGVAL2; // Set UART2's mode register to 8-bit data, no parity, 1 stop bit, enabled. // UARTEN = 1 (enable UART) // PDSEL1:PDSEL0 = 00 (8-bit data, no parity) // STSEL = 0 (1 stop bit) U2MODE = 0x8000; // Set UART2's status and control register // UTXISEL1:UTXISEL0 = 00 (U1TXIF set when character // written to trasmit buffer) // UTXEN = 1 (trasnmit enabled) // URXISEL1:URXISEL0 = 01 (U1RXIF set when any character // is received in receive buffer) // RIDLE = 0 (Reciver is active) U2STA = 0x0440; // Reset status register and enable TX & RX // Clear the UART2's RX interrupt flag. Althouhg we are not using a ISR for // the UART receive, the UART RX interrupt flag can be used to deermine if // we have recived a character from he UART. IFS1bits.U2RXIF = 0; // Enable the UART 2 RX interrupt IEC1bits.U2RXIE = 1; msgUpdate = 0; msgIndex = 0; while(1) { if( msgUpdate == 1 ) { printf("%s\n\r", msgBuffer); msgUpdate = 0; } } return 0; } // ******************************************************************************************* // // Defines an interrupt service routine that will execute whenever Timer 1's // count reaches the specfied period value defined within the PR1 register. // // _ISR and _ISRFAST are macros for specifying interrupts that // automatically inserts the proper interrupt into the interrupt vector // table // // The functionality defined in an interrupt should be a minimal as possible // to ensure additional interrupts can be processed. void _ISR _U2RXInterrupt(void) { char receivedChar; IFS1bits.U2RXIF = 0; receivedChar = U2RXREG; if( receivedChar == '\n' ) { msgBuffer[msgIndex] = 0; msgUpdate = 1; msgIndex = 0; } else { if( msgIndex < (MSG_BUFFER_LENGTH-1) ) { msgBuffer[msgIndex++] = receivedChar; } } } // ******************************************************************************************* //