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

📄 pwm_120slc20.c

📁 实现120无传感器变频输出
💻 C
📖 第 1 页 / 共 4 页
字号:
    }
  
}

/****************************************************************************/
/*                                                                          */
/*     rotor position  function                                                  */
/*                                                                          */
/****************************************************************************/
#define PWMDUTYCHGNUM	(200)
#define PWMSTANUM	(25)



static void pwm_start (void)
{
	static UINT_16 pwm_sta_cnt = PWMSTANUM;
	static UINT_16   pwm_dutychg_cnt = PWMDUTYCHGNUM;
	
	SINT_16 set_tim;
	
	idb0 = 0x1d ;//v->u, v->w//0x17;//
	idb1 = 0x19;
	
	set_tim = CARR_CNT_4K - ((pwm_duty * (SINT_32)CARR_CNT_4K) >> Sft_DUTY_120_TIME);
	if(set_tim >= (CARR_CNT_4K - 1))   set_tim = CARR_CNT_4K - 1;
	if(set_tim <= 1)                    set_tim = 1;
	ta4 = ta1 = ta2 = set_tim; 
	
	if ( pwm_dutychg_cnt > 0 )
	{
		pwm_dutychg_cnt --;
	}
	else
	{
		pwm_duty += 2;
		if(pwm_duty >= INI_DUTY_120 * 2)        pwm_duty = INI_DUTY_120 * 2;    	
		if ( pwm_sta_cnt > 0)
		{
			pwm_dutychg_cnt = PWMDUTYCHGNUM;
			pwm_sta_cnt --;					
		}
		else
		{
			out_mode = KIDO_MODE;
			pwm_dutychg_cnt = PWMDUTYCHGNUM;
			pwm_sta_cnt = PWMSTANUM;
			 /* control RAM initialize */
		            mot_theta = 0;
		            out_theta = 0;
		            kido_cnt = 0;
		           
		            cnt_wr_act = 0;
		}
	}
	
	if(stop_chk() == 1)       	out_mode = STOP_MODE;		
}


/*****************************************************
	PWM bootstrap function
*****************************************************/
#define BOOTSTRAPSTEP 	(3)

const UINT_8 pwmstraptbl0[BOOTSTRAPSTEP] = { 0x3f,0x3f,0x3f};
const UINT_8 pwmstraptbl1[BOOTSTRAPSTEP] = { 0x3d,0x35,0x15};
const UINT_16 pwmbootstrapno = 500;

static void pwm_bootstrap(void)
{
	static UINT_16 bootstrapcnt = 0;
	static UINT_8 bootstrapstepno = 0;		
	
	SINT_16 set_tim;
	
	if ( bootstrapstepno < BOOTSTRAPSTEP )
	{
		idb0 = pwmstraptbl0[bootstrapstepno] ;
		idb1 = pwmstraptbl1[bootstrapstepno] ;	
		if ( bootstrapcnt < pwmbootstrapno )
		{
			bootstrapcnt ++;
		}
		else
		{
			bootstrapcnt = 0;
			bootstrapstepno ++;			
		}
		out_mode = BOOTSTRAP_MODE;
	}
	else
	{
		bootstrapstepno = 0;
		bootstrapcnt = 0;
		
		idb0 = 0x3f ;
		idb1 = 0x3f ;		
		out_mode = KIDO_MODE;//START_MODE;//BOOTSTRAP_MODE;//
	}
	
	
	set_tim = CARR_CNT_4K - ((BOOTSTRAPDUTY * (SINT_32)CARR_CNT_4K) >> Sft_DUTY_120_TIME);
	if(set_tim >= (CARR_CNT_4K - 1))   set_tim = CARR_CNT_4K - 1;
	if(set_tim <= 1)                    set_tim = 1;
	ta4 = ta1 = ta2 = set_tim; 
	if(stop_chk() == 1)       	out_mode = STOP_MODE;	
}

