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: Si5351A NeoPixel
main.cpp
00001 #include "mbed.h" 00002 #include "neopixel.h" 00003 #include "si5351a.h" 00004 00005 I2C i2c(D0, D1); //通信Si5351A 00006 SI5351A clk(i2c, 25000000UL); //基础时钟25MHz 00007 Pixel Column[20]; //单个数组 00008 Pixel Column_All[280]; //全局数组 00009 NeoPixelOut neoPixel(D9); //数据输出 00010 Timeout PeakDecrease; 00011 00012 DigitalOut reset=D4; //重置MSGEQ7 00013 DigitalOut strobe=D3; //选择MSGEQ7输出 00014 AnalogIn analog1_7=A0; //数据读取1-7 00015 AnalogIn analog8_14=A1; //数据读取8-14 00016 //AnalogIn mod=A2; //选择模式输入 00017 AnalogIn sensitive=A5; 00018 AnalogIn brightness=A6; 00019 00020 00021 int NumColumn=14; //灯柱数量 00022 int Peak[14]={0}; 00023 int PeakDecreaseTime_us=50*1e3; //peak下降延迟时间 00024 int colorPeak; //peak颜色 00025 int __mode; 00026 float freqData[14]; 00027 float noise=0.1; 00028 00029 float mode=0.1; 00030 int num=0; 00031 00032 00033 //-------------------------------设置Peak降落的互钳函数 00034 void toggleOff(void); 00035 void toggleOn(void){ 00036 for(int i=0;i<NumColumn;i++){ 00037 if(Peak[i]>-2) Peak[i]--; 00038 } 00039 num++; 00040 if(num==200){ 00041 if(mode<=0.5) mode=mode+0.1; 00042 else mode=0.1; 00043 num=0; 00044 } 00045 PeakDecrease.attach_us(toggleOff,PeakDecreaseTime_us); 00046 } 00047 void toggleOff(void){ 00048 for(int i=0;i<NumColumn;i++){ 00049 if(Peak[i]>-2) Peak[i]--; 00050 } 00051 PeakDecrease.attach_us(toggleOn,PeakDecreaseTime_us); 00052 } 00053 //-----------------------------------------取最大值函数 00054 int max(int i,int j){ 00055 if(i>j)return i; 00056 else return j; 00057 } 00058 //---------------------------------------------读取MSGEQ7的返回值 00059 void Read() { 00060 reset = 1; 00061 //wait_ns(150); //最小值100ns 00062 reset = 0; 00063 wait_us(200); 00064 for (int i = 0; i < 7; i++){ 00065 strobe = 0; 00066 wait_us(50); //待输出稳定 00067 freqData[i*2]=analog1_7*(0.5+2*sensitive); 00068 freqData[i*2+1]=analog8_14*(0.5+2*sensitive); 00069 strobe = 1; 00070 wait_us(40); //strobe最小间隔72us,此处取90us 00071 } 00072 } 00073 //-------------------------------------------初始颜色 00074 int colorBegin(){ 00075 int __mode=(int)(mode*10); //__mode范围为1,2,3,4,5,6,7,8,9 00076 switch(__mode){ 00077 case 1: 00078 colorPeak=0xFF0000; 00079 return 0x00FF00;// 00080 case 2: 00081 colorPeak=0xFF0000; 00082 return 0x0000FF;// 00083 case 3: 00084 colorPeak=0xFFFF00; 00085 return 0xFF33CC;// 00086 case 4: 00087 colorPeak=0x990099; 00088 return 0xFF3366;// 00089 case 5: 00090 colorPeak=0x0000FF; 00091 return 0x000000;// 00092 case 6: 00093 colorPeak=0xFF0000; 00094 return 0x00FF00; 00095 case 7: 00096 colorPeak=0xFF0000; 00097 return 0x00FF00; 00098 case 8: 00099 colorPeak=0xFF0000; 00100 return 0x00FF00; 00101 case 9: 00102 colorPeak=0xFF0000; 00103 return 0x00FF00; 00104 default: 00105 colorPeak=0xFF0000; 00106 return 0x00FF00; 00107 00108 } 00109 } 00110 //------------------------------------------设置模式,每个数字对应一种颜色变换方式 00111 int colorTransformation(int __color,int i){ 00112 switch(__mode){ 00113 case 1: 00114 return __color+0x060009-0x000B00;//绿色 00115 case 2: 00116 return __color+0x0B0000-0x00000C;//蓝色 00117 case 3: 00118 switch(i){ 00119 case 0:return 0xFF33CC; case 1:return 0xFF33FF; case 2:return 0xCC33FF; case 3:return 0x9933FF; case 4:return 0x6633FF; 00120 case 5:return 0x3333FF; case 6:return 0x3366FF; case 7:return 0x3399FF; case 8:return 0x33CCFF; case 9:return 0x33FFFF; 00121 case 10:return 0x33FFCC; case 11:return 0x33FF99; case 12:return 0x33FF66; case 13:return 0x33FF33; case 14:return 0x66FF33; 00122 case 15:return 0x99FF33; case 16:return 0xCCFF33; case 17:return 0xFFFF33; case 18:return 0xFFCC33; case 19:return 0xFF9933; 00123 default: return 0x000000; 00124 }//粉黄 00125 case 4: 00126 switch(i){ 00127 case 0:return 0xFF3366; case 1:return 0xFF33CC; case 2:return 0xCC33FF; case 3:return 0x6633FF; case 4:return 0x3366FF; 00128 case 5:return 0x33CCFF; case 6:return 0x33FFCC; case 7:return 0x33FF66; case 8:return 0x66FF33; case 9:return 0xCCFF33; 00129 case 10:return 0xFFCC33; case 11:return 0xFF6633; case 12:return 0xFF33CC; case 13:return 0xFF33FF; case 14:return 0x9933FF; 00130 case 15:return 0x3333FF; case 16:return 0x3399FF; case 17:return 0x33FFFF; case 18:return 0x33FF99; case 19:return 0x33FF33; 00131 default: return 0x000000; 00132 }// 00133 case 5: 00134 return 0X000000;//红色无 00135 case 6: 00136 return __color; 00137 break; 00138 case 7: 00139 return __color; 00140 break; 00141 case 8: 00142 return __color; 00143 break; 00144 case 9: 00145 return __color; 00146 default: 00147 return __color+0x06000C; 00148 00149 } 00150 } 00151 //------------------------------------------确定单个column的颜色 00152 void run(int j){ 00153 __mode=mode*10; 00154 int __color=colorBegin(); 00155 int H=((freqData[j]-noise)*19); 00156 for(int i=0; i<20; i++) { 00157 if(i>H) Column[i].hex=0; 00158 else Column[i].hex=__color; 00159 __color=colorTransformation(__color,i); 00160 } 00161 Column[max(Peak[j],H)].hex=colorPeak; 00162 if(Peak[j]<H) Peak[j]=H;//当Peak小于H时,上拉Peak到最顶端 00163 } 00164 //-------------------------------------------main 00165 int main() 00166 { 00167 strobe=1; 00168 reset=0; //initialize 00169 clk.set_frequency(SI5351_CLK1, 104570); 00170 clk.set_frequency(SI5351_CLK0, 190000); 00171 wait_us(200*1000); // 等待HSE稳定 00172 neoPixel.normalize = false; // 使r+g+b=255 均匀每个led亮度 00173 //Peakdecrease.detach(); 00174 toggleOn();//打开PeakDecrease 00175 while(1) { 00176 Read(); 00177 neoPixel.global_scale = brightness; //亮度调节 00178 //for(int x=0;x<14;x++) printf("%4.d ",(int)(freqData[x]*100));//test 00179 //printf("\n");//test 00180 for(int j=0;j<NumColumn;j++){ 00181 run(j); 00182 for(int k=20*j;k<20*(j+1);k++){ 00183 Column_All[k].hex=Column[k%20].hex; 00184 } 00185 } 00186 neoPixel.send(Column_All, NumColumn*20); 00187 } 00188 }
Generated on Thu Jul 21 2022 04:23:47 by
1.7.2