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
- ADC2 functions used in
example application apart from MCC driver generated
Table 4-3. ADC2 Functions Apart From MCC Driver Generated Function Name File Purpose void ADCC_ComputationModeSetup(uint8_t) main.c Calls the mode specific ADC2 initialization function void ADCC_InitializeBasicMode(void) adcc.c Initializes ADC2 to Basic mode void ADCC_InitializeAccumulateMode(void) adcc.c Initializes ADC2 to Accumulate mode void ADCC_InitializeAverageMode(void) adcc.c Initializes ADC2 to Average mode void ADCC_InitializeBurstAverageMode(void) adcc.c Initializes ADC2 to Burst Average mode void ADCC_InitializeLPFMode(void) adcc.c Initializes ADC2 to LPF mode void ADCC_SetChannel(adcc_channel_t) adcc.c Configures the ADC2 channel void ADCC_ProcessResult(void) main.c Reads and process the ADC2 result void DataStreamerTxPkt(pkt_t *) data_streamer.c Initializes the data streamer packet with (SOF) Start of Frame, ADC2 mode, (EOF)End of Frame void DataStreamerInit(pkt_t *) data_streamer.c Transmits data streamer packet through EUSART void CheckButtonPress(void) main.c Reads the status of push button S1 and S2
Computation Mode | Computation Mode Number in Data Visualizer | LED Illuminated |
---|---|---|
Basic | 1 | All LEDs: D2,D3,D4,D5 |
Accumulate | 2 | D2 |
Average | 3 | D3 |
Burst Average | 4 | D4 |
LPF | 5 | D5 |
void ADCC_InitializeBasicMode(void);
void ADCC_InitializeAccumulateMode(void);
void ADCC_InitializeAverageMode(void);
void ADCC_InitializeBurstAverageMode(void);
void ADCC_InitializeLPFMode(void);
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).