Jump to content

Recommended Posts

Posted

I need to finish a program before I graduate. And I wrote one. However SIMULATOR displayed PWM CLOCK FASTER THAN FOSC When debugging. I cant 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 closed

unsigned 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 register
unsigned char sign; //Sine table output symbol
float  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 type


IOCON1bits.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 level

IOCON1bits.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 module
PTCONbits.EIPU = 1; /Register/update immediately effective cycle 
PWMCON1bits.IUE = 1; //Update immediately effective pdc register
PWMCON1bits.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 initialization
sign = 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 pots
num = 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!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...