AD0809数字电压表,加了滤波程序但是感觉没起作用。请大家帮我看看
- #include< reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code DSY_LED[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x7f};
- uchar code LED_W[]={0xf7,0xfb,0xfd,0xfe,0xff};
- sbit ST=P3^4;
- sbit OE=P3^3 ;
- sbit EOC=P3^2 ;
- void delayms(uint ms)
- {uchar i;
- while(ms--)for(i=0;i<120;i++);
- }
- void display(uchar P)
- { uchar ad_data1,ad_data2,ad_data3,ad_data4;
- long ad_0809;
- ad_0809=P;
- OE=0;
- P2=0xff;
- ad_data1=(ad_0809*4650/255)/1000;
- ad_data2=((ad_0809*4650/255)%1000)/100;
- ad_data3=(((ad_0809*4650/255)%1000)%100)/10;
- ad_data4=((((ad_0809*4650/255)%1000)%100)%10);
- P2=LED_W[0];
- P1=DSY_LED[ad_data4];
- delayms(3);
- P2=LED_W[4];
- P2=LED_W[1];
- P1=DSY_LED[ad_data3];
- delayms(3);
- P2=LED_W[4];
- P2=LED_W[2];
- P1=DSY_LED[ad_data2];
- delayms(3);
- P2=LED_W[4];
- P2=LED_W[3];
- P1=DSY_LED[ad_data1]&LED_W[10];
- delayms(3);
- P2=LED_W[4];
- }
- unsigned short AD_Filter(unsigned short* ad_data,int N)//滤
- {
- unsigned short max,min,average;
- int sum,i;
- average = 0;
- if(N>4)
- {
- max =ad_data[1];
- min = max;
- sum = 0;
- for(i=0;i<N;i++)
- {
- sum +=ad_data[i];
- if(ad_data[i]>max)
- {
- max = ad_data[i];//一个循环之后max就是最大的值
- }
- if(ad_data[i]<min)
- {
- min =ad_data[i];//一个循环之后min就是最小的值
- }
- }
- sum = sum-max-min;//去掉最大的值和最小的值
- average = sum/(N-2);//对N-2个数求平均值
- }
- return average;
- }
- void main()
- {
- while(1)
- {
- ST=0;ST=1;ST=0;
- while(EOC==0);
- P2=0xff;
- OE=1;
- display(P0);
- OE=0;
-
- }
- }
复制代码
|