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

📄 main.c

📁 基于摩托罗拉(现在飞思卡尔)MC68HC908AZ60A系列单片机
💻 C
📖 第 1 页 / 共 2 页
字号:
TB0DSR0 =  lf_sta0;
TB0DSR1 =  lf_sta1;
TB0DSR2 =  lf_sta2;



TB0IDR0  =   0x48;      //id 赋值
TB0IDR1  =   0x00;
TB0IDR2  =   0x00;
TB0IDR3  =   0x00;


TB0TBPR  =  0x01;

CTFLG  =  0x01;        //启动发送
}



else if(emptytest1 !=0)
{
TB1DLR   =  0x03;

TB1DSR0  =  lf_sta0;
TB1DSR1  =  lf_sta1;
TB1DSR2  =  lf_sta2;


TB1IDR0  =   0x48;      //id 赋值
TB1IDR1  =   0x00;
TB1IDR2  =   0x00;
TB1IDR3  =   0x00;



TB1TBPR  =  0x01;

CTFLG  =  0x02;        //启动发送
}




else if(emptytest2 !=0)
{
TB2DLR   =  0x03;

TB2DSR0  =  lf_sta0;
TB2DSR1  =  lf_sta1;
TB2DSR2  =  lf_sta2;


TB2IDR0  =   0x48;      //id 赋值
TB2IDR1  =   0x00;
TB2IDR2  =   0x00;
TB2IDR3  =   0x00;


TB2TBPR  =  0x01;

CTFLG     =  0x04;      //启动发送
}


}





void time_delay(int n)
 {
  count=0;
  while(count<n) {;}      
   
  }




void transmit_SPI(unsigned char SPI_data)
{
  PTB_PTB2=0x0;
  PTB_PTB2=0x0;

  
   while(SPSCR_SPTE==0) {; }  
       SPDR=SPI_data;
  
   PTB_PTB2=0x0;PTB_PTB2=0x0;PTB_PTB2=0x0;PTB_PTB2=0x0;
  
  PTB_PTB2=0x1; 
}


void interrupt TimerInterrupt(void)
{ 
 __RESET_WATCHDOG();                   

count++;
CAN_timer_count++;
SPI_count++;

if(CAN_timer_count==5)
{
CAN_TransmitF1();               
CAN_timer_count=0;            
}


if(SPI_count==25)           
{
 transmit_SPI(0xB3);
 SPI_count=0;
}


if(lf_sta2_mir_regu ==0x2&&lf_sta2_mir_sel==0x1&& mir_x_position<MT)
  {mir_x_position++;}                    

if(lf_sta2_mir_regu ==0x1&&lf_sta2_mir_sel ==0x1&& mir_x_position>0)
  {mir_x_position--;}                                                    

if(lf_sta2_mir_regu ==0x3&&lf_sta2_mir_sel ==0x1&& mir_y_position<MT)
  {mir_y_position++;}                    

if(lf_sta2_mir_regu ==0x4&&lf_sta2_mir_sel ==0x1&& mir_y_position>0)
  {mir_y_position--;}                                                    


if(lfwin_up_300_enable==1) {lf_up_count++;}

if(lfwin_dn_300_enable==1) {lf_dn_count++;}               


if(lf_sta1_lf_moto_sta ==0x1 && window_position<WT) { window_position++;}    

if(lf_sta1_lf_moto_sta ==0x5 && window_position<WT) { window_position++;}     

if(lf_sta1_lf_moto_sta ==0x2 && window_position>0) { window_position--;}    

if(lf_sta1_lf_moto_sta ==0x6 && window_position>0) { window_position--;}      


if(PWM_flag2==0x4 && lock_moto_count<LT) {lock_moto_count++;}     

if(PWM_flag2==0x1 && lock_moto_count>0)  {lock_moto_count--;}         


TBCR_TACK =1;
}




/***********************MSCAN初始化子程序 ************************/

static void Init_MSCAN08(void){

CMCR0  = 0x01;   //CAN模块为软复位状态
CMCR1  = 0x00;

CBTR0  = 0x43;
CBTR1  = 0xA3;   //波特率为62.5K


CIDAC   = 0x00;   //接收滤波为32位滤波

CIDAR0  = 0x00;   //滤波使它接收一帧
CIDAR1  = 0x00;   //standard identifiers


CIDMR0  = 0xFF;
CIDMR1  = 0xFF;   //standard identifiers


CMCR0  = 0x00;   //把CAN模块设置为数据输入输出状态

CRIER  = 0x01;   //CAN接收中断使能
}




