stc12c5a60s2单片机的IO口自带AD转换接口,而stc89c51需要使用AD转换模块,程序太长,懒得复制,都在压缩包里面。注意水银开关的接法,如果不需要第二种功能的可以指教短接。新手上路,请大家指教!!!
单片机源程序如下:
- #include <stc12c5a60s2.h>//"stc12c5620ad.h"
- #include <intrins.h>
- #define LongToBin(n) (((n>>21)&0x80)|((n>>18)&0x40)|((n>>15)&0x20)|((n>>12)&0x10)|((n>>9)&0x08)|((n>>6)&0x04)|((n>>3)&0x02)|((n)&0x01))
- #define BIN(n) LongToBin(0x##n##)
- #define uchar unsigned char
- #define uint unsigned int
- #define SAMPLE_NUM 64
- #define NUM_2_LOG 6
- #define FFT_OUT_MIN 1
- #define FFT_OUT_MAX 11
- uchar code BRTable[SAMPLE_NUM] ={
- 0, 32, 16, 48, 8, 40, 24, 56,4, 36, 20, 52, 12, 44, 28, 60, 2, 34, 18, 50, 10, 42, 26,
- 58, 6, 38, 22, 54, 14, 46, 30, 62, 1, 33, 17, 49, 9, 41, 25, 57,5, 37, 21, 53, 13, 45,
- 29, 61,3, 35, 19, 51, 11, 43, 27, 59,7, 39, 23, 55, 15, 47, 31, 63};
- char code sin_tabb[SAMPLE_NUM] = {
- 0 ,12 ,25 ,37 ,49 ,60 ,71 ,81 ,90 ,98 ,106 ,112 ,117 ,122 ,125 ,126 ,127 ,126 ,125 ,
- 122 ,117 ,112 ,106 ,98 ,90 ,81 ,71 ,60 ,49 ,37 ,25 ,12 ,0 ,-12 ,-25 ,-37 ,-49 ,-60 ,
- -71 ,-81 ,-90 ,-98 ,-106 ,-112 ,-117 ,-122 ,-125 ,-126 ,-127 ,-126 ,-125 ,-122 ,-117
- ,-112 ,-106 ,-98 ,-90 ,-81 ,-71 ,-60 ,-49 ,-37 ,-25 ,-12 };
-
- char code cos_tabb[SAMPLE_NUM] = {
- 127 ,126 ,125 ,122 ,117 ,112 ,106 ,98 ,90 ,81 ,71 ,60 ,49 ,37 ,25 ,12 ,0 ,-12 ,-25 ,
- -37 ,-49 ,-60 ,-71 ,-81 ,-90 ,-98 ,-106 ,-112 ,-117 ,-122 ,-125 ,-126 ,-127 ,-126 ,-125 ,
- -122 ,-117 ,-112 ,-106 ,-98 ,-90 ,-81 ,-71 ,-60 ,-49 ,-37 ,-25 ,-12 ,0 ,12 ,25 ,37 ,49
- ,60 ,71 ,81 ,90 ,98 ,106 ,112 ,117 ,122 ,125 ,126 };
- uchar a[64];
- uchar keep,keepnum,anum,timernum,timernum2,lednum3,Ltime;//用于分离
- /*加入数组用于显示相应led灯数目*/
- uchar lednum[]={0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};//0-7的显示数组 P2组控制
- int xdata FftReal[SAMPLE_NUM];
- int xdata FftImage[SAMPLE_NUM];
- xdata unsigned char num[128];
- xdata unsigned char temp[128];
- sbit p37=P3^7;
- sbit p36=P3^6;
- sbit p35=P3^5;
- sbit p34=P3^4;
- sbit p07=P0^7;
- sbit p06=P0^6;
- sbit p05=P0^5;
- sbit p04=P0^4;
- sbit p03=P0^3;
- sbit p02=P0^2;
- sbit p01=P0^1;
- sbit p00=P0^0;
- sbit p27=P2^7;
- sbit p26=P2^6;
- sbit p25=P2^5;
- sbit p24=P2^4;
- sbit p23=P2^3;
- sbit p22=P2^2;
- sbit p21=P2^1;
- sbit p20=P2^0;
- sbit key=P3^3;
- sbit zh=P3^0;
- void delay(unsigned char a)
- {
- unsigned int i;
- while(--a)
- for(i=0;i<50;i++);
- }
- void timerinit()//定时器 初始化函数
- {
- TMOD=0x11;
- TH0=(65536-2048)/256;
- TL0=(65536-2048)%256;
- TH1=(65536-50000)/256;
- TL1=(65536-50000)%256;
- EA=1;
- ET0=1;
- TR0=1;
- ET1=0;
- TR1=0;
- }
- void FFT_process() //下落迟滞
- {
- unsigned char i;
- for(i=0;i<14;i++)
- {
- if(a[i] < temp[i])
- {
- num[i]++;
- if(num[i] == 1)
- {
- if(temp[0]==0&&temp[1]==0&&temp[2]==0&&temp[3]==0&&temp[4]==0&&temp[5]==0&&temp[6]==0&&temp[7]==0&&temp[8]==0&&temp[9]==0&&temp[10]==0&&temp[11]==0&&temp[12]==0&&temp[13]==0);
- else
- {
- a[i] = --temp[i];
- num[i] = 0;
- }
- }
- }
- else num[i] = 0;
- }
- }
- void disp()
- {
- timernum++;
- if(timernum==15) timernum=1;
- P2=0x00;//显示前先关闭
- p37=1;
- p36=1;
- p35=1;
- p34=1;
- P0=0Xff;
- FFT_process();
- switch(timernum)
- {
- case 1:anum=a[0];break;//
- case 2:anum=a[1];break;//
- case 3:anum=a[2];p37=0;break;
- case 4:anum=a[3];p36=0;break;
- case 5:anum=a[4];p35=0;break;
- case 6:anum=a[5];p34=0;break;
- case 7:anum=a[6];p07=0;break;
- case 8:anum=a[7];p06=0;break;
- case 9:anum=a[8];p05=0;break;
- case 10:anum=a[9];p04=0;break;
- case 11:anum=a[10];p03=0;break;
- case 12:anum=a[11];p02=0;break;//
- case 13:anum=a[12];p01=0;break;//
- case 14:anum=a[13];p00=0;break;//
- }
- P2=lednum[anum];
- delay(2);
- }
- uchar STC_ADC() //!!根据数据手册写一个ad读取函数 buyonggai
- {
- uchar i;
- ADC_RES = 0;
- ADC_RESL = 0;
- ADC_CONTR = BIN(10001000);
- i=3;
- while(i--);
- while (1)
- {
- if (ADC_CONTR & BIN(10000))
- {
- break;
- }
- }
- ADC_CONTR = BIN(10000000);
- return( ADC_RESL<<2) ;
- }
- short sqrt_16( unsigned long M)
- {
- unsigned int N, i;
- unsigned long tmp, ttp;
- if( M == 0 )
- return 0;
-
- N = 0;
-
- tmp = ( M >> 30 );
- M <<= 2;
- if( tmp > 1 )
- {
- N ++;
- tmp -= N;
- }
-
- for( i=15; i>0; i-- )
- {
- N <<= 1;
-
- tmp <<= 2;
- tmp += (M >> 30);
-
- ttp = N;
- ttp = (ttp<<1)+1;
-
- M <<= 2;
- if( tmp >= ttp )
- {
- tmp -= ttp;
- N ++;
- }
- }
-
- return N;
- }
- void FFT()
- {
- register uchar i,bb,j,k,p,max;
- register short TR,TI,temp;
- unsigned long ulReal;
- unsigned long ulImage;
-
- for(i=0; i<SAMPLE_NUM;i++) //此处可以加入自动增益
- {
- FftReal[BRTable[i]] = STC_ADC();//使显示保持在一定范围内
- FftImage[i] = 0;
- }
-
- for( i=1; i<=NUM_2_LOG; i++)
- {
- bb=1;
- bb <<= (i-1);
- for( j=0; j<=bb-1; j++)
- {
- p=1;
- p <<= (NUM_2_LOG-i);
- p = p*j;
- for( k=j; k<SAMPLE_NUM; k=k+2*bb)
- {
- TR = FftReal[k]; TI = FftImage[k]; temp = FftReal[k+bb];
- FftReal[k] = FftReal[k] + ((FftReal[k+bb]*cos_tabb[p])>>7) + ((FftImage[k+bb]*sin_tabb[p])>>7);
- FftImage[k] = FftImage[k] - ((FftReal[k+bb]*sin_tabb[p])>>7) + ((FftImage[k+bb]*cos_tabb[p])>>7);
- FftReal[k+bb] = TR - ((FftReal[k+bb]*cos_tabb[p])>>7) - ((FftImage[k+bb]*sin_tabb[p])>>7);
- FftImage[k+bb] = TI + ((temp*sin_tabb[p])>>7) - ((FftImage[k+bb]*cos_tabb[p])>>7);
-
- FftReal[k] >>= 1;
- FftImage[k] >>= 1;
- FftReal[k+bb] >>= 1;
- FftImage[k+bb] >>= 1;
- }
- }
- }
- max=0;
- for( i=0; i<14; i++)
- {
- ulReal = FftReal[i+1];
- ulReal *= ulReal;
- ulImage = FftImage[i+1];
- ulImage *= ulImage;
-
- a[i] = sqrt_16( ulReal + ulImage ); //修改
-
- if( a[i] < FFT_OUT_MIN ) //
- a[i] = 0;//修改
- else
- a[i] = a[i]-FFT_OUT_MIN;
-
- if( a[i] >max)
- max =a[i];
- }
- if(max>8) //11
- {
- max/=8;
- for( i=0; i<14; i++) //输出a的5个分离数值
- {
- a[i]/=max;
- }
- }
- }
复制代码 /////--------------------------------剩余代码在压缩包内--------------------------------------------------
全部代码51hei下载地址:
音乐频谱.rar
(50.64 KB, 下载次数: 114)
|