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

📄 dsp_281xsoftadcspwmopen.c

📁 基于dsptms320f2812的实现spwm的控制程序源码
💻 C
字号:
//###########################################################################
//
// FILE:   Example_281xEvPwm.c
//
// TITLE:  DSP281x Event Manager SPWM Generation with Repetitive Control.
//
// ASSUMPTIONS:
//
//          This program requires the DSP281x V1.00 header files. 
//          As supplied, this project is configured for "boot to H0" operation.
//
//          Other then boot mode pin configuration, no other hardware configuration
//          is required.    
//
// DESCRIPTION:
//			Repetitive Control parameters, as follow.
//			foutput = 400Hz, fswitch = 8000Hz, fsample = 24000Hz;
//			Q(z)=0.95; 
//			S1(z)=(a1*z+a2)/(z^2-b2*z+b3), 
//		  		  a1 = 0.0076, a2 = 0.0071, b2 = 1.8212, b3 = 0.8359;
//          S2(z)=(z^(-n)+2+z^n)/4,n=25;
//			z^k = z^14;
//			Kr = 0.3
//###########################################################################
//
//    f   |   fsample  | class | Phases | Description of changes
// =======|============|=======|===============================================
//  400Hz | 24kHz      | SPWM  |    3   | No change since previous version (v.58 Alpha)
//###########################################################################          
//   
//
//###########################################################################
//
//  Ver | dd  mmm  yyyy | Who  | Description of changes
// =====|===============|======|===============================================
//  1.00| 06 April 2007 | zzh  | No change since previous version (v.58 Alpha)
//###########################################################################


#include "DSP281x_Device.h"     // DSP281x Headerfile Include File
#include "DSP281x_Examples.h"   // DSP281x Examples Include File


// Prototype statements for functions found within this file.
//-----  repetitive controller   ------
#define		Nsamp		60			//z^Nsamp ,sample points, Nsamp=fs/f0, fs for sample frequency,and fs=24kHz
									//Nout=f/f0,f for switch frequency,and f=8kHz
#define		Nout		20			//f0 for frequency of output volty,and f0=400Hz
#define		T_HSPCLK	75000		//the Timer counts of the period of switch/HSPCLK, 8e3/75e6

#define		Dif_dqPhase	15			//the dq voltages phase difference of pi/2
#define		Dif_Phase	20			//the voltages phase difference of 2*pi/3

unsigned int  adc_count;				//AD sample count variable
int			i_pwm = 0;				//How many PWM in Every period of 400Hz
int			i_period = 0;

unsigned int s_result[3];		//AD sample temp variable for the output voltage


int sin_tab[Nsamp]={//sine=(115/220)*sine(2*pi*f*t0)*2^4  
                    //(t0=0.5T,1.5T,...,14.5T)       //define the sine table
                    //Q4,digital variabl	                
    				1193,2374,3528,4643,5708,6710,7639,8484,9236,9887,
					10429,10858,11167,11354,11416,11354,11167,10858,10429,9887,
					9236,8484,7639,6710,5708,4643,3528,2374,1193,0,
					//pi
					-1193,-2374,-3528,-4643,-5708,-6710,-7639,-8484,-9236,-9887,
					-10429,-10858,-11167,-11354,-11416,-11354,-11167,-10858,-10429,-9887,
					-9236,-8484,-7639,-6710,-5708,-4643,-3528,-2374,-1193,0		
		            //2*pi	(60 points)	                
		            };	                		                

int *check_tab=sin_tab;				//check sine table point variabe

void InitGpio(void);
void InitEVAB_timer(void);
void ADC_sample(int adc_count);
void EVA_timer1_pwm(int pwm_count);


interrupt void EVA_timer1_pwm_isr(void);


void main(void)
{
  InitSysCtrl();
	
	InitGpio();
	
	DINT;
		
	InitPieCtrl();
	
	IER = 0x0000;
	IFR = 0x0000;
	
	InitPieVectTable();
	
	//EALLOW;
	//PieVectTable.T1PINT = &EVA_timer1_pwm_isr;
	//EDIS;
	
	InitEVAB_timer();
	
	
	InitAdc();
    
    
    
    PieCtrlRegs.PIEIER2.bit.INTx4 = 1;		//Enable T1PINT
    PieCtrlRegs.PIEIER3.bit.INTx1 = 1;		//Enable T2PINT
    PieCtrlRegs.PIEIER4.bit.INTx4 = 1;		//Enable T3PINT
    
        
    //IER |= M_INT1; 		// Enable CPU Interrupt 1
    IER |= M_INT2;
    IER |= M_INT3;
    IER |= M_INT4;
    EINT;          		// Enable Global interrupt INTM
    ERTM;          		// Enable Global realtime interrupt DBGM
    
    EvbRegs.EVBIFRA.bit.T3PINT = 1;
    EvaRegs.EVAIFRB.bit.T2PINT = 1;
    EvaRegs.EVAIFRA.bit.T1PINT = 1;
    
    while (1)
    	{
    	
    	while (EvbRegs.EVBIFRA.bit.T3PINT != 1)
    		{        	
    		for (adc_count=0;adc_count<Nsamp;adc_count++)
    			{
    	    	
    	    	
    	    	//if (adc_count>=2)
    	    	//	{
    	    	AdcRegs.ADCTRL2.bit.SOC_SEQ1 = 1;		//Soft to start ADC	    
    			ADC_sample(adc_count);
    			//	}
    				
    			while (EvaRegs.EVAIFRB.bit.T2PINT != 1)
    				{
    		
    				}
    							
    			if (EvaRegs.EVAIFRA.bit.T1PINT == 1) EVA_timer1_pwm(adc_count);
    				
    	    	    	    					
    			}
    			
    	
    			
    		}
    	i_pwm = 0;
    	
    	i_period++;
    					
    	EvbRegs.EVBIFRA.bit.T3PINT = 1;
    	PieCtrlRegs.PIEACK.bit.ACK4 = 1;
    
    	}   	
  
}

// =========end main programme====================================
//=================================================================

//===================start sub_programmes==========================

/*---------------------Initialization GPIO------------------------*/
void InitGpio()
{
	
// Set GPIO A port pins,AL(Bits 7:0)(input)-AH(Bits 15:8) (output) 8bits
// Input Qualifier =0, none
     EALLOW;
     GpioMuxRegs.GPAMUX.all = 0x00FF;   // GPIOA0-5: PWM pins
                                        // GPIOA6,7: GPIO pins
                                        // GPIOA8,9: QEP interface
                                        // GPIOA10-15: GPIO pins     
     EDIS;

}


/*---------------Initialization of EVA timer3----------------------*/
void InitEVAB_timer(void)
{	
	
	EvaRegs.EVAIMRA.all = 0x0000;		//Enable EVA/EVB all INT
	EvaRegs.EVAIMRB.all = 0x0000;
	EvbRegs.EVBIMRA.all = 0x0000;
	EvbRegs.EVBIMRB.all = 0x0000;
	
	EvaRegs.GPTCONA.all = 0x0000;		
	EvaRegs.GPTCONA.bit.TCMPOE = 1;			//Enable Timer 1/2 compare
	EvaRegs.GPTCONA.bit.T1CMPOE = 1;
	EvaRegs.GPTCONA.bit.T1PIN = 0x2;		//Polarity of GP Timer 1 Compare = Active High
											      //Timer1=X,X is HSPCLK
	EvaRegs.T1PR = (unsigned long)0x249F;	//Timer1 period 1/8kHz 
	EvaRegs.T1CNT = 0x0000;     			// Timer1 counter
	
	EvaRegs.T1CON.all = 0x1046;				//Timer1 as sampling and computing clock
											//TMODE = continuous up
	EvaRegs.CMPR1 = 0x0000;
  EvaRegs.CMPR2 = 0x0000;
  EvaRegs.CMPR3 = 0x0000;					//TMODE = continuous up
        
  EvaRegs.ACTRA.all = 0x0666;			//PWM1-3-5 is active for High
    									//PWM2-4-6 is active for Low
  EvaRegs.DBTCONA.all = 0x0AF0; 	   	// deadband time is 5.2us 
  EvaRegs.COMCONA.all = 0xA6E0;				// Compare action control.  Action that takes place
  												// on a cmpare event 
	
	EvaRegs.T2PR = (unsigned long)0x0C35;	//Timer2 period 1/24kHz 	        						
	EvaRegs.T2CNT = 0x0000;     			// Timer2 counter
	EvaRegs.T2CON.all = 0x1046;				//Disable the Timer2 compare
    
	
	//Timer3 as the Output Voltage Period Control clock
	EvbRegs.T3PR = (unsigned long)0x2DC7;	//T3PR Period is 2.5ms (400Hz)
	EvbRegs.T3CNT = 0x0000;
	EvbRegs.T3CON.all = 0x1446;				//TMODE = continuous up
											//Timer3 CLK = HSPCLK/16
 
}


