plecs实现数字滤波
1、PLECS仿真
2、declaration
#include <stdio.h>
#define VOLT_SAMPLE Input(0)
#define SYS_TIME1S_BS12US5 (80000)
#define TIME1MS_BS12US5 (80000 / 1000)
#define SYS_TIMER_PRD SYS_TIME1S_BS12US5
int g_sysTimerCnt;
int g_isrTaskIndex;
double g_voltSample;
double g_voltFilt1;
double g_voltFilt2;
int g_isrCnt;
inline void SysTimerRun(void)
{
if (++g_sysTimerCnt >= SYS_TIMER_PRD) {
g_sysTimerCnt = 0;
}
}
typedef void (*Fun)(void);
void IsrTask1(void)
{
g_isrCnt++;
}
void IsrTask2(void) {}
void IsrTask3(void) {}
void IsrTask4(void) {}
void IsrTask5(void) {}
void IsrTask6(void) {}
void IsrTask7(void) {}
void IsrTask8(void) {}
const Fun g_isrFun[8] = {
IsrTask1,
IsrTask2,
IsrTask3,
IsrTask4,
IsrTask5,
IsrTask6,
IsrTask7,
IsrTask8,
};
void Sample(void)
{
g_voltFilt1 += (g_voltSample - g_voltFilt1) / 1024;
}
inline void IsrTaskRun(void)
{
Sample();
g_isrFun[g_isrTaskIndex]();
if (++g_isrTaskIndex >= 8) {
g_isrTaskIndex = 0;
}
}
void TaskRun(void)
{
static int sysTimerCntFor1msPre = 0;
int delta = g_sysTimerCnt - sysTimerCntFor1msPre;
delta = delta < 0 ? delta + SYS_TIMER_PRD : delta;
if (delta >= TIME1MS_BS12US5) {
g_voltFilt2 += (g_voltSample - g_voltFilt2) / 64;
sysTimerCntFor1msPre = g_sysTimerCnt;
}
}
3、start_function
g_sysTimerCnt = 0;
g_isrTaskIndex = 0;
g_isrCnt = 0;
4、output_function
Output(0) = g_voltFilt1;
Output(1) = g_voltFilt2;
5、update_funcion
g_voltSample = VOLT_SAMPLE;
g_voltFilt1 = DiscState(0);
g_voltFilt2 = DiscState(1);
SysTimerRun();
IsrTaskRun();
TaskRun();
DiscState(0) = g_voltFilt1;
DiscState(1) = g_voltFilt2;