• AVR Freaks

Problem with interruption in PIC32MX

Author
MrChunckuee
New Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2012/10/01 00:24:30
  • Location: 0
  • Status: offline
2017/01/13 10:32:04 (permalink)
0

Problem with interruption in PIC32MX

Hi, i'm try to generate an interrupt with TMR1 = 1milliseconds but without result. I'm using MPLAPX IDE v3.05, XC32 v1.42 & PIC32MX270F256D. SYSCLK=80MHz, PBCLK=10MHz & internal OSC. This my code but don't work:
 
Configuration Bit Settings:
#include <xc.h>

// DEVCFG3
// USERID = No Setting
#pragma config PMDL1WAY = ON // Peripheral Module Disable Configuration (Allow only one reconfiguration)
#pragma config IOL1WAY = ON // Peripheral Pin Select Configuration (Allow only one reconfiguration)
#pragma config FUSBIDIO = OFF // USB USID Selection (Controlled by Port Function)
#pragma config FVBUSONIO = OFF // USB VBUS ON Selection (Controlled by Port Function)

// DEVCFG2
#pragma config FPLLIDIV = DIV_1 // PLL Input Divider (1x Divider)
#pragma config FPLLMUL = MUL_20 // PLL Multiplier (20x Multiplier)
#pragma config UPLLIDIV = DIV_12 // USB PLL Input Divider (12x Divider)
#pragma config UPLLEN = OFF // USB PLL Enable (Disabled and Bypassed)
#pragma config FPLLODIV = DIV_2 // System PLL Output Clock Divider (PLL Divide by 2)

// DEVCFG1
#pragma config FNOSC = FRCPLL // Oscillator Selection Bits (Fast RC Osc with PLL)
#pragma config FSOSCEN = ON // Secondary Oscillator Enable (Enabled)
#pragma config IESO = ON // Internal/External Switch Over (Enabled)
#pragma config POSCMOD = OFF // Primary Oscillator Configuration (Primary osc disabled)
#pragma config OSCIOFNC = ON // CLKO Output Signal Active on the OSCO Pin (Enabled)
#pragma config FPBDIV = DIV_8 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/8)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)
#pragma config FWDTEN = ON // Watchdog Timer Enable (WDT Enabled)
#pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window Size is 25%)

// DEVCFG0
#pragma config JTAGEN = OFF // JTAG Enable (JTAG Disabled)
#pragma config ICESEL = ICS_PGx4 // ICE/ICD Comm Channel Select (Communicate on PGEC4/PGED4)
#pragma config PWP = OFF // Program Flash Write Protect (Disable)
#pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF // Code Protect (Protection Disabled)

 
The main is this:
//Include all libraries that use in project
#include "FUSES_Config.h" //Only here
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <sys/attribs.h>

#define SYS_FREQ (80000000L)
#define LED_STATUS LATCbits.LATC2

void __ISR (_TIMER_1_VECTOR, IPL1) Timer1Handler (void){
    LED_STATUS ^= 1;
    IFS0bits.T1IF = 0;
}

void main (void){
    MCU_Init();
    while(1){
    }
}

void MCU_Init(void){
    DDPCONbits.JTAGEN = 0; // Disable JTAG port
    ANSELC = 0;
    TRISCbits.TRISC2 = 0; // LED_Status is output
    LATCbits.LATC2 = 0;
    TMR1_Init();
}

void TMR1_Init(void){
    //Config TMR1 = 1mS
    T1CON = 0;
    T1CONbits.TCKPS = 0b11; //1:256 prescale value
    T1CONbits.TCS = 0; //Internal peripheral clock
    //T1CONbits.TGATE = 1;
    //INTCONbits.MVEC = 1; //Interrupt controller configured for multi vectored mode
    INTCONbits.TPC = 0b001; //Interrupts of group priority 1 start the Interrupt Proximity timer
    TMR1 = 0;
    PR1 = 0x27;
    IEC0bits.T1IE = 1;
    IFS0bits.T1IF = 0;
    IPC1bits.T1IP = 0b001;
    IPC1bits.T1IS = 0b00;
    T1CONbits.ON = 1; //Timer is enabled
}

 
I did not find my error, I saw in the internet examples where they use the libiberia plib.h and everything they have, but it seems that in this version of XC32 is no longer available. I would appreciate some help or suggestion, I am studying the datasheet and the xc32 user manual to see if I can solve it.
#1

