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

📄 hptv._c

📁 MCU控制程序
💻 _C
📖 第 1 页 / 共 4 页
字号:
 
 DDRC  = 0xf4;  //pwrbt 去掉更改为备用的实时的中断输入引脚;
 PORTD = 0x60;
 DDRD  = 0xF3;
 
   WDR();
}





/******************************************
//Watchdog initialize
// prescale: 1024K
******************************************/
void watchdog_init(void)
{
 WDR(); //this prevents a timout on enabling
 WDTCR = 0x0E; //WATCHDOG ENABLED - dont forget to issue WDRs
}



/*****************************************
//TIMER0 initialize - prescale:64
// WGM: Normal
// desired value: 1KHz
// actual value:  1.002KHz (0.2%)
//主要实现时序中的毫秒级的计数
//同时实现输入端口的电平检测
*****************************************/
void timer0_init(void)
{
 TCCR0 = 0x00; //stop
 TCNT0 = 0x8D; //set count
 OCR0  = 0x73;  //set compare
 TCCR0 = 0x03; //start timer
}
void time0_cl(void)
{
 if(machine_open_start==1)                 //开机进入第一阶段reset未开始
   {
      
/*      if(pwrbt_count>=0&&pwrbt_count<pwrbt_high_time)
	    {
		   DDRC|=0x08;
		   PORTC|=0x08;                    //pwrbt=1
		}
*/		
      if(pwrbt_count>=pwrbt_high_time&&pwrbt_count<pwrbt_low_time)
	    {
//		   DDRC|=0x08;
//		   PORTC&=0xf7;                    //pwrbt=0
		   open_to_close_station=0;        //强制赋值,出于稳定考虑,可以省略
		   close_machine_enable=0;         //强制赋值,出于稳定考虑,可以省略
		}
	  if(pwrbt_count>=pwrbt_low_time&&pwrbt_count<255)
	    {
		  // PORTC|=0x04;                    //rly_on=1
		   PORTC&=0xfb;                    //rly_on=0
           open_to_close_count=0;
		   vs_on_open_count=0;          
		   reset_open_count=0;             //强制付值,出于稳定考虑,可以省略
		   machine_open_start=2;           //开机进入第二阶段reset开始
		   open_to_close_station=1;        //开机计时,到20s可以关机
		   pwrbt_count=0;                  //防止益处,出于稳定考虑,可以省略
		   
		}
      pwrbt_count++;
	  if(pwrbt_count>=(pwrbt_low_time+1)&&pwrbt_count<=255)
	    {
		   pwrbt_count=0;                  //防止益处,出于稳定考虑,可以省略
		}
/*		
	  if((PINA&0x08)==0)                   //掉电检测
	  {
	    power_down_count++;
	    if(power_down_count>=20)           //10ms检测  
	      {
		    power_down_count=0;
	       // power_on_close_enable=1;       //关机使能
			machine_open_start=0;         //清除10s限制
			close_machine_enable=1;       //立即关机,没有10s限制
		  //  power_check_up=1;              //延时开机使能
			//power_check_down=1;            //立即关机,没有10s限制
			//open_machine();               //立即关机
			//UDR=0x33;//测试
	      }
	  }*/	
   }
// else
//  {
//	   pwrbt_count=0;                      //防止益处,出于稳定考虑,可以省略
//   }
                                   
					//开机进入第二阶段reset开始   set vs_on 
   if(machine_open_start==2)
	  {
	    vs_on_open_count++;
	    if(vs_on_open_count>vs_on_open_time&&vs_on_open_count<=255)
		   {
		     // PORTD|=0x40;               //vs_on=1
			  PORTD&=0xff;               //vs_on=0
    	//	  PORTD&= 0xdf;   //pin14  pd5=0
		//	  PORTD&= 0xef;   //pin13  pd4=0
			  machine_open_start=3;
			  vs_on_open_count=0;        //防止益处,出于稳定考虑,可以省略
		   }
	/****************	   
        if((PINA&0x08)==0)//开机时掉电检测PA3脚,如持续10ms则执行关机,开机操作
        {
	        power_down_count++;
	        if(power_down_count>=10)//10ms检测
	        {
		        power_down_count=0;
//	            power_on_close_enable=1;       //关机使能
			
			  //  open_to_close_station=0;      //取消20s状态
			    machine_open_start=0;         //清除10s限制
			    close_machine_enable=1;       //立即关机,没有10s限制
		     //   power_check_up=1;              //延时开机使能
			//power_check_down=1;            //立即关机,没有10s限制
			//open_machine();               //立即关机
			//UDR=0x33;//测试
	        }
	     }************/
	  }
    
//////////////////////////////////////////////////////////////
                                      
									      //关机代码
    if(machine_close_start==1)
	  {
	     if(reset_close_count>=reset_close_time&&reset_close_count<reset_l_close_time)
		   {
		      PORTA&=0xfe;                //pa0=0  实现关机静音;
		   }
		if((reset_close_count>=reset_l_close_time)&&(reset_close_count<255))
		   {
		      //PORTC|=0x30;                //reset=1,reset_l=1
			  DDRC|=0x30;
		      PORTC&=0xcf;                //reset=0,reset_l=0
			  PORTA&=0xdf;                //pa5=0 IR_SW关闭;这一轮板R1074应不装 
		   }
		 reset_close_count++;
		 rly_on_count++;
		 if((reset_close_count>rly_on_time)&&(reset_close_count<=255))
		   {
	         // DDRC|=0x04;
		      //PORTC&=0xfb;                //rly_on=0
			  PORTC|=0x04;                    //rly_on=1
			 // rly_on_count=0;
		   }
		 vs_on_close_count++;
		 if((reset_close_count>vs_on_close_time)&&(reset_close_count<255)) 
		   {
		     // PORTD&=0xbf;                //vs_on=0;
			  PORTD|=0x40;               //vs_on=1
			  vs_on_close_count=0;
			  reset_close_count=0;
			  rly_on_count=0;
			  close_to_open_station=1;
			  machine_close_start=0;
			  PORTD&=0xef;
		      PORTD|=0x20;                //指示灯
			  port_close_machine_set();   //set the port after you close the machine
			  System_State=standby;
			   
		   }
		 if(reset_close_count>(vs_on_close_count+1)&&reset_close_count<=255)
		   {
			  reset_close_count=0;
			  rly_on_count=0;
			  vs_on_close_count=0;
			  machine_close_start=0;
		   }                              //防止益处,出于稳定考虑,可以省略
	  }

//////////////////////////////////////////////////////pw_key open the machine
/*##############################################################*/

  if(tv_state)  //开机时检测
  {
    if((PINA&0x08)==0)//开机时掉电检测PA3脚,如持续10ms则执行关机,开机操作
     {
	    power_down_count++;
	    if((power_down_count>=20)&&(machine_open_start==0))//10ms检测
	      {
		    power_down_count=0;
			
	        power_on_close_enable=1;       //关机使能
			open_to_close_station=0;      //取消20s状态
			//machine_open_start=0;         //清除10s限制
			close_machine_enable=1;       //立即关机,没有10s限制
		    power_check_up=1;              //延时开机使能
//			power_check_down=1;            //立即关机,没有10s限制
			//open_machine();               //立即关机
			
	      }
	 }
    else                                 //为0正常
     {
	        power_down_count=0;              //计数器清零
	 }
  }
  else         
  {                                   //关机时检测是否需要掉电开机
  
     if(power_check_up)               //开机
		{
		 
		  power_down_count=0;
		  
		  power_check_up=0;
		  power_key_open_enable=1;
		
		}
  }	 
	  
 WDR();
  
}
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
 //WDR();
 TCNT0 = 0x8D; //reload counter value
 time0_b=1;
 time0_num++;
 time1_num++;
 
 
 if(time1_num>=50)//time时间计时
   {
     time1_num=0;
     time1_b=1;
   }
 if(key_open)     //按键保持时间计数
 {
   keycl_num++;
 }
 if(irda_b)       //红外键处理
 {
    irda_num++;
	if(irda_num>IRDA_WAIT)
	{
	  TCCR2=0x02;
	  irda_b=0;
	  irda_num=0;
	}
 } 
 // WDR();
// TCNT0 = 0x8D; //reload counter value
}
//##########################原time1定时器代码
void time1_cl(void)
{
    if(machine_open_start==3)                       //开机进入第三阶段reset开始
   {  
	  if(reset_open_count>=port_open_machine_delaytime)//call port_open_machine_set
	    {
		   port_open_machine_set();              //delay 0.6s reset port
		}
	  if(reset_open_count>=0&&reset_open_count<reset_open_time)
	    {
		   //PORTC|=0x30;                          //reset=1,reset_l=1
		   DDRC|=0x30;
		   PORTC&=0xcf;                          //reset=0,reset_l=0
		}
     /* if(reset_open_count>=reset_open_time&&reset_open_count<reset_l_open_time)
	    {
		   PORTC|=0x10;
		   PORTC&=0xdf;                          //reset=0,reset_l=1
		}*/
		
	  if(reset_open_count>=reset_open_time&&reset_open_count<255)
	    {
		   //PORTC&=0xcf;                          //reset=0,reset_l=0
		   DDRC|=0x30;
		   PORTC|=0x30;                          //reset=1,reset_l=1
//		   PORTD|=0x10;                          
//		   PORTD&=0xdf;                          //指示灯
		   PORTA|=0x20;
		}
	  if(reset_open_count>=pa0_l_open_delay&&reset_open_count<=255)
	    {
		   machine_open_start=0;
		   vs_on_open_count=0;
		   reset_open_count=0;                   //防止益处,出于稳定考虑,可以省略
		}
      reset_open_count++;
      if((PINA&0x08)==0)//开机时掉电检测PA3脚,如持续10ms则执行关机,开机操作
        {
//	        UDR=22;
			power_down_count++;
	        if((power_down_count>=20)&&(machine_open_start==0))//10ms检测
	        {
		        power_down_count=0;
//				UDR=0x22;								   
	            power_on_close_enable=1;       //关机使能
			
			    open_to_close_station=0;      //取消20s状态
			    machine_open_start=0;         //清除10s限制
				close_machine_enable=1;       //立即关机,没有10s限制
		     //   power_check_up=1;              //延时开机使能
			//power_check_down=1;            //立即关机,没有10s限制
			//open_machine();               //立即关机
			//UDR=0x33;//测试
	        }
		}	
   }
 ////////////////////////////////////////////////////////////  
                                                 //counting the time during opening the machine
 if(open_to_close_station>=1)  
   { 
     if(open_to_close_count>=open_to_close_time) //>2s
	    {
		   close_machine_enable=1;               //close machine enable
		   PORTD&= 0xdf;   //pin14  pd5=0
           PORTD|= 0x10;   //pin13  pd4=1		   										 
		  // start_iocheck();                      //10s后第一次检测io口状态
		}
	 else
	    {
		   close_machine_enable=0;               //if <20s then you can not close the machine
		}
     open_to_close_count++;
	 if(open_to_close_count>=(open_to_close_time+1))
	    {
		   open_to_close_count=0;
		   open_to_close_station=0;             //防止益处,出于稳定考虑,可以省略
		}
	 
   }
 //////////////////////////////////////////////////////////  
                                                //counting the time during closing the machine
 if(close_to_open_station>=1)  
   {
      if(close_to_open_count>=close_to_open_time&&close_to_open_count<255)//>2s
	    {
		   open_machine_enable=1;               //open machine enable
		}
	 else
	    {
		   open_machine_enable=0;               //if <2s then you can not open the machine
		}
     close_to_open_count++;
	 if(close_to_open_count>=(close_to_open_time+1)&&close_to_open_count<=255)
	    {
		   open_to_close_count=0;
		   close_to_open_station=0;             //防止益处,出于稳定考虑,可以省略
		}
   }
 WDR();
}
//ICC-AVR application builder : 2006-10-11 下午 02:37:20
// Target : M16
// Crystal: 7.3728Mhz


