#include "DSP2833x_Project.h"
#include
#include
#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; iBUF_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;
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------