⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 这是一个很好的学习工程
💻 C
字号:
#include <reg51.h>

#define uchar unsigned char
#define uint  unsigned int
#define ulong unsigned long

sfr     IPH=0xB7;
sfr     AUXR=0x8E;
sfr     AUXR1=0xA2;
sfr     XICON=0xC0;
sfr     WDT_CONTR=0xE1;
sfr     ISP_CONTR=0xE7;

uchar   Dispbuff[4];
bit     Firstint;
bit     Firstfilt;
ulong   Filter_result;
ulong   Last_samp_data;
ulong   Measure;
ulong   Sampbuff[3];
uchar   Time_samp;
uchar   P_buff;

extern  uchar  Samp_data_h;
extern  uchar  Samp_data_l;
extern  uchar  Samp_data_c;
extern  uchar  Time_int_t0;
extern  bit    Samp_ok;

#pragma   noaregs

void ini_sfr()
{
	PSW=0x00;
	AUXR=0x03;
	AUXR1=0x00;
	ISP_CONTR=0x00;
	WDT_CONTR=0x38;
	XICON=0x00;
	IP=0x02;               //int0=2,t0=1,t1=0
	IPH=0x01;
	TCON=0x01;             //int0:下降沿中断
	TMOD=0x11;             //T0:16位计数器,T1:16位定时器
	TH1=0xE3;              //22.1184/4ms
	TL1=0x33;
	TH0=0x00;              
	TL0=0x00;
	SCON=0x00;
	Firstint=0;
	Firstfilt=1;
	Samp_data_h=0;
    Samp_data_l=0;
    Samp_data_c=0;
	Time_int_t0=0;
    Samp_ok=0;
	IE=0x8B;
	TR1=1;
}

void disp_logo()
{
    uchar i=250;
	Dispbuff[0]=43;
	Dispbuff[1]=22;
	Dispbuff[2]=11;
	Dispbuff[3]=0;
	while(i--)
	PCON|=0x01;
}

void samp()
{
    uint  temp1;
	ulong temp2;
    Time_samp=4;
	P_buff=0;
	while(--Time_samp)
	{
        while(Samp_ok==0);
		Samp_ok=0;
		temp1=Samp_data_h;
		temp1=(temp1<<8)|Samp_data_l;
		temp2=65535*Samp_data_c;
		Sampbuff[P_buff++]=(temp1+temp2);
	}
}

void filter()
{
	uchar i,j;
	ulong temp;
	for(i=0;i<=1;i++)
		for(j=0;j<=(1-i);j++)
			if(Sampbuff[j]>Sampbuff[j+1])
			{
			temp=Sampbuff[j];Sampbuff[j]=Sampbuff[j+1];Sampbuff[j+1]=temp;
			}
	if(Firstfilt)
    {
		Firstfilt=0;
		Firstint=0;
    	Filter_result=Sampbuff[1];
		Last_samp_data=Sampbuff[1];
    }
    else if(-250<(Sampbuff[1]-Last_samp_data)<250)
   {	
        
    	Firstint=0;
		Last_samp_data=Sampbuff[1];
    	Filter_result=(Filter_result*9+Sampbuff[1])/10;
	}
	else
   {
    	if (Firstint)
    	{
      		Firstint=0;
      		Filter_result=Sampbuff[1];
		    Last_samp_data=Sampbuff[1];
    	}
    	else
      		Firstint=1;
    } 	

}


void data_manage()
{	
	ulong temp=184320000;
	Measure=temp/Filter_result;
}

void renovate_disp(ulong Measure)
{
    uint  i=Measure;
	uchar temp[4];
	temp[0]=i/1000;
	temp[1]=(i-temp[0]*1000)/100;
	temp[2]=(i-temp[0]*1000-temp[1]*100)/10;
	temp[3]=i-temp[0]*1000-temp[1]*100-temp[2]*10;
	if (temp[0]==0)
	{
		temp[0]=43;
	}
	Dispbuff[0]=temp[0];
	Dispbuff[1]=temp[1]+10;
	Dispbuff[2]=temp[2];
	Dispbuff[3]=temp[3];
}

void main()
{
	SP=0x5F;
	ini_sfr();
	disp_logo();

	while(1)
	{
		samp();
		filter();
		data_manage();
		renovate_disp(Measure);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -