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

📄 timer.h

📁 客车车身控制模块 分为仪表部分 前控左 前控右 尾控和中控程序模块
💻 H
字号:
unsigned int	auto_lubricate=0;			//自动润滑计数器
unsigned char can_timer_count=0;    //CAN错位错位计数器,防冲突
unsigned char dog389_count=0;
unsigned char buzz_timer_count,temp11;
unsigned char doorwake_count;
////////////////////////////////////////////////
//***************时基函数初始化***************//
static void init_tbm(void){     //时基定时器
	//
	/*时基STOP模式是否使能及OSCCLK时钟源由CONFIG决定*/
	//OSCCLK为内部时钟
	TBCR_TBIE =1;				//.2  时基中断使能
	TBCR_TACK =1;				//.3  TBM中断标志清除
            
	TBCR_TBR0 =1;
	TBCR_TBR1 =0;
	TBCR_TBR2 =1;				//分频系数见下表
  /*TBR2	TBR1	TBR0	Divider Tap

  	0	  	0		  0	  	32,768	4,194,304
  	0	  	0	  	1	  	8192	  1,048,576
  	0	  	1	  	0	  	2048  	262144
  	0	  	1	  	1	  	128	  	16,384
  	1	  	0	  	0	  	64	  	8192
  	1	  	0	  	1	  	32	  	4096
  	1	  	1	  	0	   	16	  	2048
  	1	  	1	  	1	  	8	    	1024
  */
	TBCR_TBIF =1;				//.7 中断标志
	TBCR_TBON =1;				//.1 时基模块使能
}

//***************定时器初始化***************//
static void init_tim1(void){

	T1SC=0x46;		      //TIM1 64分频,中断使能
	T1MODH=0x30;	      //预置数12500×64×0.125=100000us=0.1s
	T1MODL=0xd4;
}
//***************时基定时器中断函数***************//
void interrupt 17 int_tbm(void)	//1.024ms
{
	dog389_count++;
	if(dog389_count==107)					//110MS
	{
		dog389_count=0;
		mc389_dog();					      //MC33389喂狗
		mc389_fault1=mc389_oper(0x50,0x00);		      //读CANH error
  	mc389_fault2=mc389_oper(0x5c,0x00);		      //读CANL error
	}
	//背光PWM调节---13个周期
	if((vcc_wakeup_flag || warn_wakeup_flag) && !gz60_sleep_flag)
	{          //非休眠
    if(bgtz_oth_flag)
    {
	    bgtz_cyc++;				 //++
	    if(bgtz_cyc>=13)
	    {
	      bgtz_cyc=0;
	      back_light_on;
	    }
	    if(bgtz_cyc==bgtz_1_timer)
	    { //--/++
	      back_light_off;
	    }
	}
	  /////////////////////////////////
	  if(comp_motor_flag)
	  {
      if(yqy_step_change!=yqy_stepall) yqy_step_oper();
      if(zqy_step_change!=zqy_stepall) zqy_step_oper();
      if(cs_step_change!=cs_stepall)   cs_step_oper();  
      if(zs_step_change!=zs_stepall)   zs_step_oper();  
      if(dy_step_change!=dy_stepall)   dy_step_oper();  
      if(ry_step_change!=ry_stepall)   ry_step_oper();  
      if(sw_step_change!=sw_stepall)   sw_step_oper();  
      if(jy_step_change!=jy_stepall)   jy_step_oper();
	  }
    //以上仪表操作
	}
    
  if(((vcc_wakeup_flag || warn_wakeup_flag) &&!gz60_sleep_flag) || door_wakeup_flag)
  {
    can_timer_count++;
    if(can_timer_count==49){//定时CAN仪表数据发送
      can_timer_count=0;
      can_tr(CAN_YB_C);
      
      if(++doorwake_count>=3)
      {
        doorwake_count=0;
        door_wakeup_flag=0;
        CAN_YB3.Bit.BIT6=0;
			  CAN_YB3.Bit.BIT7=0;
			  CAN_YB4.Bit.BIT0=0;
			  CAN_YB4.Bit.BIT1=0;
      }
			//门状态复位
			if(CAN_YB1.Bit.BIT6==0 && CAN_YB1.Bit.BIT7==0 && CAN_YB2.Bit.BIT0==0 && CAN_YB2.Bit.BIT1==0)
			  warn_wakeup_flag=0;
			//紧急状态复位
			
    }
    //仪表CAN发送
    else if(can_timer_count==48)
    {//ID122b
      //can_timer_count=0;
      //door_wakeup_flag=0;				 //门唤醒只发一次
			can_tr_jly();
    }
    ///记录仪CAN发送 
  }
  /////
	TBCR_TACK =1;				//清除中断标志	
}


