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

📄 task_time.c.bak

📁 CC1000 Rf modem C codes for philips sLpc213X ARM MCU.
💻 BAK
字号:

#include "config_GD61.h"
#include "include_GD61.h"
#include <stdio.h>

//////////////////////////////////////////////////////////////////////////////
TIMEACTION ActionTime[MAX_CONTROL_TABLE_NUM];

uint8 AutoLightCntMode;

uint8  MaxInputIICnt;
extern uint16 MaxInputLimitII;

extern CALENDAR CurrentTime;
extern uint16 TransferKKK;
extern uint16 ControlTime;

extern volatile char   TimerActSecTick;		// for timer_control function
extern volatile uint16 enTimerLightCnt;		// enable timer automatic function 
extern volatile uint16 CC1000ResetTimer;
extern volatile uint16 SysReadyTimer;
extern volatile uint16 MP_FailedmSecondCnt;	// for main power failed detector

extern uint16 ModuleAddress;				//module address
extern uint16 Pkg_SerialNum;				//serial number of package.
extern uint16 ActionValue;			//action value corresponding to action code.

//extern int32 CurInput_UU;
//extern int32 CurOutput_U;

//////////////////////////////////////////////////////////////////////////////

void CommandLightControl(uint16 ActValue);
void AutomaticLightControl(void);
void InitActTimer(void);
void TimerLigthScheduler(void);
void TimerReset(void);				////reset the system every day at HOUR:MIN

//////////////////////////////////////////////////////////////////////////////
// (Line_Num==PowerLine_ID)&&(DestinationAddr==ModuleAddress)
//////////////////////////////////////////////////////////////////////////////
//#define Light_toON	(0x20-1)				//0x1f
//#define Light_toOFF	(Light_toON-1)		//0x1e
//////////////////////////////////////////////////////////////////////////////
void CommandLightControl(uint16 ActValue)
{
char dbuffer[100];

//[1] directly control
		if(ActValue < Light_toOFF){

			LampStateAbs(ActValue);			// light control, control voltage adj level directly
			AutoVoltageFlag = DISABLE_FLAG;
			Uart0SendString("control Voltage directly.");
		}
//[2] turn light off
		else if(ActValue == Light_toOFF){

			LampStateAbs(Light_toOFF);			//
			AutoVoltageFlag  = DISABLE_FLAG;	//turn on, and set default voltage control to automatic mode
			Uart0SendString("turn light off.");
		}
//[3] turn light on
		else if(ActValue == Light_toON){
		
			if((StateValue&LIGHT_ON_FLG_BIT)!=LIGHT_ON_FLG_BIT){
				AutoVoltageFlag  = ENABLE_FLAG;	//turn on, and set default voltage control to automatic mode
				TargetVoltage 	 = START_TARGET_VOLTAGE;	//set starting voltage=200V.
				AutoVoltageTimer = AUTO_VOLTAGE_DEFAULT_TIME+(ModuleAddress % ACT_TIME_GROUP)-ACT_TIME_GROUP;

				PreToLightOn();					//preset the relay.
				LampStateAbs(Light_toON);

				sprintf(dbuffer,"Sets up TargetVoltage = %d,AutoVoltageTimer = %d,enable automatic control.",
				                         (TargetVoltage/10),AutoVoltageTimer);	Uart0SendString(dbuffer);
			}else{
				Uart0SendString("Light on now, cancle the command.");
			}
		}
//[4] automatically control
		else if(ActValue > Light_toON){

			AutoVoltageFlag  = ENABLE_FLAG;
			TargetVoltage 	 = ActValue;		//(3) automatic voltage control
//			AutoVoltageTimer = AUTO_VOLTAGE_DEFAULT_TIME+(ModuleAddress % ACT_TIME_GROUP)-ACT_TIME_GROUP;
			AutoVoltageTimer = 60;
			sprintf(dbuffer,"Sets up TargetVoltage = %d ,enable automatic control.",(TargetVoltage/10));
			Uart0SendString(dbuffer);
		}
//[5] unknowm control value
		else{
			Uart0SendString("cancel unsupported voltage control value");
		}
	}		

