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

📄 airpress.c

📁 单片机c语言程序设计,请大家指教,分享一下
💻 C
字号:
// Header:
// File Name: 	airpress.c
// Author:		liu.yingjun
// Date:		2008/10/09


#include <reg51.h>
#include <rtx51tny.h>                 /* RTX-51 tiny functions & defines      */

#include "YAJIANGCOUNT_DISPLAY.h"
#include 	"ERASEINT.H"
#include 	"airpress.h"

#define  AIRPRESS_COUNT_T 5	/* 每次计数时间长度 AIRPRESS_COUNT_T X 100毫秒 */
							/* 注意要满足: T_100MS_TICKS * AIRPRESS_COUNT_T<= 0xffff  */
signed long int airpress0_count;  	/* 在没有负压的情况下的计数 */ 
signed long int airpress1_count;  	/* 在有料的情况下的计数 */ 
signed long int airpress_count_int;	/* 现在测的负压计数,由中断程序写,任务读 */ 
signed long int airpress_count;		/* 现在测的负压计数,任务从airpress_count_int读得,并加TH1,TL1 */  
#define	 AIRPRESS_COUNT_DIFF_MIN	300	/* 判断有料和没料时计数的最小差异 */

void T1_init(void)
{	TMOD =  (TMOD & 0x0F) |  0x50 ;			/* 16位计数器 */
	ET1=1 ; 
}

void T1_start(void)
{	TH1=TL1=0; 	TF1=0;
	TR1=1;				 
}

void T1_stop()
{  TR1=0;				 
}

static void T1_isr (void) interrupt 3
{	airpress_count_int += 0x10000u;
}

void start_task ( void ) _task_ START_TASK_N  
{   

	enable_reenter_int();               
	EA=1;				/* 容许中断 */
	os_create_task (COUNT_TASK_N);		   //计数任务
  	os_create_task (PUMP_RUN_INTERVAL);    //bump 任务
	display_init();

  	while (1)  
  	{ 	DOG_IN=  ~DOG_IN;		//喂狗
		refreshDisplay();
  		os_wait (K_TMO, T_100MS_TICKS/12+1, 0);
  	}
}

unsigned long read_param_4(unsigned int addr1)
{	return (byte_read (addr1) +byte_read (addr1+1) *256uL
			+byte_read (addr1+2)*256uL*256  +byte_read (addr1+3)*256uL*256*256 ) ;

}

void write_param_4(unsigned int addr1,unsigned int longint1)
{	 byte_program_and_verify (addr1, longint1 &0xff) ;
	byte_program_and_verify (addr1+1, (longint1>>8) &0xff) ;
	byte_program_and_verify (addr1+2, (longint1>>16) &0xff);
	byte_program_and_verify (addr1+3, (longint1>>24) &0xff) ; 
}


void count_task ( void ) _task_ COUNT_TASK_N  
{	unsigned int wait_t;
	unsigned char saved_param=0;

	airpress0_count =airpress1_count=0 ;  	/* 在没有负压的情况下的计数 */ 
	/*  airpress0_count= 从flash rom 读数 */ 
	airpress0_count= (signed long ) read_param_4(AIRPRESS0_COUNT_ADDR);
	airpress1_count= (signed long ) read_param_4(AIRPRESS1_COUNT_ADDR);


	airpress_count_int = 0;	/* 现在测的负压计数,由中断程序写,任务读 */ 
	airpress_count = 0 ;		/* 现在测的负压计数,任务从airpress_count_int读得,并加TH1,TL1 */  
	HAVE_OK=0;		//没料

	T1_init();

  	while (1)  
  	{	T1_start();		//开始计数
		wait_t= T_100MS_TICKS * AIRPRESS_COUNT_T;		  /* 等待超时,计时 AIRPRESS_COUNT_T X100毫秒                 */
  		while (wait_t>255u) 
		{	os_wait ( K_TMO, 255u, 0);  
			wait_t -= 255;
		}
		os_wait ( K_TMO, wait_t, 0); 
		T1_stop();	//停止计数
		airpress_count=	 airpress_count_int + TH1*(unsigned int)256+TL1;	//计算计数值

		os_switch_task (); 	//任务切换,执行别的任务

		if( AIRPRESS0_MEASURE_ING )		// 设置参数,现在测的是 没料的计数
		{	airpress0_count=airpress_count;
			displayDec( airpress_count );	//显示
		}
		else if( AIRPRESS1_MEASURE_ING )	//设置参数,现在测的是 有料的计数
		{	airpress1_count=airpress_count;
			displayDec( airpress_count );	//显示
		}
		else if( airpress0_count !=0 && airpress1_count!=0  
				&& airpress0_count!=0xffffffffL && airpress1_count!=0xffffffffL  ) 		 /* 正常使用,归一化显示*/
			displayDec( AIRPRESS0_COUNT_DISP
				+(airpress_count-airpress0_count)/(airpress1_count-airpress0_count)
					*(AIRPRESS1_COUNT_DISP-AIRPRESS0_COUNT_DISP) );

		if(AIRPRESS_COUNT_SAVE ==0 && saved_param==0 )	  //这样,每次上电只能保存一次参数,因为Flash有写的次数限制
		{	sector_erase( AIRPRESS0_COUNT_ADDR );
			 write_param_4(AIRPRESS0_COUNT_ADDR, airpress0_count );	// : 保存参数airpress0_count到flash
			 write_param_4(AIRPRESS1_COUNT_ADDR, airpress1_count );	// : 保存参数airpress1_count到flash
			 saved_param= 1;
		}

		os_switch_task (); 	//任务切换,执行别的任务

#if AIRPRESS_COUNT_ADD==1		    //	   //如果有料时,计数比无料时大
		if( (airpress_count-airpress0_count) > (airpress1_count-airpress0_count)*2/3 )	 //如果差异>有无料差异的2/3,有料
			HAVE_OK=1;
		if( (airpress_count-airpress0_count) < (airpress1_count-airpress0_count)/3 )	 //如果差异<有无料差异的1/3,无料
			HAVE_OK=0;
#else		//	   //如果有料时,计数比无料时小
		if( (airpress0_count-airpress_count) > (airpress0_count-airpress1_count)*2/3 )	 //如果
			HAVE_OK=1;
		if( (airpress0_count-airpress_count) < (airpress0_count-airpress1_count)/3 )	 //如果
			HAVE_OK=0;
#endif
  			
  	}
}


void pump_task ( void ) _task_ PUMP_TASK_N  
{	unsigned long int wait_t;	 //PUMP_RUN_INTERVAL+PUMP_RUN_LONG
	PUMP_RUN = 0;
  	while (1)  
  	{   wait_t=PUMP_RUN_INTERVAL*10* T_100MS_TICKS  ;		  /* 等待超时,计时 AIRPRESS_COUNT_T X100毫秒                 */
  		while (wait_t>255u) 
		{	os_wait ( K_TMO, 255u, 0);  
			wait_t -= 255;
		}
		os_wait ( K_TMO, wait_t, 0); 

		PUMP_RUN = 1;

		wait_t=PUMP_RUN_LONG*10* T_100MS_TICKS  ;		  /* 等待超时,计时 AIRPRESS_COUNT_T X100毫秒                 */
  		while (wait_t>255u) 
		{	os_wait ( K_TMO, 255u, 0);  
			wait_t -= 255;
		}
		os_wait ( K_TMO, wait_t, 0); 
		PUMP_RUN = 0;
  	}
}

⌨️ 快捷键说明

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