/****************************************************************************/
/*                                                                          */
/*      start-up function                                                   */
/*                                                                          */
/****************************************************************************/
#ifdef Kido//houfei 2006-8-18 18:21
static  void    pwm_kido(void)//pwm_kido
{
	SINT_16    set_tim;
	SINT_16    stg_p;
        //SINT_16    w_si16;
	static UINT_16 StartInt;
	//static UINT_16 s_IntCnt1;
	static UINT_16 prestage;
	static UINT_16 CheckOnCnt;

        /* PWM DUTY of the start operation is calculated. */
        pwm_duty = INI_DUTY_120;
        if(pwm_duty >= MAX_DUTY_120)           pwm_duty = MAX_DUTY_120;
        if(pwm_duty <= MIN_DUTY_120)            pwm_duty = MIN_DUTY_120;
        
        stage = out_theta / THETA_60DEG;
        
        if( prestage != stage )
	{
		tb2s = 0;
		tb2 = 10;
		tb2s = 1;
		tb2 = CARR_CNT_4K - 1;
	}
	stg_p = stage;   
        idb0 = pwm_out_tbl0[stg_p];
        idb1 = pwm_out_tbl1[stg_p];
        
       
	set_tim = ((pwm_duty * (SINT_32)CARR_CNT_4K) >> Sft_DUTY_120_TIME);
        if(set_tim >= (CARR_CNT_4K - 1))   set_tim = CARR_CNT_4K - 1;
        if(set_tim <= 1)                   set_tim = 1;
        ta4 = ta1 = ta2 = set_tim; 
		
        if (prestage == stage)
        {
        	if (CheckOnCnt>0)
        	{ 
        		CheckOnCnt--;
        	        int4ic &= 0xf7;                     
        		int3ic &= 0xf7;
        		int5ic &= 0xf7; 
        	}
        	switch(stage)
		{
		case 0:                                              /* pattern 1 output */
			if(((int3ic&0x08)!=0)&&(CheckOnCnt ==0))      /* rising edge detection of a v phase? */
			{						                				       
			        int3ic &= 0xf7;// 2006-7-25 13:30	                   	                    	                
			        stage = 1;			                
			        StartInt++;
			        mot_theta = THETA_60DEG+1;
			        CheckOnCnt = THETA_15DEG;	                     	                        
			}
			int4ic &= 0;
			int5ic &= 0;	                                  
			break;
		case 1:                                               /* pattern 2 output */
		        if(((int5ic&0x08)!=0)&&(CheckOnCnt ==0))   /* rising edge detection of a u phase? */
		        {	                    		             
		                int5ic &= 0xf7;	                   	                    	                
		                stage = 2;
		                StartInt++;
		                mot_theta = THETA_60DEG * 2+1;	                     
                        	CheckOnCnt = THETA_15DEG;
		        }
		        int3ic &= 0;
		        int4ic &= 0;	                  	                   	                             
		        break;
		case 2:                               /* pattern 3 output */
			if(((int4ic&0x08)!=0)&&(CheckOnCnt ==0))              /* rising edge detection of a w phase? */
		        {
		                int4ic &= 0xf7;	                		                   	                    	                
		                stage = 3;
		                StartInt++;
		                mot_theta = THETA_60DEG * 3+1;
		                CheckOnCnt = THETA_15DEG;	                     
                       
		        }
		        int3ic &= 0;
		        int5ic &= 0;	           	              	                
		        break;
		case 3:                               /* pattern 4 output */
		        if(((int3ic & 0x08) != 0)&&(CheckOnCnt ==0))              /* rising edge detection of a v phase? */
		        {
		                int3ic &= 0xf7;                  	                    	                
		                stage = 4;
		                StartInt++;
		                mot_theta = THETA_60DEG * 4+1;	                     
		                CheckOnCnt = THETA_15DEG;	
		        }
		        int4ic &= 0;
		        int5ic &= 0;	                   	  	               
		        break;
		case 4:                              /* pattern 5 output */
		        if(((int5ic & 0x08) != 0)&&(CheckOnCnt ==0))             /* rising edge detection of a u phase? */
		        {
		                int5ic &= 0xf7;		                				                                	                    	                
		                stage = 5;		                       
		                StartInt++;
		                mot_theta = THETA_60DEG * 5+1;	                     
		               	CheckOnCnt = THETA_15DEG;                        
		        }
		        int3ic &= 0;
		        int4ic &= 0;                                   	               
		        break;
		case 5:                                 /* pattern 0 output */
		        if(((int4ic & 0x08) != 0)&&(CheckOnCnt ==0))               /* rising edge detection of a w phase? */
		        {
		                int4ic &= 0xf7;		                		                   	                    	                
		                stage = 0;
		                StartInt++;
		                mot_theta = 1;		                             	                     
	                        CheckOnCnt = THETA_15DEG; 
		        }
		        int3ic &= 0;
		        int5ic &= 0;	                   	           	                  
		        break;
		}
		
		if (StartInt >=6)
		{
		        wr_act = KIDO_Hz;
	                out_mode = TUJO_MODE;//KIDO_MODE;//
	                reload_stg = stage;
	         
	                #ifdef SIM
	            		dlt_cnt = phaseperiodbuf/6;
	                #else
	            		dlt_cnt =  K_WR_ACT_120SL / (KIDO_Hz *12) + 1; //phaseperiodbuf/6;//5
	                #endif
	                reload_cnt = 0;     
	           
	                #ifdef SIM
	                //for simulator         
	                p10 = BEMF_ini_level [reload_stg];
	                pd10 = 0xe0;
	                phaseperiod = INIperiod;//30;//1200;//1200;//30;//600;//60;//60;	//1000rpm 20000 * 60 /1000
	
	                W_phase_cnt = BEMF_ini_phase[reload_stg][0];//INIperiod/6;//200;//5;//1200 - 1000;//800;// 20;//400;	
	                V_phase_cnt = BEMF_ini_phase[reload_stg][1];//INIperiod - INIperiod/6;//1000;//25;//1200 - 200;//400;//10;//200;//200;//400;	
	                U_phase_cnt = BEMF_ini_phase[reload_stg][2];//INIperiod/2;//600;//15;//1200 - 600;       
	                #endif
			tb2chg1 = 1;                 
		        TIMERINI(TB1);// timer_TB1_ini() 
			int4ic &= 0;                     
        		int3ic &= 0;
        		int5ic &= 0;  
			return;
	        }
	}         
        else
        {
        	CheckOnCnt = THETA_15DEG;
        	StartInt = 0;
        	int4ic &= 0;                     
        	int3ic &= 0;
        	int5ic &= 0;       	        	
        }
        prestage = stage;
                   
        if(wr_ref > 0)  mot_theta += (KIDO_Hz * (SINT_32)(CTRL_TS )) >> Sft_KIDO_MODE1;    
        if(mot_theta >= THETA_360DEG)   
        {
    		mot_theta -= THETA_360DEG;    	
        }
        /* output angle calculation */
        dlt_theta = 0;
        if(wr_ref > 0)  out_theta = mot_theta + dlt_theta;    
        if(out_theta >= THETA_360DEG)   	out_theta -= THETA_360DEG; 
               			
	if(stop_chk() == 1)       	out_mode = STOP_MODE;        	
}
#else

