Professional Documents
Culture Documents
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;
unsigned inext;
unsigned input[BUFFER_SIZE];
// Filter order
// b coefficients of the filter
// Input buffer length
// Input buffer
// Current sample
// inext := (inext + 1) mod BUFFFER_SIZE;
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;
}
// 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;
IEC0 = 0x0808;
IPC0.F12= 1;
IPC2.F13= 1;
// Timer2 setup
PR1 = 0x0320;
T1CON = 0x8000;
while (1);
}//