5 Replies Related Threads

    cgiordan
    Super Member
    • Total Posts : 1364
    • Reward points : 0
    • Status: offline
    Re: Problem with interruption in PIC32MX 2017/01/13 12:35:27 (permalink)
    5 (1)
    You need to enable interrupts globally...  Either use some MCHP library, Harmony, or read the reference material for your device and figure it out the old fashioned way. 
    #2
    NKurzman
    A Guy on the Net
    • Total Posts : 17606
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Problem with interruption in PIC32MX 2017/01/13 13:37:54 (permalink)
    4 (2)
    The peripheral library is still available as a seperate download. Is is on the same web page as the compiler.
    #3
    simong123
    Lab Member No. 003
    • Total Posts : 1305
    • Reward points : 0
    • Joined: 2012/02/07 18:21:03
    • Location: Future Gadget Lab (UK Branch)
    • Status: online
    Re: Problem with interruption in PIC32MX 2017/01/13 19:37:50 (permalink)
    4 (2)
    A couple of points:-
    1. Don't get into the habit of clearing an interrupt flag using e.g.
    IFS0bits.T1IF=0;
    This is not safe as it involves RMW. Use e.g.
    IFS0CLR=_IFS0_T1IF_MASK;

    2. You need to set INTCONbits.MVEC=1.
    3. You need to enable interrupts globally as cgiordan said. You can use
    asm volatile ("ei");

    or one of the PLIB functions.
    4. The proper definition of the interrupt function is
    void __ISR (_TIMER_1_VECTOR, IPL1AUTO) Timer1Handler (void)
    The valid options are IPLxAUTO, IPLxSOFT or IPLxSRS where x=interrupt level.
     
    5. Don't mess with the interrupt proximity timer unless you really need to.
     
    #4
    MrChunckuee
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2012/10/01 00:24:30
    • Location: 0
    • Status: offline
    Re: Problem with interruption in PIC32MX 2017/01/17 13:03:14 (permalink)
    3 (1)
    Regards! Thanks for your answers, I have reviewed and I have managed to operate the code, thanks to your help.
     
    I corrected the working frequency, max according to the datasheet 40MHz.
     
    I commented that in my hardware, I lacked GND, sometimes I had problems trying to record the uc.
     
    Here the code of the TMR1 to 1ms and the LED lighting every 1 second:
     
    //Include all libraries that use in project
    #include <stdio.h>
    #include <stdlib.h>
    #include <xc.h>
    #include <sys/attribs.h> //Atributos para las interrupciones

    // DEVCFG3
    // USERID = No Setting
    #pragma config PMDL1WAY = ON // Peripheral Module Disable Configuration (Allow only one reconfiguration)
    #pragma config IOL1WAY = ON // Peripheral Pin Select Configuration (Allow only one reconfiguration)
    #pragma config FUSBIDIO = OFF // USB USID Selection (Controlled by Port Function)
    #pragma config FVBUSONIO = OFF // USB VBUS ON Selection (Controlled by Port Function)

    // DEVCFG2
    #pragma config FPLLIDIV = DIV_1 // PLL Input Divider (4x Divider)
    #pragma config FPLLMUL = MUL_20 // PLL Multiplier (20x Multiplier)
    #pragma config UPLLIDIV = DIV_1 // USB PLL Input Divider (1x Divider)
    #pragma config UPLLEN = OFF // USB PLL Enable (Disabled and Bypassed)
    #pragma config FPLLODIV = DIV_2 // System PLL Output Clock Divider (PLL Divide by 2)

    // DEVCFG1
    #pragma config FNOSC = FRCPLL // Oscillator Selection Bits (Fast RC Osc with PLL)
    #pragma config FSOSCEN = ON // Secondary Oscillator Enable (Enabled)
    #pragma config IESO = ON // Internal/External Switch Over (Enabled)
    #pragma config POSCMOD = OFF // Primary Oscillator Configuration (Primary osc disabled)
    #pragma config OSCIOFNC = ON // CLKO Output Signal Active on the OSCO Pin (Enabled)
    #pragma config FPBDIV = DIV_1 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/2)
    #pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
    #pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
    #pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)
    #pragma config FWDTEN = ON // Watchdog Timer Enable (WDT Enabled)
    #pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window Size is 25%)

    // DEVCFG0
    #pragma config JTAGEN = OFF // JTAG Enable (JTAG Disabled)
    #pragma config ICESEL = ICS_PGx4 // ICE/ICD Comm Channel Select (Communicate on PGEC4/PGED4)
    #pragma config PWP = OFF // Program Flash Write Protect (Disable)
    #pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)
    #pragma config CP = OFF // Code Protect (Protection Disabled)

    #define SYS_FREQ (40000000L) //Frecuencia del sistema
        
    #define LED_STATUS LATCbits.LATC2 //LED indicador en harware
    #define INTDisable_Interrupt() asm volatile("di") // Disable all interrupts
    #define INTEnable_Interrupt() asm volatile ("ei") // Enable all interrupts


    int count=0;
     
    void MCU_Init(void);
    void TMR1_Init(void);

    void __ISR (_TIMER_1_VECTOR, IPL1AUTO) ISR_Timer1 (void){
        count++;
        if(count>=1000){
            count = 0;
            LED_STATUS ^= 1;
        }
        IFS0CLR=_IFS0_T1IF_MASK;// Clear the timer interrupt status flag
    }

    void main (void){
        MCU_Init();
        while(1){
        }
    }

    void MCU_Init(void){
        DDPCONbits.JTAGEN = 0; //Disable JTAG port
        ANSELC = 0; //PORTC all digital
        TRISCbits.TRISC2 = 0; //LED_Status is output
        LATCbits.LATC2 = 0; //Clear LED
        TMR1_Init(); //Config TMR1
    }

    void TMR1_Init(void){
        //Config TMR1 = 1mS
        INTDisable_Interrupt();
        T1CONSET = 0x0000;
        INTCONSET = _INTCON_MVEC_MASK;// Set the MVEC bit
        TMR1 = 0;
        PR1 = 156;
        IEC0bits.T1IE = 1;
        IFS0bits.T1IF = 0;
        IPC1SET = 0x00000004; // Set priority level = 1, subpriority level = 0
        T1CONSET = 0x8030; // Start the timer, prescaller 1:256
        INTEnable_Interrupt();
    }

     
    I still have to learn a lot, it's the first thing I do with PIC32
    #5
    MrChunckuee
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2012/10/01 00:24:30
    • Location: 0
    • Status: offline
    Re: Problem with interruption in PIC32MX 2017/01/17 13:22:15 (permalink)
    0
    Here the video in action: https://www.youtube.com/watch?v=UJfG1bqdxsw
    #6
    Jump to:
    © 2019 APG vNext Commercial Version 4.5