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_ACM1602NI UIT_ADDA mbed
main.cpp
00001 //------------------------------------------------------------------ 00002 // Cutoff frequency variable LPF and HPF by FIR 160th-order filter 00003 // A0: Signal to be filtered 00004 // A2: Value which controls cutoff frequency 00005 // 00006 // 2014/12/08, Copyright (c) 2014 MIKAMI, Naoki 00007 //------------------------------------------------------------------ 00008 00009 #include "mbed.h" 00010 00011 #include "ADC_Interrupt.hpp" // for ADC using interrupt 00012 #include "DAC_MCP4922.hpp" // for DAC MCP4922 00013 #include "ACM1602NI.hpp" // for LCD display 00014 00015 #include "WindowingDesignLH.hpp" // for design of FIR filter 00016 00017 using namespace Mikami; 00018 00019 const int FS_ = 16000; // Sampling frequency: 16 kHz 00020 ADC_Intr myAdc_(A0, FS_, A1, A2); 00021 DAC_MCP4922 myDac_; 00022 00023 const int ORDER_ = 160; 00024 float hm_[ORDER_/2+1]; 00025 float xn_[ORDER_+1]; 00026 00027 DigitalIn sw1_(D2, PullDown); // 0: disable filter 00028 // 1: enable filter 00029 DigitalIn sw2_(D3, PullDown); // 0: LPF 00030 // 1: HPF 00031 WindowingDesign design_(ORDER_, FS_); 00032 DigitalOut dOut_(D7); 00033 00034 uint16_t a2_ = 0; // Inputted data from A2 pin 00035 00036 // Interrupt service routine for ADC 00037 void AdcIsr() 00038 { 00039 dOut_.write(1); 00040 xn_[0] = myAdc_.Read(); // Read from A0 00041 00042 myAdc_.Select3rdChannel(); // Select A2 00043 myAdc_.SoftStart(); // ADC start for A2 input 00044 00045 //----------------------------------------- 00046 // Execute FIR filter 00047 float yn = hm_[ORDER_/2]*xn_[ORDER_/2]; 00048 for (int k=0; k<ORDER_/2; k++) 00049 yn = yn + hm_[k]*(xn_[k] + xn_[ORDER_-k]); 00050 00051 for (int k=ORDER_; k>0; k--) 00052 xn_[k] = xn_[k-1]; // move input signals 00053 //----------------------------------------- 00054 00055 if (sw1_ == 0) myDac_.Write(xn_[0]); // Using no filter 00056 else myDac_.Write(yn); // Using filter 00057 00058 // Read value which controls cutoff frequency 00059 a2_ = myAdc_.ReadWait_u16(); 00060 00061 myAdc_.Select1stChannel(); // Select A0 00062 myAdc_.ClearPending_EnableIRQ();// Clear pending interrupt 00063 // and enable ADC_IRQn 00064 dOut_.write(0); 00065 } 00066 00067 int main() 00068 { 00069 myDac_.ScfClockTim3(670000); // cutoff frequency: 6.7 kHz 00070 00071 Acm1602Ni lcd; // objetc for display using LCD 00072 00073 // Clear buffer in FIR filter 00074 for (int n=0; n<=ORDER_; n++) 00075 xn_[n] = 0; 00076 myAdc_.SetIntrVec(AdcIsr); // Assign ISR for ADC interrupt 00077 00078 float fc1 = 0; 00079 WindowingDesign::Type pb = WindowingDesign::LPF; 00080 while (true) 00081 { 00082 // fc: cutoff frequency, 100 -- 2000 Hz 00083 float fc = 1900.0f*(a2_/4095.6f) + 100.0f; 00084 00085 if (sw1_ == 0) 00086 { 00087 printf("Through\r\n"); 00088 lcd.ClearLine(1); 00089 lcd.WriteStringXY("Through ", 0, 0); 00090 wait(0.2f); 00091 lcd.ClearLine(0); 00092 fc1 = 0; 00093 } 00094 else 00095 { 00096 if (fabs(fc - fc1) > 10.0f) 00097 { 00098 printf("fc = %4d\r\n", int(fc+0.5f)); 00099 char str[18]; 00100 sprintf(str, "fc = %4d Hz", int(fc+0.5f)); 00101 lcd.WriteStringXY(str, 0, 0); 00102 if (sw2_ == 0) 00103 { 00104 pb = WindowingDesign::LPF; 00105 printf("LPF\r\n"); 00106 lcd.WriteStringXY("LPF", 0, 1); 00107 } 00108 else 00109 { 00110 pb = WindowingDesign::HPF; 00111 printf("HPF\r\n"); 00112 lcd.WriteStringXY("HPF", 0, 1); 00113 } 00114 fc1 = fc; 00115 00116 // Design new coefficients based on new fc 00117 design_.Design(ORDER_, pb, fc1, hm_); 00118 } 00119 } 00120 wait(0.1f); 00121 } 00122 } 00123
Generated on Wed Jul 20 2022 18:57:52 by
1.7.2