//---------------------ISR of ADC sampling------------------------
void  ADC_sample(int adc_count)     // ADC
{
  	
  	//int vdq_d = 0,vdq_q = 0;								//dq convert temp variable value
	
//	int temp_vedq_d1 = 0,temp_vedq_d2 = 0,temp_vedq_d3 = 0;	//intermediuate variable variable for computing
//	int temp_vedq_d4 = 0,temp_vedq_d5 = 0,temp_vedq_d6 = 0;	//control variable udq_d[] and udq_d[]
		
//	int	temp_vedq_q1 = 0,temp_vedq_q2 = 0,temp_vedq_q3 = 0;
//	int temp_vedq_q4 = 0,temp_vedq_q5 = 0,temp_vedq_q6 = 0;
	
  	
  	EvaRegs.EVAIFRB.bit.T2PINT = 1;
	
	  asm(" RPT #10 || NOP");   	//running 10+1 NOP
  	
  	s_result[0] = AdcRegs.ADCRESULT0>>4;	// APF output voltage -Vab
   	s_result[1] = AdcRegs.ADCRESULT1>>4;	// APF output voltage -Vbc
	  s_result[2] = AdcRegs.ADCRESULT2>>4;	// APF output voltage -Vca

  	
	
	// ------Reset the system configuration----- 	
	AdcRegs.ADCST.bit.INT_SEQ1_CLR = 1;       	// Clear INT SEQ1 bit
	AdcRegs.ADCTRL2.bit.RST_SEQ1 = 1;         	// Reinitialize for next ADC sequence
	//PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;   	// Acknowledge interrupt to PIE
  	PieCtrlRegs.PIEACK.bit.ACK3 = 1;
  	
  	
	return;
}



//----------------------------
void EVA_timer1_pwm(int pwm_count)
{
	
	i_pwm++;
	
	EvaRegs.EVAIFRA.bit.T1PINT = 1;
	EvaRegs.EVAIFRB.bit.T2PINT = 1;
			
			
	EvaRegs.CMPR1 = ((3*T_HSPCLK*(1365+((*(check_tab+adc_count))>>4))/4095)>>4);	//tpwm=(Tsw/2)*(1+ma*sin_tab[k])/Thspclk
																	// = (fhspclk/(2*fsw))*(1+ma*sin_tab[k])
																	// = (75e6/(2*8e3))(1+ma*sin_tab[k])
																	// = 75e3*(1+ma*sin_tab[k])/2^4
	if (adc_count<Dif_Phase)			
		EvaRegs.CMPR2 = ((3*T_HSPCLK*(1365+((sin_tab[Nsamp-Dif_Phase+adc_count])>>4))/4095)>>4);						
	else if (Dif_Phase<=adc_count<Nsamp)
		EvaRegs.CMPR2 = ((3*T_HSPCLK*(1365+((sin_tab[adc_count-Dif_Phase])>>4))/4095)>>4);
							
	if (adc_count<Nsamp-Dif_Phase)
		EvaRegs.CMPR3 = ((3*T_HSPCLK*(1365+((sin_tab[adc_count+Dif_Phase])>>4))/4095)>>4);
	else if (Nsamp-Dif_Phase<=adc_count<Nsamp)
		EvaRegs.CMPR3 = ((3*T_HSPCLK*(1365+((sin_tab[adc_count-Nsamp+Dif_Phase])>>4))/4095)>>4);
	
							
	
	PieCtrlRegs.PIEACK.bit.ACK2 = 1;				
	
	return;

}


//===================Over sub_programmes==========================
//===========================================================================
// No more.
//===========================================================================

⌨️ 快捷键说明

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