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

📄 main.c

📁 基于摩托罗拉(现在飞思卡尔)MC68HC908AZ60A系列单片机
💻 C
📖 第 1 页 / 共 2 页
字号:
/**********************************************/

/**file name: left_front node program        **/

/**Author :Xiaoming Li                       **/

/**date/time: 12.3.2004                      **/

/**company: Tongji university                **/

/**********************************************/


#include <MC68HC908GZ16.H>

#include <hidef.h>

#include <signal_group.h>

#define WT 5000       
#define LT 2000          
#define MT 4000          


unsigned char CAN_timer_count=0;           

unsigned int count=0;                  

unsigned char SPI_count=0;

unsigned char lf_up_count=0;              
unsigned char lf_dn_count=0;                
             
unsigned int window_position=0;            

unsigned int mir_x_position=2000;
unsigned int mir_y_position=2000;         

unsigned int lock_moto_count=0;        

unsigned char  lf_button_sta[8]={0,0,0,0,0,0,0,0};
unsigned char  lf_butt_sta1[8]={0,0,0,0,0,0,0,0};
unsigned char  lf_butt_sta2[8]={0,0,0,0,0,0,0,0};                                                          


unsigned char inlock1=0;
unsigned char inlock2=0;
unsigned char inlock=0;

unsigned char outlock1=0;
unsigned char outlock2=0;
unsigned char outlock=0;

unsigned char rflock1=0;
unsigned char rflock2=0;
unsigned char rflock=0;

unsigned char judge=0;

unsigned char speedlock=0;
unsigned char speedlock1=0;
unsigned char speedlock2=0;

unsigned char PWM_flag1=0;     
unsigned char PWM_flag2=0;
unsigned char PWM_flag3=0;     
unsigned char PWM_flag4=0;

unsigned char T2_CH0_flag=0;
unsigned char T2_CH1_flag=0;




void TIM2_CH0_start(void)
{ 
        T2SC_TSTOP =1;
        T2SC_TRST =1;     
     
 
      
        T2SC0_MS0A =1;
        T2SC0_MS0B =0;     
        T2SC0_ELS0A =0;           
        T2SC0_ELS0B =1;
        
        T2SC1_MS1A =1;    
        T2SC1_ELS1A =0;
        T2SC1_ELS1B =1;
        
        
        T2SC0_MS0B =1;
        T2SC1_TOV1 =0;   
           
        T2SC0_TOV0 =1;
        T2SC0_CH0MAX=0;
        
        T2SC_TSTOP =0; 
  }     

void TIM2_CH0_stop(void)
{
  T2SC_TSTOP =1;
  T2SC_TRST =1;
  
        T2SC0_MS0A =1;
        T2SC0_ELS0A =0;           
        T2SC0_ELS0B =1;
        
        T2SC1_MS1A =1;    
        T2SC1_ELS1A =0;
        T2SC1_ELS1B =1;
  
  T2SC1_TOV1 =0;
  T2SC0_TOV0 =0;
 
  T2SC_TSTOP =0;
}


void TIM2_CH1_start(void)
{
        T2SC_TSTOP =1;
        T2SC_TRST =1;     
     

     
        
         T2SC0_MS0A =1;
         T2SC0_MS0B =1;    
         T2SC0_ELS0A =0;           
         T2SC0_ELS0B =1;
        
        T2SC1_MS1A =1;    
        T2SC1_ELS1A =0;
        T2SC1_ELS1B =1;
        
        T2SC0_MS0B =0; 
        T2SC0_TOV0 =1;   
        T2SC0_CH0MAX=1;
           
        T2SC1_TOV1 =1;
        T2SC1_CH1MAX=0;
        
        T2SC_TSTOP =0;   
 }  




void TIM2_CH1_stop(void)
{
  T2SC_TSTOP =1;
  T2SC_TRST =1;
  
        T2SC0_MS0A =1;
      
        T2SC0_ELS0A =0;           
        T2SC0_ELS0B =1;
        
        T2SC1_MS1A =1;    
        T2SC1_ELS1A =0;
        T2SC1_ELS1B =1;
 
  T2SC0_MS0B =0;      
  T2SC0_TOV0 =0;
  T2SC0_CH0MAX=1;
  
  T2SC1_TOV1 =1;
  T2SC1_CH1MAX=1;
 
   T2SC_TSTOP =0;
 }  




