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

📄 ad.c

📁 一个基于DSP 2812的开关磁阻电机的控制程序的例子。用霍尔传感器测相位
💻 C
📖 第 1 页 / 共 2 页
字号:

#include "DSP28_Device.h"



unsigned int count=0,isss;

// **************VARIABLES INITIALIZATION*************
 int KP;           //位置环比例系数
 int KI;           //位置环积分系数 
 int KD;
 
 int KP_T;           //位置环比例系数
 int KI_T;           //位置环积分系数 
 int KD_T;
 
 int W1_SAMPLE;       
 int W1_ERROR; 
 int W1_ERR;       
 int W1_REF;          
 int W1_SUMERROR;     
 int W1_IRESULT;
 int W1_PRESULT;
 int W1_DRESULT;
 int W1_PIDRESULT;   
 
 int W2_SAMPLE;       
 int W2_ERROR; 
 int W2_ERR;        
 int W2_REF;          
 int W2_SUMERROR;     
 int W2_IRESULT;
 int W2_PRESULT;
 int W2_DRESULT;
 int W2_PIDRESULT; 
 
 int W3_SAMPLE;       
 int W3_ERROR;
 int W3_ERR;         
 int W3_REF;          
 int W3_SUMERROR;     
 int W3_IRESULT;
 int W3_PRESULT;
 int W3_DRESULT;
 int W3_PIDRESULT; 
 
 int W4_SAMPLE;       
 int W4_ERROR; 
 int W4_ERR;        
 int W4_REF;          
 int W4_SUMERROR;     
 int W4_IRESULT;
 int W4_PRESULT;
 int W4_DRESULT;
 int W4_PIDRESULT; 
 
 int W5_SAMPLE;       
 int W5_ERROR;
 int W5_ERR;         
 int W5_REF;          
 int W5_SUMERROR;     
 int W5_IRESULT;
 int W5_PRESULT;
 int W5_DRESULT;
 int W5_PIDRESULT; 
 
 
 int I_KP;           //电流环比例系数
 int I_KI;           //电流环积分系数
 
 int I11_SAMPLE;       //本次电流采样值
 int I11_ERROR;        //本次电流偏差值 
 int I11_REF;          //电流设定值
 int I11_SUMERROR;     //电流偏差累计和
 int I11_IRESULT;
 int I11_PRESULT;
 int I11_PIRESULT;    //电流PID结果
 
 int I12_SAMPLE;       //本次电流采样值
 int I12_ERROR;        //本次电流偏差值 
 int I12_REF;          //电流设定值
 int I12_SUMERROR;     //电流偏差累计和
  int I12_IRESULT;
 int I12_PRESULT;
 int I12_PIRESULT;    //电流PID结果
 
 int I21_SAMPLE;       //本次电流采样值
 int I21_ERROR;        //本次电流偏差值 
 int I21_REF;          //电流设定值
 int I21_SUMERROR;     //电流偏差累计和
  int I21_IRESULT;
 int I21_PRESULT;
 int I21_PIRESULT;    //电流PID结果
 
 int I22_SAMPLE;       //本次电流采样值
 int I22_ERROR;        //本次电流偏差值 
 int I22_REF;          //电流设定值
 int I22_SUMERROR;     //电流偏差累计和
 int I22_IRESULT;
 int I22_PRESULT;
 int I22_PIRESULT;    //电流PID结果
 
 int I31_SAMPLE;       //本次电流采样值
 int I31_ERROR;        //本次电流偏差值 
 int I31_REF;          //电流设定值
 int I31_SUMERROR;     //电流偏差累计和
 int I31_IRESULT;
 int I31_PRESULT;
 int I31_PIRESULT;    //电流PID结果
 
 int I32_SAMPLE;       //本次电流采样值
 int I32_ERROR;        //本次电流偏差值 
 int I32_REF;          //电流设定值
 int I32_SUMERROR;     //电流偏差累计和
 int I32_IRESULT;
 int I32_PRESULT;
 int I32_PIRESULT;    //电流PID结果
 
 int I41_SAMPLE;       //本次电流采样值
 int I41_ERROR;        //本次电流偏差值 
 int I41_REF;          //电流设定值
 int I41_SUMERROR;     //电流偏差累计和
 int I41_IRESULT;
 int I41_PRESULT;
 int I41_PIRESULT;    //电流PID结果
 
 int I42_SAMPLE;       //本次电流采样值
 int I42_ERROR;        //本次电流偏差值 
 int I42_REF;          //电流设定值
 int I42_SUMERROR;     //电流偏差累计和
 int I42_IRESULT;
 int I42_PRESULT;
 int I42_PIRESULT;    //电流PID结果
 
 int I51_SAMPLE;       //本次电流采样值
 int I51_ERROR;        //本次电流偏差值 
 int I51_ERROR1;
 int I51_ERROR2;
 int I51_ERR;
 int I51_ERR1;
 int I51_ERR2;
 int I51_REF;          //电流设定值
 int I51_SUMERROR;     //电流偏差累计和
 int I51_IRESULT;
 int I51_PRESULT;
 int I51_PIRESULT;    //电流PID结果
 int I51_SATERR;
 int I51_PI;
 
 int I52_SAMPLE;       //本次电流采样值
 int I52_ERROR;        //本次电流偏差值 
 int I52_REF;          //电流设定值
 int I52_SUMERROR;     //电流偏差累计和
 int I52_IRESULT;
 int I52_PRESULT;
 int I52_PIRESULT;    //电流PID结果
 
// Prototype statements for functions found within this file.
interrupt void ad(void);
void init_eva();

void main(void)
{
    //unsigned  int i=0;
   	InitSysCtrl();

	DINT;
	IER = 0x0000;
	IFR = 0x0000;

	InitPieCtrl();


	InitPieVectTable();	
	
	EALLOW;
   // Enable PWM pins
     GpioMuxRegs.GPAMUX.all = 0x00FF; // EVA PWM 1-6  pins
     GpioMuxRegs.GPBMUX.all = 0x00FF; // EVB PWM 7-12 pins
     GpioMuxRegs.GPADIR.all = 0x0FFF;  //bit 9 10 11 12输出功能
     GpioDataRegs.GPADAT.all = 0xFFFF;
    EDIS;
    // 初始化转子位置
    
    /*
    EvaRegs.T1PR = 15000;       // Timer1 period
    EvaRegs.T1CMPR = 7550;     // Timer1 compare
    EvaRegs.T1CNT = 0x0000;      // Timer1 counter
    EvaRegs.ACTR.all =0xfffa;       //A项通电
    EvbRegs.ACTRB.all =0xffff;
    EvaRegs.T1CON.all = 0x1042; 
    EvaRegs.GPTCONA.bit.TCOMPOE =1;
    EvaRegs.DBTCONA.all = 0x0000; // Disable deadband
    EvaRegs.COMCONA.all = 0xC600; //1100 0110 0000 0000
    for (i=0;i<0xFFFF;i++)
    {
       ;
    
    } */
	EALLOW;	// This is needed to write to EALLOW protected registers
	PieVectTable.T2PINT=&T2PINT_ISR;
	PieVectTable.T2CINT=&T2CINT_ISR;
    PieVectTable.ADCINT=&ad;
	EDIS;   // This is needed to disable write to EALLOW protected registers
	
    PieCtrl.PIEIER3.all |= (M_INT2 | M_INT1);
    
    InitAdc();
    init_availables();
    InitEv();
    for(isss=0;isss<2000;isss++)
    {isss+=1;
    
    }

    // Enable INT14 which is connected to CPU-Timer 2:
	IER |= (M_INT1 | M_INT3);
	//KickDog();
    // Enable global Interrupts and higher priority real-time debug events:
	
	EINT;   // Enable Global interrupt INTM
	ERTM;	// Enable Global realtime interrupt DBGM
/*	while(AdcRegs.ADC_ST_FLAG.bit.SEQ1_BSY==0)
		{			

			AdcRegs.ADCTRL2.bit.SOC_SEQ1=1;

		}
*/
	for(;;)
	{
			KickDog();
	}
} 	


