新會員
Joined
: 2008/5/10 16:39 最後登入時間
: 2008/7/29 11:27
Group:
註冊會員
Level : 1 HP : 0 / 0 MP : 0 / 32 EXP : 2
|
我想弄一个SPWM的波,但是现在什么也看不见,一直是高电平,这个是内部晶振32倍频的,所用调试环境MPLAB ICD2 v8.10 编译器是C30,外部有4M的晶振,再有就是PTPER时间怎么算,希望各位不吝赐教,程序如下:
#include "p30f2020.h" #include "math.h" #define PI 3.14159
/* Configuration Bit Settings */
_FOSCSEL(FRC_PLL) _FOSC(CSW_FSCM_OFF & FRC_HI_RANGE & OSC2_CLKO) _FPOR(PWRT_128) _FGS(CODE_PROT_OFF) _FBS(BSS_NO_FLASH) /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
int f,N,k; double Pul,w,M;
void __attribute__((__interrupt__)) _PWM1Interrupt() { IFS1bits.PWM1IF = 0; M=1.0,w=2*PI*f; /* ~~~~~~ Load PDC1 register with initial Duty Cycle value ~~~~~~~~~~*/ Pul=(M/w)*(cos((k-1)*PI/N)-cos(k*PI/N)); PDC1=Pul*952*1000000; k++; }
int main() { N=1000,f=50,Pul=0.0,k=1;
//~~~~~~~~~~~~~~~~~~~~~~~ FRC Dither Configuration ~~~~~~~~~~~~~~~~~~~~~~~~ __builtin_write_OSCCONL(4); // Built-in function to unlock // OSCCON register // Writing 0x0004 sets the TSEQEN // bit to enable FRC tune sequencer
// FRC will tune to one of the following every 8 PWM cycles OSCTUNbits.TUN = 8; // Minimum frequency OSCTUNbits.TSEQ1 = 10; OSCTUNbits.TSEQ2 = 12; OSCTUNbits.TSEQ3 = 14; //~~~~~~~~~~~~~~~~~~~~End FRC Dither Configuration ~~~~~~~~~~~~~~~~~~~~~~~~ TRISE = 0; /* Set RE as a digital output */ PORTE=0x00; LATE=0x00; IOCON1bits.PENH = 1; // PWM1 outputs controlled by PWM IOCON1bits.PENL = 1; // module IOCON1bits.PMOD = 1; //Select Independent Output PWM mode PTCONbits.EIPU = 1; //立即更新有效周期寄存器 PWMCON1bits.IUE = 1; //立即对有效的pdc寄存器进行更新 /*~~~~~~~~~~~~~~~~~~~~~~ End PWM Configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* -------------------- INTERRUPTS INITIALIZATION -------------------------------------*/ IPC4bits.PWM1IP = 4; /* PWM1 Interrupt Priority */ IFS1bits.PWM1IF = 0; /* Clear PWM1 Interrupt Flag */ IEC1bits.PWM1IE = 1; /* PWM1 Interrupt Enable */ /* -------------------- PWM1 INITIALIZATION -------------------------------------------- */
TRGCON1 = 0x0000; /* PWM Trigger interrupt generated every PWM cycle*/ / / Trigger generated 32nsec from beginning // of PWM period TRIG1 = 0X0008; /* TRIGx value of 0 does not produce a trigger. So choose smallest possible Trigger time = TRIG1*1.05nsec = 8.4 nsec */ PWMCON1bits.TRGIEN = 1; // Enable PWM1 Trigger Interrupt, PWMCON1bits.DTC = 2; // Primary Time Base provides timing, // PDC1 provides Duty Cycle, // Dead Time function is disabled, // Enable immediate duty cycle update PTPER = 9520; PDC1=4761; PHASE1 = 0; /* No Phase shift for PWM1 */ PTCONbits.PTEN = 1; // Turn ON PWM module //PWM_LOOP(); while(1); // Infinite loop }
發表於: 2008/5/10 17:04
|