![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Sin generator using 2nd-ordered IIR filter for ST Nucleo F401RE.
main.cpp@0:8edfedfcdd26, 2014-10-21 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Tue Oct 21 12:52:45 2014 +0000
- Revision:
- 0:8edfedfcdd26
- Child:
- 2:799a843be414
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:8edfedfcdd26 | 1 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:8edfedfcdd26 | 2 | // Sin generator using IIR filter |
MikamiUitOpen | 0:8edfedfcdd26 | 3 | // Analog Output: MCP4922 using SPI |
MikamiUitOpen | 0:8edfedfcdd26 | 4 | // 2014/10/21, Copyright (c) 2014 MIKAMI, Naoki |
MikamiUitOpen | 0:8edfedfcdd26 | 5 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:8edfedfcdd26 | 6 | |
MikamiUitOpen | 0:8edfedfcdd26 | 7 | #include "mbed.h" |
MikamiUitOpen | 0:8edfedfcdd26 | 8 | |
MikamiUitOpen | 0:8edfedfcdd26 | 9 | #include "ADC_Base.hpp" // for ADC not using interrupt |
MikamiUitOpen | 0:8edfedfcdd26 | 10 | #include "DAC_MCP4922.hpp" // for DAC MCP4922 |
MikamiUitOpen | 0:8edfedfcdd26 | 11 | #include "ScfClockTim3.hpp" // for clock supplied to SCF |
MikamiUitOpen | 0:8edfedfcdd26 | 12 | |
MikamiUitOpen | 0:8edfedfcdd26 | 13 | using namespace Mikami; |
MikamiUitOpen | 0:8edfedfcdd26 | 14 | |
MikamiUitOpen | 0:8edfedfcdd26 | 15 | const int FS_ = 12000; // Sampling frequency: 12 kHz |
MikamiUitOpen | 0:8edfedfcdd26 | 16 | ADC_Base adc_(A0, FS_); // for AD |
MikamiUitOpen | 0:8edfedfcdd26 | 17 | DAC_MCP4922 myDac_(DAC_MCP4922::DAC_A); // for DA |
MikamiUitOpen | 0:8edfedfcdd26 | 18 | |
MikamiUitOpen | 0:8edfedfcdd26 | 19 | int main() |
MikamiUitOpen | 0:8edfedfcdd26 | 20 | { |
MikamiUitOpen | 0:8edfedfcdd26 | 21 | const float PI2 = 6.283185f; |
MikamiUitOpen | 0:8edfedfcdd26 | 22 | const float F0 = 440.0f; |
MikamiUitOpen | 0:8edfedfcdd26 | 23 | const float A1 = 2.0f*cosf(PI2*F0/(float)FS_); |
MikamiUitOpen | 0:8edfedfcdd26 | 24 | const float B1 = 0.8f*sinf(PI2*F0/(float)FS_); |
MikamiUitOpen | 0:8edfedfcdd26 | 25 | |
MikamiUitOpen | 0:8edfedfcdd26 | 26 | ScfClockTim3(500000); // cutoff frequency: 5 kHz |
MikamiUitOpen | 0:8edfedfcdd26 | 27 | |
MikamiUitOpen | 0:8edfedfcdd26 | 28 | float yn1 = B1; |
MikamiUitOpen | 0:8edfedfcdd26 | 29 | float yn2 = 0; |
MikamiUitOpen | 0:8edfedfcdd26 | 30 | |
MikamiUitOpen | 0:8edfedfcdd26 | 31 | while (true) |
MikamiUitOpen | 0:8edfedfcdd26 | 32 | { |
MikamiUitOpen | 0:8edfedfcdd26 | 33 | float xn = adc_.Read(); // Dummy read for synchronization to Fs |
MikamiUitOpen | 0:8edfedfcdd26 | 34 | //----------------------------------------------- |
MikamiUitOpen | 0:8edfedfcdd26 | 35 | |
MikamiUitOpen | 0:8edfedfcdd26 | 36 | float yn = A1*yn1 - yn2; // y[n] = a1*y[n-1] - y[n-2] |
MikamiUitOpen | 0:8edfedfcdd26 | 37 | yn2 = yn1; // y[n-2] <= y[n-1] |
MikamiUitOpen | 0:8edfedfcdd26 | 38 | yn1 = yn; // y[n-1] <= y[n] |
MikamiUitOpen | 0:8edfedfcdd26 | 39 | |
MikamiUitOpen | 0:8edfedfcdd26 | 40 | //----------------------------------------------- |
MikamiUitOpen | 0:8edfedfcdd26 | 41 | myDac_.Write(yn); // Write to DAC |
MikamiUitOpen | 0:8edfedfcdd26 | 42 | } |
MikamiUitOpen | 0:8edfedfcdd26 | 43 | } |