interrupt void ad(void)
{
	//^^^^^^^^ 采样CURRENT,电流环程序^^^^^^^^^
    DINT;
   
    	count+=1;
	if (count==2000) count=0;
	if (count==1000) I51_REF=1365;
	if (count==0)   I51_REF=1365;
	//进行位置计算
	/*
            W5_SAMPLE=AdcRegs.RESULT14>>4;
	        W5_ERROR=W5_REF-W5_SAMPLE;
                
	        W5_SUMERROR+=W5_ERROR;
        	if(W5_SUMERROR<-400) W5_SUMERROR=-400;
            else if (W5_SUMERROR>400) W5_SUMERROR=400;
            W5_IRESULT=W5_SUMERROR;
            W5_IRESULT=W5_IRESULT>>4;
            W5_IRESULT=W5_IRESULT*KI_T;
           
	        W5_PRESULT=W5_ERROR*KP_T;
	       // W5_PRESULT=W5_PRESULT>>7;
	        
	        W5_DRESULT=KD_T*(W5_ERROR-W5_ERR);
	       W5_DRESULT=W5_DRESULT>>1;
	        
	       W5_PIDRESULT=W5_PRESULT+W5_IRESULT+W5_DRESULT;
	       //W5_PIDRESULT=-W5_PIDRESULT;
         
           	I51_REF-=W5_PIDRESULT;
           	I52_REF+=W5_PIDRESULT;
	
       	if(I51_REF<1) I51_REF=1;
	    else if (I51_REF>2730) I51_REF=2730;
	    
	    if(I52_REF<1) I52_REF=1;
	    else if (I52_REF>2730) I52_REF=2730;
	    
	    W5_ERR=W5_ERROR;
	    */
	  
    
    
    // **位置1 ACDINT10  **
  
  /*  
    W1_SAMPLE=AdcRegs.RESULT10>>4;
	W1_ERROR=W1_REF-W1_SAMPLE;
                
	        W1_SUMERROR+=W1_ERROR;
        	if(W1_SUMERROR<-5000) W1_SUMERROR=-5000;
            else if (W1_SUMERROR>5000) W1_SUMERROR=5000;
            W1_IRESULT=W1_SUMERROR;
            W1_IRESULT=W1_IRESULT>>4;
            W1_IRESULT=W1_IRESULT*KI;
           
	        W1_PRESULT=W1_ERROR*KP;
	        W1_PRESULT=W1_PRESULT>>8;
	        
	        W1_DRESULT=KD*(W1_ERROR-W1_ERR);
	        
	        W1_PIDRESULT=W1_PRESULT+W1_IRESULT+W1_DRESULT;
         
           	I11_REF+=W1_PIDRESULT;
           	I12_REF-=W1_PIDRESULT;
	
       	if(I11_REF<0) I11_REF=0;
	    else if (I11_REF>2730) I11_REF=2730;
	    
	    if(I12_REF<0) I12_REF=0;
	    else if (I12_REF>2000) I12_REF=2000;
	    
	    W1_ERR=W1_ERROR;
   	 */   
   	    
   	    
   
    
     // **位置2 ACDINT11  **
     
      // **位置3 ACDINT12  **
      
       // **位置4 ACDINT13  **
       
        // **位置5 ACDINT14  **
        
        
    
    // **第一路电流 ACDINT0 CMPR1 **
	I11_SAMPLE=AdcRegs.RESULT0>>4;
	//I11_SAMPLE=I11_SAMPLE-220;
	I11_ERROR=I11_REF-I11_SAMPLE;
                
	        I11_SUMERROR+=I11_ERROR;
        	if(I11_SUMERROR<-5000) I11_SUMERROR=-5000;
            else if (I11_SUMERROR>5000) I11_SUMERROR=5000;
            I11_IRESULT=I11_SUMERROR;
            I11_IRESULT=I11_IRESULT>>8;
            I11_IRESULT=I11_IRESULT*I_KI;
           
	        I11_PRESULT=I11_ERROR*I_KP;
	        I11_PRESULT=I11_PRESULT>>2;
	        
	        I11_PIRESULT=I11_PRESULT;
         
           	EvaRegs.CMPR1+=I11_PIRESULT;
	
       	if(EvaRegs.CMPR1<5000) EvaRegs.CMPR1=5000;
	    else if (EvaRegs.CMPR1>9000) EvaRegs.CMPR1=9000;
	    
      // EvaRegs.CMPR1=5000;
	    
	
    // **第二路电流 ACDINT1 CMPER2**
    
	I12_SAMPLE=AdcRegs.RESULT1>>4;
	
//	if (I12_SAMPLE>2000) EvaRegs.ACTR.all = 0x0a0f;
//	else if(I12_SAMPLE<1024) EvaRegs.ACTR.all=0x0aaf;
	I12_ERROR=I12_REF-I12_SAMPLE;
                
	        I12_SUMERROR+=I12_ERROR;
        	if(I12_SUMERROR<-5000) I12_SUMERROR=-5000;
            else if (I12_SUMERROR>5000) I12_SUMERROR=5000;
            I12_IRESULT=I12_SUMERROR;
            I12_IRESULT=I12_IRESULT*I_KI;
            I12_IRESULT=I12_IRESULT>>8; 
            
           
	        I12_PRESULT=I12_ERROR*I_KP;
	        I12_PRESULT=I12_PRESULT>>2;
	        
	        I12_PIRESULT=I12_PRESULT+I12_IRESULT;
        
           	EvaRegs.CMPR2+=I12_PIRESULT;
	
       	if(EvaRegs.CMPR2<6000) EvaRegs.CMPR2=6000;
	    else if (EvaRegs.CMPR2>9000) EvaRegs.CMPR2=9000;
	    //EvaRegs.CMPR2=9000;

	

⌨️ 快捷键说明

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