8.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 make the following configurations:
    • Oscillator Select: HFINTOSC
    • HF Internal Clock: 1 MHz
    • Clock Divider: 1
    • In the Watchdog Timer Enable field in the WWDT tab, WDT Disabled has to be selected
    • In the Programming tab, Low-Voltage Programming Enable has to be checked
  4. From the Device Resources window, add TMR2 and ADCC and then make the following configurations:

    Timer2 Configuration:

    • Enable Timer: checked
    • Control Mode: Roll over pulse
    • Start/Reset Option: Software control
    • Timer Clock tab
      • Clock Source: LFINTOSC
      • Clock Prescaler: 1:64
      • Postscaler: 1:1
    • Set 100 ms period in the Timer Period tab

    ADCC Configuration:

    • Enable ADCC: checked
    • Operating: Basic mode
    • In the ADC tab choose the following options:
      • ADC Clock → Clock Source: Select FRC
      • Auto-conversion Trigger: Select TMR2
    • CVD Features tab:
      • Enable ADC Interrupt: checked
  5. Open Pin Manager → Grid View window and select UQFN40 in the MCU package field and select the I/O pins outputs to enable the internal signal access to the I/O.
    Figure 8-1. Pin Mapping
  6. Click Pin Module in the Project Resources and set the custom name for RE0 to LED0, select Output box and for the ANA0 pin select the Analog box.
  7. Click Generate in the Project Resources tab.
  8. Add into the main.c file, the following lines of code:
    #define DesiredThreshold    300      /* Desired threshold value */
    volatile uint16_t adcVal;
    static void ADCC_interrupt(void);
    
    static void ADCC_interrupt(void)
    {
        /* Toggle LED0 at the Timer2Period frequency */
        LED0_Toggle();
        adcVal = ADCC_GetConversionResult();
    }
    
    void main(void)
    {
        // Initialize the device
        SYSTEM_Initialize();  
    ADCC_SetADIInterruptHandler(ADCC_Interrupt_by_TMR2);
        // Enable the Global Interrupts
        INTERRUPT_GlobalInterruptEnable();
        // Enable the Peripheral Interrupts
        INTERRUPT_PeripheralInterruptEnable();
    
        while (1)
        {
            if (adcVal > DesiredThreshold)
            {
                LED0_SetLow();
            }
        }
    }