Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: UIT_IIR_Filter UIT_ACM1602NI UITDSP_ADDA mbed UIT_AQM1602
main.cpp
00001 //-------------------------------------------------------------- 00002 // Cutoff frequency variable LPF by IIR 8th-order filter 00003 // A0: Signal to be filtered 00004 // A2: Value which controls cutoff frequency 00005 // 00006 // 2015/09/11, Copyright (c) 2015 MIKAMI, Naoki 00007 //-------------------------------------------------------------- 00008 00009 #include "ADC_Interrupt.hpp" // for ADC using interrupt 00010 #include "DAC_MCP4921.hpp" // for DAC MCP4921, MCP4922 00011 using namespace Mikami; 00012 00013 #include "BilinearDesignLH.hpp" // for design of IIR filter 00014 #include "IIR_Cascade.hpp" // for IIR filter of cascade structure 00015 00016 // ACM1602Ni を使う場合は次の define 文をコメントにすること 00017 #define AQM1602 00018 00019 #ifdef AQM1602 00020 #include "AQM1602.hpp" 00021 Aqm1602 Lcd_; 00022 #else 00023 #include "ACM1602NI.hpp" 00024 Acm1602Ni Lcd_; 00025 #endif 00026 00027 const int FS_ = 24000; // Sampling frequency: 24 kHz 00028 ADC_Intr myAdc_(A0, FS_, A2); 00029 DAC_MCP4921 myDac_; 00030 00031 const int ORDER_ = 8; 00032 IirCascade<ORDER_> iirLpf_; // IIR filter object 00033 00034 DigitalIn sw1_(D2, PullDown); // 0: disable filter 00035 // 1: enable filter 00036 00037 uint16_t a2_ = 0; // Inputted data from A2 pin 00038 00039 // Interrupt service routine for ADC 00040 void AdcIsr() 00041 { 00042 float xn = myAdc_.Read(); // Read from A0 00043 00044 myAdc_.Select2ndChannel(); // Select A2 00045 myAdc_.SoftStart(); // ADC start for A2 input 00046 00047 // Execute IIR filter 00048 float yn = iirLpf_.Execute(xn); 00049 00050 if (sw1_ == 0) myDac_.Write(xn); // Using no filter 00051 else myDac_.Write(yn); // Using filter 00052 00053 // Read value which controls cutoff frequency 00054 a2_ = myAdc_.ReadWait_u16(); 00055 00056 myAdc_.Select1stChannel(); // Select A0 00057 myAdc_.ClearPending_EnableIRQ();// Clear pending interrupt 00058 // and enable ADC_IRQn 00059 } 00060 00061 int main() 00062 { 00063 myDac_.ScfClockTim3(1000000); // cutoff frequency: 10 kHz 00064 00065 BilinearDesign lpfDsgn(ORDER_, FS_, BilinearDesign::LPF); 00066 00067 myAdc_.SetIntrVec(AdcIsr); // Assign ISR for ADC interrupt 00068 00069 float fc1 = 0; 00070 while (true) 00071 { 00072 // fc: cutoff frequency, 100 -- 2000 Hz 00073 float fc = 1900.0f*(a2_/4095.6f) + 100.0f; 00074 00075 if (sw1_ == 0) 00076 { 00077 printf("Through\r\n"); 00078 Lcd_.WriteStringXY("Through ", 0, 0); 00079 wait(0.2f); 00080 Lcd_.ClearLine(0); 00081 fc1 = 0; 00082 } 00083 else 00084 { 00085 if (fabs(fc - fc1) > 10.0f) 00086 { 00087 printf("fc = %4d\r\n", int(fc+0.5f)); 00088 char str[18]; 00089 sprintf(str, "fc = %4d Hz", int(fc+0.5f)); 00090 Lcd_.WriteStringXY(str, 0, 0); 00091 fc1 = fc; 00092 00093 // Design new coefficients based on new fc 00094 BilinearDesign::Coefs coefsLpf[ORDER_/2]; 00095 float g0; 00096 lpfDsgn.Execute(fc, coefsLpf, g0); 00097 00098 // Update new coefficients 00099 Biquad::Coefs coefsIir[ORDER_/2]; 00100 for (int n=0; n<ORDER_/2; n++) 00101 { 00102 coefsIir[n].a1 = coefsLpf[n].a1; 00103 coefsIir[n].a2 = coefsLpf[n].a2; 00104 coefsIir[n].b1 = coefsLpf[n].b1; 00105 coefsIir[n].b2 = 1.0f; 00106 } 00107 iirLpf_.SetCoefs(g0, coefsIir); 00108 } 00109 } 00110 wait(0.1f); 00111 } 00112 }
Generated on Thu Jul 14 2022 01:14:10 by
1.7.2