# IIR Filter implementation on dsPIC33

Hello,

I am currently working on a project with dspic33ev on Mplab X IDE and xc16 compiler

I need to remove DC component from a SineWave so to do that I thought that it might be useful to work with DSP Library and IIR Transposed predefined functions

The first step I did was to calculate the filter coefficients on Matlab by specifying the filter order and the cut off frequency (See the picture below)

NB : Filter Order = 2

Cut off frequency = 5Hz

Sampling frequency = 2 KHz

Then, on simulink, I specified a sine wave input with a constant offset and I supposed to get only the offset at the output scope (See attached the output signal)

The second step I did was to divide all the coefficients by 2 (so that they will be between -1 and 1) and then convert them to Q15

The problem is that the output in Mplab takes random values (Normally, I should get values near to 2045 ADC which is the value of the offset I applied using GBF : 2.5 V )

See the output values in a picture attached

****************************************************************************************************

; File iir_filter.s

.equ iir_filterNumSections, 1

; Allocate and initialize filter coefficients

; These coefficients have been designed for use in the Transpose filter only

.section .xdata,data,xmemory

iir_filterCoefs:

.hword 0x0001 ; b( 1,0)/2

.hword 0x0002 ; b( 1,1)/2

.hword 0x3E98 ; a( 1,1)/2

.hword 0x0001 ; b( 1,2)/2

.hword 0x816C ; a( 1,2)/2

; ..............................................................................

; Allocate states buffers in (uninitialized) Y data space

.section .ybss, bss, ymemory

iir_filterStates1:

.space iir_filterNumSections*2

iir_filterStates2:

.space iir_filterNumSections*2

; ..............................................................................

; Allocate and intialize filter structure

.section .data

.global _Filter1

_Filter1:

.hword iir_filterNumSections-1

.hword iir_filterCoefs

.hword 0xFF00

.hword iir_filterStates1

.hword iir_filterStates2

.hword 0x0000

***********************************************************************************************

Main.c

***********************************************************************************************

/******************************************************************************/

/* Files to Include */

/******************************************************************************/

#include <xc.h>

#include <dsp.h>

#include "system.h" /* System funct/params, like osc/peripheral config */

#include "ADC.h"

#include "dsp.h"

#define SEUIL 1000 /*seuil de test : 1.22V environ*/

/******************************************************************************/

/* Global Variable Declaration */

/******************************************************************************/

extern IIRTransposedStruct Filter1;

/*buffer utilisé par la voie 0 du module DMA ==> mesures brutes*/

volatile unsigned int AcquistionsTab[DMA_BUFFER_SIZE] __attribute__((aligned(16)));

#define DATA_LENGTH 255

unsigned int data_in[DATA_LENGTH] __attribute__((space(xmemory)));

unsigned int data_out[DATA_LENGTH];

unsigned int OutVoltage = 0;

unsigned char counter = 0;

void Timer1_config(void)

{

T1CONbits.TON = 0;

T1CONbits.TSIDL = 0;

T1CONbits.TCKPS = 0;

T1CONbits.TCS = 0;

PR1 = 15000;

IPC0bits.T1IP = 2;

TMR1 = 0;

T1CONbits.TON = 1;

IFS0bits.T1IF = 0;

IEC0bits.T1IE = 1;

}

void __attribute__((__interrupt__,__no_auto_psv__)) _T1Interrupt(void)

{ //LATC6_bit =~ LATC6_bit;

OutVoltage = Read_ADC(TENSION_SORTIE);

data_in[counter] = OutVoltage;

counter++;

IFS0bits.T1IF = 0;

}

int main(void)

{

/* broches de test*/

/* Configure the oscillator for the device */

ConfigureOscillator();

Init_ADC();

// int cpt = 0;

T5CONbits.TCKPS = 0;

PR5 = 3000;

IEC1bits.T5IE = 1;

IFS1bits.T5IF = 0;

IPC7bits.T5IP = 7;

T5CONbits.TON = 1;

Timer1_config();

IIRTransposedInit(&Filter1);

aa =1;

while(1)

{

IIRTransposed(DATA_LENGTH, (fractional*)data_out, (fractional*)data_in, &Filter1);

}

}

****************************************************************************************************

Thank you in advance for your help !

Nada