//TIMER1 initialize - prescale:1024
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 9Sec
// actual value:  9.000Sec (0.0%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0x02; //setup
 TCNT1L = 0xE1;
 OCR1AH = 0xFD;
 OCR1AL = 0x1F;
 OCR1BH = 0xFD;
 OCR1BL = 0x1F;
 ICR1H  = 0xFD;
 ICR1L  = 0x1F;
 TCCR1A = 0x00;
 TCCR1B = 0x05; //start Timer
}

#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
 //TIMER1 has overflowed
 TCNT1H = 0x02; //reload counter high value
 TCNT1L = 0xE1; //reload counter low value
 timer1_b=1;    //9秒计数
}


//For NEC Code
void timer2_init(void)
{
 TCCR2 = 0x00; //stop
 ASSR  = 0x00; //set async mode
 TCNT2 = 0xA2; //setup
 OCR2  = 0x5E;
 TCCR2 = 0x02; //start
}

//For RC5 Code
/*#pragma interrupt_handler timer2_ovf_isr:5
void timer2_ovf_isr(void)
{
 WDR();
 TCNT2 = 0xCD; //reload counter value
 ///////////////////////////////
 rc5_read();
 WDR();
 if(irda_b)//收到红外键后关闭250ms
 {TCCR2=0;
 }
// TCNT2 = 0xCD; //reload counter value
}
*/