static void Init_GZ16(void)
{
  CONFIG2 =0x0D;        
  CONFIG1 =0x4A;       
  
  TBCR_TBIE =1;
  TBCR_TACK =1;
  TBCR_TBR0 =1;
  TBCR_TBR1 =1;
  TBCR_TBR2 =0;             
  TBCR_TBIF =0;                

  TBCR_TBON =1;            

Init_MSCAN08();   

EnableInterrupts; 
}



void main(void)
{
Init_GZ16();         

DDRB_DDRB2=1;

DDRC_DDRC6=1;
DDRB_DDRB5=1;
DDRB_DDRB3=1;            

DDRE_DDRE4=0;
DDRE_DDRE5=0;           

PTB_PTB2=0x1;

PTC_PTC6=0x0;
PTB_PTB5=0x0;
PTB_PTB3=0x0;


DDRB_DDRB6=1;
DDRB_DDRB7=1;      
             

PTB_PTB6=0x0;
PTB_PTB7=0x0;        
            


DDRC_DDRC4=1;
DDRC_DDRC5=1;         
             

PTC_PTC4=0x0;
PTC_PTC5=0x0;         
                 


DDRA_DDRA6=1;
DDRA_DDRA7=1;       
             



PTA_PTA6=0x0;
PTA_PTA7=0x0;       
               

DDRA_DDRA2=1;
DDRA_DDRA3=1;        
             


PTA_PTA2=0x0;
PTA_PTA3=0x0;



        
        T1MODH  = 0x03;
        T1MODL  = 0xe8;   


        T1CH0H  =  0x03 ;
        T1CH0L  =  0x20 ;  

        T1CH1H  =  0x03 ;
        T1CH1L  =  0x20 ;   
        

       
        T2MODH  = 0x03;
        T2MODL  = 0xe8;   


        T2CH0H  =  0x03 ;
        T2CH0L  =  0x20 ; 

        T2CH1H  =  0x03 ;
        T2CH1L  =  0x20 ;    
                          


 cent_sta0=0x14;
 cent_sta1=0x05;
 cent_sta2=0x00;
 cent_sta3=0x00;
 


 lf_sta0=0x08;
 lf_sta1=0x40;
 lf_sta2=0x00;
    
 rf_sta0=0x08;
 rf_sta1=0x42;  
             
 lr_sta0=0x08;
 lr_sta1=0x40; 
              
 rr_sta0=0x08;
 rr_sta1=0x40;                               
	  

 win_moto_sta0=0x00;
 win_moto_sta1=0x00;
	  

SPCR_SPE =1;  


SPSCR_SPR0=0;  
SPSCR_SPR1=0;  


PTB_PTB2=0x1;

transmit_SPI(0xB3);       
transmit_SPI(0x11);     
transmit_SPI(0x53);    
transmit_SPI(0x31);     
transmit_SPI(0x90);    
   

inlock1=cent_sta1_cent_inlock;
outlock1=cent_sta1_cent_outlock;
rflock1=rf_sta1_rf_cent_outlock;

speedlock1=cent_sta0_speed;




while(1)
{

PTB_PTB3=0x0 ;
PTB_PTB5=0x0 ;
PTC_PTC6=0x0 ;
lf_butt_sta1[0]=PTE&0x10;       
if(lf_butt_sta1[0]!=0) time_delay(5);                 
lf_butt_sta2[0]=PTE&0x10;        
if(lf_butt_sta1[0]==lf_butt_sta2[0])
  {  lf_button_sta[0]=lf_butt_sta1[0];
       if(lf_button_sta[0]!=0)
        { lf_sta2_mir_sel =0x1;}
       else { lf_sta2_mir_sel =0x2;}         
   }


PTB_PTB3=0x1 ;
PTB_PTB5=0x0 ;
PTC_PTC6=0x0 ;
lf_butt_sta1[1]=PTE&0x10;        
if(lf_butt_sta1[1]!=0) time_delay(5);                
lf_butt_sta2[1]=PTE&0x10;        
if(lf_butt_sta1[1]==lf_butt_sta2[1])
  {  lf_button_sta[1]=lf_butt_sta1[1];
      if(lf_button_sta[1]!=0 &&lf_button_sta[2]==0 && lf_button_sta[3]==0 && lf_button_sta[4]==0 )  
          lf_sta2_mir_regu =0x2;    
       else if( lf_button_sta[1]==0 && lf_button_sta[2]==0 && lf_button_sta[3]==0 && lf_button_sta[4]==0)  lf_sta2_mir_regu=0x0 ;              
  }   
        
 if( (lf_button_sta[1]!=0 &&lf_sta2_mir_sel==0x1&&mir_x_position<MT) && cent_sta0_fire ==0x2 )
   {
      if(PWM_flag3!=4)
    {  mirror_x_forward_start();              
      PWM_flag3=4;
  
       }
   
 
                                    
   }
 
 if(  ( lf_button_sta[1]==0  || lf_sta2_mir_sel!=0x1 || mir_x_position>=MT || cent_sta0_fire ==0x1 )  && PWM_flag3==4)
    {
      if(PWM_flag3!=8)
      { mirror_x_forward_stop();         
         PWM_flag3=8 ;}
  
                               
     }  
        

PTB_PTB3=0x0 ;
PTB_PTB5=0x1 ;
PTC_PTC6=0x0 ;
lf_butt_sta1[2]=PTE&0x10;          
if(lf_butt_sta1[2]!=0)  time_delay(5);                   
lf_butt_sta2[2]=PTE&0x10;         
if(lf_butt_sta1[2]==lf_butt_sta2[2])
  {  lf_button_sta[2]=lf_butt_sta1[2];
       if( lf_button_sta[1]==0 && lf_button_sta[2]!=0 && lf_button_sta[3]==0 && lf_button_sta[4]==0) 
         lf_sta2_mir_regu =0x1;             
      else if(  lf_button_sta[1]==0 && lf_button_sta[2]==0 && lf_button_sta[3]==0 && lf_button_sta[4]==0)  lf_sta2_mir_regu=0x0 ;               
   }      
          
 if( (lf_button_sta[2]!=0 && lf_sta2_mir_sel ==0x1&& mir_x_position>0) && cent_sta0_fire ==0x2 )
     {
       if(PWM_flag3!=1)
      { mirror_x_reverse_start();               
        PWM_flag3=1;

         }
     
                                   
      }

 if(  ( lf_button_sta[2]==0  || lf_sta2_mir_sel !=0x1  || mir_x_position<=0 || cent_sta0_fire ==0x1 )  &&PWM_flag3==1)
     {
     
      if(PWM_flag3!=2)
    { mirror_x_reverse_stop();           
       PWM_flag3=2;}
       
     }     

           

PTB_PTB3=0x1 ;
PTB_PTB5=0x1 ;
PTC_PTC6=0x0 ;
lf_butt_sta1[3]=PTE&0x10;         
if(lf_butt_sta1[3]!=0)    time_delay(5);                 
lf_butt_sta2[3]=PTE&0x10;          
if(lf_butt_sta1[3]==lf_butt_sta2[3])
  { lf_button_sta[3]=lf_butt_sta1[3];
       if( lf_button_sta[1]==0 && lf_button_sta[2]==0 && lf_button_sta[3]!=0 && lf_button_sta[4]==0)
        lf_sta2_mir_regu =0x3;              
     else if( lf_button_sta[1]==0 && lf_button_sta[2]==0 && lf_button_sta[3]==0 && lf_button_sta[4]==0)  lf_sta2_mir_regu=0x0 ;              
   }      
         
if( (lf_button_sta[3]!=0 &&lf_sta2_mir_sel ==0x1&& mir_y_position<MT) && cent_sta0_fire ==0x2)
     {
        if(PWM_flag4!=1)
      { mirror_y_forward_start();           
        PWM_flag4=1;
    
         }
     

                                 
     }
if(  ( lf_button_sta[3]==0  || lf_sta2_mir_sel !=0x1  || mir_y_position>=MT || cent_sta0_fire ==0x1 ) && PWM_flag4==1)
    {  if(PWM_flag4!=2)
      { mirror_y_forward_stop();          
         PWM_flag4=2;}
        
     }
          
          

PTB_PTB3=0x0 ;
PTB_PTB5=0x0 ;
PTC_PTC6=0x1 ;
lf_butt_sta1[4]=PTE&0x10;          
if(lf_butt_sta1[4]!=0)    time_delay(5);                  
lf_butt_sta2[4]=PTE&0x10;          
if(lf_butt_sta1[4]==lf_butt_sta2[4])
    {  lf_button_sta[4]=lf_butt_sta1[4];
       if( lf_button_sta[1]==0 && lf_button_sta[2]==0 && lf_button_sta[3]==0 && lf_button_sta[4]!=0) 
         lf_sta2_mir_regu =0x4;            
      else if(  lf_button_sta[1]==0 && lf_button_sta[2]==0 && lf_button_sta[3]==0 && lf_button_sta[4]==0)  lf_sta2_mir_regu=0x0 ;                 
     }     
           
if(  (lf_button_sta[4]!=0 &&lf_sta2_mir_sel ==0x1&& mir_y_position>0) && cent_sta0_fire ==0x2 )
   { if(PWM_flag4!=4)
     { mirror_y_reverse_start();       
       PWM_flag4=4;
  
       }
   
  
                           
    }      
          
if(  ( lf_button_sta[4]==0 || lf_sta2_mir_sel !=0x1 || mir_y_position<=0 || cent_sta0_fire ==0x1)&& PWM_flag4==4 )
   {   if(PWM_flag4!=8)
    { mirror_y_reverse_stop();         
      PWM_flag4 =8;}
        
   }
    


PTB_PTB3=0x1 ;
PTB_PTB5=0x0 ;
PTC_PTC6=0x1 ;
lf_butt_sta1[5]=PTE&0x10;          
if(lf_butt_sta1[5]!=0)   time_delay(5);                 
lf_butt_sta2[5]=PTE&0x10;          
if(lf_butt_sta1[5]==lf_butt_sta2[5])
  {  lf_button_sta[5]=lf_butt_sta1[5];
       if(lf_button_sta[5]!=0)   lfwin_dn_300_enable=1;          
       else  lfwin_dn_300_enable=0;                             
   }  
       
if(lf_dn_count>=75) { lfwin_dn_300_reach=1; lfwin_dn_300_enable=0;}
else lfwin_dn_300_reach=0;
     
if(lfwin_dn_300_reach==1 && lf_button_sta[5]!=0)            
  {lf_sta1_lf_moto_sta=0x2;}                        
if(lfwin_dn_300_reach==0 && lf_dn_count>0 && lf_button_sta[5]==0)
  {lf_sta1_lf_moto_sta=0x6;lf_dn_count=0;}             
if((lf_sta1_lf_moto_sta==0x2 && lf_button_sta[5]==0)&&cent_sta2_lf_manual_dn==0x0)
  {lf_sta1_lf_moto_sta=0x0;lf_dn_count=0;}         
if(lf_sta1_lf_moto_sta==0x6 && lfwin_dn_300_reach==1 && lf_button_sta[5]!=0)
  {lf_sta1_lf_moto_sta=0x2;}                    

if(lf_sta1_lf_moto_sta==0x6&& lfwin_up_300_reach==0 && lf_up_count>0 && lf_button_sta[6]==0)
  {lf_sta1_lf_moto_sta=0x5;}                   
if(lf_sta1_lf_moto_sta==0x6&& lfwin_up_300_reach==1 && lf_button_sta[6]!=0)
  {lf_sta1_lf_moto_sta=0x1;}              

           


if(  cent_sta2_lf_auto_dn==0x1   && window_position>0 )
 { lf_sta1_lf_moto_sta=0x6; 
   if(PWM_flag1!=1)
  { window_down_start();        
     PWM_flag1=1;}
   

   } 

if(  cent_sta2_lf_manual_dn==0x1   && window_position>0 )
 { lf_sta1_lf_moto_sta=0x2;
  if(PWM_flag1!=1)
  {window_down_start();        
    PWM_flag1=1;}
    

   } 
 
if( (cent_sta2_lf_auto_dn==0x0&&cent_sta2_lf_manual_dn==0x0)&& (lf_sta1_lf_moto_sta==0x2||lf_sta1_lf_moto_sta==0x6) && window_position>0  )
   { 
   if(PWM_flag1!=1)
  { window_down_start();        
    PWM_flag1=1;}
   }
                           
      
if( lf_sta1_lf_moto_sta==0x0 && cent_sta2_lf_auto_up==0x0 && cent_sta2_lf_manual_up==0x0 && cent_sta2_lf_auto_dn==0x0 && cent_sta2_lf_manual_dn==0x0 && PWM_flag1==1 )      
    { 
     if(PWM_flag1!=2)
    { window_down_stop();        
     PWM_flag1=2;}
    }
                       
           
if(  window_position<=0 )       
   {  if( lf_sta1_lf_moto_sta!=0x1 && lf_sta1_lf_moto_sta!=0x5) 
     { lf_sta1_lf_moto_sta=0x3;
     if(PWM_flag1==1)
     { window_down_stop(); 
       PWM_flag1=2;}
      } 

   }          


PTB_PTB3=0x0 ;
PTB_PTB5=0x1 ;
PTC_PTC6=0x1 ;
lf_butt_sta1[6]=PTE&0x10;           
if(lf_butt_sta1[6]!=0)   time_delay(5);                  
lf_butt_sta2[6]=PTE&0x10;         
if(lf_butt_sta1[6]==lf_butt_sta2[6])
  {  lf_button_sta[6]=lf_butt_sta1[6];
       if(lf_button_sta[6]!=0)   lfwin_up_300_enable=1;           
       else  lfwin_up_300_enable=0;                               
   }  
       
if(lf_up_count>=75) { lfwin_up_300_reach=1; lfwin_up_300_enable=0;}
else lfwin_up_300_reach=0;
     
if(lfwin_up_300_reach==1 && lf_button_sta[6]!=0)            
  {lf_sta1_lf_moto_sta=0x1;}                          
if(lfwin_up_300_reach==0 && lf_up_count>0 && lf_button_sta[6]==0)
  {lf_sta1_lf_moto_sta=0x5;lf_up_count=0;}             
if((lf_sta1_lf_moto_sta==0x1 && lf_button_sta[6]==0)&& cent_sta2_lf_manual_up==0x0)
  {lf_sta1_lf_moto_sta=0x0;lf_up_count=0;}          
if(lf_sta1_lf_moto_sta==0x5 && lfwin_up_300_reach==1 && lf_button_sta[6]!=0)
  {lf_sta1_lf_moto_sta=0x1;}                      

if(lf_sta1_lf_moto_sta==0x5&& lfwin_dn_300_reach==0 && lf_dn_count>0  && lf_button_sta[5]==0)
  {lf_sta1_lf_moto_sta=0x6;}                  
if(lf_sta1_lf_moto_sta==0x5&& lfwin_dn_300_reach==1 && lf_button_sta[5]!=0)
  {lf_sta1_lf_moto_sta=0x2;}                 


if(  cent_sta2_lf_auto_up==0x1   && window_position<WT )
 {lf_sta1_lf_moto_sta=0x5;
 
 if(PWM_flag1!=4)
 { window_up_start();        
  PWM_flag1=4;}

   } 

if(  cent_sta2_lf_manual_up==0x1  && window_position<WT )
 {lf_sta1_lf_moto_sta=0x1;
 if(PWM_flag1!=4)
  {window_up_start();         
   PWM_flag1=4;}

   } 
 
if(  (cent_sta2_lf_auto_up==0x0&&cent_sta2_lf_manual_up==0x0)&& (lf_sta1_lf_moto_sta==0x1||lf_sta1_lf_moto_sta==0x5) && window_position<WT  )
   { 
  if(PWM_flag1!=4)
  { window_up_start();       
   PWM_flag1=4;}
 
   }
                           
      
if( lf_sta1_lf_moto_sta==0x0 && cent_sta2_lf_auto_up==0x0 && cent_sta2_lf_manual_up==0x0 && cent_sta2_lf_auto_dn==0x0 && cent_sta2_lf_manual_dn==0x0 && PWM_flag1==4 )      
    { 
     if(PWM_flag1!=8)
   {  window_up_stop();        
      PWM_flag1=8;}

    }
                       
           
if(  window_position>=WT )       
   {           
    if( lf_sta1_lf_moto_sta!=2 &&  lf_sta1_lf_moto_sta!=6)
     { lf_sta1_lf_moto_sta=0x4;
     if(PWM_flag1==4)
      {window_up_stop(); 
      PWM_flag1=8;}
       }  
     

   }



lf_butt_sta1[7]=PTE&0x20;         
if(lf_butt_sta1[7]!=0)   time_delay(5);               
lf_butt_sta2[7]=PTE&0x20;         
if(lf_butt_sta1[7]==lf_butt_sta2[7])
  {  lf_button_sta[7]= lf_butt_sta1[7];
       if(lf_button_sta[7]!=0)    lf_sta0_lf_sta=0x1;       
       else  lf_sta0_lf_sta=0x2;                            
   }




if(lock_moto_count<=0  )         
       { 
            if ( PWM_flag2==1)
             {
                   open_lock_stop();                 
                     PWM_flag2=2; 
              }       
               lf_sta1_lf_lock_sta=0x1;
         
        } 
                     
         
 
 
if(lock_moto_count>=LT )           
       {  if(PWM_flag2==4)
            { 
                 close_lock_stop();                
                 PWM_flag2=8;
             }    
              lf_sta1_lf_lock_sta=0x2;
             
        }

 }           


}          



























⌨️ 快捷键说明

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