You are on page 1of 3

FILTRO FIR EN: mikroC PRO for dsPIC

Tomado y modificado de Filter Design Tool


//
//
//
//
//
//
//
//
//
//
//

Device setup:
Device name: P30F4013
Device clock: 064.000000 MHz
Sampling Frequency: 20000 Hz
Filter setup:
Filter kind: FIR
Filter type: Lowpass filter
Filter order: 20
Filter window: Rectangular
Filter borders:
Wpass:600 Hz

//#include <Spi_Const.h>
const unsigned int
BUFFER_SIZE = 32,
FILTER_ORDER = 20;
const int
COEFF_B[FILTER_ORDER+1] = {
0x0698, 0x084F, 0x0A01, 0x0BA3, 0x0D28, 0x0E85,
0x0FB2, 0x10A6, 0x1159, 0x11C7, 0x11EC, 0x11C7,
0x1159, 0x10A6, 0x0FB2, 0x0E85, 0x0D28, 0x0BA3,
0x0A01, 0x084F, 0x0698};
const unsigned int
LOAD_PIN = 4,
CS_PIN
= 5;

// DAC load pin


// DAC CS pin

unsigned inext;
unsigned input[BUFFER_SIZE];

// Input buffer index


// Input 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 {
unsigned CurrentValue;
input[inext] = ADCBUF0;

// ADC interrupt handler


// Fetch sample

CurrentValue = FIR_Radix( FILTER_ORDER+1,


COEFF_B,
BUFFER_SIZE,
input,
inext);
inext = (inext+1) & (BUFFER_SIZE-1);
while (SPI1STAT.F1 == 1);

// Filter order
// b coefficients of the filter
// Input buffer length
// Input buffer
// Current sample
// inext := (inext + 1) mod BUFFFER_SIZE;

// wait for SPI module to finish, if doing something

LATF.CS_PIN = 0;
// CS enable for DAC
SPI1BUF = 0x1000 | CurrentValue; // Write CurrentValue to DAC ($3 is required by DAC)
while (SPI1STAT.F1 == 1)
// Wait for SPI module to finish write
LATF.LOAD_PIN = 0;
// Load data in DAC
LATF.LOAD_PIN = 1;
//
LATF.CS_PIN = 1;
// CS disable for DAC
IFS0.F11 = 0;
// Clear AD1IF
}
// This is Timer1 interrupt handler.
// It is used to start ADC at periodic intervals.
void Timer1Int() org 0x1A {
ADCON1.F1 = 1;
ADCON1.F15 = 1;
IFS0.F3 = 0;
}

// Timer1 interrupt handler


// Start sampling
// Start conversion
// 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() {
// DAC setup
TRISF.LOAD_PIN = 0;
TRISF.CS_PIN = 0;
LATF.CS_PIN = 1;
LATF.LOAD_PIN = 1;

// LOAD pin
// CS pin
// Set CS to inactive
// Set LOAD to inactive

// SPI setup
SPI1_Init_Advanced(_SPI_MASTER, _SPI_16_BIT, _SPI_PRESCALE_SEC_1,
_SPI_PRESCALE_PRI_1, _SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_HIGH,
_SPI_ACTIVE_2_IDLE);
inext = 0;
// Initialize buffer index
Vector_Set(input, BUFFER_SIZE, 0); // Clear input buffer
// ADC setup
TRISB = 0xFFFF;
ADCON1 = 0x01E2;
ADCON2 = 0x0000;
ADCON3 = 0x0215;
ADPCFG = 0x0000;
ADCHS = 0x0001;
ADCSSL = 0;
// Interrupts setup
IFS0 = 0;
IFS1 = 0;
IFS2 = 0;
INTCON1 = 0x8000;
INTCON2 = 0;

// Use PORTB for input signal


// Auto-stop sampling, unsigned integer out
// Sampling time= 3*Tad, minimum Tad selected
// Configure PORTB as ADC input port
// Sample input on RB1
// No input scan

// Nested interrupts DISABLED

IEC0 = 0x0808;
IPC0.F12= 1;
IPC2.F13= 1;

// Timer1 and ADC interrupts ENABLED


// Timer1 interrupt priority level = 1
// ADC interrupt priority level = 2

// Timer2 setup
PR1 = 0x0320;
T1CON = 0x8000;

// Sampling = 20000 Hz. Value of PR1 is dependent on clock.


// Timer1 ON, internal clock FCY, prescaler 1:1

while (1);
}//

// Infinite loop, wait for interrupts

You might also like