Hot!PIC32MK: PWM interruption

Author
lelis
Junior Member
  • Total Posts : 94
  • Reward points : 0
  • Joined: 2010/03/27 15:41:52
  • Location: 0
  • Status: offline
2018/03/13 15:01:05 (permalink)
0

PIC32MK: PWM interruption

I'm trying to add ADC conversion to PWM1 (two pins, one channel, Center-Aligned). To do it, I'm adding the interrupt Service. To verify if routine is working I'm applying a simple putchar and would like to observe with oscilloscope
one of two flags (RF0 or RF1). The problem is that putchar works well but LATCH for digital IO never. Thanks for help.
That is related code:
//......
SetPara(&IPC43,1,10);       //! Priority = 1
SetPara(&IPC43,5,8);        //! sub-Priority = 5
SETBIT(IEC5,13);            //! Interrupt Enable
SETBIT(TRGCON1,6);          //! STRGIS=1 Interrupt selected
SETBIT(PWMCON1,21);         //! TRIGIEN=1 Primary Trigger Interrupt Enable
//.......
CLRBIT(ANSELF,0); CLRBIT(TRISF,0); //! Two digital pins as Output
CLRBIT(ANSELF,1); CLRBIT(TRISF,1);
LATFbits.LATF0 = 1;           // O.K.
//.......
void __ISR(_PWM1_VECTOR, IPL5AUTO) PwmHandler1(void) //_PWM1_VECTOR = 173
{   
    count++;
    if (count == 2000){
        count = 0;
        LATFbits.LATF0 = 0;    // never works
       putchar         ;       // works well but I'm not able to insert correct form in this thread
    }  
    CLRBIT(IFS5,13);        //! clear flag
}
post edited by lelis - 2018/03/13 15:19:15
#1

