1. 程式人生 > >EPwm觸發ADC取樣

EPwm觸發ADC取樣

 
#include "DSP2833x_Project.h"
#include <stdio.h>
#include <string.h>

#define DELAY 35.700L

// ADC start parameters
#define ADC_MODCLK 0x3 // HSPCLK = SYSCLKOUT/2*ADC_MODCLK2 = 150/(2*3)   = 25.0 MHz
#define ADC_CKPS   0x0   // ADC module clock = HSPCLK/1      = 25.5MHz/(1)   = 25.0 MHz
#define ADC_SHCLK  0x1   // S/H width in ADC module periods                  = 2 ADC cycle
#define AVG        1000  // Average sample limit
#define ZOFFSET    0x00  // Average Zero offset
#define BUF_SIZE   2  // Sample buffer size
#define OFFSET    0.015609

float adoffset;
float adsum;

void StartEPwm();

#if (CPU_FRQ_150MHZ)
  #define CPU_CLK   150e6
#endif
#if (CPU_FRQ_100MHZ)
  #define CPU_CLK   100e6
#endif
#define PWM_CLK   10e3                // If diff freq. desired, change freq here.
#define SP        CPU_CLK/(2*PWM_CLK)
#define TBCTLVAL  0x200E              // Up-down cnt, timebase = SYSCLKOUT

// Global variable for this example

float SampleTable[BUF_SIZE];
interrupt void epwm_int(void);
Uint16 led=0;
Uint16 array_index=0;


void main(void)
{
 	Uint16 i;

   InitSysCtrl();

	  EALLOW;
	  SysCtrlRegs.HISPCP.all = ADC_MODCLK;	// HSPCLK = SYSCLKOUT/ADC_MODCLK
	  EDIS;

   InitGpio();
   DINT;

   InitPieCtrl();

   IER = 0x0000;
   IFR = 0x0000;

   InitPieVectTable();

  InitAdc();

  EALLOW;
  PieVectTable.EPWM1_INT=&epwm_int;                     //指向中斷子程式的地址
  EDIS;

   AdcRegs.ADCTRL2.bit.EPWM_SOCA_SEQ1=1;             //允許EPwm的觸發訊號啟動SEQ1
   AdcRegs.ADCTRL2.bit.INT_ENA_SEQ1 = 1 ;          //SEQ1中斷使能


  IER|= M_INT3;
  PieCtrlRegs.PIEIER3.bit.INTx1=1;
  EINT;
  ERTM;



   AdcRegs.ADCTRL1.bit.ACQ_PS = ADC_SHCLK;  // 算出取樣頻率
   AdcRegs.ADCTRL3.bit.ADCCLKPS = ADC_CKPS;     //沒有對高速時鐘分頻
 //  AdcRegs.ADCTRL1.bit.CPS=0;                        //ADC初始化中預設為0
   AdcRegs.ADCTRL1.bit.SEQ_CASC = 1;        // 級聯模式
   AdcRegs.ADCTRL3.bit.SMODE_SEL=0;             //順序取樣
   AdcRegs.ADCTRL1.bit.CONT_RUN=1;               //連續執行
   AdcRegs.ADCTRL1.bit.SEQ_OVRD = 1;       // Enable Sequencer override feature

   AdcRegs.ADCMAXCONV.bit.MAX_CONV1 = 0x1;
   AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 0x0;


   for (i=0; i<BUF_SIZE; i++)
   {
     SampleTable[i] = 0;
   }


   StartEPwm();

 //  AdcRegs.ADCTRL2.bit.SOC_SEQ1=1;

   while(1){}

}

interrupt void epwm_int(void)
{ 

	led++;
	if(led==199){led=0;}                              //觀察視窗檢視值是否有值變化就可以知道是否進入中斷
    if(array_index>BUF_SIZE) {array_index = 0;}
//
       while (AdcRegs.ADCST.bit.INT_SEQ1== 0){}         //當轉換完後中斷標誌位會被置位,這個是判斷是否轉換完
       AdcRegs.ADCST.bit.INT_SEQ1_CLR = 1;               //清除中斷標誌位

       SampleTable[array_index++]= ( (AdcRegs.ADCRESULT0)>>4)*3.0/4095.0-OFFSET;     //計算出模擬量

       EPwm1Regs.ETCLR.bit.INT=1;                    //使中斷標誌位清除
       PieCtrlRegs.PIEACK.bit.ACK3=1;               //清除應答標誌位

}


void configtestledON(void)
{
   EALLOW;
    SYSRUN_SET = 1;
   EDIS;
}

void configtestledOFF(void)
{
   EALLOW;
    SYSRUN_CLEAR = 1;
   EDIS;
}

void delay (Uint16 t)
{ Uint16 i;
  while(t--)
  {
     for(i=0;i<125;i++)
      asm(" RPT #3 || NOP");
  }
}


  void StartEPwm(void)
	{
		InitEPwm1Gpio();

	    SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;       //PWM模組時基時鐘同步使能
	   	EPwm1Regs.TBSTS.all=0;
		EPwm1Regs.TBPHS.half.TBPHS=0;
		EPwm1Regs.TBCTR=0;

		EPwm1Regs.CMPCTL.all=0x50;        // Immediate mode for CMPA and CMPB
		EPwm1Regs.CMPA.half.CMPA =SP/2;
		EPwm1Regs.CMPB=0;

		EPwm1Regs.AQCTLA.all=0x60;        // EPWMxA = 1 when CTR=CMPA and counter inc
		                                  // EPWMxA = 0 when CTR=CMPA and counter dec
		EPwm1Regs.AQCTLB.all=0;
		EPwm1Regs.AQSFRC.all=0;
		EPwm1Regs.AQCSFRC.all=0;

		EPwm1Regs.DBCTL.all=0xb;          // EPWMxB is inverted
		EPwm1Regs.DBRED=0;
		EPwm1Regs.DBFED=0;

		EPwm1Regs.TZSEL.all=0;
		EPwm1Regs.TZCTL.all=0;
		EPwm1Regs.TZEINT.all=0;
		EPwm1Regs.TZFLG.all=0;
		EPwm1Regs.TZCLR.all=0;
		EPwm1Regs.TZFRC.all=0;

		EPwm1Regs.ETSEL.all=0;            // Interrupt when TBCTR = 0x0000
		EPwm1Regs.ETFLG.all=0;
		EPwm1Regs.ETCLR.all=0;
		EPwm1Regs.ETFRC.all=0;

		EPwm1Regs.PCCTL.all=0;               //pwm斬波控制暫存器


//EPwmxSOCA觸發ADC,利用EPwm的中斷觸發中斷訊號
		EPwm1Regs.ETSEL.bit.SOCAEN=1;
		EPwm1Regs.ETSEL.bit.SOCASEL=1;

		EPwm1Regs.ETSEL.bit.INTEN=1;
		EPwm1Regs.ETSEL.bit.INTSEL=1;

		EPwm1Regs.ETPS.bit.SOCAPRD=1;
		EPwm1Regs.ETPS.bit.INTPRD=1;

		EPwm1Regs.ETFLG.bit.INT=1;


		EPwm1Regs.TBCTL.all=0x0010+TBCTLVAL;			// Enable Timer
		EPwm1Regs.TBPRD=SP;
		EDIS;
}

void InitEPwm1Gpio(void)
{
EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO0=0;
GpioCtrlRegs.GPAMUX1.bit.GPIO0=1;
EDIS;
}

//------------------------------------------------------------------------------------
 
//------------------------------------------------------------------------------------