//***************定时器中断函数***************//
void interrupt 6 int_tim1(void)								//100ms
{
    mc993_oper(0x00,0x00,0x00);
		mc993_ready_flag=1;

		//添加按钮功能
		if((vcc_wakeup_flag || warn_wakeup_flag) && !gz60_sleep_flag)
    { //非休眠
    		if(SP_status.Bit.BIT2)
    		{     //小计正控
    		  xjql_timer_count++;
    		  if(xjql_timer_count>16)
    		  {
    		    xjql_timer_count=0;
    		    xjql_b_flag=1;
    		  }
    		}
    		else
    		{
    		    xjql_timer_count=0;
    		}
    		//小计清零
    		if(SGH_status.Bit.BIT1 || SGH_status.Bit.BIT4)
      	{    //小灯一档或前雾灯有效时可调光
      	  if(light_first_on)
      	  {
        	  bgtz_timer_count=0;
        	  light_first_on=0;
      	  }
      	  
      		if(SP_status.Bit.BIT4)
      		{//背光正控
      		  bgtz_timer_count++;
      		  if(bgtz_timer_count>=120)   bgtz_timer_count=0;
      		}
      		//0...17 18...57 58...79 80...119//120*110ms=13.2s
      		//最亮2s->->4s->->关断2s->->4s->->最亮//12s
      		if(bgtz_timer_count<=17)
      		{
            back_light_on;bgtz_cyc=0;
            bgtz_oth_flag=0;
          }
          else if(bgtz_timer_count<=79 && bgtz_timer_count>=58)
          {
            back_light_off;bgtz_cyc=0;
            bgtz_oth_flag=0;
          }
          else
          {
            if(bgtz_timer_count>=18 && bgtz_timer_count<=57)
            {
              temp11=58-bgtz_timer_count;
            }
            else if(bgtz_timer_count>=80)
            {
              temp11=bgtz_timer_count-79;
            }
            
            temp11>>=2;bgtz_1_timer=temp11+2; //--
            bgtz_oth_flag=1;         
          }
      	}
        else
        {
          back_light_off;bgtz_oth_flag=0;light_first_on=1;
        }
    		//背光调整
    		if(SP_status.Bit.BIT3)
    		{       //时间正控--闭合
    		  sjtz_timer_out=0;
    		  if(sjtz_timer_count<20)
    		  {    //大于2S进调时状态
    		    sjtz_timer_count++;
    		  }
    		  else if(sjtz_timer_count==20)
    		  {
    		    sjtz_timer_count++;
    		    modify_time_flag=1;
    		    if(timer_button_change==0)
    		    {
    		      timer_button_change=1;
    		      timer_bit_change=1;
    		      timer_add_change=0;
    		    }
    		  }
    		}
    		else
    		{
    		  if(sjtz_timer_out<80)	 sjtz_timer_out++;
    		  else
    		  {
    		    modify_time_flag=0;
    		    timer_button_change=0;
    		  }
    		  //
    		  if(sjtz_timer_count>4 && sjtz_timer_count<20)
    		  {		 //
    		    if(modify_time_flag && timer_button_change==0)
    		    {
    		      timer_button_change=1;
    		      timer_bit_change=0;
    		      timer_add_change=1;
    		    }
    		  }
    		  sjtz_timer_count=0;
    		}
    		//test
    		/*CAN_YB5.Bit.BIT0=timer_add_change;
    		CAN_YB5.Bit.BIT1=timer_bit_change;
    		CAN_YB5.Bit.BIT2=timer_button_change;
    		CAN_YB5.Bit.BIT7=modify_time_flag;
    		*///时间调整
    		if((cs_last>110) || (zs_last>4300))
    		{     //蜂鸣器响
    		  if(buzz_timer_count>20) buzz_en=1;  
    		  else
    		  {
    		    buzz_en=0;buzz_timer_count++;
    		  }
    		}
        else
        {
          buzz_en=1;buzz_timer_count=0;
        }
        //////    		
    		
    	LED_count++;
    	if(LED_count==5)
    	{               //0.5s
    		LED_count=0;
        LED=!LED;								      //工作指示灯
        
    		if(safe_flag)
    		{                //安全带指示灯
    			led_safety=!led_safety;
    			led_safety_timer++;
    			if(led_safety_timer==20)		//LED初始化闪10s
    			{
    				led_safety_timer=0;
    				led_safety=1;
    				safe_flag=0;
    			}
    		}
    		//安全带灯闪10s
    		
    		if(led_warm==0){              //冷启动=冷启动灯亮
    			led_warm_timer++;
    			if(led_warm_timer==3600)		//30m
    			{
    				led_warm_timer=0;
    				led_warm=1;
    			}
    		}
    		//冷启动灯亮30分
    		
    		if(CAN_WKD2.Byte!=0)				//车速不为0,自动润滑2.5m/6h
    		{
    			auto_lubricate++;
    			if(auto_lubricate==43200)		//6h
    //			if(auto_lubricate==120)
    			{
    				CAN_YB3.Bit.BIT4=1;
    				CAN_YB3.Bit.BIT5=0;
    				//led_aulub=0;
    			}
    			else if(auto_lubricate>=43500)	//6h2m30s
    //			else if(auto_lubricate>=240)
    			{
    				auto_lubricate=0;
    				CAN_YB3.Bit.BIT4=0;
    				CAN_YB3.Bit.BIT5=0;
    				//led_aulub=1;
    			}
    		}
    		//运行6小时自动润滑一次
    				
    	}
    }
		//test
		//lcd_hour=CAN_YB6.Byte;
		//lcd_min=CAN_YB7.Byte>>1;
			
		if(++lcd_sec==600)
		{
			lcd_sec=0;
			if(jly_ture_flag){      //行使记录仪状态
			  jly_ture_flag=0;
			  jly_err_flag=0;
			}
			else{
			  jly_err_flag=1;
			}
			if(++lcd_min==60)
			{
				lcd_min=0;
				if(++lcd_hour==24)	lcd_hour=0;
			}
		}
		//时钟计数器
		
	//转向灯控制
	if(SGL_status.Bit.BIT6||SGL_status.Bit.BIT7||SGL_status.Bit.BIT5)
	{
		turn_timer_count++;
		if(turn_timer_count==3)
		{
			turn_timer_count=0;
			CAN_YB1.Bit.BIT7=turn_flag;
			CAN_YB2.Bit.BIT1=turn_flag;
			turn_flag=!turn_flag;
			if(SGL_status.Bit.BIT6||SGL_status.Bit.BIT5){
			  CAN_YB1.Bit.BIT6=turn_flag;
			  led_left=turn_flag;
			}
			if(SGL_status.Bit.BIT7||SGL_status.Bit.BIT5){
			  CAN_YB2.Bit.BIT0=turn_flag;
			  led_right=turn_flag;
			}
		}
		if(SGL_status.Bit.BIT5) warn_wakeup_flag=1;
	}
	else
	{
		turn_flag=0;
		led_left=1;
		led_right=1;
		turn_timer_count=0;
		CAN_YB1.Bit.BIT6=0;
		CAN_YB1.Bit.BIT7=0;
		CAN_YB2.Bit.BIT0=0;
		CAN_YB2.Bit.BIT1=0;
	}
	CAN_JLY1.Bit.BIT6=CAN_YB1.Bit.BIT6;
  CAN_JLY1.Bit.BIT7=CAN_YB1.Bit.BIT7;			 //左转向灯状态
  CAN_JLY2.Bit.BIT0=CAN_YB2.Bit.BIT0;
  CAN_JLY2.Bit.BIT1=CAN_YB2.Bit.BIT1;			 //右转向灯状态
	///////////////////
	T1SC_TOF=0;
}

⌨️ 快捷键说明

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