6 Replies Related Threads

    qɥb
    Monolothic Member
    • Total Posts : 3329
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: PIC32MK: PWM interruption 2018/03/13 15:57:46 (permalink)
    0
    lelis
           putchar         ;       // works well but I'm not able to insert correct form in this thread

    Any occurrence of "char" followed by "(" triggers the web server firewall.
    Pretty useful on a programming forum...
     
    Using putch() instead is safe.
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #2
    andersm
    Super Member
    • Total Posts : 2466
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: online
    Re: PIC32MK: PWM interruption 2018/03/13 16:33:44 (permalink)
    0
    Your interrupt priorities don't match:
    lelisSetPara(&IPC43,1,10);       //! Priority = 1
    void __ISR(_PWM1_VECTOR, IPL5AUTO) PwmHandler1(void) //_PWM1_VECTOR = 173



    #3
    lelis
    Junior Member
    • Total Posts : 94
    • Reward points : 0
    • Joined: 2010/03/27 15:41:52
    • Location: 0
    • Status: offline
    Re: PIC32MK: PWM interruption 2018/03/14 01:16:06 (permalink)
    0
    After modification there are the same results: putch works well but never LATCH!!!!
    Related code:
    //......
    SetPara(&IPC43,1,10);       //! Priority = 1
    SetPara(&IPC43,5,8);        //! sub-Priority = 5
    SETBIT(IEC5,13);            //! Interrupt Enable
    SETBIT(IFS5,13);            //! Set flag
    SETBIT(TRGCON1,6);          //! STRGIS=1 Interrupt selected
    SETBIT(PWMCON1,21);         //! TRIGIEN=1 Primary Trigger Interrupt Enable
    //.......
    CLRBIT(ANSELF,0); CLRBIT(TRISF,0); //! Two digital pins as Output
    CLRBIT(ANSELF,1); CLRBIT(TRISF,1);
    LATFbits.LATF0 = 1;                         // Initial value (verified before PWM's start)
    //.......
    void __ISR(_PWM1_VECTOR, IPL1AUTO) Pwm1Handler(void) //_PWM1_VECTOR = 173
    {   
        count++;                       
        if (count == 2000){         // That is applied to divide the 20 kHz of PWM
            count = 0;
            LATFbits.LATF0 = 0;    // never works !!!!!!!??????
           putch('.');                   // works well
        }  
        CLRBIT(IFS5,13);        //! clear flag
    }
    #4
    qɥb
    Monolothic Member
    • Total Posts : 3329
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: PIC32MK: PWM interruption 2018/03/14 05:24:10 (permalink)
    0
    You've never shown us the whole code.
    Just checking, have you disabled the WDT?
    If the WDT was triggering, that would keep setting the pin again.
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #5
    lelis
    Junior Member
    • Total Posts : 94
    • Reward points : 0
    • Joined: 2010/03/27 15:41:52
    • Location: 0
    • Status: offline
    Re: PIC32MK: PWM interruption 2018/03/14 06:24:45 (permalink)
    0
    Thanks for your quick answer. I will try to add my test one-side example.
    //!----------------------------------------------------------------------------
    //! electronics-lis
    //! CH-2000 Neuchatel
    //! info@electronics-lis.com
    //! https://electronics-lis.com
    //! L. Lisowski 20 January 2018
    //!----------------------------------------------------------------------------
    //!----------------------------------------------------------------------------
    // PIC32MK ADC tests
    //!----------------------------------------------------------------------------
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    #include <C:\Program Files (x86)\Microchip\xc32\v1.44\pic32mx\include\proc\p32mk1024mcf064.h>
    #include <xc.h>            
    #include <sys/attribs.h>
    //!----------------------------------------------------------------------------
    //!----------------------------------------------------------------------------
    //! This part need to be adapted for your application.
    // DEVCFG3
    #pragma config PWMLOCK = OFF, FUSBIDIO2 = OFF, FVBUSIO2 = OFF, PGL1WAY = OFF    
    #pragma config PMDL1WAY = OFF, IOL1WAY = OFF, FUSBIDIO1 = OFF, FVBUSIO1 = OFF                    
    // DEVCFG2
    #pragma config FPLLIDIV = DIV_1, FPLLRNG = RANGE_5_10_MHZ, FPLLICLK = PLL_POSC         
    #pragma config FPLLMULT = MUL_48, FPLLODIV = DIV_4, VBATBOREN = ON, DSBOREN = ON      
    #pragma config DSWDTPS = DSPS32, DSWDTOSC = LPRC, DSWDTEN = OFF, FDSEN = OFF          
    #pragma config BORSEL = HIGH, UPLLEN = OFF               
    // DEVCFG1
    #pragma config FNOSC = SPLL, DMTINTV = WIN_127_128, FSOSCEN = OFF, IESO = ON
    #pragma config POSCMOD = HS, OSCIOFNC = ON, FCKSM = CSECME, WDTPS = PS1048576   
    #pragma config WDTSPGM =STOP,WINDIS = NORMAL, FWDTEN = OFF, FWDTWINSZ = WINSZ_25
    #pragma config DMTCNT = DMT31, FDMTEN = OFF
    // DEVCFG0
    #pragma config DEBUG = OFF, JTAGEN = OFF, ICESEL = ICS_PGx1, TRCEN = ON
    #pragma config BOOTISA = MIPS32,FSLEEP = OFF, DBGPER = PG_ALL, SMCLR = MCLR_NORM     
    #pragma config SOSCGAIN = GAIN_2X, SOSCBOOST = ON, POSCGAIN = GAIN_LEVEL_3
    #pragma config POSCBOOST = ON, EJTAGBEN = NORMAL
    // DEVCP
    #pragma config CP = OFF                 // Code Protect (Protection Disabled)
    //! That are my Configuration Bits version, you need adapt them for your application
    //!----------------------------------------------------------------------------
    //!----------------------------------------------------------------------------
    /*********************************/
    //! Type declarations
    /*********************************/
    typedef signed char  int8;
    typedef unsigned char uint8;
    typedef short int16;
    typedef unsigned short uint16;
    typedef signed int int32;
    typedef unsigned int uint32;
    /************************************//**
    ** Macro Definitions
    ****************************************/
    //! Don't forget !!! the result are uint32_l !!!!!!!!
    #define TSTBIT(D,i) (D>>i & 1)
    #define SETBIT(D,i) (D |= ((uint32)1<<i))
    #define CLRBIT(D,i) (D &= ~((uint32)1 << i))
    //#define SETPARA(D,p,i) (D |= (p<<i))
    #define REG_SHIFT               4
    #define ADC_MAX_LOOPS           100
    #define SYSCLK                  120000000L
    #define PBCLK                   (SYSCLK/2)
    #define UARTspeed               115200
    #define TRUE                    1
    #define FALSE                   0
    /*********************************/
    //! Local Variables
    /*********************************/
    char i, k;
    uint32  reg ,pos, New;
    volatile uint32 *Val;
    uint8   MaxLoops,NbrLoops;
    uint16 count;
    uint8   flag0_state;
    /*********************************/
    //! Function prototype
    /*********************************/
    void initU1ART(void);
    void initPWMgen(void);
    uint8 initPWMcentred(void);
    uint8 VerifStatZERO( volatile uint32 * reg, uint32 BitPos);
    uint8 VerifStatONE( volatile uint32 *reg, uint32 mask);
    void SetPara(volatile uint32 * reg, uint32 para, uint32 ParaPos);
    /*********************************/
    //! main
    void main()
    {  
        __builtin_enable_interrupts();  // global interrupt enable
        __XC_UART = 1;                  // printf on the U1ART    
        INTCONbits.MVEC = 1; // Set the interrupt controller for multi-vector mode  
        PMCONbits.ON = 0;               //! no PMP  
        // Function initialization
        PMD5bits.U1MD = 0; //! Enable clock
        initU1ART();
        printf ("\n\r This is my first PWM and Interruption test \n\r");
        initPWMgen();       //! PWM part initialization
        if(initPWMcentred() == TRUE){
            printf ("\n\r initPWMcentred accomplished \n\r");
        }
        else{
            printf ("\n\r initPWMcentred not accomplished \n\r");
        }               
        while (1)
        {
            while( !U1STAbits.URXDA);   //! wait until data available in RX buffer
            i = U1RXREG;                //1 Hit any key
            while( U1STAbits.UTXBF);    //! wait while TX buffer full
            U1TXREG = i;                //! Echo
            while( !U1STAbits.TRMT);    //! wait for last transmission to finish
            if (i >= '1' && i<='9')     //! U1ART test
            {
                PDC1 = (i-'0')*PHASE1/10; //! i * 10 /100 % calculation
                printf("\n\r Duty=%u \tPhase1=%u  \tNewDuty (1-9)*10 % = ", PDC1, PHASE1);
            }
            else {
                printf("\n\r Argument has to be number");
            }
        }
    }
    /*********************************/
    void initU1ART(void)
    {
        // UART init
        // Pins initialization. !!!!! have to be adapted to hardware        
        SETBIT(TRISC,6);            //!  C6 digital input   
        SetPara(&U1RXR,5,0);        //!  SET RX to RC6
        SETBIT(RPC7R,0);            //!  SET RC7 to TX
        // disable UART1 and autobaud, TX and RX enabled only,8N1,idle=HIGH
        U1MODE = 0x0000;
        SETBIT(U1STA,12);           //! Enable RX
        SETBIT(U1STA,10);           //! Enable TX             
        U1BRG = (PBCLK/(16*UARTspeed)) - 1;
        //U1BRG = 32;               //! Baud Speed => 115200
        // Interrupt         
        SetPara(&IPC9,3,26);   
        SetPara(&IPC9,3,24);          
        SetPara(&U1STA,0,6);
        //! For future applications    
        SetPara(&IPC10,3,2);   
        SetPara(&IPC10,2,0);  
        SetPara(&U1STA,0,14);   
        CLRBIT(IFS1,8);    
        CLRBIT(IEC1,8);   
        CLRBIT(IFS1,7);  
        CLRBIT(IEC1,7);
        SETBIT(U1MODE,15);
    }
    void initPWMgen(void)
    {
        //! all digital
        ANSELA = 0; ANSELB = 0; ANSELC = 0; ANSELD = 0; ANSELE = 0; ANSELF = 0;
        ANSELG = 0; ANSELF = 0;
        //! No pull-up resistor => necessairy to define ditital Output
        CNPUA = 0; CNPUB= 0; CNPUC= 0; CNPUD= 0; CNPUE= 0; CNPUF= 0; CNPUG= 0;
        CNPUF= 0;
        //! No pull-down resistor => necessairy to define ditital Output
        CNPDA = 0; CNPDB= 0; CNPDC= 0; CNPDD= 0; CNPDE= 0; CNPDF= 0;
        CNPDG= 0; CNPDF= 0;
         //! No Change Notification (interruption generated by I/O )
        CNCONA = 0;  CNCONB = 0; CNCONC = 0; CNCOND = 0; CNCONE = 0; CNCONF = 0;
        CNCONG = 0;   
        /* Initialize pins as PWM outputs */
        //CLRBIT(TRISB,14);  //! PIn B14 as PWMlH  
        //CLRBIT(TRISB,15);  //! PIn B15 as PWMlL
        /* Configure ADCCMPCONx No analog comparators are used.*/
            CM1CON = CM2CON = CM3CON = CM4CON = CM5CON = 0;
        /* DAC disabled */
            DAC1CON = DAC2CON = DAC3CON = 0;
        //! Disable and enable peripheral modules
        PMD2 = 1; //! Ampli op and comparator without clock
        return;
    }
    uint8 initPWMcentred(void)
    {       
        SETBIT(PB6DIV,15);          //! Output clock is enabled
        SetPara(&PB6DIV,1,0);       //! Peripheral Bus ?6? Clock Divisor = /2
        //! PTCON initialization (Primary timer)
        PTCON = 0;                  //! Initial disable
        SetPara(&PTCON,0,2);        //! PCLKDIV = 1 = Sysclock /2
        //! CHOP initialization (Chop timer)
        CHOP =0;
        //! PWMCON1 initialization
        PWMCON1 = 0;                //! Initial settings
        CLRBIT(PWMCON1,3);          //! MTBS = 0 Primary master is the clock source for the MCPWM module
        SetPara(&PWMCON1,1,10);     //! ECAM=1= Symmetric Center-Aligned mode
        SetPara(&PWMCON1,3,6);      //! Dead Time Compensation mode enabled
        SETBIT(PWMCON1,9);          //! ITB=1 PHASE1 registers provide time base
        //! IOCON1 initialization
        IOCON1 = 0;                 //! Initial settings
        SETBIT(IOCON1,15);          //! PENH=1 PWM module controls PWM1H pin
        SETBIT(IOCON1,14);          //! PENL=1 PWM module controls PWM1L pin
        CLRBIT(IOCON1,13);          //! PWMxH pin is active-high
        CLRBIT(IOCON1,12);          //! PWMxL pin is active-high
        SetPara(&IOCON1,0,10);      //! PMOD=0>PWM I/O pin pair is in Complementary mode
        PHASE1 = 2000;              //! phase1 PHASEx registers provide time base
        PDC1 = 400;                 //! Duty cycles
        DTR1 = 25;                  //! Dead Time Values
        ALTDTR1 = 25;               //!
        
        //! PWM1 trigger event Interrupt implementation
        //! _PWM_PRI_VECTOR
        /*SetPara(&IPC42,1,26);       //! Priority
        SetPara(&IPC42,5,24);        //! sub-Priority
        SETBIT(IEC5,11);            //! Interrupt Enable
        SETBIT(IFS5,11);            //! Set flag*/
        //! _PWM1_VECTOR
        SetPara(&IPC43,1,10);       //! Priority
        SetPara(&IPC43,5,8);        //! sub-Priority
        SETBIT(IEC5,13);            //! Interrupt Enable
        SETBIT(IFS5,13);            //! Set flag
                
        CLRBIT(TRGCON1,7);          //! DTM=0 Secondary trigger event is not combined
        CLRBIT(TRGCON1,6);          //! STRGIS=0 Interrupt selected
        SETBIT(PWMCON1,21);         //! TRIGIEN=1 Primary Trigger Interrupt Enable
        count = 0;
        //! Digital Flag F0 and F1 initialization
        CLRBIT(ANSELF,0); CLRBIT(TRISF,0); //! PWM pins as Digital Output
        CLRBIT(ANSELF,1); CLRBIT(TRISF,1);
        SETBIT(LATF,0);   SETBIT(LATF,1);   //! Initial value
        SETBIT(PTCON,15);           //! PWM module enable
        return (TRUE);
     }
    /*********************************/
    uint8 VerifStatZERO(volatile uint32 * reg, uint32 BitPos)
    {     
        MaxLoops = ADC_MAX_LOOPS;
        NbrLoops = 0;               //! To test if loops is well implemented
        while (MaxLoops--){
            NbrLoops++;
            if(TSTBIT(*reg,BitPos) == 0){
                //printf("\n\r ==Zero NbrLoops=%u \tRegAddr=x%x",NbrLoops,reg);
                return (TRUE);
            }
        }
        printf("\n\r Error==0 BitPos=x%x  RegAddr=x%x", BitPos,reg);
        return (FALSE);
    }
    uint8 VerifStatONE(volatile uint32 * reg, uint32 BitPos)
    {     
        MaxLoops = ADC_MAX_LOOPS;
        NbrLoops = 0;               //! To test if loops is well implemented
        while (MaxLoops--){
            NbrLoops++;
            if(TSTBIT(*reg,BitPos) == 1){
                //printf("\n\r ==ONE \tNbrLoops=%u RegAddr=x%x",NbrLoops,reg);
                return (TRUE);
            }
        }
        printf("\n\r Error==1 \tBitPos=%u  \tRegAddr=x%x", BitPos,reg);
        return (FALSE);
    }
    void SetPara(volatile uint32 * reg, uint32 para, uint32 ParaPos)
    {     
        *reg |= (para<<ParaPos);
        return;
    }
    //! ----------------------------------------------------------------------------
    //! Brief of the exported inline code definition
    //! Result     
    //! Notes:
    //! First line of the function description \n
    //! Second line of the function description
    //! \param[in]  Tutu : description of the variable
    //! \return     Description of the return value
    //! ----------------------------------------------------------------------------
    void __ISR(_PWM_PRI_VECTOR, IPL1AUTO) PwmPri1Handler(void) //_PWM1_VECTOR = 171
    {    
        count++;
        if (count == 2000)
        {
            count = 0;
            //CLRBIT(LATF,0);
            //LATFbits.LATF0 = 0;
            CLRBIT(LATF,0);
            putchar(';');        
        }   
        if (flag0_state == 1) flag0_state = 0;
        else flag0_state = 1;
        //LATFbits.LATF0 = flag0_state;
        CLRBIT(IFS5,11);        //! clear flag
    }
    void __ISR(_PWM1_VECTOR, IPL1AUTO) Pwm1Handler(void) //_PWM1_VECTOR = 173
    {    
        count++;
        if (count == 2000)
        {
            count = 0;
            CLRBIT(LATF,0);
            //LATFbits.LATF0 = 0;
            putchar(':');        
        }   
        if (flag0_state == 1) flag0_state = 0;
        else flag0_state = 1;
        //LATFbits.LATF0 = flag0_state;
        CLRBIT(IFS5,13);        //! clear flag
    }
     
    #6
    lelis
    Junior Member
    • Total Posts : 94
    • Reward points : 0
    • Joined: 2010/03/27 15:41:52
    • Location: 0
    • Status: offline
    Re: PIC32MK: PWM interruption 2018/03/14 06:29:03 (permalink)
    0
    I've added also this (not working) PIC32MK-PWM-INTERUPT-R01 example on my Drive:
    https://drive.google.com/...9bLHBHLHNNuLICvfFldl_P
     the putch works well but changing of F0 never !!!!! (Strange!!!!)
     
    post edited by lelis - 2018/03/14 06:34:04
    #7
    Jump to:
    © 2018 APG vNext Commercial Version 4.5