📄 iir2.c
字号:
/*******************************************************************************
File: IIR2.c
Version: 1.0 (Not verified)
Date: 02.02.20 [yy.mm.dd]
Author: jllassen
Target device: AVRs with HW multiplier
Compiler:
IAR EWAVR 2.26C
Describtion:
Initialization and call of a 2nd order casadeable FIR filter.
The filter is implementes in assembly. To illustrated to
possibility to call the filter in cascade to generate a higher
order filter the output or the first filter is feed into the
second filter.
Usage:
Implemented so that the filter can be called from IAR EWAVR C compiler.
address to the filter nodes and filter coefficients should be provided
as first calling argument (will thus be placed in R16:R17) and the new
input sample to the filter should be provided as the second calling
argument (placed in R18:R19).
int FIR_filter(*myFilter, newDataSample);
The structure for the filter nodes and filter coefficients should be
as follows:
struct FIR_filter{
int filterNodes[FILTER_ORDER]; //memory nodes required to store x(n-1), x(n-2)
int filterCoefficients[(FILTER_ORDER)+1]; //filter coefficients B0, B1, B2
}, myFilter = {0, 0, B0, B1, B2}
The filter nodes should be initialized prior to the first call of the
filter function and the filter coefficients should be scaled to use
signed 13-bit resolution (max)
Registers usage:
r0-4, r16-23, Z (all scratch/volatile registers)
Memory usage for variables and constants:
4 bytes SRAM for filter nodes (filter memory)
6 bytes SRAM for filter coefficients
Stats:
INIT: Depending on compiler settings?
FILTER: 60 instructions; 88 cycles (excl. ret)
*******************************************************************************/
#include <inavr.h>
#define FILTER_ORDER 2
/**********************************************************/
//Filter coefficients; Bandpass filter cutoff frequencies at [0.4 0.6]*(fs/2)
#define B0 4
#define B1 0
#define B2 -5
#define A1 10
#define A2 10
/**********************************************************/
//Global variables
/**********************************************************/
struct IIR_filter{
int filterNodes[FILTER_ORDER*2]; //memory nodes required to store x(n-1), x(n-2)
int filterCoefficients[(FILTER_ORDER*2)+1]; //filter coefficients B0, B1, B2, A1, A2
};
/**********************************************************/
//Function declarations
/**********************************************************/
extern int IIR2( struct IIR_filter *thisFilter, int newSample );
/**********************************************************/
//Main function
/**********************************************************/
void main(void)
{
struct IIR_filter
filter04_06 = {0,0,0,0, B0, B1, B2, A1, A2}; //initialize filter with 0.4 cutoff frequency
int filterInput = 0;
volatile int filterOutput;
//feed filterInput into filter
filterOutput = IIR2((struct IIR_filter*)&filter04_06, filterInput);
for(;;)
__no_operation();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -