4.4 Source Code Overview

Source Code Overview Using the PIC MCU Q10

  • CPU clock: 32 MHz
  • Peripherals used:
    • ADC2
      • ADC input channel is AIN1: pin RA1
      • ADC reference voltage: VDD
      • ADC clock: 1 MHz (Fosc/32)
      • ADC Conversion time for single sample: 11.5 μs
    • EUSART
      • TX pin RC6
      • Baud rate: 500000, ADC result is sent to the serial terminal
    • TMR0:
      • Timer 0 is configured to have 625 μs (1.6 kHz sampling frequency) trigger period, which the ADC in auto conversion trigger uses.
    • GPIO
      • pin RB4: Switch S1, to increase ADCC mode
      • Pin RC5: Switch S2, to decrease ADCC mode
      • pin RA4: LED D2
      • pin RA5: LED D3
      • pin RA6: LED D4
      • pin RA7: LED D5
  • ADC2 functions used in example application apart from MCC driver generated
    Table 4-3. ADC2 Functions Apart From MCC Driver Generated
    Function NameFile Purpose
    void ADCC_ComputationModeSetup(uint8_t) main.cCalls the mode specific ADC2 initialization function
    void ADCC_InitializeBasicMode(void)adcc.cInitializes ADC2 to Basic mode
    void ADCC_InitializeAccumulateMode(void)adcc.cInitializes ADC2 to Accumulate mode
    void ADCC_InitializeAverageMode(void)adcc.cInitializes ADC2 to Average mode
    void ADCC_InitializeBurstAverageMode(void)adcc.cInitializes ADC2 to Burst Average mode
    void ADCC_InitializeLPFMode(void)adcc.cInitializes ADC2 to LPF mode
    void ADCC_SetChannel(adcc_channel_t)adcc.cConfigures the ADC2 channel
    void ADCC_ProcessResult(void)main.cReads and process the ADC2 result
    void DataStreamerTxPkt(pkt_t *)data_streamer.cInitializes the data streamer packet with (SOF) Start of Frame, ADC2 mode, (EOF)End of Frame
    void DataStreamerInit(pkt_t *)data_streamer.cTransmits data streamer packet through EUSART
    void CheckButtonPress(void)main.cReads the status of push button S1 and S2
Table 4-4. Computation Mode: Associated Number and LED
Computation Mode Computation Mode Number in Data VisualizerLED Illuminated
Basic1All LEDs: D2,D3,D4,D5
Accumulate2D2
Average3D3
Burst Average4D4
LPF5D5
The ADC2 is configured in the adcc.c in the project folder. Below are the mode specific functions defined in adcc.c which are used in this application.
void ADCC_InitializeBasicMode(void);
void ADCC_InitializeAccumulateMode(void);
void ADCC_InitializeAverageMode(void);
void ADCC_InitializeBurstAverageMode(void);
void ADCC_InitializeLPFMode(void);
Note: In adcc.c the ADCC_Initialize() function is a driver generated function and called in SYSTEM_Initialize() as part of standard driver initialization.

Upon power-up, the default mode is the Basic mode, which calls the initialization routine ADCC_InitializeBasicMode() configured for the Basic mode. The computation mode number will be configured to 1 to represent it in the Data Visualizer and all LEDs are turned on. When S1 push-button is pressed, the computation mode will be increased and it will initialize the ADC with the selected mode. Different LEDs are turned on depending on selected computation mode. The Computation mode and Associated Computation mode number and LED illumination are shown in Table 4-4.

The interrupt, regardless of threshold test results, is configured in ADCON3 registers (ADTMD[2:0] = 7). When ADC result is ready, the ADTIF flag will be set and the ADC result and selected computation mode number will be sent to the Data Visualizer over EUSART.

The ADTIF flag is set on every ADC conversion for the Basic and Accumulate modes. For the Average, Burst Average, and LPF modes it will be set after accumulating configured n number of samples. In this example source code, it will be set after accumulating 32 samples (as ADCRS is configured to 5).