15.5.8 Channel Filter (Secondary Accumulator)

In Second Order Low Pass Filter Example, the second order low pass filter is implemented using the second accumulator.

Second Order Low Pass Filter Example

#include <xc.h>

// VARIABLES OF THE SOFTWARE PART OF THE FILTER.
// These global variables are used in an interrupt.
// That's why they must be declared as "volatile".
// Input for the software part of the filter's first stage.
volatile long ch19_current_1 = 0;
// Input delayed for the first stage.
volatile long ch19_previous_1 = 0;
// Input for the software part of the filter's second stage.
volatile long ch19_current_2 = 0;
// Input delayed for the second stage.
volatile long ch19_previous_2 = 0;
// The filter output.
volatile long filtered_result = 0; 

// Define peripheral clock frequency.
#define FCY (150000000UL) // 150MHz
// Define the CCP1 timer frequency.
#define TIMER_FREQUENCY (100UL) // 1kHz

int main() { 
    
    //Set up clock for 40Msps operation
    clock_ADC_for_40Msps_from_PLL2();
    
    // This example assumes that dsPIC33AK128MC106 device is used.
    // dsPIC33AK128MC106 device has 3 channels with the secondary
    // accumulator implemented: ## 17, 18 and 19.
    // This example will use channel #19.
    // Enable accumulators roll-over to enable the secondary accumulator.
    AD1CH19CONbits.ACCRO = 1;
    // Select integration sampling mode.
    AD1CH19CONbits.MODE = 2;
    // CCP1 Timer starts conversions (1kHz frequency).
    AD1CH19CONbits.TRG1SRC = 12;
    // CCP1 Timer re-triggers (1kHz frequency).
    AD1CH19CONbits.TRG2SRC = 12;
    // Select the AN1 analog input/pin for the signal to b filtered.
    AD1CH19CONbits.PINSEL = 1;
    // Select signal sampling time (6.5 TADs = 81nS).
    AD1CH19CONbits.SAMC = 3;
    
    // Set number of conversions = 8 for the filter (sub-sampler).
    // The CH19RDY bit will be set after 8 conversions.
    // The conversions frequency is 1kHz defined by CCP1 Timer period.
    // The signal maximum frequency is in twice less = 500 Hz. 
    // The filter cut-off frequency is 500kHz/8 = 62.5 Hz.
    AD1CH19CNT = 8;
    
    // Set ADC to RUN mode.
    AD1CONbits.MODE = 2;
    // Enable ADC.
    AD1CONbits.ON = 1;
    // Wait when ADC will be ready/calibrated.
    while(AD1CONbits.ADRDY == 0);
    
    // Configure CCP1 Timer to trigger the channel # 19.
    CCP1CON1bits.MOD = 0;
    // Set 32-bit timer.
    CCP1CON1bits.T32 = 1;
    // Set period.
    CCP1PR = FCY/TIMER_FREQUENCY;
    // Run timer.
    CCP1CON1bits.ON = 1;
    
    // Enable channel # 19 interrupt.
    _AD1CH19IE = 1;
    // The AN1 pin filtered result is available in the channel # 19 interrupt.
    while(1);
    
    return 1;
}

// Channel # 19 interrupt.
// Called when integration is finished (every AD1CNT19 = 8 conversions). 
void __attribute__((interrupt)) _AD1CH19Interrupt(){
    
    long primary_accumulator; 
    
    // Clear interrupt flag. If the interrupt is persistent then
    // to clear the flag it is required to read the ADC channel
    // result register first.
    primary_accumulator = AD1CH19DATA;
    _AD1CH19IF = 0; 
    
    // Process software part of the filter.
    ch19_current_1 = AD1CH19ACC;
    ch19_current_2 = ch19_previous_1 - ch19_current_1; 
    ch19_previous_1 = ch19_current_1;
    filtered_result = ch19_previous_2 - ch19_current_2;
    ch19_previous_2 = ch19_current_2;
    // Divide by 1:(8*8) or 1:64 or shift right by 6
    filtered_result >>= 6; 
}