![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
IIR filter of 1st order for ST Nucleo F401RE.
main.cpp@3:b17e5d790a55, 2014-11-15 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Sat Nov 15 06:17:50 2014 +0000
- Revision:
- 3:b17e5d790a55
- Parent:
- 2:ed56284ebeb7
4
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 | 2:ed56284ebeb7 | 5 | // 2014/11/12, 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 | |
MikamiUitOpen | 0:22eafb1b3124 | 13 | using namespace Mikami; |
MikamiUitOpen | 0:22eafb1b3124 | 14 | |
MikamiUitOpen | 0:22eafb1b3124 | 15 | const int FS_ = 12000; // Sampling frequency: 12 kHz |
MikamiUitOpen | 0:22eafb1b3124 | 16 | ADC_Base adc_(A0, FS_); // for AD |
MikamiUitOpen | 0:22eafb1b3124 | 17 | DAC_MCP4922 myDac_; // for DA |
MikamiUitOpen | 0:22eafb1b3124 | 18 | |
MikamiUitOpen | 0:22eafb1b3124 | 19 | int main() |
MikamiUitOpen | 0:22eafb1b3124 | 20 | { |
MikamiUitOpen | 2:ed56284ebeb7 | 21 | myDac_.ScfClockTim3(500000); // cutoff frequency: 5 kHz |
MikamiUitOpen | 0:22eafb1b3124 | 22 | |
MikamiUitOpen | 0:22eafb1b3124 | 23 | const float A1 = 0.8f; |
MikamiUitOpen | 0:22eafb1b3124 | 24 | const float B0 = 1.0f - A1; |
MikamiUitOpen | 0:22eafb1b3124 | 25 | float yn = 0; |
MikamiUitOpen | 0:22eafb1b3124 | 26 | |
MikamiUitOpen | 0:22eafb1b3124 | 27 | while (true) |
MikamiUitOpen | 0:22eafb1b3124 | 28 | { |
MikamiUitOpen | 0:22eafb1b3124 | 29 | float xn = adc_.Read(); // Read from A0 |
MikamiUitOpen | 0:22eafb1b3124 | 30 | //----------------------------------------------- |
MikamiUitOpen | 0:22eafb1b3124 | 31 | |
MikamiUitOpen | 0:22eafb1b3124 | 32 | yn = A1*yn + B0*xn; |
MikamiUitOpen | 0:22eafb1b3124 | 33 | |
MikamiUitOpen | 0:22eafb1b3124 | 34 | //----------------------------------------------- |
MikamiUitOpen | 0:22eafb1b3124 | 35 | myDac_.Write(yn); // Write to DAC |
MikamiUitOpen | 0:22eafb1b3124 | 36 | } |
MikamiUitOpen | 0:22eafb1b3124 | 37 | } |