⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fir2.c

📁 硬件设计资料,刚上网查找资料,是个新手,望大家提携了,
💻 C
字号:
/*******************************************************************************
 File: FIR2.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)
*******************************************************************************/

#define FILTER_ORDER 2

/**********************************************************/
//Filter coefficients; LP filter with cut off at 0.4*(fs/2) 
#define B10   4
#define B11   0
#define B12   -5

//Filter coefficients; LP filter with cut off at 0.6*(fs/2) 
#define B20   4
#define B21   0
#define B22   -5

/**********************************************************/
//Global variables
/**********************************************************/
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
	   }; 
//		filter04 = {0,0, B10, B11, B12},          //initialize filter with 0.4 cutoff frequency
//		filter06 = {0,0, B20, B21, B22};          //initialize filter with 0.6 cutoff frequency

/**********************************************************/
//Function declarations
/**********************************************************/
extern int FIR2( struct FIR_filter *thisFilter, int newSample );

/**********************************************************/
//Main function
/**********************************************************/
void main(void)
{
  struct FIR_filter 
    filter04 = {0,0, B10, B11, B12},          //initialize filter with 0.4 cutoff frequency
	filter06 = {0,0, B21, B21, B22};          //initialize filter with 0.6 cutoff frequency
  int filterInput = 0;
  volatile int filterOutput;
  
  //feed filterInput into filter
  filterOutput = FIR2((struct FIR_filter*)&filter04, filterInput);
  
  //Cascade filters by feeding the output of one filter into the input of the other filter
  filterOutput = FIR2(&filter06, (int)filterOutput);

  for(;;);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -