- #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, ,lednum3,Ltime;//óÃóú·Öàë
- /*¼óèëêy×éóÃóúÏÔê¾Ïàó|ledμÆêyÄ¿*/
- uchar lednum[]={0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};//0-7μÄÏÔê¾êy×é P2×é¿ØÖÆ
- int xdata FftReal[SAMPLE_NUM];
- int xdata FftImage[SAMPLE_NUM];
- xdata unsigned char num[128];
- xdata unsigned char temp[128];
- sbit p35=P3^5;//9-11μÄled¿ØÖÆ
- sbit p36=P3^6;
- sbit p37=P3^7;
- sbit p34=P3^4;
- sbit p33=P3^3;
- sbit p32=P3^2;
- sbit p31=P3^1;
- sbit p30=P3^0;
- 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;
- void delay(unsigned char a)
- {
- unsigned int i;
- while(--a)
- for(i=0;i<50;i++);
- }
- void timerinit()//¶¨ê±Æ÷ 3õê¼»ˉoˉêy
- {
- TMOD=0x01; //16붨ê±Æ÷
- TH0=(65536-2048)/256;
- TL0=(65536-2048)%256;
- EA=1; //¿a×üÖD¶Ï
- ET0=1; //¿a¶¨ê±Æ÷0ÖD¶Ï
- TR0=1; //Æô¶ˉ¶¨ê±Æ÷
- }
- void disp()
- {
- timernum++;
- if(timernum==14) timernum=1;
- P2=0x00;//ÏÔê¾Ç°Ïè1رÕ
- p37=0;
- p36=0;
- p35=0;
- p34=1;
- p32=1;
- p31=1;
- p30=1;
- P0=0Xff;
- switch(timernum)
- {
- case 1:anum=a[0];p33=0;break;//
- case 2:anum=a[1];p34=0;break;
- case 3:anum=a[2];p32=0;break;
- case 4:anum=a[3];p31=0;break;
- case 5:anum=a[4];p30=0;break;
- case 6:anum=a[5];p07=0;break;
- case 7:anum=a[6];p06=0;break;
- case 8:anum=a[7];p05=0;break;
- case 9:anum=a[8];p04=0;break;
- case 10:anum=a[9];p03=0;break;
- case 11:anum=a[10];p02=0;break;
- case 12:anum=a[11];p01=0;break;
- case 13:anum=a[12];p00=0;break;//
- }
- if(anum<=8){P2=lednum[anum];p35=0;p36=0;p37=0;}//Æá±Î¸ßèyλ
- if(anum==9){P2=0xff;p35=1;p36=0;p37=0;}
- if(anum==10){P2=0xff;p35=1;p36=1;p37=0;}
- if(anum==11){P2=0xff;p35=1;p36=1;p37=1;}
- delay(5);
- }
- uchar STC_ADC() //£¡£¡¸ù¾Yêy¾YêÖ2áD′ò»¸öad¶áè¡oˉêy
- {
- 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<13; 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;//DT¸Ä
- else
- a[i] = a[i]-FFT_OUT_MIN;
-
- if( a[i] >max)
- max =a[i];
- }
- if(max>11) //11
- {
- max/=11;
- for( i=0; i<13; i++) //êä3öaμÄ5¸ö·ÖàëêyÖμ
- {
- a[i]/=max;
- }
- }
- }
- void main()
- {
- P2M0=0xff;// BIN(11111111);//P2×ééèÖÃÎaíÆíìêä3ö
- P2M1=0x00;
- P3M0=0xff;// BIN(11111111);//P3×ééèÖÃÎaíÆíìêä3ö
- P3M1=0x00;
- P1M0=0x00;
- P1M1=0x01;
- P1ASF =1; //éèÖÃP1.0ÎaAD¿ú AD¿ú½«Ä£ÄaDÅoÅ×a»»Îaêy×ÖDÅoÅ
- AUXR1 =BIN(100);
- keep=0;
- keepnum=0;
- timerinit()
- timernum=3;
- timernum2=0;
- while(1)
- {
- FFT();
- }
- }
- int t=0;
- void timer0() interrupt 1
- {
- unsigned char i;
- t++;
- if(t == 24)
- {
- for(i=0;i<13;i++)temp[i] = a[i];
- t = 0;
- }
- disp();
- TH0=(65536-1000)/256;
- TL0=(65536-1000)%256;
- }
复制代码 |