// ******************************************************************************************* // // 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 // ******************************************************************************************* // // 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 BAUDRATE 115200 #define BRGVAL ((FCY/BAUDRATE)/16)-1 // ******************************************************************************************* // volatile unsigned char sampleADC; // ******************************************************************************************* // int main(void) { short int rawADCValue; // ******************************************************************************* // // UART1 is connected 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 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 = BRGVAL; // 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 // ******************************************************************************* // // Set Timer1's Prescaler to 1:256 T1CONbits.TCKPS = 3; // Set Timer 1's period value regsiter to value for 250ms. Please note // T1CON's register settings below (internal Fosc/2 and 1:256 prescalar). // // Fosc = XTFREQ * PLLMODE // = 7372800 * 4 // = 29491200 // // Fosc/2 = 29491200 / 2 // = 14745600 // // Timer 1 Freq = (Fosc/2) / Prescaler // = 14745600 / 256 // = 57600 // // PR1 = 250 ms / (1 / (T1 Freq)) - 1 // = 250e-3 / (1 / 57600) - 1 // = 250e-3 * 57600 - 1 // = 14400 - 1 PR1 = 14399; // Clear Timer value (i.e. current tiemr value) to 0 TMR1 = 0; // Clear Timer 1 interrupt flag. This allows us to detect the // first interupt. IFS0bits.T1IF = 0; // Enable the interrupt for Timer 1 IEC0bits.T1IE = 1; // Start Timer 1 T1CONbits.TON = 1; // ******************************************************************************* // // TODO: Set AN5 as Analog inputs. // TODO: Configure ADC MUXA channel for AN5 input and MUXB channel for AN4 input // TODO: Internal counter used to stop sampling and start conversion // TODO: Use AVdd+ (pin 28) and AVss- (pin 27) as reference voltage signals. // TODO: Use system clock to determine auto sample time and converson time. // TODO: Setup sample time to 1 ADC cycle (minimum possible sample time) and // conversion time to 1 ADC cycle (minimum possible conversion time) // Turn ADC On AD1CON1bits.ADON = 1; // ******************************************************************************* // while(1) { if( sampleADC == 1 ) { sampleADC = 0; // TODO: Clear AD1IF so we can detect end of ADC sampling/conversion. // TODO: Start sampling/conversion by setting ASAM to 1. // TODO: Wait for sampling/conversion to complete by checking AD1IF. // TODO: Reset ASAM to 0 so we can sample again. // TODO: Read 10-bit ADC valie from ADC1BUF0 regiser. // Let's see what the value is. printf("%i (%04x)\n\r", rawADCValue, rawADCValue); } } return 0; } // ******************************************************************************************* // void _ISR _T1Interrupt(void) { IFS0bits.T1IF = 0; sampleADC = 1; } // ******************************************************************************************* //