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

📄 pwm.c

📁 motorola mc9s12dg128 uCOS的实现
💻 C
字号:
//#include <hidef.h>
#include <mc9s12c32.h>     /* derivative information */
#include  "main.h"

#define pwm_psck 0x11
#define pwm_scla 0x01
#define pwm_sclb 0x01
#define period   5000		 //发出的PWM波形为10KHz

#define duration_5   0xcc
#define duration_25  0x400
#define duration_50  0x800
#define duration_55  0x8cc
#define duration_60  0x999
#define duration_65	 0xa66
#define duration_70  0xb33
#define duration_75  0xc00
#define duration_80  0xccc
#define duration_85  0xd99
#define duration_90  0xe66
#define duration_95  0xf33
#define duration_100 0xfff

#define pedal_15      0x99
#define pwm5_en       0x20
#define pwm3_en       0x08
#define pwm5_dis			0x00
#define pwm3_dis			0x00
#define step_total    0x1000

void Init_PWM(void) {
PWMCLK=0x00;	     //CLOCK A FOR pwm5,clock B for pwm3
PWMPRCLK=pwm_psck; //预分频为A B均为二分频
PWMSCLA=pwm_scla;  //CLOCK SA=CLOCK A/(2*PWMSCLA),2分频
PWMSCLB=pwm_sclb;  //CLOCK SB=CLOCK B/(2*PWMSCLB),2分频
PWMPOL=0x00;       //pwm5/pwm3 output is low at the bigginning
PWMCTL|=0x40;      //通道4和5连接组成一个16位pwm控制口
PWMCTL|=0x20;      //通道2和3连接组成一个16位pwm控制口 
PWMCAE=0x00;       //左对齐
}
/*void pwm100k(void) 
{
  Init_PWM1();
  PWMPER45=period;
  PWMDTY45=2000;
  PWME=pwm5_en;
}*/
void Clutch_Start_on(void) {
		 int brake,temp_pedal,tempdata_int,gun_cur,gun_des;
		 float tempdata_f;
		 brake=read_165();
     if(brake&0x0002)		 //踩下刹车
     {
         Init_PWM();
 		     PWMPER45=period;				//以100%的占空比推进
 		      {
 		      PWMDTY45=duration_100;
 		      PWME=pwm5_en; 
 		      PTAD_PTAD4=1;		  //读离合器行程
 		      PTAD_PTAD4=0; 
 		      }while(PORTE&0x08)  //离合器未到25%行程处
 		 
 	        { PWMDTY45=duration_95;// 在25%~50%行程之间以95%的占空比推进
 	          PTAD_PTAD4=1;		  //读离合器行程
 		        PTAD_PTAD4=0;
          }while(PORTE&0x10)   //离合器未到50%行程处,
    										
          while(AD[2]<pedal_15) //读油门踏板位置
    				  PWME=pwm5_dis;
    		 
    		 
    		  asm sei
    		  PWME=pwm5_en;
    		  temp_pedal=AD[2];					//读油门踏板位置1
    		  PWMDTY45=duration_85;
    		  tempdata_f=(float)temp_pedal;
    		  tempdata_f*=0.3;
    		  tempdata_int=(int)tempdata_f;	 //tempdata_int对应油门踏板1的30%处对应的数字量
   				gun_des=tempdata_int;
   				
   				gun_cur=AD[1];             //读当前的油门开度
   				motor_run(MOTORU,((gun_des-gun_cur)/0x3ff)*step_total,20); //油门电机转到踏板位置1的30%处
   			  
   			  PWMDTY45=duration_80;
   			  tempdata_f=(float)temp_pedal;	
   	 		  tempdata_f*=0.5;
    		  tempdata_int=(int)tempdata_f;	 //tempdata_int对应油门踏板1的30%处对应的数字量
   				gun_des=tempdata_int;
   				
   				gun_cur=AD[1];             //读当前的油门开度
   				motor_run(MOTORU,((gun_des-gun_cur)/0x3ff)*step_total,20); //油门电机转到踏板位置1的30%处
         
         {
     			PWMDTY45=duration_75;
   				tempdata_f=(float)temp_pedal;
    		  tempdata_f*=0.75;
    		  tempdata_int=(int)tempdata_f;	 //tempdata_int对应油门踏板1的30%处对应的数字量
   				gun_des=tempdata_int;
   				
   				gun_cur=AD[1];             //读当前的油门开度
   				motor_run(MOTORU,((gun_des-gun_cur)/0x3ff)*step_total,20); //油门电机转到踏板位置1的30%处
    		  PTAD_PTAD4=1;		  //读离合器行程
 		      PTAD_PTAD4=0; 
    		 }while(PORTE&0x20)   //离合器未结合到75%处,以最后的占空比和油门开度推进
         /////////////////////////////////////////////////////////
         temp_pedal=AD[2];       //读油门踏板位置2
     		 PWMDTY45=duration_70;
     		 tempdata_f=((float)temp_pedal);
    		 tempdata_f*=0.8;
    		 tempdata_int=(int)tempdata_f;	 //tempdata_int对应油门踏板1的30%处对应的数字量
   			 gun_des=tempdata_int;
   			 gun_cur=AD[1];             //读当前的油门开度
   			 motor_run(MOTORU,((gun_des-gun_cur)/0x3ff)*step_total,20); //油门电机转到踏板位置1的30%处 
         ////////////////////////////////////////////
         {
     			PWMDTY45=duration_80;
   				tempdata_f=(float)temp_pedal;
    		  tempdata_f*=0.9;
    		  tempdata_int=(int)tempdata_f;	 //tempdata_int对应油门踏板1的30%处对应的数字量
   				gun_des=tempdata_int;
   				
   				gun_cur=AD[1];             //读当前的油门开度
   				motor_run(MOTORU,((gun_des-gun_cur)/0x3ff)*step_total,20); //油门电机转到踏板位置1的30%处
    		  PTAD_PTAD4=1;		  //读离合器行程
 		      PTAD_PTAD4=0; 
    		 }while(PORTE&0x40)   //离合器未结合到90%处,以最后的占空比和油门开度推进
    
         {
     			PWMDTY45=duration_100;
   				tempdata_f=(float)temp_pedal;
    		  tempdata_int=(int)tempdata_f;	 //tempdata_int对应油门踏板1的30%处对应的数字量
   				gun_des=tempdata_int;
   				
   				gun_cur=AD[1];             //读当前的油门开度
   				motor_run(MOTORU,((gun_des-gun_cur)/0x3ff)*step_total,20); //油门电机转到踏板位置1的30%处
    		  PTAD_PTAD4=1;		  //读离合器行程
 		      PTAD_PTAD4=0; 
    		 }while(PORTE&0x80)   //离合器未结合到90%处,以最后的占空比和油门开度推进
          //EnableInterrupts;
          asm cli	 
     }		
 }		
 
 void Clutch_Run_on(void) {
				 unsigned char gun_flag=0,clutch_flag=0;
				 int temp_pedal,tempdata_int,gun_cur,gun_des,i;
				 float tempdata_f;
				 PTAD_PTAD4=1;		  //读离合器行程
 		     PTAD_PTAD4=0;  
         if(!(PORTE&0x04))  //离合器位置不在0%处置出错标志
         		clutch_flag=1;
         if(AD[1]>0x5)      //油门开度大于一定范围,置出错标志
            gun_flag=1;
         gun_cur=AD[1];
         gun_des=0xff;      //油门开度为25%处
         motor_run(MOTORU,((gun_des-gun_cur)/0x3ff)*step_total,20); //油门电机转到油门开度的25%处 
				 if(AD[1]>0xff)	
					  gun_flag=0;
				 
				 Init_PWM();
 		     PWMPER45=period;				//以100%的占空比推进
 		     {
 		      PWMDTY45=duration_100;
 		      PWME=0x20; 
 		      PTAD_PTAD4=1;		  //读离合器行程
 		      PTAD_PTAD4=0; 
 		      }while(PORTE&0x08)  //离合器未到25%行程处
 		 
 	        { PWMDTY45=duration_95;// 在25%~50%行程之间以95%的占空比推进
 	          PTAD_PTAD4=1;		  //读离合器行程
 		        PTAD_PTAD4=0;
          }while(PORTE&0x10)   //离合器未到50%行程处,
	
	       
	        //DisableInterrupts;
	        asm sei
    		  PWME=pwm5_en;
    		  temp_pedal=AD[2];					//读油门踏板位置1
    		  PWMDTY45=duration_85;
    		  tempdata_f=(float)temp_pedal;
    		  tempdata_f*=0.3;
    		  tempdata_int=(int)tempdata_f;	 //tempdata_int对应油门踏板1的30%处对应的数字量
   				gun_des=tempdata_int;
   				
   				gun_cur=AD[1];             //读当前的油门开度
   				motor_run(MOTORU,((gun_des-gun_cur)/0x3ff)*step_total,20); //油门电机转到踏板位置1的30%处
   			  
   			  PWMDTY45=duration_80;
   			  tempdata_f=(float)temp_pedal;	
   	 		  tempdata_f*=0.5;
    		  tempdata_int=(int)tempdata_f;	 //tempdata_int对应油门踏板1的30%处对应的数字量
   				gun_des=tempdata_int;
   				
   				gun_cur=AD[1];             //读当前的油门开度
   				motor_run(MOTORU,((gun_des-gun_cur)/0x3ff)*step_total,20); //油门电机转到踏板位置1的30%处
         
          do{
     			PWMDTY45=duration_75;
   				tempdata_f=(float)temp_pedal;
    		  tempdata_f*=0.75;
    		  tempdata_int=(int)tempdata_f;	 //tempdata_int对应油门踏板1的30%处对应的数字量
   				gun_des=tempdata_int;
   				
   				gun_cur=AD[1];             //读当前的油门开度
   				motor_run(MOTORU,((gun_des-gun_cur)/0x3ff)*step_total,20); //油门电机转到踏板位置1的30%处
    		  PTAD_PTAD4=1;		  //读离合器行程
 		      PTAD_PTAD4=0; 
    		 }while(PORTE&0x20);   //离合器未结合到75%处,以最后的占空比和油门开度推进
         
         temp_pedal=AD[2];       //读油门踏板位置2
     		 PWMDTY45=duration_70;
     		 tempdata_f=(float)temp_pedal;
    		 tempdata_f*=0.8;
    		 tempdata_int=(int)tempdata_f;	 //tempdata_int对应油门踏板1的30%处对应的数字量
   			 gun_des=tempdata_int;
   			 gun_cur=AD[1];             //读当前的油门开度
   			 motor_run(MOTORU,((gun_des-gun_cur)/0x3ff)*step_total,20); //油门电机转到踏板位置1的30%处 
    
         {
     			PWMDTY45=duration_80;
   				tempdata_f=(float)temp_pedal;
    		  tempdata_f*=0.9;
    		  tempdata_int=(int)tempdata_f;	 //tempdata_int对应油门踏板1的30%处对应的数字量
   				gun_des=tempdata_int;
   				
   				gun_cur=AD[1];             //读当前的油门开度
   				motor_run(MOTORU,((gun_des-gun_cur)/0x3ff)*step_total,20); //油门电机转到踏板位置1的30%处
    		  PTAD_PTAD4=1;		  //读离合器行程
 		      PTAD_PTAD4=0; 
    		 }while(PORTE&0x40)   //离合器未结合到90%处,以最后的占空比和油门开度推进
    
         {
     			PWMDTY45=duration_100;
   				tempdata_f=(float)temp_pedal;
    		  tempdata_int=(int)tempdata_f;	 //tempdata_int对应油门踏板1的30%处对应的数字量
   				gun_des=tempdata_int;
   				
   				gun_cur=AD[1];             //读当前的油门开度
   				motor_run(MOTORU,((gun_des-gun_cur)/0x3ff)*step_total,20); //油门电机转到踏板位置1的30%处
    		  PTAD_PTAD4=1;		  //读离合器行程
 		      PTAD_PTAD4=0; 
    		 }while(PORTE&0x80)   //离合器未结合到90%处,以最后的占空比和油门开度推进
          //EnableInterrupts;
          TCTL3=0; 
          TCTL4=0;
          i=TC0+TC1+TC2+TC3+TC4+TC5+TC6+TC7;
          i=ATDDR0+ATDDR1+ATDDR2+ATDDR3;
          asm cli	/**/ 
}

void Clutch_off(void) {
			int gun_cur,gun_des=0;
		  gun_cur=AD[1];	
	    motor_run(MOTORU,((gun_des-gun_cur)/0x3ff)*step_total,20);
	    Init_PWM();
	    PWMPER3=0xff;
	    PWMDTY3=0xff;   
}/**/
 			 

⌨️ 快捷键说明

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