![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
FIR filter using DSP instructions for ST Nucleo F401RE.
Dependencies: UITDSP_ADDA mbed
main.cpp@8:d226de4c3951, 2015-11-16 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Mon Nov 16 02:24:22 2015 +0000
- Revision:
- 8:d226de4c3951
- Parent:
- 6:f005cc78b9aa
9
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:797a9c10261e | 1 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:797a9c10261e | 2 | // FIR フィルタ,基本的な構造, 固定小数点演算用 DSP 命令を使う |
MikamiUitOpen | 0:797a9c10261e | 3 | // Analog Input : A0 |
MikamiUitOpen | 0:797a9c10261e | 4 | // Analog Output: MCP4922 using SPI |
MikamiUitOpen | 8:d226de4c3951 | 5 | // 2015/11/16, Copyright (c) 2015 MIKAMI, Naoki |
MikamiUitOpen | 0:797a9c10261e | 6 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:797a9c10261e | 7 | |
MikamiUitOpen | 8:d226de4c3951 | 8 | #include "ADC_BuiltIn.hpp" // for ADC not using interrupt |
MikamiUitOpen | 8:d226de4c3951 | 9 | #include "DAC_MCP4921.hpp" // for DAC MCP4921, MCP4922 |
MikamiUitOpen | 0:797a9c10261e | 10 | #include "Coefficients_200_LPF_Fixed.hpp" |
MikamiUitOpen | 0:797a9c10261e | 11 | |
MikamiUitOpen | 0:797a9c10261e | 12 | using namespace Mikami; |
MikamiUitOpen | 0:797a9c10261e | 13 | |
MikamiUitOpen | 8:d226de4c3951 | 14 | const int FS_ = 12000; // Sampling frequency: 12 kHz |
MikamiUitOpen | 4:0b3b70b0af0b | 15 | const uint16_t OFFSET = 2047; // Correspond to "0" |
MikamiUitOpen | 8:d226de4c3951 | 16 | ADC_BuiltIn adc_(A0, FS_); // for AD |
MikamiUitOpen | 8:d226de4c3951 | 17 | DAC_MCP4921 myDac_; // for DA |
MikamiUitOpen | 0:797a9c10261e | 18 | |
MikamiUitOpen | 0:797a9c10261e | 19 | int main() |
MikamiUitOpen | 0:797a9c10261e | 20 | { |
MikamiUitOpen | 6:f005cc78b9aa | 21 | myDac_.ScfClockTim3(500000); // cutoff frequency: 5 kHz |
MikamiUitOpen | 0:797a9c10261e | 22 | |
MikamiUitOpen | 0:797a9c10261e | 23 | int16_t xn[ORDER_+1]; |
MikamiUitOpen | 0:797a9c10261e | 24 | for (int n=0; n<=ORDER_; n++) |
MikamiUitOpen | 0:797a9c10261e | 25 | xn[n] = 0; |
MikamiUitOpen | 0:797a9c10261e | 26 | |
MikamiUitOpen | 0:797a9c10261e | 27 | while (true) |
MikamiUitOpen | 0:797a9c10261e | 28 | { |
MikamiUitOpen | 4:0b3b70b0af0b | 29 | xn[0] = adc_.Read_u16() - OFFSET; // Read from A0 |
MikamiUitOpen | 0:797a9c10261e | 30 | //----------------------------------------------- |
MikamiUitOpen | 0:797a9c10261e | 31 | |
MikamiUitOpen | 0:797a9c10261e | 32 | int32_t yn = hm_[ORDER_]*xn[ORDER_];; |
MikamiUitOpen | 0:797a9c10261e | 33 | for (int k=0; k<ORDER_; k+=2) |
MikamiUitOpen | 0:797a9c10261e | 34 | { |
MikamiUitOpen | 0:797a9c10261e | 35 | uint32_t x = __PKHBT(xn[k], xn[k+1], 16); |
MikamiUitOpen | 0:797a9c10261e | 36 | uint32_t h = __PKHBT(hm_[k], hm_[k+1], 16); |
MikamiUitOpen | 0:797a9c10261e | 37 | yn = __SMLAD(x, h, yn); // Dual multiply and accumulate |
MikamiUitOpen | 0:797a9c10261e | 38 | } |
MikamiUitOpen | 0:797a9c10261e | 39 | for (int k=ORDER_; k>0; k--) |
MikamiUitOpen | 0:797a9c10261e | 40 | xn[k] = xn[k-1]; // move input signals |
MikamiUitOpen | 0:797a9c10261e | 41 | |
MikamiUitOpen | 4:0b3b70b0af0b | 42 | // rounding and devide by 32768 + DC offset |
MikamiUitOpen | 8:d226de4c3951 | 43 | uint16_t yOut = ((yn + 0x4000) >> 15) + OFFSET; |
MikamiUitOpen | 0:797a9c10261e | 44 | //----------------------------------------------- |
MikamiUitOpen | 4:0b3b70b0af0b | 45 | myDac_.Write(yOut); // Write to DAC |
MikamiUitOpen | 0:797a9c10261e | 46 | } |
MikamiUitOpen | 0:797a9c10261e | 47 | } |