//For NEC Code
#pragma interrupt_handler timer2_ovf_isr:5
void timer2_ovf_isr(void)
{
 TCNT2 = 0xA2; //reload counter value
 
 if(BNecFallEdge==1)
 {
  	TimeNecFallEdge++;
 }
 if(BHasReceiveNec==1)
 {
    NecRepeatCodeNum++;
 }
}


#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
 //external interupt on INT0

// if(BTvState)                		 	   		//是否允许进行遥控检测																				
// {
  	if(BHasReceiveNec==0)
	{
		BNecFallEdge=1;                             //有下降沿启动定时计数
		if(TimeNecFallEdge<=5)                      //判断是否为起始位 
		{
    	 	goto int0end;                           //检测到起始位
		}
		RTimeNecFallEdge=TimeNecFallEdge;           //读取时间间隔
		TimeNecFallEdge=0;
		Nec_Receive();
	}
	else
	{
	   RNecRepeatCodeNum=NecRepeatCodeNum;
	   NecRepeatCodeNum=0;
	   RepeatReceive();
	}   
// }

/* 
 if(BTvState)                		 	   		//是否允许进行遥控检测																				
 {
  	if(BHasReceiveNec==0)
	{
		BNecFallEdge=1;                             //有下降沿启动定时计数
		if(BTheFirstFallEdge==0)                      //判断是否为第一个下降沿,做为起始时间基准 
		{
    	 	BTheFirstFallEdge=1;
			TimeNecFallEdge=0;
			goto int0end;                           //检测到起始位
		}
		RTimeNecFallEdge=TimeNecFallEdge;           //读取时间间隔
		UDR=TimeNecFallEdge;
		TimeNecFallEdge=0;
//		Nec_Receive();
	}
	else
	{
	   RNecRepeatCodeNum=NecRepeatCodeNum;
	   NecRepeatCodeNum=0;
	   RepeatReceive();
	}   
 }
*/ 
 
int0end: ;
}

/**********************************************/
/*#######################################################*/
//读写e2prom  函数 EEPROMread(addr)
//                 EEPROMwrite(addr,value)
//直接使用即可
/*######################################################*/
void key_powercl(void)
{
    UDR=0x66;
	if(power_num==0)
	{
	   if(System_State==standby)  //启动8秒检测,是否为进入工厂状态
	   {
	         while(!(UCSRA&(1<<UDRE)));
			 UDR=0x67;
			                            //开机不用申请发送
	         if(fac_state==FAC_OPEN)    //是否为工厂状态,如是则开机
             {
               open_machine();
			   PORTB&=~(1<<EDID_RWEN);  //打开i2c参数使能
             }
			 else
			 {
		      s8_state=1;  
		      power_key_open_enable=1;//只执行一次开机代码 ,开机见主程序
			  Open_Panel_flag=1;
			 }
	   }
	   else                        //按键关机
	   {
	     while(!(UCSRA&(1<<UDRE)));
		 UDR=0x68;
		 
		 if(System_State==working)

⌨️ 快捷键说明

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