|
发表于 2010-3-20 10:50:33
|
显示全部楼层
关于SPWM产生正弦波的问题
#include "DSP280x_Device.h" // DSP280x Headerfile Include File
#include "DSP280x_Examples.h" // DSP280x Examples Include File
#include "IQmathLib.h"
// Prototype statements for functions found within this file.
void InitEPwm1Example(void);
interrupt void epwm1_isr(void);
#define PI 3.1415926
_iq27 pwmsin,Pi;
unsigned int EPWM1_TIMER_TBPRD;
unsigned int flt,n,T1,counter;
float i,j,T,T0,t,t1,f,flt1;
unsigned char flat;
void main(void)
{
flat = 1;
i=0;
f=1000;
T=1/f;
T0 = T*600000;
counter = (Uint16)(T0);
EPWM1_TIMER_TBPRD=100;
// This example function is found in the DSP2802x_SysCtrl.c file.
InitSysCtrl();
// Step 2. Initalize GPIO:
// This example function is found in the DSP2802x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio(); // Skipped for this example
// For this case just init GPIO pins for ePWM1, ePWM2, ePWM3
// These functions are in the DSP2802x_EPwm.c file
InitEPwm1Gpio();
// 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.
// This function is found in the DSP2802x_PieCtrl.c file.
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP2802x_DefaultIsr.c.
// This function is found in DSP2802x_PieVect.c.
InitPieVectTable();
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.EPWM1_INT = &epwm1_isr;
EDIS; // This is needed to disable write to EALLOW protected registers
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP2802x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
// For this example, only initialize the ePWM
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;
EDIS;
InitEPwm1Example();
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
EDIS;
// Step 5. User specific code, enable interrupts:
// Enable CPU INT3 which is connected to EPWM1-3 INT:
IER |= M_INT3;
// Enable EPWM INTn in the PIE: Group 3 interrupt 1-3
PieCtrlRegs.PIEIER3.bit.INTx1 = 1;
PieCtrlRegs.PIEIER3.bit.INTx2 = 1;
PieCtrlRegs.PIEIER3.bit.INTx3 = 1;
// Enable global Interrupts and higher priority real-time debug events:
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
// Step 6. IDLE loop. Just sit and loop forever (optional):
for(;;)
{
asm(" NOP");
}
}
interrupt void epwm1_isr(void)
{
// Update the CMPA and CMPB values
//update_compare(&epwm1_info);
if(i<=2*PI)
{
Pi=_IQ27(i);
pwmsin= _IQ27sin(Pi);
j=_IQ27toF(pwmsin)+1;
t=j/2.0*100.0;
flt=(Uint16)(t);
EPwm1Regs.CMPA.half.CMPA =flt;
i = i + 2.0*PI/counter;
}
else
{
i=0;
}
// Clear INT flag for this timer
EPwm1Regs.ETCLR.bit.INT = 1;
// Acknowledge this interrupt to receive more interrupts from group 3
PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
}
void InitEPwm1Example()
{
// Setup TBCLK
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP; // Count up
// EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;
EPwm1Regs.TBPRD = EPWM1_TIMER_TBPRD; // Set timer period
EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE; // Disable phase loading
// EPwm1Regs.TBCTL.bit.PRDLD = TB_SHADOW;
EPwm1Regs.TBPHS.half.TBPHS = 0x0000; // Phase is 0
EPwm1Regs.TBCTR = 0x0000; // Clear counter
EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1; // Clock ratio to SYSCLKOUT
//EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV2;
EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV1;
// Setup shadow register load on ZERO
EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
EPwm1Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;
EPwm1Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;
// Set Compare values
// EPwm1Regs.CMPA.half.CMPA = EPWM1_MIN_CMPA; // Set compare A value
// Set actions
//****************************************
EPwm1Regs.AQCTLA.bit.ZRO = AQ_SET; // Set PWM1A on Zero
EPwm1Regs.AQCTLA.bit.CAU = AQ_CLEAR; // Clear PWM1A on event A, up count
//******************************************
// EPwm1Regs.AQCTLA.bit.PRD = AQ_CLEAR;
// EPwm1Regs.AQCTLA.bit.CAU = AQ_SET;
// Clear PWM1B on event B, up count
// Interrupt where we will change the Compare Values
EPwm1Regs.ETSEL.bit.INTSEL = ET_CTR_ZERO; // Select INT on Zero event
EPwm1Regs.ETSEL.bit.INTEN = 1; // Enable INT
// EPwm1Regs.ETPS.bit.INTPRD = ET_3RD; // Generate INT on 3rd event
EPwm1Regs.ETPS.bit.INTPRD = ET_1ST;
}
我用的是改变占空比的方法产生正弦波,输入为设定的一个频率大小值,程序中以脉冲周期恒定,脉冲个数可调来实现信号周期的变化。输出为一个与输入相对应的SPWM信号,通过滤波产生正弦波。
问题:通过公式:1/f_sin/counter=f_pulse=C*(1/f_sys) ( f_sin:设定输出信号的频率值。counter:信号周期中单位脉冲个数。f_pulse:单位脉冲的频率。C:单位脉冲周期的计数值。f_sys:系统时钟。)计算出相应的各个参数,但是现在我计算出来的数据和实际的不一致
谢谢大家指教!!! |
|