![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Moving average for ST Nucleo F401RE. The number of average is controled by sw.
main.cpp@0:2e17a28353a0, 2014-10-29 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Wed Oct 29 13:16:24 2014 +0000
- Revision:
- 0:2e17a28353a0
- Child:
- 1:2b1ce47a1fd7
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:2e17a28353a0 | 1 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:2e17a28353a0 | 2 | // 移動平均, 平均数:1, 2, 4, 8, 16, 32, 64:スイッチで選択 |
MikamiUitOpen | 0:2e17a28353a0 | 3 | // Analog Input : A0 |
MikamiUitOpen | 0:2e17a28353a0 | 4 | // Analog Output: MCP4922 using SPI |
MikamiUitOpen | 0:2e17a28353a0 | 5 | // 2014/10/29, Copyright (c) 2014 MIKAMI, Naoki |
MikamiUitOpen | 0:2e17a28353a0 | 6 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:2e17a28353a0 | 7 | |
MikamiUitOpen | 0:2e17a28353a0 | 8 | #include "mbed.h" |
MikamiUitOpen | 0:2e17a28353a0 | 9 | |
MikamiUitOpen | 0:2e17a28353a0 | 10 | #include "ADC_Base.hpp" // for ADC not using interrupt |
MikamiUitOpen | 0:2e17a28353a0 | 11 | #include "DAC_MCP4922.hpp" // for DAC MCP4922 |
MikamiUitOpen | 0:2e17a28353a0 | 12 | #include "ScfClockTim3.hpp" // for clock supplied to SCF |
MikamiUitOpen | 0:2e17a28353a0 | 13 | |
MikamiUitOpen | 0:2e17a28353a0 | 14 | using namespace Mikami; |
MikamiUitOpen | 0:2e17a28353a0 | 15 | |
MikamiUitOpen | 0:2e17a28353a0 | 16 | const int FS_ = 12000; // Sampling frequency: 12 kHz |
MikamiUitOpen | 0:2e17a28353a0 | 17 | ADC_Base adc_(A0, FS_); // for AD |
MikamiUitOpen | 0:2e17a28353a0 | 18 | DAC_MCP4922 myDac_; // for DA |
MikamiUitOpen | 0:2e17a28353a0 | 19 | |
MikamiUitOpen | 0:2e17a28353a0 | 20 | DigitalIn sw1_(D2, PullDown); |
MikamiUitOpen | 0:2e17a28353a0 | 21 | DigitalIn sw2_(D3, PullDown); |
MikamiUitOpen | 0:2e17a28353a0 | 22 | DigitalIn sw4_(D4, PullDown); |
MikamiUitOpen | 0:2e17a28353a0 | 23 | DigitalIn sw8_(D5, PullDown); |
MikamiUitOpen | 0:2e17a28353a0 | 24 | |
MikamiUitOpen | 0:2e17a28353a0 | 25 | int main() |
MikamiUitOpen | 0:2e17a28353a0 | 26 | { |
MikamiUitOpen | 0:2e17a28353a0 | 27 | ScfClockTim3(500000); // cutoff frequency: 5 kHz |
MikamiUitOpen | 0:2e17a28353a0 | 28 | |
MikamiUitOpen | 0:2e17a28353a0 | 29 | const int M = 64; |
MikamiUitOpen | 0:2e17a28353a0 | 30 | |
MikamiUitOpen | 0:2e17a28353a0 | 31 | float xn[M]; |
MikamiUitOpen | 0:2e17a28353a0 | 32 | for (int n=0; n<M; n++) xn[n] = 0; |
MikamiUitOpen | 0:2e17a28353a0 | 33 | |
MikamiUitOpen | 0:2e17a28353a0 | 34 | while (true) |
MikamiUitOpen | 0:2e17a28353a0 | 35 | { |
MikamiUitOpen | 0:2e17a28353a0 | 36 | int sw = (sw4_ << 3) | (sw4_ << 2) | (sw2_ << 1) | sw1_; |
MikamiUitOpen | 0:2e17a28353a0 | 37 | int nAv = 1 << sw; |
MikamiUitOpen | 0:2e17a28353a0 | 38 | if (nAv > 64) nAv = 64; |
MikamiUitOpen | 0:2e17a28353a0 | 39 | |
MikamiUitOpen | 0:2e17a28353a0 | 40 | xn[0] = adc_.Read(); // Read from A0 |
MikamiUitOpen | 0:2e17a28353a0 | 41 | //----------------------------------------------- |
MikamiUitOpen | 0:2e17a28353a0 | 42 | |
MikamiUitOpen | 0:2e17a28353a0 | 43 | float yn = 0; |
MikamiUitOpen | 0:2e17a28353a0 | 44 | for (int k=0; k<nAv; k++) yn = yn + xn[k]; |
MikamiUitOpen | 0:2e17a28353a0 | 45 | yn = yn/(float)nAv; // yn: average |
MikamiUitOpen | 0:2e17a28353a0 | 46 | |
MikamiUitOpen | 0:2e17a28353a0 | 47 | // Update input buffer |
MikamiUitOpen | 0:2e17a28353a0 | 48 | for (int k = nAv-1; k>0; k--) |
MikamiUitOpen | 0:2e17a28353a0 | 49 | xn[k] = xn[k-1]; |
MikamiUitOpen | 0:2e17a28353a0 | 50 | |
MikamiUitOpen | 0:2e17a28353a0 | 51 | //----------------------------------------------- |
MikamiUitOpen | 0:2e17a28353a0 | 52 | myDac_.Write(yn); // Write to DAC |
MikamiUitOpen | 0:2e17a28353a0 | 53 | } |
MikamiUitOpen | 0:2e17a28353a0 | 54 | } |