///////////////////////////////////////////////////////////////////////
void AutomaticLightControl(void)
{
int32 databuff;
int32 V_AdjustStep;
char  dbuffer[100];

	if((StateValue & LIGHT_ON_FLG_BIT)!=LIGHT_ON_FLG_BIT) 	return;	
//// the higest bit indicates light on/off state

	if((AutoVoltageFlag == ENABLE_FLAG) && (AutoVoltageTimer == 0)){
//// print info_1
		if(SysReadyTimer!=0){
			Uart0SendString("AutomaticLightControl");
			Uart0SendString("But don't get ready after powerUp.");
			AutoVoltageTimer=5;
			return;
		}

//// print info_2
#ifdef EN_MAIN_POWER_ALARM
//		if((IO0PIN&MPF_P0_07)==MPF_P0_07){	// level detecting mode
		if(MP_FailedmSecondCnt>=MAIN_POWER_FAILED_MSECOND){	//pulse detecting mode
			Uart0SendString("In Main_Power failed alarm state.");
////		return;
		}
#endif

		Uart0SendEnter();					// hint the current time firstly
//// print info_3
#ifdef EN_LOAD_LIMIT_PROTECT
		if((CurInput_II>MaxInputLimitII)&&(MaxInputLimitII>500)){
			sprintf(dbuffer,"Load current limit alarm: CurInput_II=%4d(mA), MaxInputLimitII=%4d(mA)", 
													   CurInput_II,         MaxInputLimitII);
			Uart0SendString(dbuffer);
		
			if(MaxInputIICnt>OVER_LOAD_TIMES){
				LampStateAbs(Light_toOFF);	Uart0SendString("Turn OFF load.");
				StateValue = StateValue|LOAD__ALARM_TYPE;
				return;
			}else{
				MaxInputIICnt++;
			}
		}else{
			MaxInputIICnt=0;
			StateValue = StateValue&(~LOAD__ALARM_TYPE);
		}
#endif

////		    
		V_AdjustStep	 = (CurInput_UU*TransferKKK)/2200;	// standard Voltage = 220V, *10V
		AutoVoltageTimer =  60;					// set the time for next control action

//// print info_4
		sprintf(dbuffer,"[!!] Current time: %4d.%2d.%2d.%2d.%2d.%2d",CurrentTime.xYEAR,CurrentTime.xMONTH,
		CurrentTime.xDOM,CurrentTime.xHOUR,CurrentTime.xMIN,CurrentTime.xSEC);	Uart0SendString(dbuffer);
		sprintf(dbuffer,"Auto Control: Input(V)=%4d, Output(V)=%4d, Target(V)=%4d, V_Step(V)=%4d, IntervalTime(S)=%3d.",
		   							   CurInput_UU,  CurOutput_U,   TargetVoltage, V_AdjustStep,  AutoVoltageTimer);
		Uart0SendString(dbuffer);

//// task get ready.
		switch(AutoLightCntMode){

		case vFromInPut_0:
		case vFromInPut_1:
//		case vFromInPut_2:

#ifdef EN_CLOSE_LOOP_CONTROL
			AutoLightCntMode++;			//change to Relative mode
#endif
			Uart0SendString("Open loop Mode.");
////
#ifdef U_MAIN_ADJUST_DECREASE
			databuff=(int16)CurInput_UU-(int16)TargetVoltage;	// adjust voltage in decreasing mode
#else
			databuff=(int16)TargetVoltage-(int16)CurInput_UU;	// adjust voltage in increasing mode
#endif

////// decreasing the output voltage
			if(databuff>=0){	// if positive
				if(databuff >= (V_AdjustStep>>1)){

#ifdef U_MAIN_ADJUST_DECREASE
					databuff= (databuff+(V_AdjustStep>>1)) / V_AdjustStep;
#else
					databuff= (databuff-(V_AdjustStep>>1)) / V_AdjustStep;
#endif
					if(databuff>Minus_vMAX){		// if Voltage too high, force -37.5V
						LampStateAbs(Minus_vMAX);	//this is the max negative adjust
					}else{							// if TransferKKK< xxxx < Minus_37V5, adjust voltage.
						LampStateAbs((uint16)databuff);
					}
				}else{								//databuff < TransferKKK
					sprintf(dbuffer,"AutomaticLightControl(1): Keep state = 0x%x",StateValue&0xff); 
					Uart0SendString(dbuffer);
				}
			}

////// increasing the output voltage
			else{						// if negative
				databuff = -databuff;	// change to positive firstly.
				if(databuff >= (V_AdjustStep>>1)){

					LampStateAbs(Minus_00V0);		//this is the max negative adjust

				}else{					//databuff < TransferKKK
					sprintf(dbuffer,"AutomaticLightControl(2): Keep state = 0x%x",StateValue&0xff); 
					Uart0SendString(dbuffer);
				}
			}
			
			break;

//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////

		AutoVoltageTimer =  ControlTime;					// set the time for next control action
		default:

			Uart0SendString("Close loop Mode.");
////
#ifdef U_MAIN_ADJUST_DECREASE
			databuff=(int16)CurOutput_U-(int16)TargetVoltage;	// adjust voltage in decreasing mode
#else
			databuff=(int16)TargetVoltage-(int16)CurOutput_U;	// adjust voltage in increasing mode
#endif

////// decreasing the output voltage
			if(databuff>=0){	// if positive
				if(databuff >= Voltage_Up){

#ifdef U_MAIN_ADJUST_DECREASE
					databuff= (databuff+(V_AdjustStep>>1)) / V_AdjustStep;
#else
					databuff= (databuff-(V_AdjustStep>>1)) / V_AdjustStep;
#endif
					if(databuff>2) 
					{
						LampStateRelative(-2);	//two step every time
						AutoVoltageTimer=60;
					}
					else LampStateRelative((int16)(-databuff));
					
				}else{										//databuff < TransferKKK
					sprintf(dbuffer,"AutomaticLightControl(3): Keep state = 0x%x",StateValue&0xff); 
					Uart0SendString(dbuffer);
				}
			}
			
////// increasing the output voltage
			else{	// if negative 
				databuff     = -databuff;					// change to positive firstly.
				if(databuff >= Voltage_Down){			// 

#ifdef U_MAIN_ADJUST_DECREASE
					databuff= (databuff-(V_AdjustStep>>1)) / V_AdjustStep;
#else
					databuff= (databuff+(V_AdjustStep>>1)) / V_AdjustStep;
#endif					
					if(databuff>1) 
					{
						LampStateRelative(2);	//two step every time
						AutoVoltageTimer=60;
					}
					else LampStateRelative((int16)databuff);
					
				}else{										//databuff < TransferKKK
					sprintf(dbuffer,"AutomaticLightControl(4): Keep state = 0x%x",StateValue&0xff); 
					Uart0SendString(dbuffer);
				}
			}
			
			break;
		}	//switch
	}		//if
}			//

