5.1 MCC Generated Code

To generate this project using MPLAB Code Configurator (MCC), follow the next steps:

  1. Create a new MPLAB X IDE project for PIC18F47Q10.
  2. Open MCC from the toolbar (more information about how to install the MCC plug-in can be found here.
  3. Go to Project Resources → System → System Module and do the following configuration:
    • Oscillator Select: HFINTOSC
    • HF Internal Clock: 1 MHz
    • Clock Divider: 4
    • In the Watchdog Timer Enable field in WWDT tab, make sure “WDT Disabled” is selected
    • In the Programming tab, make sure Low-Voltage Programming Enable is checked.
  4. From the Device Resources window, add ADCC, then do the following configuration:
    • Enable ADC: Check
    • Operating: Average_mode
    • Clock Source: FOSC/ADCLK
    • Clock: FOSC/128
    • Result Alignment: Right
    • Positive Reference: VDD
    • Negative Reference: VSS
    • Enable Continuous Operation: Check
    • In Computation Features tab:
      • Error Calculation: First Derivative of Single measurement
      • Threshold Interrupt: ADERR < ADLTH or ADERR > ADUTH
      • Lower Threshold: -35
      • Upper Threshold: 35
      • Repeat: 16
      • Arc Right Shift: 4
    • In CVD Features:
      • Enable ADC Threshold Interrupt: Check
  5. Go to Pin Manager → Grid View and select the RA0 pin as ANx input for the ADCC:
    Figure 5-1. Pin Mapping
  6. Go to the Pin Module in the Project Resources tab and set the RA0 pin as analog and as input (check to see if Output is not checked).
  7. Press the Generate button. The generated code can now be found in the project folder.
  8. Add the following code to the main.c file. The code sets the Interrupt Service Routine (ISR) for the ADCC threshold interrupt to the ThresholdISR() function that returns the value of the error when the interrupt is triggered and clears the Interrupt flag. The rest of the code enables the global and peripheral interrupts as well as discharging the sampling capacitor and starting the conversion on the analog channel.
    uint16_t volatile errVal;
    
    void main(void)
    {
    
        SYSTEM_Initialize();
    
        INTERRUPT_GlobalInterruptEnable();
    
        INTERRUPT_PeripheralInterruptEnable();
        ADCC_SetADTIInterruptHandler(ThresholdISR);
        ADCC_DischargeSampleCapacitor();
        ADCC_StartConversion(channel_ANA0);
        
    
        while (1)
        {
           ;
        }
    }
    
    void ThresholdISR(void)
    {
        errVal = ADCC_GetErrorCalculation();
        PIR1bits.ADTIF = 0;
        
    }