void  open_lock_start(void)
{       PTA_PTA6=0;  
        PTA_PTA7=1;
        
        T1SC_TSTOP =1;
        T1SC_TRST =1;     
     
      
        T1SC0_MS0A =1;
        T1SC0_MS0B =0;   
        T1SC0_ELS0A =0;           
        T1SC0_ELS0B =1;
        
        T1SC1_MS1A =1;    
        T1SC1_ELS1A =0;
        T1SC1_ELS1B =1;
        
        
        T1SC0_MS0B =1;
        T1SC1_TOV1 =0;   
           
        T1SC0_TOV0 =1;
        T1SC0_CH0MAX=0;
        
        T1SC_TSTOP =0;       
        
}        
        

        


 
void open_lock_stop(void)
{ 
  PTA_PTA6=1 ;
  PTA_PTA7=0 ;
 
  T1SC_TSTOP =1;
  T1SC_TRST =1;
  
        T1SC0_MS0A =1;
        T1SC0_ELS0A =0;           
        T1SC0_ELS0B =1;
        
        T1SC1_MS1A =1;    
        T1SC1_ELS1A =0;
        T1SC1_ELS1B =1;
  
  T1SC1_TOV1 =0;
  T1SC0_TOV0 =0;
 
  T1SC_TSTOP =0;
}





 void close_lock_start(void)
 {      PTA_PTA6=0; 
        PTA_PTA7=1; 
 
        T1SC_TSTOP =1;
        T1SC_TRST =1;    
     
        
         T1SC0_MS0A =1;
         T1SC0_MS0B =1;    
         T1SC0_ELS0A =0;           
         T1SC0_ELS0B =1;
        
        T1SC1_MS1A =1;    
        T1SC1_ELS1A =0;
        T1SC1_ELS1B =1;
        
        T1SC0_MS0B =0; 
        T1SC0_TOV0 =1;   
        T1SC0_CH0MAX=1;
           
        T1SC1_TOV1 =1;
        T1SC1_CH1MAX=0;
        
        T1SC_TSTOP =0;  
}




void close_lock_stop(void)
{ 
  PTA_PTA6=1;
  PTA_PTA7=0;

  T1SC_TSTOP =1;
  T1SC_TRST =1;
  
        T1SC0_MS0A =1;
      
        T1SC0_ELS0A =0;           
        T1SC0_ELS0B =1;
        
        T1SC1_MS1A =1;  
        T1SC1_ELS1A =0;
        T1SC1_ELS1B =1;
 
  T1SC0_MS0B =0;      
  T1SC0_TOV0 =0;
  T1SC0_CH0MAX=1;
  
  T1SC1_TOV1 =1;
  T1SC1_CH1MAX=1;
 
   T1SC_TSTOP =0;       
 
   
 
}






void window_up_start(void)
{       PTA_PTA2=0;  
        PTA_PTA3=1;
     
        T2SC_TSTOP =1;
        T2SC_TRST =1;      
     
  
    
        T2SC0_MS0A =1;
        T2SC0_MS0B =0;     
        T2SC0_ELS0A =0;           
        T2SC0_ELS0B =1;
        
        T2SC1_MS1A =1;    
        T2SC1_ELS1A =0;
        T2SC1_ELS1B =1;
        
        
        T2SC0_MS0B =1;
        T2SC1_TOV1 =0;   
           
        T2SC0_TOV0 =1;
        T2SC0_CH0MAX=0;
        
        T2SC_TSTOP =0; 
       
       
  
 }
 
 

 
