Problem with filter desinger
Posted: 10 Dec 2009 19:20
Hi!
I'm doing a digital filter using the filter design tool mickro c for dsPIC, but I dont have the card mikroe development, I'm using the DSPIC30F4013 and the first problem I have is the ADC sampling rate, I sampled at 22kHz and just rebuild it up to 4kHz frequency, someone can tell me where I'm wrong. the ADC is programmed using the design tool mikroc filters.
and sorry for my english.
// This code was generated by filter designer tool by mikroElektronika
// Date/Time: 10/12/2009 10:28:37 a.m.
// Support info: http://www.mikroe.com
// Device setup:
// Device name: P30F4013
// Device clock: 012.000000 MHz
// Sampling Frequency: 22050 Hz
// Filter setup:
// Filter kind: IIR
// Filter type: Lowpass filter
// Filter order: 6
// Design method: Chebyshev type II
const unsigned int
BUFFER_SIZE = 8;
const unsigned int
FILTER_ORDER = 6;
const signed int
COEFF_B[FILTER_ORDER+1] = {
0x12AF, 0x226B, 0x3BC5, 0x4218, 0x3BC5, 0x226B, 0x12AF};
const signed int
COEFF_A[FILTER_ORDER+1] = {
0x2000, 0xAD3E, 0x6A84, 0xB1D2, 0x2287, 0xF78D, 0x00E7};
const unsigned int
SCALE_B = 4; //
const unsigned int
SCALE_A = -2; //
const unsigned int
LOAD_PIN = 4; // DAC load pin
const unsigned int
CS_PIN = 5; // DAC CS pin
unsigned int inext; // Input buffer index
unsigned int input[BUFFER_SIZE]; // Input buffer
unsigned int output[BUFFER_SIZE]; // Output buffer
// This is ADC interrupt handler.
// Analogue input is sampled and the value is stored into input buffer.
// Input buffer is then passed through filter.
// Finally, the resulting output sample is sent to DAC.
void ADC1Int() org 0x2A { // ADC interrupt handler
unsigned int CurrentValue;
input[inext] = ADCBUF0; // Fetch sample
CurrentValue = IIR_Radix( SCALE_B, //
SCALE_A, //
COEFF_B, // b coefficients of the filter
COEFF_A, // a coefficients of the filter
FILTER_ORDER+1,// Filter order + 1
input, // Input buffer
BUFFER_SIZE, // Input buffer length
output, // Input buffer
inext); // Current sample
output[inext] = CurrentValue;
inext = (inext+1) & (BUFFER_SIZE-1); // inext = (inext + 1) mod BUFFER_SIZE;
LATB=ADCBUF0;
IFS0.F11 = 0; // Clear AD1IF
} //~
// This is Timer1 interrupt handler.
// It is used to start ADC at
// periodic intervals.
void Timer1Int() org 0x1A { // Timer1 interrupt handler
ADCON1.F1 = 1; // Start sampling
ADCON1.F15 = 1; // Start conversion
IFS0.F3 = 0; // Clear TMR1IF
} //~
// Main program starts here.
// Firstly, hardware peripherals are initialized and then
// the program goes to an infinite loop, waiting for interrupts.
void main() {
inext = 0; // Initialize buffer index
Vector_Set(input, BUFFER_SIZE, 0); // Clear input buffer
Vector_Set(output, BUFFER_SIZE, 0); // Clear output buffer
// ADC setup
TRISB = 0x0003; // Use PORTB for input signal
ADCON1 = 0x00E2; // Auto-stop sampling, unsigned integer out
ADCON2 = 0x0000;
ADCON3 = 0x0204; // Sampling time= 3*Tad, minimum Tad selected
ADPCFG = 0x0000; // Configure PORTB as ADC input port
ADCHS = 0x0001; // Sample input on RB1
ADCSSL = 0; // No input scan
// Interrupts setup
IFS0 = 0;
IFS1 = 0;
IFS2 = 0;
INTCON1 = 0x8000; // Nested interrupts DISABLED
INTCON2 = 0;
IEC0 = 0x0808; // Timer1 and ADC interrupts ENABLED
IPC0.F12= 1; // Timer1 interrupt priority level = 1
IPC2.F13= 1; // ADC interrupt priority level = 2
// Timer2 setup
PR1 = 0x0015; // Sampling = 22050 Hz. Value of PR1 is dependent on clock.
T1CON = 0x8000; // Timer1 ON, internal clock FCY, prescaler 1:1
while (1); // Infinite loop,
// wait for interrupts
} //~
I'm doing a digital filter using the filter design tool mickro c for dsPIC, but I dont have the card mikroe development, I'm using the DSPIC30F4013 and the first problem I have is the ADC sampling rate, I sampled at 22kHz and just rebuild it up to 4kHz frequency, someone can tell me where I'm wrong. the ADC is programmed using the design tool mikroc filters.
and sorry for my english.
// This code was generated by filter designer tool by mikroElektronika
// Date/Time: 10/12/2009 10:28:37 a.m.
// Support info: http://www.mikroe.com
// Device setup:
// Device name: P30F4013
// Device clock: 012.000000 MHz
// Sampling Frequency: 22050 Hz
// Filter setup:
// Filter kind: IIR
// Filter type: Lowpass filter
// Filter order: 6
// Design method: Chebyshev type II
const unsigned int
BUFFER_SIZE = 8;
const unsigned int
FILTER_ORDER = 6;
const signed int
COEFF_B[FILTER_ORDER+1] = {
0x12AF, 0x226B, 0x3BC5, 0x4218, 0x3BC5, 0x226B, 0x12AF};
const signed int
COEFF_A[FILTER_ORDER+1] = {
0x2000, 0xAD3E, 0x6A84, 0xB1D2, 0x2287, 0xF78D, 0x00E7};
const unsigned int
SCALE_B = 4; //
const unsigned int
SCALE_A = -2; //
const unsigned int
LOAD_PIN = 4; // DAC load pin
const unsigned int
CS_PIN = 5; // DAC CS pin
unsigned int inext; // Input buffer index
unsigned int input[BUFFER_SIZE]; // Input buffer
unsigned int output[BUFFER_SIZE]; // Output buffer
// This is ADC interrupt handler.
// Analogue input is sampled and the value is stored into input buffer.
// Input buffer is then passed through filter.
// Finally, the resulting output sample is sent to DAC.
void ADC1Int() org 0x2A { // ADC interrupt handler
unsigned int CurrentValue;
input[inext] = ADCBUF0; // Fetch sample
CurrentValue = IIR_Radix( SCALE_B, //
SCALE_A, //
COEFF_B, // b coefficients of the filter
COEFF_A, // a coefficients of the filter
FILTER_ORDER+1,// Filter order + 1
input, // Input buffer
BUFFER_SIZE, // Input buffer length
output, // Input buffer
inext); // Current sample
output[inext] = CurrentValue;
inext = (inext+1) & (BUFFER_SIZE-1); // inext = (inext + 1) mod BUFFER_SIZE;
LATB=ADCBUF0;
IFS0.F11 = 0; // Clear AD1IF
} //~
// This is Timer1 interrupt handler.
// It is used to start ADC at
// periodic intervals.
void Timer1Int() org 0x1A { // Timer1 interrupt handler
ADCON1.F1 = 1; // Start sampling
ADCON1.F15 = 1; // Start conversion
IFS0.F3 = 0; // Clear TMR1IF
} //~
// Main program starts here.
// Firstly, hardware peripherals are initialized and then
// the program goes to an infinite loop, waiting for interrupts.
void main() {
inext = 0; // Initialize buffer index
Vector_Set(input, BUFFER_SIZE, 0); // Clear input buffer
Vector_Set(output, BUFFER_SIZE, 0); // Clear output buffer
// ADC setup
TRISB = 0x0003; // Use PORTB for input signal
ADCON1 = 0x00E2; // Auto-stop sampling, unsigned integer out
ADCON2 = 0x0000;
ADCON3 = 0x0204; // Sampling time= 3*Tad, minimum Tad selected
ADPCFG = 0x0000; // Configure PORTB as ADC input port
ADCHS = 0x0001; // Sample input on RB1
ADCSSL = 0; // No input scan
// Interrupts setup
IFS0 = 0;
IFS1 = 0;
IFS2 = 0;
INTCON1 = 0x8000; // Nested interrupts DISABLED
INTCON2 = 0;
IEC0 = 0x0808; // Timer1 and ADC interrupts ENABLED
IPC0.F12= 1; // Timer1 interrupt priority level = 1
IPC2.F13= 1; // ADC interrupt priority level = 2
// Timer2 setup
PR1 = 0x0015; // Sampling = 22050 Hz. Value of PR1 is dependent on clock.
T1CON = 0x8000; // Timer1 ON, internal clock FCY, prescaler 1:1
while (1); // Infinite loop,
// wait for interrupts
} //~