main.c

来自「基于摩托罗拉(现在飞思卡尔)MC68HC908AZ60A系列单片机」· C语言 代码 · 共 1,308 行 · 第 1/2 页

C
1,308
字号
  }


}                           //end of CAN_Receive function



/**************************** 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   =  0x02;

TB0DSR0 =  rf_sta0;
TB0DSR1 =  rf_sta1;



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


TB0TBPR  =  0x01;

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



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

TB1DSR0  =  rf_sta0;
TB1DSR1  =  rf_sta1;


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


TB1TBPR  =  0x01;

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




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

TB2DSR0  =  rf_sta0;
TB2DSR1  =  rf_sta1;



TB2IDR0  =   0x44;      //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==0x2 && mirror_x_position<MT)
  {mirror_x_position++;}                    

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

if(lf_sta2_mir_regu ==0x3&&lf_sta2_mir_sel ==0x2 && mirror_y_position<MT)
  {mirror_y_position++;}                    

if(lf_sta2_mir_regu ==0x4&&lf_sta2_mir_sel ==0x2 && mirror_y_position>0)
  {mirror_y_position--;}                                                  


if(rfwin_up_300_enable==1) {rf_up_count++;}

if(rfwin_dn_300_enable==1) {rf_dn_count++;}            


if(  (rf_sta1_rf_moto_sta ==0x1||rf_sta1_rf_moto_sta ==0x5) && window_position<WT) { window_position++;}     


if(  (rf_sta1_rf_moto_sta ==0x2 ||rf_sta1_rf_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=0x0;

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 ;
rf_butt_sta1[0]=PTE&0x10;       
if(rf_butt_sta1[0]!=0)   time_delay(5);                
rf_butt_sta2[0]=PTE&0x10;          
if(rf_butt_sta1[0]==rf_butt_sta2[0])
  {  rf_button_sta[0]=rf_butt_sta1[0];
       if(rf_button_sta[0]!=0)   rfwin_dn_300_enable=1;         
       else  rfwin_dn_300_enable=0;                               
   }  
       
if(rf_dn_count>=75) { rfwin_dn_300_reach=1; rfwin_dn_300_enable=0;}
else rfwin_dn_300_reach=0;
     
if(rfwin_dn_300_reach==1 && rf_button_sta[0]!=0)            
  {rf_sta1_rf_moto_sta=0x2;}                         
if(rfwin_dn_300_reach==0&& rf_dn_count>0 && rf_button_sta[0]==0)
  {rf_sta1_rf_moto_sta=0x6;rf_dn_count=0;}             
if((rf_sta1_rf_moto_sta==0x2 && rf_button_sta[0]==0)&&cent_sta2_rf_manual_dn==0x0)
  {rf_sta1_rf_moto_sta=0x0;rf_dn_count=0;}          
if(rf_sta1_rf_moto_sta==0x6 && rfwin_dn_300_reach==1 && rf_button_sta[0]!=0)
  {rf_sta1_rf_moto_sta=0x2;}                       

if(rf_sta1_rf_moto_sta==0x6&& rfwin_up_300_reach==0 && rf_up_count>0 && rf_button_sta[1]==0)
  {rf_sta1_rf_moto_sta=0x5;}                 
if(rf_sta1_rf_moto_sta==0x6&& rfwin_up_300_reach==1 && rf_button_sta[1]!=0)
  {rf_sta1_rf_moto_sta=0x1;}             


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

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

   } 
 
if( (cent_sta2_rf_auto_dn==0x0&&cent_sta2_rf_manual_dn==0x0)&& (rf_sta1_rf_moto_sta==0x2||rf_sta1_rf_moto_sta==0x6) && window_position>0  )
   { 
   if(PWM_flag1!=1)
  { window_down_start();        
    PWM_flag1=1;}
   }
                           
      
if( rf_sta1_rf_moto_sta==0x0 && cent_sta2_rf_auto_up==0x0 && cent_sta2_rf_manual_up==0x0 && cent_sta2_rf_auto_dn==0x0 && cent_sta2_rf_manual_dn==0x0 && PWM_flag1==1 )      
    { 
     if(PWM_flag1!=2)
    { window_down_stop();        
     PWM_flag1=2;}
    }
                       
           
if(  window_position<=0 )       
   {  if( rf_sta1_rf_moto_sta!=0x1 && rf_sta1_rf_moto_sta!=0x5) 
     { rf_sta1_rf_moto_sta=0x3;
     if(PWM_flag1!=2)
     { window_down_stop(); 
       PWM_flag1=2;}
      }   

   }  
        


PTB_PTB3=0x1 ;
PTB_PTB5=0x0 ;
PTC_PTC6=0x0 ;
rf_butt_sta1[1]=PTE&0x10;        
if(rf_butt_sta1[1]!=0)   time_delay(5);                
rf_butt_sta2[1]=PTE&0x10;        
if(rf_butt_sta1[1]==rf_butt_sta2[1])
  {  rf_button_sta[1]=rf_butt_sta1[1];
       if(rf_button_sta[1]!=0)   rfwin_up_300_enable=1;         
       else  rfwin_up_300_enable=0;                             
   }  
       
if(rf_up_count>=75) { rfwin_up_300_reach=1; rfwin_up_300_enable=0;}
else rfwin_up_300_reach=0;
     
if(rfwin_up_300_reach==1 && rf_button_sta[1]!=0)            
  {rf_sta1_rf_moto_sta=0x1;}                          
if(rfwin_up_300_reach==0&& rf_up_count>0  && rf_button_sta[1]==0)
  {rf_sta1_rf_moto_sta=0x5;rf_up_count=0;}           
if((rf_sta1_rf_moto_sta==0x1 && rf_button_sta[1]==0)&& cent_sta2_rf_manual_up==0x0)
  {rf_sta1_rf_moto_sta=0x0;rf_up_count=0;}          
if(rf_sta1_rf_moto_sta==0x5 && rfwin_up_300_reach==1 && rf_button_sta[1]!=0)
  {rf_sta1_rf_moto_sta=0x1;}                   

if(rf_sta1_rf_moto_sta==0x5&& rfwin_dn_300_reach==0 && rf_dn_count>0 && rf_button_sta[0]==0)
  {rf_sta1_rf_moto_sta=0x6;}                  
if(rf_sta1_rf_moto_sta==0x5&& rfwin_dn_300_reach==1 && rf_button_sta[0]!=0)
  {rf_sta1_rf_moto_sta=0x2;}                  


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

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

   } 
 
if( (cent_sta2_rf_auto_up==0x0&&cent_sta2_rf_manual_up==0x0)&& (rf_sta1_rf_moto_sta==0x1||rf_sta1_rf_moto_sta==0x5) && window_position<WT  )
   { 
  if(PWM_flag1!=4)
  { window_up_start();    
   PWM_flag1=4;}
   }
                           
      
if( rf_sta1_rf_moto_sta==0x0 && cent_sta2_rf_auto_up==0x0 && cent_sta2_rf_manual_up==0x0 && cent_sta2_rf_auto_dn==0x0 && cent_sta2_rf_manual_dn==0x0 && PWM_flag1==4 )      
    { 
     if(PWM_flag1!=8)
   {  window_up_stop();        
      PWM_flag1=8;}
    }
                       
           
if(  window_position>=WT )       
   {           
    if( rf_sta1_rf_moto_sta!=2 &&  rf_sta1_rf_moto_sta!=6)
     { rf_sta1_rf_moto_sta=0x4;
     if(PWM_flag1!=8)
      {window_up_stop(); 
      PWM_flag1=8;}
       } 
     
   }

 
 
PTB_PTB3=0x0 ;
PTB_PTB5=0x1 ;
PTC_PTC6=0x0 ;
rf_butt_sta1[2]=PTE&0x10;        
if(rf_butt_sta1[2]!=0)   time_delay(5);              
rf_butt_sta2[2]=PTE&0x10;        
if(rf_butt_sta1[2]==rf_butt_sta2[2])
  {  rf_button_sta[2]= rf_butt_sta1[2];
       if(rf_button_sta[2]!=0)    rf_sta0_rf_sta=0x1;       
       else  rf_sta0_rf_sta=0x2;                            
   }



PTB_PTB3=0x1 ;
PTB_PTB5=0x1 ;
PTC_PTC6=0x0 ;
rf_butt_sta1[3]=PTE&0x10;       
if(rf_butt_sta1[3]!=0)  time_delay(5);                
rf_butt_sta2[3]=PTE&0x10;        
if(rf_butt_sta1[3]==rf_butt_sta2[3]) 
  {  rf_button_sta[3]= rf_butt_sta1[3];
       if(rf_button_sta[3]!=0)  rf_sta1_rf_cent_outlock =0x1;       
       else  rf_sta1_rf_cent_outlock =0x2;                 
   }                
  



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



 if( (lf_sta2_mir_regu ==0x2&&lf_sta2_mir_sel==0x2&&mirror_x_position<MT) &&  cent_sta0_fire ==0x2)
   { 
     if(PWM_flag3!=4)
    {  mirror_x_forward_start();            
      PWM_flag3=4; }
                    
   }
   
 if(  ( lf_sta2_mir_regu==0x0  || lf_sta2_mir_sel!=0x2  || mirror_x_position>=MT ||  cent_sta0_fire ==0x1) && PWM_flag3==4)
    { 
      if(PWM_flag3!=8)
      { mirror_x_forward_stop();        
         PWM_flag3=8 ;}
     }  
        
 if(  (lf_sta2_mir_regu ==0x1&& lf_sta2_mir_sel ==0x2&& mirror_x_position>0) && cent_sta0_fire ==0x2 )
     { 
      if(PWM_flag3!=1)
      { mirror_x_reverse_start();             
        PWM_flag3=1; }
        
      }

 if(  ( lf_sta2_mir_regu==0x0  || lf_sta2_mir_sel!=0x2  || mirror_x_position<=0 || cent_sta0_fire ==0x1 )  &&PWM_flag3==1)
     { 
       if(PWM_flag3!=2)
    { mirror_x_reverse_stop();        
       PWM_flag3=2;}
                       
     }     

if(  (lf_sta2_mir_regu ==0x3&&lf_sta2_mir_sel ==0x2&& mirror_y_position<MT) && cent_sta0_fire ==0x2 )
     {  
       if(PWM_flag4!=1)
      { mirror_y_forward_start();      
        PWM_flag4=1;}
        
     }
     
if(  ( lf_sta2_mir_regu==0x0  || lf_sta2_mir_sel!=0x2 ||  mirror_y_position>=MT ||  cent_sta0_fire ==0x1)  && PWM_flag4==1)
    { 
      if(PWM_flag4!=2)
      { mirror_y_forward_stop();         
         PWM_flag4=2;}
         
     }
         
if(  (lf_sta2_mir_regu ==0x4 && lf_sta2_mir_sel ==0x2&& mirror_y_position>0) && cent_sta0_fire ==0x2)
   { 
    if(PWM_flag4!=4)
     { mirror_y_reverse_start();       
       PWM_flag4=4;}
       
                            
    }      
          
if(  ( lf_sta2_mir_regu==0x0 || lf_sta2_mir_sel!=0x2 || mirror_y_position<=0 ||  cent_sta0_fire ==0x1) && PWM_flag4==4 )
   { 
    if(PWM_flag4!=8)
    { mirror_y_reverse_stop();       
      PWM_flag4 =8;}
                       
   }
    

}                       



	  
}	                   
	  
	  

























⌨️ 快捷键说明

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