void window_up_stop(void)
{ 
  PTA_PTA2=1;
  PTA_PTA3=0;

  T2SC_TSTOP =1;
  T2SC_TRST =1;
  
        T2SC0_MS0A =1;
        T2SC0_ELS0A =0;           
        T2SC0_ELS0B =1;
        
        T2SC1_MS1A =1;     
        T2SC1_ELS1A =0;
        T2SC1_ELS1B =1;
  
  T2SC1_TOV1 =0;
  T2SC0_TOV0 =0;
 
  T2SC_TSTOP =0;



}





 
 void window_down_start(void)
 {    
        PTA_PTA2=0;  
        PTA_PTA3=1;
 
        T2SC_TSTOP =1;
        T2SC_TRST =1;     
 
        
  
         T2SC0_MS0A =1;
         T2SC0_MS0B =1;    
         T2SC0_ELS0A =0;           
         T2SC0_ELS0B =1;
        
        T2SC1_MS1A =1;   
        T2SC1_ELS1A =0;
        T2SC1_ELS1B =1;
        
        T2SC0_MS0B =0; 
        T2SC0_TOV0 =1;   
        T2SC0_CH0MAX=1;
           
        T2SC1_TOV1 =1;
        T2SC1_CH1MAX=0;
        
        T2SC_TSTOP =0;   
     
      
      
}




void window_down_stop(void)
{ PTA_PTA2=1;
  PTA_PTA3=0;

  T2SC_TSTOP =1;
  T2SC_TRST =1;
  
        T2SC0_MS0A =1;
     
        T2SC0_ELS0A =0;           
        T2SC0_ELS0B =1;
        
        T2SC1_MS1A =1;    
        T2SC1_ELS1A =0;
        T2SC1_ELS1B =1;
 
  T2SC0_MS0B =0;      
  T2SC0_TOV0 =0;
  T2SC0_CH0MAX=1;
  
  T2SC1_TOV1 =1;
  T2SC1_CH1MAX=1;
 
   T2SC_TSTOP =0;  


}
 




void mirror_x_forward_start(void)
{       PTB_PTB6=0;
        PTB_PTB7=1;  
      if( T2_CH0_flag!=1)
      { TIM2_CH0_start();
        T2_CH0_flag=1;}
 }
 
 

 

void mirror_x_forward_stop(void)
{ 
  PTB_PTB6=1;
  PTB_PTB7=0;

 if( T2_CH0_flag!=2)
   {TIM2_CH0_stop();
    T2_CH0_flag=2;} 

}






 void mirror_x_reverse_start(void)
 {      PTB_PTB6=0;
        PTB_PTB7=1;  
  if( T2_CH1_flag!=1)      
    { TIM2_CH1_start();
     T2_CH1_flag=1;}  
   
}





void mirror_x_reverse_stop(void)
{ PTB_PTB6=1;
  PTB_PTB7=0;
if( T2_CH1_flag!=2) 
   {TIM2_CH1_stop();
    T2_CH1_flag=2;} 
}





void mirror_y_forward_start(void)
{       PTC_PTC4=0;
        PTC_PTC5=1;  
     
        if( T2_CH0_flag!=1)
      { TIM2_CH0_start();
        T2_CH0_flag=1;}
       
 }
 
 

 

void mirror_y_forward_stop(void)
{ 
  PTC_PTC4=1;
  PTC_PTC5=0;
 if( T2_CH0_flag!=2)
   {TIM2_CH0_stop();
    T2_CH0_flag=2;} 


}





 
 void mirror_y_reverse_start(void)
 {      PTC_PTC4=0;
        PTC_PTC5=1;  
 
       if( T2_CH1_flag!=1)      
    { TIM2_CH1_start();
       T2_CH1_flag=1;}  
}




void mirror_y_reverse_stop(void)
{ PTC_PTC4=1;
  PTC_PTC5=0;
  
if( T2_CH1_flag!=2) 
   {TIM2_CH1_stop();
    T2_CH1_flag=2;} 
    
}




 

/******************** CAN接收中断子程序**********************/
void interrupt CAN_Receive(void)

