9s12forpwm.c

来自「此程序是用9s12单片机控制直流电机的控制程序」· C语言 代码 · 共 619 行 · 第 1/2 页

C
619
字号
                        if(PWMPer>=0x20)
                         {
          
                           PWMhalfdty=PWMPer>>1;
                           PWMPER0=PWMPer; //pwm 通道周期寄存器 周期为256
                           PWMPER1=PWMPer; 
           
                           PWMDTY0= PWMhalfdty; 		//极性位为1时 PPOLx=1 DutyCycle=[PWMDTYx/PWMPERx]*100%
                           PWMDTY1= PWMhalfdty;
                         } 
                        else
                          {PWMInit2();
                           startstep=2;
                           
                           PWMPer=0xfe;
                           PWMhalfdty=PWMPer>>1;
                           PWMPER0=PWMPer; //pwm 通道周期寄存器 周期为256
                           PWMPER1=PWMPer; 
           
                           PWMDTY0= PWMhalfdty; 		//极性位为1时 PPOLx=1 DutyCycle=[PWMDTYx/PWMPERx]*100%
            	             PWMDTY1= PWMhalfdty;
                            
                          }
                    }
                
                 	  if(startstep==2)
                 	    {   PWMPer=PWMPer-2;
                        if(PWMPer>=NOMALSPEED)
                         {
          
                           PWMhalfdty=PWMPer>>1;
                           PWMPER0=PWMPer; //pwm 通道周期寄存器 周期为256
                           PWMPER1=PWMPer; 
           
                           PWMDTY0= PWMhalfdty; 		//极性位为1时 PPOLx=1 DutyCycle=[PWMDTYx/PWMPERx]*100%
                           PWMDTY1= PWMhalfdty;
                         } 
                        else
                          {startflag=0;
                            
                          }
                    }
                
                
                  
                }
             else
                startflag=0x00;
            
          }
          else if(commandpre==STOP)				//soft start from the very beginning
                {startflag=0x01;
                 PWMInit1();//the first pwmper to start
                 startstep=1;
                 
                 PWMPer=0xfe;
                 PWMhalfdty=PWMPer>>1;
                 PWMPER0=PWMPer; //pwm 通道周期寄存器 周期为256
                 PWMPER1=PWMPer; 
           
                 PWMDTY0= PWMhalfdty; 		//极性位为1时 PPOLx=1 DutyCycle=[PWMDTYx/PWMPERx]*100%
            	   PWMDTY1= PWMhalfdty;
                    
                }
          break; 
  ///////////////////////////////////////////////////////////////////////////////        
  case BACK:
          PORTB=PORTB&0XFC;
  
          if(commandpre==BACK)  //正常启动或前进
          {  if(startflag==0x01)//starting...
                {
                   if(startstep==1)
                   {   PWMPer=PWMPer-2;
                        if(PWMPer>=0x20)
                         {
          
                           PWMhalfdty=PWMPer>>1;
                           PWMPER0=PWMPer; //pwm 通道周期寄存器 周期为256
                           PWMPER1=PWMPer; 
           
                           PWMDTY0= PWMhalfdty; 		//极性位为1时 PPOLx=1 DutyCycle=[PWMDTYx/PWMPERx]*100%
                           PWMDTY1= PWMhalfdty;
                         } 
                        else
                          {PWMInit2();
                           startstep=2;
                           
                           PWMPer=0xfe;
                           PWMhalfdty=PWMPer>>1;
                           PWMPER0=PWMPer; //pwm 通道周期寄存器 周期为256
                           PWMPER1=PWMPer; 
           
                           PWMDTY0= PWMhalfdty; 		//极性位为1时 PPOLx=1 DutyCycle=[PWMDTYx/PWMPERx]*100%
            	             PWMDTY1= PWMhalfdty;
                            
                          }
                    }
                
                 	  if(startstep==2)
                 	    {   PWMPer=PWMPer-2;
                        if(PWMPer>=NOMALSPEED)
                         {
          
                           PWMhalfdty=PWMPer>>1;
                           PWMPER0=PWMPer; //pwm 通道周期寄存器 周期为256
                           PWMPER1=PWMPer; 
           
                           PWMDTY0= PWMhalfdty; 		//极性位为1时 PPOLx=1 DutyCycle=[PWMDTYx/PWMPERx]*100%
                           PWMDTY1= PWMhalfdty;
                         } 
                        else
                          {startflag=0;
                            
                          }
                    }
                
                
                  
                }
             else
                startflag=0x00;
            
          }
          else if(commandpre==STOP)				//soft start from the very beginning
                {startflag=0x01;
                 PWMInit1();//the first pwmper to start
                 startstep=1;
                 
                 PWMPer=0xfe;
                 PWMhalfdty=PWMPer>>1;
                 PWMPER0=PWMPer; //pwm 通道周期寄存器 周期为256
                 PWMPER1=PWMPer; 
           
                 PWMDTY0= PWMhalfdty; 		//极性位为1时 PPOLx=1 DutyCycle=[PWMDTYx/PWMPERx]*100%
            	   PWMDTY1= PWMhalfdty;
                    
                }
          
           
          break; 
  ////////////////////////////////////////////////////////////////////         
  case LEFT:
          PORTB|=0X01;
          PORTB&=0XFD;
  
          if(commandpre==LEFT)  //正常启动或前进
          {  if(startflag==0x01)//starting...
                {
                   if(startstep==1)
                   {   PWMPer=PWMPer-2;
                        if(PWMPer>=0x20)
                         {
          
                           PWMhalfdty=PWMPer>>1;
                           PWMPER0=PWMPer; //pwm 通道周期寄存器 周期为256
                           PWMPER1=PWMPer; 
           
                           PWMDTY0= PWMhalfdty; 		//极性位为1时 PPOLx=1 DutyCycle=[PWMDTYx/PWMPERx]*100%
                           PWMDTY1= PWMhalfdty;
                         } 
                        else
                          {PWMInit2();
                           startstep=2;
                           
                           PWMPer=0xfe;
                           PWMhalfdty=PWMPer>>1;
                           PWMPER0=PWMPer; //pwm 通道周期寄存器 周期为256
                           PWMPER1=PWMPer; 
           
                           PWMDTY0= PWMhalfdty; 		//极性位为1时 PPOLx=1 DutyCycle=[PWMDTYx/PWMPERx]*100%
            	             PWMDTY1= PWMhalfdty;
                            
                          }
                    }
                
                 	  if(startstep==2)
                 	    {   PWMPer=PWMPer-2;
                        if(PWMPer>=NOMALSPEED)
                         {
          
                           PWMhalfdty=PWMPer>>1;
                           PWMPER0=PWMPer; //pwm 通道周期寄存器 周期为256
                           PWMPER1=PWMPer; 
           
                           PWMDTY0= PWMhalfdty; 		//极性位为1时 PPOLx=1 DutyCycle=[PWMDTYx/PWMPERx]*100%
                           PWMDTY1= PWMhalfdty;
                         } 
                        else
                          {startflag=0;
                            
                          }
                    }
                
                
                  
                }
             else
                startflag=0x00;
            
          }
          else if(commandpre==STOP)				//soft start from the very beginning
                {startflag=0x01;
                 PWMInit1();//the first pwmper to start
                 startstep=1;
                 
                 PWMPer=0xfe;
                 PWMhalfdty=PWMPer>>1;
                 PWMPER0=PWMPer; //pwm 通道周期寄存器 周期为256
                 PWMPER1=PWMPer; 
           
                 PWMDTY0= PWMhalfdty; 		//极性位为1时 PPOLx=1 DutyCycle=[PWMDTYx/PWMPERx]*100%
            	   PWMDTY1= PWMhalfdty;
                    
                }
          break; 
  ///////////////////////////////////////////////////////////////////////        
  case RIGHT:
           PORTB|=0X02;
          PORTB&=0XFE;
  
          if(commandpre==RIGHT)  //正常启动或前进
          {  if(startflag==0x01)//starting...
                {
                   if(startstep==1)
                   {   PWMPer=PWMPer-2;
                        if(PWMPer>=0x20)
                         {
          
                           PWMhalfdty=PWMPer>>1;
                           PWMPER0=PWMPer; //pwm 通道周期寄存器 周期为256
                           PWMPER1=PWMPer; 
           
                           PWMDTY0= PWMhalfdty; 		//极性位为1时 PPOLx=1 DutyCycle=[PWMDTYx/PWMPERx]*100%
                           PWMDTY1= PWMhalfdty;
                         } 
                        else
                          {PWMInit2();
                           startstep=2;
                           
                           PWMPer=0xfe;
                           PWMhalfdty=PWMPer>>1;
                           PWMPER0=PWMPer; //pwm 通道周期寄存器 周期为256
                           PWMPER1=PWMPer; 
           
                           PWMDTY0= PWMhalfdty; 		//极性位为1时 PPOLx=1 DutyCycle=[PWMDTYx/PWMPERx]*100%
            	             PWMDTY1= PWMhalfdty;
                            
                          }
                    }
                
                 	  if(startstep==2)
                 	    {   PWMPer=PWMPer-2;
                        if(PWMPer>=NOMALSPEED)
                         {
          
                           PWMhalfdty=PWMPer>>1;
                           PWMPER0=PWMPer; //pwm 通道周期寄存器 周期为256
                           PWMPER1=PWMPer; 
           
                           PWMDTY0= PWMhalfdty; 		//极性位为1时 PPOLx=1 DutyCycle=[PWMDTYx/PWMPERx]*100%
                           PWMDTY1= PWMhalfdty;
                         } 
                        else
                          {startflag=0;
                            
                          }
                    }
                
                
                  
                }
             else
                startflag=0x00;
            
          }
          else if(commandpre==STOP)				//soft start from the very beginning
                {startflag=0x01;
                 PWMInit1();//the first pwmper to start
                 startstep=1;
                 
                 PWMPer=0xfe;
                 PWMhalfdty=PWMPer>>1;
                 PWMPER0=PWMPer; //pwm 通道周期寄存器 周期为256
                 PWMPER1=PWMPer; 
           
                 PWMDTY0= PWMhalfdty; 		//极性位为1时 PPOLx=1 DutyCycle=[PWMDTYx/PWMPERx]*100%
            	   PWMDTY1= PWMhalfdty;
                    
                }
          break;
 //////////////////////////////////////////////////////////////////////////         
  //case SPEEDUP:
         // break; 
 ///////////////////////////////////////////////////////////////////////////         
 // case SLOWDOWN:
         // break;
 ////////////////////////////////////////////////////////////////////////////         
  
  default:
          PWMDTY0=0;
          PWMDTY1=0;
          PWMDTY4=0;
          break;        
}
commandpre=command;
return;
}
#pragma CODE_SEG DEFAULT

⌨️ 快捷键说明

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