display Posted June 14, 2017 Report Share Posted June 14, 2017 I need to finish a program before I graduate. And I wrote one. However SIMULATOR displayed PWM CLOCK FASTER THAN FOSC When debugging. I can’t found troubles. My code is given below. Hope someoone here can help me ! The datasheet of DSPIC30F2020 #include <p30F2020.h>_FOSCSEL(FRC_PLL); //enable PLL,FRC crystal oscillator_FOSC(CSW_FSCM_OFF&FRC_HI_RANGE); //ban clock to switch,High frequency mode_FPOR(PWRT_128);//power on reset128MS_FWDT(FWDTEN_OFF); //The watchdog timer is prohibited _FGS(CODE_PROT_OFF);//Code protection closedunsigned int const pwmtab[200] = {0,631,1262,1892,2522,3152,3781,4409,5035,5661,6285,6908,7528,8147,8764,9379,9992,10602,11209,11814,12416,13015,13610,14202,14791,15376,15957,16534,17107,17676,18241,18801,19356,19907,20453,20993,21529,22059,22584,23103,23617,24124,24626,25122,25611,26094,26571,27041,27505,27961,28411,28854,29289,29718,30139,30553,30959,31357,31748,32131,32506,32873,33232,33582,33925,34259,34584,34901,35210,35509,35800,36082,36355,36620,36875,37121,37358,37586,37804,38013,38213,38403,38584,38755,38917,39069,39212,39345,39468,39581,39685,39779,39863,39937,40001,40056,40100,40135,40160,40175,40180,40175,40160,40135,40100,40056,40001,39937,39863,39779,39685,39581,39468,39345,39212,39069,38917,38755,38584,38403,38213,38013,37804,37586,37358,37121,36875,36620,36355,36082,35800,35509,35210,34901,34584,34259,33925,33582,33232,32873,32506,32131,31748,31357,30959,30553,30139,29718,29289,28854,28411,27961,27505,27041,26571,26094,25611,25122,24626,24124,23617,23103,22584,22059,21529,20993,20453,19907,19356,18801,18241,17676,17107,16534,15957,15376,14791,14202,13610,13015,12416,11814,11209,10602,9992,9379,8764,8147,7528,6908,6285,5661,5035,4409,3781,3152,2522,1892,1262,631};unsigned char num; //Look-up table order registerunsigned char sign; //Sine table output symbolfloat sin_l;void PortInit() { // LATE= 0x0000; // E pot reset TRISE = 0x0000; //set E pot to output , 0: out, 1: in TRISB = 0x0000; //set E pot to output, 0: out, 1: in PORTE = 0x0000; PORTB = 0x0000; OSCCONbits.TSEQEN=0;//OSCTUN TUN<3:0> in register adjust FRC oscillator OSCTUNbits.TUN = 0111; /* FRC时钟为15M */ } void PWM_Config() { FCLCON1=0X0003; // Disable the current limit and fault typeIOCON1bits.PENH = 1; /* PWM1H PWM module will control PWM output pin */ IOCON1bits.PENL = 1; /* PWM1L PWM module will control PWM output pin */ IOCON1bits.PMOD = 00; /* Complementary output mode */ IOCON1bits.POLH = 0; //Effective if PWMxH pin is high levelIOCON1bits.POLL = 0; /Effective if PWMxL pin is high leve/* Load PDTR1 and ALTDTR2 register with preset dead time value */ DTR1 = 64; /* Dead band time Settings */ ALTDTR1 = 64; /* dead time */ /* PHASE1 register */ PHASE1 = 0; /* No phase shift */ /* PWM Time Base Control Register */ PTCONbits.PTEN = 1; // enable PWM modulePTCONbits.EIPU = 1; /Register/update immediately effective cycle PWMCON1bits.IUE = 1; //Update immediately effective pdc registerPWMCON1bits.MDCS = 0;//PDC provide duty ratio PTPER = 47620; //30mips为47620 ,cycle=(PTPER+1)/120MHZ PDC1 = 0; //Duty ratio reset _PWM1IE=1;//Allow the interrupt request_PWM1IF=0;//Interrupt flag reset _PWM1IP=6; //set interrupt priority level to be 6 _IPL=1;//CPU interrupt priority level is 1} int main() { num = 0; // A variable initializationsign = 0; PortInit() ;PWM_Config();while(1);//Have been circulating wait for interrupt}void __attribute__((interrupt, no_auto_psv)) _PWM1Interrupt(void){ _PWM1IF=0;//Interrupt flag bit reset if (!num) /fi num=0,mean that every sign invert the look-up table cycle, used to determine the positive and negative half cycle waveform sign = ~sign; //0 to be 1 ++num; // Sine table order value if (num>=200) //reset num after checking 200 potsnum = 0; sin_l= pwmtab[num]; PDC1=(unsigned char)sin_l;//The assignment if(sign) {PORTBbits.RB5=1;}//reversing else {PORTBbits.RB5=0;}// reversing}[/code[ Appreciation! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.