{
unsigned char doors_closed;

volatile unsigned char idr0test;

idr0test = RBIDR0;

if(idr0test==0x50)          //接受中央控制器数据
{
cent_sta0=RBDSR0;
cent_sta1=RBDSR1;
cent_sta2=RBDSR2;
cent_sta3=RBDSR3;
}




else if(idr0test == 0x44)     //接受右前门数据
{
rf_sta0=RBDSR0;
rf_sta1=RBDSR1;
}

else if(idr0test == 0x42)     //接受左后门数据
{
lr_sta0=RBDSR0;
lr_sta1=RBDSR1;
}

else if(idr0test == 0x41)     //接受左后门数据
{
rr_sta0=RBDSR0;
rr_sta1=RBDSR1;
}


CRFLG = 0x01;          //receive buffer full


   
inlock2=cent_sta1_cent_inlock;
outlock2=cent_sta1_cent_outlock;
rflock2=rf_sta1_rf_cent_outlock;


speedlock2=cent_sta0_speed;


if(inlock2==inlock1)  {  inlock=0;   inlock1=inlock2;   }
if(inlock2!=inlock1)  {  inlock=1;   judge=inlock2;  inlock1=inlock2;   }


if(outlock2==outlock1)  {  outlock=0;   outlock1=outlock2;   }
if(outlock2!=outlock1)  {  outlock=1;   judge=outlock2;  outlock1=outlock2;   }


if(rflock2==rflock1)  {  rflock=0;   rflock1=rflock2;   }
if(rflock2!=rflock1)  {  rflock=1;   judge=rflock2;      rflock1=rflock2;   }



if( speedlock2==speedlock1) {speedlock=0;  speedlock1=speedlock2; }
if( speedlock2!=speedlock1) {speedlock=1;  speedlock1=speedlock2; }


doors_closed = ( lf_sta0_lf_sta==0x1 && rf_sta0_rf_sta==0x1&& lr_sta0_lr_sta==0x1&& rr_sta0_rr_sta==0x1 );



if( ( inlock==1||outlock==1||rflock==1) && lf_sta1_lf_lock_sta==0x2 && lf_sta0_lf_sta==0x1 )          
   {  if( judge==1 )
       { 
          if(PWM_flag2!=1)
        { open_lock_start();    
          PWM_flag2=1;}
       
        } 
    }       
                                       
      
   
if( ( inlock==1||outlock==1||rflock==1)  && lf_sta1_lf_lock_sta==0x1  && lf_sta0_lf_sta==0x1 && cent_sta0_crash==1&& doors_closed ==1 )        
  {    if( judge ==2)
      { 
      
         if(PWM_flag2!=4)
       { close_lock_start();   
          PWM_flag2=4;}
      
    
                                  
       }        
   }       

if( cent_sta0_crash==2  && lf_sta1_lf_lock_sta==0x2 && lf_sta0_lf_sta==0x1 )   
 {       if(PWM_flag2!=1)  
       { open_lock_start();    
         PWM_flag2=1;}
 
   } 




if ( speedlock==1  && cent_sta0_crash==1  && lf_sta1_lf_lock_sta==0x1 && lf_sta0_lf_sta==0x1 && doors_closed ==1 )   
 { if(speedlock2==1 )
       { 
          if(PWM_flag2!=4)
          { close_lock_start();    
            PWM_flag2=4;} 
       
        
         }                       
  }
     


if (  speedlock==1  && lf_sta1_lf_lock_sta==0x2 && lf_sta0_lf_sta==0x1 ) 
 { if( speedlock2==0  && cent_sta1_cent_inlock ==0x1 && cent_sta1_cent_outlock ==0x1 && rf_sta1_rf_cent_outlock ==0x1 )
      { 
         if(PWM_flag2!=1)  
       { open_lock_start();    
         PWM_flag2=1;}

       }                   
   } 



if ( (cent_sta0_fire==1 && (cent_sta1_cent_outlock==2 || rf_sta1_rf_cent_outlock==2) ) && cent_sta0_crash==1   && lf_sta1_lf_lock_sta==0x1 && lf_sta0_lf_sta==0x1&& doors_closed ==1 )   
  {  
       if(PWM_flag2!=4)
       { close_lock_start();   
        PWM_flag2=4;}
       
       if(window_position<WT) 
        { 
          if(PWM_flag1!=4)
           { window_up_start();
             PWM_flag1=4;}
         }         

                               
  }

              

}                          //end of CAN_Receive funtion



/********************* CAN发送子程序第一帧**********************/

void CAN_TransmitF1(void){

unsigned char emptytest;
unsigned char emptytest0;
unsigned char emptytest1;
unsigned char emptytest2;

emptytest = CTFLG;

emptytest0 = emptytest&0x01;        //判断接受区为空
emptytest1 = emptytest&0x02;
emptytest2 = emptytest&0x04;

if(emptytest0 !=0)
{
TB0DLR   =  0x03;

⌨️ 快捷键说明

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