![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
IIR filter of 1st order for ST Nucleo F401RE.
main.cpp@0:22eafb1b3124, 2014-10-29 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Wed Oct 29 09:46:52 2014 +0000
- Revision:
- 0:22eafb1b3124
- Child:
- 2:ed56284ebeb7
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:22eafb1b3124 | 1 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:22eafb1b3124 | 2 | // 1次の IIR フィルタ |
MikamiUitOpen | 0:22eafb1b3124 | 3 | // Analog Input : A0 |
MikamiUitOpen | 0:22eafb1b3124 | 4 | // Analog Output: MCP4922 using SPI |
MikamiUitOpen | 0:22eafb1b3124 | 5 | // 2014/10/29, Copyright (c) 2014 MIKAMI, Naoki |
MikamiUitOpen | 0:22eafb1b3124 | 6 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:22eafb1b3124 | 7 | |
MikamiUitOpen | 0:22eafb1b3124 | 8 | #include "mbed.h" |
MikamiUitOpen | 0:22eafb1b3124 | 9 | |
MikamiUitOpen | 0:22eafb1b3124 | 10 | #include "ADC_Base.hpp" // for ADC not using interrupt |
MikamiUitOpen | 0:22eafb1b3124 | 11 | #include "DAC_MCP4922.hpp" // for DAC MCP4922 |
MikamiUitOpen | 0:22eafb1b3124 | 12 | #include "ScfClockTim3.hpp" // for clock supplied to SCF |
MikamiUitOpen | 0:22eafb1b3124 | 13 | |
MikamiUitOpen | 0:22eafb1b3124 | 14 | using namespace Mikami; |
MikamiUitOpen | 0:22eafb1b3124 | 15 | |
MikamiUitOpen | 0:22eafb1b3124 | 16 | const int FS_ = 12000; // Sampling frequency: 12 kHz |
MikamiUitOpen | 0:22eafb1b3124 | 17 | ADC_Base adc_(A0, FS_); // for AD |
MikamiUitOpen | 0:22eafb1b3124 | 18 | DAC_MCP4922 myDac_; // for DA |
MikamiUitOpen | 0:22eafb1b3124 | 19 | |
MikamiUitOpen | 0:22eafb1b3124 | 20 | int main() |
MikamiUitOpen | 0:22eafb1b3124 | 21 | { |
MikamiUitOpen | 0:22eafb1b3124 | 22 | ScfClockTim3(500000); // cutoff frequency: 5 kHz |
MikamiUitOpen | 0:22eafb1b3124 | 23 | |
MikamiUitOpen | 0:22eafb1b3124 | 24 | const float A1 = 0.8f; |
MikamiUitOpen | 0:22eafb1b3124 | 25 | const float B0 = 1.0f - A1; |
MikamiUitOpen | 0:22eafb1b3124 | 26 | float yn = 0; |
MikamiUitOpen | 0:22eafb1b3124 | 27 | |
MikamiUitOpen | 0:22eafb1b3124 | 28 | while (true) |
MikamiUitOpen | 0:22eafb1b3124 | 29 | { |
MikamiUitOpen | 0:22eafb1b3124 | 30 | float xn = adc_.Read(); // Read from A0 |
MikamiUitOpen | 0:22eafb1b3124 | 31 | //----------------------------------------------- |
MikamiUitOpen | 0:22eafb1b3124 | 32 | |
MikamiUitOpen | 0:22eafb1b3124 | 33 | yn = A1*yn + B0*xn; |
MikamiUitOpen | 0:22eafb1b3124 | 34 | |
MikamiUitOpen | 0:22eafb1b3124 | 35 | //----------------------------------------------- |
MikamiUitOpen | 0:22eafb1b3124 | 36 | myDac_.Write(yn); // Write to DAC |
MikamiUitOpen | 0:22eafb1b3124 | 37 | } |
MikamiUitOpen | 0:22eafb1b3124 | 38 | } |