/////////////////////////////////////////////////////////////////////
//turnn off all the time control task
//status:
void InitActTimer(void)
{
char i;

	for(i=0;i<MAX_CONTROL_TABLE_NUM;i++)
	{
		ActionTime[i].ActionFlag 	= ENABLE_FLAG;
		ActionTime[i].xHOUR		 	= (uint8)(~NOTHING_TO_DO);
		ActionTime[i].xMIN		 	= (uint8)(~NOTHING_TO_DO);
		ActionTime[i].Action_Value	= NOTHING_TO_DO;	//nothing_to_do=0x00
	}
}

///////////////////////////////////////////////////////////////////
void TimerLigthScheduler(void)
{
char i;
char dbuffer[100];

////
  if(TimerActSecTick==1){			//check time second tick
	 TimerActSecTick=0;				//clear the second tick semaphore
////
	for(i=0;i<MAX_CONTROL_TABLE_NUM;i++){
	  if(ActionTime[i].ActionFlag  == ENABLE_FLAG){	//working state and second tick

		  if((ActionTime[i].xHOUR  == DISABLE_FLAG) || (ActionTime[i].xHOUR	== CurrentTime.xHOUR)){
			if((ActionTime[i].xMIN == DISABLE_FLAG) || (ActionTime[i].xMIN	== CurrentTime.xMIN)){
			  if( CurrentTime.xSEC == 0x00){
////
				Uart0SendEnter();
				Uart0SendString("TimerLigthScheduler()");
			    sprintf(dbuffer,"[!!] Current time: %4d.%2d.%2d.%2d.%2d.%2d",CurrentTime.xYEAR,CurrentTime.xMONTH,
			    CurrentTime.xDOM,CurrentTime.xHOUR,CurrentTime.xMIN,CurrentTime.xSEC);	Uart0SendString(dbuffer);
				CommandLightControl(ActionTime[i].Action_Value);
		      }
		    }
    	  }
	  }
	}
  }
}

//////////////////////////////////////////////////////////////////////
void TimerReset(void)		////reset the system every day at HOUR:MIN
{
char i;

//// CC1000 reset timer action
	if(CC1000ResetTimer == 0){
		CC1000ResetTimer = CC1000_TIMER_RESET_TIME;
		Uart0SendEnter();
		Uart0SendString("WatchDog re_initialize CC1000");
		RF_StartCC1000();
	}

////
	if(enTimerLightCnt==0){
		enTimerLightCnt = EN_TIMER_SCHEDULER_TIME;	// reset the timer_control time		
		Uart0SendString("WatchDog enable TimerLigthScheduler");
		for(i=0;i<MAX_CONTROL_TABLE_NUM;i++){		// maximum sub_line is 3
			ActionTime[i].ActionFlag=ENABLE_FLAG;
		}
	}

//// system reset timer action 	
	if(CurrentTime.xHOUR==WATCHDOG_RESET_SYS_HOUR){
		if(CurrentTime.xMIN==WATCHDOG_RESET_SYS_MIN){
//		    if((CurrentTime.xSEC & 0xfe) == 0x00){
		 	if( CurrentTime.xSEC         == 0x00){
				Uart0SendString("WatchDog  Reset System");
				RF_SendingOver();	Uart0SendingOver();
				WatchDogStart(20);	//20 ms Watchdog reset time
				while(1);			//wait for watchdog reset							
			}

		}
	}
}

////////////////////////////////////////////////////////////////////
//End Of File
////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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