static  void    pwm_kido(void)
{
    SINT_16    set_tim;
    SINT_16    stg_p;
    SINT_16    w_si16;
    
  

    /* PWM DUTY of the start operation is calculated. */
    pwm_duty = INI_DUTY_120;
    if(pwm_duty >= MAX_DUTY_120)           pwm_duty = MAX_DUTY_120;
    if(pwm_duty <= MIN_DUTY_120)            pwm_duty = MIN_DUTY_120;

    stage = out_theta / THETA_60DEG;
    stg_p = stage;   
    idb0 = pwm_out_tbl0[stg_p];
    idb1 = pwm_out_tbl1[stg_p];
    //set_tim = CARR_CNT_4K - ((pwm_duty * (SINT_32)CARR_CNT_4K) >> Sft_DUTY_120_TIME);
    set_tim = ((pwm_duty * (SINT_32)CARR_CNT_4K) >> Sft_DUTY_120_TIME);
    if(set_tim >= (CARR_CNT_4K - 1))   set_tim = CARR_CNT_4K - 1;
    if(set_tim <= 1)                    set_tim = 1;
    ta4 = ta1 = ta2 = set_tim; 
   
   if(wr_ref > 0)  mot_theta += (KIDO_Hz * (SINT_32)(CTRL_TS )) >> Sft_KIDO_MODE1;   
   
    if(mot_theta >= THETA_360DEG)   
    {
    	mot_theta -= THETA_360DEG;    	
     }
  

    /* output angle calculation */
    dlt_theta = 0;
    if(wr_ref > 0)  out_theta = mot_theta + dlt_theta;    
    if(out_theta >= THETA_360DEG)   	out_theta -= THETA_360DEG;    

  
   if(kido_cnt < 0x7fff)   kido_cnt ++;
   
    
	        //2006.6.9 
// MODIFIED BY WangRD 2006/06/20			   
   if(kido_cnt >= KIDO_MODE1_TIME )
    {
       // out_mode = STOP_MODE;
		//kido_cnt = 0;
        if(stage != reload_stg)
        {   
           
            wr_act = KIDO_Hz;
            out_mode = TUJO_MODE;//KIDO_MODE;//
            reload_stg = stage;
         
            #ifdef SIM
            	dlt_cnt = phaseperiodbuf/6;
            #else
            	dlt_cnt = K_WR_ACT_120SL / (KIDO_Hz *12) + 1; //phaseperiodbuf/6;//5
            #endif
            reload_cnt = 0;     
           
           #ifdef SIM
            //for simulator         
            p10 = BEMF_ini_level [reload_stg];
            pd10 = 0xe0;
            phaseperiod = INIperiod;//30;//1200;//1200;//30;//600;//60;//60;	//1000rpm 20000 * 60 /1000

            W_phase_cnt = BEMF_ini_phase[reload_stg][0];//INIperiod/6;//200;//5;//1200 - 1000;//800;// 20;//400;	
            V_phase_cnt = BEMF_ini_phase[reload_stg][1];//INIperiod - INIperiod/6;//1000;//25;//1200 - 200;//400;//10;//200;//200;//400;	
            U_phase_cnt = BEMF_ini_phase[reload_stg][2];//INIperiod/2;//600;//15;//1200 - 600;       
            #endif
			tb2chg1 = 1;
            TIMERINI(TB1);
		//	pwm_duty = 370 * 2 * 2;
       }
    }
    else
    {
        if(dlt_cnt < RPM500_60DGREE_4K)    dlt_cnt ++;
        if(stage != reload_stg) dlt_cnt = 0;
        reload_stg = stage;          
        // w_si16 = wr_act;       
         #ifdef SIM
         	chk_on_cnt = phaseperiodbuf/6 - 5;//remin_tbl[w_si16 >> 6];
         #else
        	if ((w_si16 >>6) > 49) w_si16 = (49 << 6);
        	chk_on_cnt = remin_tbl[w_si16 >> 6];    //phaseperiodbuf/6 - 5;//100;//0;//remin_tbl[w_si16 >> 6];    
        #endif
        reload_cnt = 0;                                          // output delay counter        
    }
     //2006.6.9
// End of MODIFIED BY WangRD 2006/06/20
    int4ic &= 0x00;
    int3ic &= 0x00;
    int5ic &= 0x00;
    
   if(stop_chk() == 1)       	out_mode = STOP_MODE;
}
#endif
/****************************************************************************/
/*                                                                          */
/*      stationary function                                                 */
/*                                                                          */
/****************************************************************************/
//static UINT_8 tb2chg = 0;
//static UINT_8 tb2chg1 = 0;

⌨️ 快捷键说明

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