PWM Interruption

6 years 2 weeks ago #29532911 by ailex1995
PWM Interruption was created by ailex1995
Hello~all
I am a novice that learning programing. Recently I am trying to program an project that using PWM to produce interruption. The develop board I used is DSPIC33EP32MC202 . This is the DSPIC33EP32MC202's datasheet .Before I added interruption, the PWM wave is normal. I don’t know what it occurred after adding interruption. Can anyone help me have a look? Thank you in advance.
#include <P33ep32mc202.h>
//Configure a bit 
_FGS(0xff);//Write protect closed
_FOSCSEL(0xfb);//Double speed oscillator begin to make  PWM closed  master oscillator with PLL
_FOSC(0xdd);//ban clock to change ,more configuration in the outside pins,  SOC2 digita IO XT crystal vibration mode
_FWDT(0x7f);//ban watchdog
_FPOR(0xff);//I2C1map on SAD1/SCL1pin Under voltage reset_
_FICD(0x03);//ban JTAG to communicate byPGEC1 PGED1

void Delay_1ms(unsigned int t)//t = 1000 about 1s
{
    unsigned int i,j;
    for(i = 0;i < t;i ++)
        for(j = 0;j < 2200;j ++);
}
//ossilator configuration
void System_Colck(void)
{
    //产生Fosc = 140MHz 70MIPS
    CLKDIVbits.PLLPRE = 0;//N1 = 2  7.3728MHz/2= 2MHz
    PLLFBDbits.PLLDIV = 74;//M = 76  3.6864*76 = 279.68MHz
    CLKDIVbits.PLLPOST = 0;//N2 = 2 279.68/2 = 139.84MHz
    while (OSCCONbits.COSC!= 0b011)
    while (OSCCONbits.LOCK!= 1) {};//PLL at the situation of closing
}
void System_Init(void)
{
    TRISB &= ~0xfc00;//RB10-RB15 reset
    ANSELB = 0x0000;//digital pin
    //IOCON1 = 0x0000;//GPIO module control  PWMxH PWMxL pin

}

void PWM_Init(void)
{
    //complementation PWM mode  independent duty ratio and phase,stable mian cycle,
    //PWM deblocking
    asm volatile ("mov #0xabcd,w10"); 
    asm volatile ("mov #0x4321,w11"); 
    asm volatile ("mov #0x0003,w0");//ban fault input 
    asm volatile ("mov w10, PWMKEY"); 
    asm volatile ("mov w11, PWMKEY");
    asm volatile ("mov w0,FCLCON1");
    asm volatile ("mov #0xabcd,w10"); 
    asm volatile ("mov #0x4321,w11"); 
    asm volatile ("mov #0xC000,w0");//complementated output,effective high level
    asm volatile ("mov w10, PWMKEY");
    asm volatile ("mov w11, PWMKEY");
    asm volatile ("mov w0,IOCON1");
    
    asm volatile ("mov #0xabcd,w10"); 
    asm volatile ("mov #0x4321,w11"); 
    asm volatile ("mov #0x0003,w0"); 
    asm volatile ("mov w10, PWMKEY"); 
    asm volatile ("mov w11, PWMKEY");
    asm volatile ("mov w0,FCLCON2");
    asm volatile ("mov #0xabcd,w10"); 
    asm volatile ("mov #0x4321,w11"); 
    asm volatile ("mov #0xC000,w0");
    asm volatile ("mov w10, PWMKEY");
    asm volatile ("mov w11, PWMKEY");
    asm volatile ("mov w0,IOCON2");
    
    asm volatile ("mov #0xabcd,w10"); 
    asm volatile ("mov #0x4321,w11"); 
    asm volatile ("mov #0x0003,w0"); 
    asm volatile ("mov w10, PWMKEY"); 
    asm volatile ("mov w11, PWMKEY");
    asm volatile ("mov w0,FCLCON3");
    asm volatile ("mov #0xabcd,w10"); 
    asm volatile ("mov #0x4321,w11"); 
    asm volatile ("mov #0xC000,w0");
    asm volatile ("mov w10, PWMKEY");
    asm volatile ("mov w11, PWMKEY");
    asm volatile ("mov w0,IOCON3");
    
    PTPER = 7000;// cycle register =Fosc/(Fpwm*PCLRDIV(2:0)) PWM frequency 20K
    PHASE1 = 0;
    PHASE2 = 2333;
    PHASE3 = 4667;//PWM register (Adjust the phase shift, is the offset between the waveform)
    PDC1 = 3500;
    PDC2 = 3500;
    PDC3 = 3500;//PWM Generator duty ratio register  1/2
    DTR1 = DTR2 = DTR3 = 25;//PWM Dead zone registers
    ALTDTR1 = ALTDTR2 = ALTDTR3 = 25;//PWM Standby dead zone registers
    PWMCON1 = 0x0400;
    PWMCON2 = 0x0400;
    PWMCON3 = 0x0400;//PWM control register ,Edge alignment ,Are dead zone
    PTCON2bits.PCLKDIV = 0;//PWM Clock frequency division than register a frequency division
    SEVTCMP = 0;// Special events more value
    //enable PWM to allow every time interrupted match when special events, frequency 20K
    PTCONbits.SEIEN = 1;
    PTCONbits.SEVTPS = 0;
    while (PTCONbits.SESTAT == 1);
    
    //TRIG1 = 0;                 
    //TRGCON1bits.TRGDIV = 0;
    //TRGCON1bits.TRGSTRT = 0;
    //PWMCON1bits.TRGIEN = 1;
    //while (PWMCON1bits.TRGSTAT == 1);
    
    PTCONbits.PTEN = 1;
}       

void Interrupt_Int1_Init(void)
{
    INTCON1bits.NSTDIS = 0;//interrupt nesting enable
    CORCONbits.IPL3 = 0;
    SRbits.IPL = 0;//set CPU priority to be  0
    
    IPC14bits.PSEMIP = 5;//PWM special events,interrupt priority
    IFS3bits.PSEMIF = 0;//PWM special events interrupt signal resetting    IEC3bits.PSEMIE = 1;//allow interruption
    
    //IPC23bits.PWM1IP = 6;//PWM1trigger event interrupt priority6
    //IFS5bits.PWM1IF = 0;//PWM1interrupt signal reset
    //IEC5bits.PWM1IE = 1;//allow interruption
    
    INTCON2bits.GIE = 1;//Global interruption allows
}

void __attribute__((__interrupt__, auto_psv)) _INT1Interrupt(void)
{
    //IFS5bits.PWM1IF = 0;//PWM1interrupt signal reset
    IFS1bits.INT1IF = 0;//outside interrupt signal reset
}

int main()
{
    System_Colck();
    System_Init();
    PWM_Init();
    Interrupt_Int1_Init();
    while(1)
    {
   
    }
}

Best wishes~

Please Log in or Create an account to join the conversation.

Moderators: tonysantoni
Time to create page: 0.077 seconds