STM32F072B-Discovery 实现DA AD IIR 25HZ低通滤波器

 当输入16hz时,滤波输出幅值2000左右(蓝色波形)当输入50hz时,滤波输出幅值600左右(蓝色波形),滤波效果很明显

 

 

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f0xx_it.h"
#include "iir_filter.h"
/** @addtogroup STM32F072B_DISCOVERY_Examples
* @{
*/
/** @addtogroup ADC_DMA
* @{
*/

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADC1_DR_ADDRESS ((uint32_t)0x40012440)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* You can monitor the converted value by adding the variable "uhADC1ConvertedValue"
to the debugger watch window */

/* Private function prototypes -----------------------------------------------*/
static void ADC_Config(void);
static void DMA_Config(void);
static void TIM7_Config(void);
/** @addtogroup DAC_SignalsGeneration
* @{
*/

/* Private typedef -----------------------------------------------------------*/
DAC_InitTypeDef DAC_InitStructure;

/* Private define ------------------------------------------------------------*/
#define DAC_DHR12R1_ADDRESS 0x40007408
#define DAC_DHR8R2_ADDRESS 0x4000741C

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
const uint16_t Sine12bit[256] = {
//正弦波描点
2048, 2098, 2148, 2198, 2248, 2298, 2348, 2398, 2447, 2496,
2545, 2594, 2642, 2690, 2737, 2785, 2831, 2877, 2923, 2968,
3013, 3057, 3100, 3143, 3185, 3227, 3267, 3307, 3347, 3385,
3423, 3460, 3496, 3531, 3565, 3598, 3631, 3662, 3692, 3722,
3750, 3778, 3804, 3829, 3854, 3877, 3899, 3920, 3940, 3958,
3976, 3992, 4007, 4021, 4034, 4046, 4056, 4065, 4073, 4080,
4086, 4090, 4093, 4095, 4095, 4095, 4093, 4090, 4086, 4080,
4073, 4065, 4056, 4046, 4034, 4021, 4007, 3992, 3976, 3958,
3940, 3920, 3899, 3877, 3854, 3829, 3804, 3778, 3750, 3722,
3692, 3662, 3631, 3598, 3565, 3531, 3496, 3460, 3423, 3385,
3347, 3307, 3267, 3227, 3185, 3143, 3100, 3057, 3013, 2968,
2923, 2877, 2831, 2785, 2737, 2690, 2642, 2594, 2545, 2496,
2447, 2398, 2348, 2298, 2248, 2198, 2148, 2098, 2047, 1997,
1947, 1897, 1847, 1797, 1747, 1697, 1648, 1599, 1550, 1501,
1453, 1405, 1358, 1310, 1264, 1218, 1172, 1127, 1082, 1038,
995, 952, 910, 868, 828, 788, 748, 710, 672, 635,
599, 564, 530, 497, 464, 433, 403, 373, 345, 317,
291, 266, 241, 218, 196, 175, 155, 137, 119, 103,
88, 74, 61, 49, 39, 30, 22, 15, 9, 5,
2, 0, 0, 0, 2, 5, 9, 15, 22, 30,
39, 49, 61, 74, 88, 103, 119, 137, 155, 175,
196, 218, 241, 266, 291, 317, 345, 373, 403, 433,
464, 497, 530, 564, 599, 635, 672, 710, 748, 788,
828, 868, 910, 952, 995, 1038, 1082, 1127, 1172, 1218,
1264, 1310, 1358, 1405, 1453, 1501, 1550, 1599, 1648, 1697,
1747, 1797, 1847, 1897, 1947, 1997 };
const uint8_t Escalator8bit[6] = {0x0, 0x33, 0x66, 0x99, 0xCC, 0xFF};
__IO uint8_t SelectedWavesForm = 1;
__IO uint8_t KeyPressed = SET;
__IO uint16_t uhADC1ConvertedValue = 0;
__IO uint32_t uwADC1ConvertedVoltage = 0;
uint16_t DACVALUE;
int16_t ADLEVEL;
extern uint16_t cnt;
/* Private function prototypes -----------------------------------------------*/
static void DAC_Config(void);
static void TIM2_Config(void);
static void DAC_Sine_EscalatorConfig(void);
static void DAC_Noise_TriangleConfig(void);

/* Private functions ---------------------------------------------------------*/

/**
* @brief Main program.
* @param None
* @retval None
*/程序修改如上,详细内容太大了省略。

IIR程序如下:

#include "stdint.h"
#include "iir_filter.h"
#include "iir_coefs.h"

static float y[IIR_NSEC][3];
static float x[IIR_NSEC+1][3];


int16_t iir_filter(int16_t in)
{
uint16_t i;

x[0][0] = in;
for(i=0;i<IIR_NSEC;i++)
{
y[i][0] =x[i][0]*IIR_B[i][0]+x[i][1]*IIR_B[i][1]+x[i][2]*IIR_B[i][2]-y[i][1]*IIR_A[i][1]-y[i][2]*IIR_A[i][2];
y[i][0] /= IIR_A[i][0];

y[i][2]=y[i][1];y[i][1]=y[i][0];
x[i][2]=x[i][1];x[i][1]=x[i][0];

x[i+1][0] = y[i][0];
}

if( x[IIR_NSEC][0]>32767) x[IIR_NSEC][0]=32767;
if( x[IIR_NSEC][0]<-32768) x[IIR_NSEC][0]=-32768;
return ((int16_t)x[IIR_NSEC][0]);

}


//复位滤波器
void iir_reset(void)
{
uint16_t i,j;

for(i=0;i<IIR_NSEC+1;i++)
{
for(j=0;j<3;j++)
{
x[i][j]=0;
}
}


for(i=0;i<IIR_NSEC;i++)
{
for(j=0;j<3;j++)
{
y[i][j]=0;
}
}
}

系数由MATLAB生成加以删减

#define IIR_NSEC 3
const int NL[IIR_NSEC] = { 1,3,1 };
const float IIR_B[IIR_NSEC][3] = {
{
0.03456650674, 0, 0
},
{
1, 2, 1
},
{
0.8912509084, 0, 0
}
};
const int DL[IIR_NSEC] = { 1,3,1 };
const float IIR_A[IIR_NSEC][3] = {
{
1, 0, 0
},
{
1, -1.515684485, 0.6539504528
},
{
1, 0, 0
}
};

posted @ 2020-06-04 14:39  飞胡  阅读(738)  评论(0编辑  收藏  举报