刚开始学习dsp 28335 , 在学习了epwm 和adc 模块之后,自己想做个实验,但是不知道思路对不对?麻烦大家指导下。 我想通过Epwmx A 的soc 触发ADC 中断, 在中断里边进行adc 采样 和PI 调节占空比,然后通过示波器观察输出的pwm 波的占空比是否有变化, 不知道这样可行吗? ( 另外,我把采样的电压作为反馈电压和参考值对比,得到误差Err . ) 因为没有实际的系统,所以我的pi 参数随便给了个值, 做这个实验目的只是为了把epwm , ADC ,PI 调节纳合在一块,验证下自己的想法和掌握的情况。。 不知道自己编写程序的时序不对还是缺少什么东西, 在示波器上看不见PWM 波。 我把自己写的程序贴上来,大家帮我看看,谢谢了。。。
/*
使用epwm6A 产生pwm波,并且触发ADC 采样 ADCA0(就是板子上外扩的那个点位器的电压)
*/
#include "DSP2833x_Device.h" // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h" // DSP2833x Examples Include File
typedef struct
{
float Ref ;
float Fdb ;
float Kp;
float Ki ;
float Kd;
float T ; // 此值待修改
float a0 ;
float a1 ;
float a2 ;
float Err ;
float Err_1 ;
float ERR_2 ;
float Out ;
float Out_1 ;
float OutMax ;
float OutMin ;
}PID;
PID epwm_info ;
float adc[10] ;
float adcresultsa0[] ;
float SampleVoltage ;
void ADC_Convert(void) ;
void InitEPwm6Example(void);
void pid_cal(PID*);
void sequence(float a[],int i) ;
interrupt void adc_isr(void);
#define EPWM6_TIMER_TBPRD 750 // Period register value
#define ADC_MODCLK 0x3 // HSPCLK = SYSCLKOUT/2*ADC_MODCLK2 = 150/(2*3) = 25.0 MHz
#define ADC_CKPS 0x1 // ADC module clock = HSPCLK/2*ADC_CKPS = 25MHz/(2*1) = 12.5MHz
#define ADC_SHCLK 0xf // S/H width in ADC module periods = 16 ADC clocks
void main(void)
{
// Step 1. Initialize System Control:
InitSysCtrl();
EALLOW;
SysCtrlRegs.HISPCP.all = ADC_MODCLK; // HSPCLK = SYSCLKOUT/(2*ADC_MODCLK)=25MHZ
EDIS;
// Step 2. Initialize GPIO:
InitEPwm6Gpio();
//Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;
// Initialize the PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
EALLOW;
PieVectTable.ADCINT=&adc_isr;
EDIS;
// Step 4. Initialize all the Device Peripherals:
InitAdc();
// Specific ADC setup for this example:
AdcRegs.ADCTRL3.bit.ADCCLKPS = ADC_CKPS;//ADC内核时钟分频:HSPCLK/2=12.5MHZ
AdcRegs.ADCTRL1.bit.ACQ_PS = ADC_SHCLK;//设置采样窗口时间:(15+1)*ADCCLK
AdcRegs.ADCTRL1.bit.SEQ_CASC = 1; // 1选择级联模式
AdcRegs.ADCTRL1.bit.CONT_RUN = 1; // 设置为连续运行
AdcRegs.ADCMAXCONV.all = 0x0000; // Setup 1 conv's on SEQ1
AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 0x0; // Setup ADCINA0 as 1st SEQ1 conv.
AdcRegs.ADCTRL2.bit.EPWM_SOCA_SEQ1 = 1;// Enable SOCA from ePWM to start SEQ1
AdcRegs.ADCTRL2.bit.INT_ENA_SEQ1 = 1; // Enable SEQ1 interrupt (every EOS)
// configure EPwm module
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;
EDIS;
InitEPwm6Example();
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
EDIS;
// Step 5. User specific code, enable interrupts:
IER |= M_INT1;
PieCtrlRegs.PIEIER1.bit.INTx6 = 1;
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
}
interrupt void adc_isr()
{
ADC_Convert();
epwm_info.Ref=1.5 ;
epwm_info.Fdb=SampleVoltage ;
epwm_info.Err=epwm_info.Ref-epwm_info.Fdb ;
epwm_info.Kp=0.02 ;
epwm_info.Ki=0.001 ;
epwm_info.Kd=0 ;
epwm_info.OutMax=0.5 ;
epwm_info.OutMin=0 ;
pid_cal(&epwm_info) ;
AdcRegs.ADCST.bit.INT_SEQ1_CLR = 1; // Clear INT SEQ1 bit
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}
void InitEPwm6Example(void)
{
// Setup TBCLK
EPwm6Regs.TBCTL.bit.CTRMODE=TB_COUNT_UP; // Count up;
EPwm6Regs.TBPRD= EPWM6_TIMER_TBPRD ;
EPwm6Regs.TBCTL.bit.PHSEN=TB_DISABLE ;
EPwm6Regs.TBPHS.half.TBPHS=0x0000 ;
EPwm6Regs.TBCTR=0x0000;
EPwm6Regs.TBCTL.bit.HSPCLKDIV=TB_DIV2 ;
EPwm6Regs.TBCTL.bit.CLKDIV=TB_DIV1 ;
// Setup shadow register load on ZERO
EPwm6Regs.CMPCTL.bit.SHDWAMODE= CC_SHADOW;
EPwm6Regs.CMPCTL.bit.LOADAMODE= CC_CTR_ZERO;
// Set Compare values
EPwm6Regs.CMPA.half.CMPA= (epwm_info.Out)*EPWM6_TIMER_TBPRD; ////此值待修改
// Set actions
EPwm6Regs.AQCTLA.bit.ZRO=AQ_SET;
EPwm6Regs.AQCTLA.bit.CAU=AQ_CLEAR;
EPwm6Regs.ETSEL.bit.SOCAEN = 1; // Enable SOC on A group
EPwm6Regs.ETSEL.bit.SOCASEL = 4; // Select SOC from from CPMA on upcount
EPwm6Regs.ETPS.bit.SOCAPRD = 1; // Generate pulse on 1st event
}
void ADC_Convert(void)
{
AdcRegs.ADCTRL2.bit.RST_SEQ1=1 ;
SampleVoltage=((AdcRegs.ADCRESULT0)>>4)*3/4096.0 ;
}
void pid_cal(PID *epwm_info)
{
float a0 ,a1 ,a2 ;
a0=epwm_info->Kp+epwm_info->Ki*epwm_info->T+epwm_info->Kd/epwm_info->T ;
a1=epwm_info->Kp+2*epwm_info->Kd/epwm_info->T ;
a2=epwm_info->Kd/epwm_info->T ;
epwm_info->Out=epwm_info->Out_1+a0*epwm_info->Err-a1*epwm_info->Err_1+a2*epwm_info->ERR_2 ;
if(epwm_info->Out>epwm_info->OutMax)
epwm_info->Out=epwm_info->OutMax ;
if(epwm_info->Out<epwm_info->OutMin)
epwm_info->Out=epwm_info->OutMin ;
epwm_info->Out_1=epwm_info->Out ;
epwm_info->ERR_2=epwm_info->Err_1 ;
epwm_info->Err_1=epwm_info->Err ;
}
|