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

📄 9s12forpwm.c

📁 此程序是用9s12单片机控制直流电机的控制程序
💻 C
📖 第 1 页 / 共 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 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -