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

📄 main.c

📁 以C8051F020单片机做的多功能计数器
💻 C
字号:

#include "main.h"

extern idata Counter periodCounters[10];

xdata uchar state=NORMAL;
xdata Counter tCounter;
xdata uchar liangcheng;

xdata uchar s_MHz;//the second part of the period,The Millions of the frequency value
xdata uint ms_KHz,us_Hz;////the ms and us part of the period
xdata uint decimal;//the part after point of the  frequency and period
bit canceled=0;
xdata uchar MeasureType=1;//1-frequency & period;  2-interval
xdata double dCounter;
xdata double fdCounter;
xdata char restorePos=0;//the length of HisData[10]
xdata char showPos=0;
xdata uchar coefficient;
//bit interrupted=0;

xdata refreshTime=0;
xdata refreshTemp=0;

xdata uchar ran;

xdata struct History HisData[10];



//xdata char ucCurtime[7];

void main()
{
uchar keyvalue;

SP=0xd0;

	OpenWatchDog(0);                             //禁止看门狗                   
	InitSysClock();                           	 //时钟初始化
	InitPort();  
	InitInterrupt();

	LcdInit();
	
//	Delayms(5000);	
	//数据初始化

//	setDateTime();

  
 //for(keyvalue=0;keyvalue<200;keyvalue++)
//	   v_Get1302(ucCurtime);

	start1302();

	keyvalue=NOKEY;
	ShowDateTime();
	
	while(1)
	{
		keyvalue=KeyScan();
		if(keyvalue!=NOKEY)
			ET0=0;
		else
			ET0=1;

		if(state==NORMAL && keyvalue==HISTORY)
		{						
			showPos=0;
			if(restorePos==0)
			{
				LcdClear();	
				LcdShow(1,1,"无历史记录");
				continue;
			}

			showHistory(showPos);	
			state=SHOW_HIST;	
		
		}
		else if(state==SHOW_HIST && keyvalue==UP)
		{
			if(showPos<0)
				showPos=restorePos-1;
			showHistory(showPos);
			showPos--;
	
		}
		else if(state==SHOW_HIST && keyvalue==DOWN)
		{
			if(++showPos==restorePos)
				showPos=0;
			showHistory(showPos);	
		
		}
		else if(state==NORMAL && keyvalue==ZHOUQI)
		{
			LcdClear();	
			LcdShow(0,0,"周期测量");
			MeasureType=1;
			state=GET_PF;
		}
		else if(state==NORMAL && keyvalue==PINLV)
		{
			LcdClear();	
			LcdShow(0,0,"频率测量");
			MeasureType=1;
			state=GET_PF;
		}
		else if(state==NORMAL && keyvalue==JIANGE)
		{
			LcdClear();	
			LcdShow(0,0,"时间间隔测量");
			NotifyStart();
			if(canceled)
			{
				canceled=0;
				returnMainGUI();
				continue;
			}
			MeasureType=2;
			state=GET_PF;
			MeasureInterval();
		}

		else if(state==GET_PF && keyvalue==Hz1)
		{
			LcdClear();	
			LcdShow(1,0,"量程4M-10MHz");
			Measure(4,100);
			
		}
		else if(state==GET_PF && keyvalue==Hz2)
		{
			LcdClear();	
			LcdShow(1,0,"量程200K-4MHz");
			Measure(4,50);
		}
		else if(state==GET_PF && keyvalue==Hz3)
		{
			LcdClear();	
			LcdShow(1,0,"量程2K-200KHz");
			Measure(3,5);
		}
		else if(state==GET_PF && keyvalue==Hz4)
		{
			LcdClear();	
			LcdShow(1,0,"量程400-2KHz");
			Measure(3,1);
		}
		else if(state==GET_PF && keyvalue==Hz5)
		{
			LcdClear();	
			LcdShow(1,0,"量程40-400Hz");
			Measure(2,1);
		}
		else if(state==GET_PF && keyvalue==Hz6)
		{
			LcdClear();	
			LcdShow(1,0,"量程4-40Hz");
			Measure(1,1);
		}
		else if(state==GET_PF && keyvalue==Hz7)
		{
			LcdClear();	
			LcdShow(1,0,"量程4Hz以下");
			Measure(5,1);
		}
		

		if(keyvalue==CANCEL)
		{
			LcdClear();
			ShowDateTime();
			state=NORMAL;
		}
		if(state==CALCU)
		{
			EX0=0;//外部中断0
			tCounter=sortPeriod();
			if(1==MeasureType)
			{
				switch(liangcheng)
				{
				case 4:	
					dCounter=(tCounter/65.536)*2/coefficient;
					fdCounter=32768.00/tCounter*1000000*coefficient;
					break;//65536 periods//定时器1延时为2微秒,这一步得到单位为0.001微秒的时间
				case 3:
					dCounter=(tCounter/1000.00)*2000/coefficient;
					fdCounter=500000000.00/tCounter*coefficient;
					break;//1000 periods//,这一步得到单位为0.001微秒的时间
				case 2:	
					dCounter=(tCounter/100.00)*2000/(coefficient);
					fdCounter=50000000.00/tCounter*(coefficient);
					break;//100 periods//,这一步得到单位为0.001微秒的时间
				case 1:	
					dCounter=(tCounter/10.00)*2000/(coefficient);
					fdCounter=5000000.00/tCounter*(coefficient);
					break;//10 periods//,这一步得到单位为0.001微秒的时间
				case 5:	
					dCounter=(tCounter/5.00)*2000/(coefficient);
					fdCounter=2500000.00/tCounter*(coefficient);
					break;//5 periods//,这一步得到单位为0.001微秒的时间
				}
			}
			else 
			{
				dCounter=tCounter*2000;//2us ,这一步得到单位为0.001微秒的时间
			}
			
			show_sound_Result();
			restore();//保存此次测量结果
			//
			while(CANCEL!=KeyScan());//waiting for CANCEL
			state=NORMAL;
		}
		else if(state==NORMAL && keyvalue==NOKEY)
		{
			ET0=1;
			EA=1;
			if(refreshTime==1)
			{
				ShowTime();
				refreshTime=0;
			}
			if(refreshTemp==1)
			{
				ShowTemp();
				refreshTemp=0;
			}
		}

	}//end while

}

Counter  sortPeriod()
{
 	Counter  c;
	uchar i;
	if(MeasureType==2)//use the biggest number at this range
	{
		c=periodCounters[0];		
		for(i=1;i<COUNTERS_LEN;i++)
		{
			if(c<periodCounters[i])
				c=periodCounters[i];
		}
	}
	else if(MeasureType==1)
		c=periodCounters[2];
			
	return c;

}


void AnalyzePeriod()//输入的周期以0.001微秒为单位,

⌨️ 快捷键说明

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