• AVR Freaks

Hot!Timer Interrupt do not work when trying to increment a variable.

Author
laserline
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2010/04/16 15:44:11
  • Location: Brazil
  • Status: offline
2020/07/07 12:35:13 (permalink)
0

Timer Interrupt do not work when trying to increment a variable.

Sorry for my stupidity guys, but someone can explain to me why I am not getting this code to work?
When I try to increment the "f" variable inside the Timer3 interrupt it stops (no LED toggle on Pin B5). If I comment the "f++;" line, the interrupt returns to normal and I can read the frequency of 1.25MHz on the oscilloscope.
Someone could please try to explain why is it happening?
 
Here is my code:
=========================================================================

#include "xc.h"
unsigned char f = 0;
 
=========================================================================
 
// FBS
#pragma config BWRP = WRPROTECT_OFF
#pragma config BSS = NO_FLASH
#pragma config RBS = NO_RAM
// FSS
#pragma config SWRP = WRPROTECT_OFF
#pragma config SSS = NO_FLASH
#pragma config RSS = NO_RAM
// FGS
#pragma config GWRP = OFF
#pragma config GSS = OFF
// FOSCSEL
#pragma config FNOSC = PRIPLL
#pragma config IESO = ON
// FOSC
#pragma config POSCMD = HS // Primary Oscillator Source (HS Oscillator Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function (OSC2 pin has clock out function)
#pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow Only One Re-configuration)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are disabled)
// FWDT
#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler (1:32,768)
#pragma config WDTPRE = PR128 // WDT Prescaler (1:128)
#pragma config WINDIS = OFF // Watchdog Timer Window (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (Watchdog timer enabled/disabled by user software)
// FPOR
#pragma config FPWRT = PWR128 // POR Timer Value (128ms)
#pragma config ALTI2C = OFF // Alternate I2C pins (I2C mapped to SDA1/SCL1 pins)
// FICD
#pragma config JTAGEN = OFF // JTAG Port Enable (JTAG is Disabled)
 
=========================================================================

int setup(void) {
    __builtin_write_OSCCONH(0b00000011);
    __builtin_write_OSCCONL(0b00000001);

    CLKDIVbits.PLLPRE = 0b00010; // Setting N1 to 4 (N1 = n+2)
    PLLFBDbits.PLLDIV = 0b000011110; // Setting M to 32 (M = m+2)
    CLKDIVbits.PLLPOST = 0b00; // Setting N2 to 2 (Output/2)

    T2CONbits.T32 = 0; // Setup T32 register to 2x 16-bit Timers
    T3CONbits.TCS = 0; // Internal Clock (Fosc/2)
    T3CONbits.TCKPS = 0b00; // Prescaler (00=1:1 / 01=1:8 / 10=1:64 / 11=1:256)
    PR3 = 1; // Set Timer3 count
    IPC2bits.T3IP = 1; // Set Timer3 priority
    IEC0bits.T3IE = 1; // Enable TMR3 Interrupt
    IFS0bits.T3IF = 0; // Interrupt request has not occurred

    TRISBbits.TRISB5 = 0;
    T3CONbits.TON = 1;
}
 
=========================================================================
 
void __attribute__((interrupt,auto_psv)) _T3Interrupt(void) {
    f++;  // *** If I remove this line the INT works. I also tried "no_auto_psv" with no different results.
    __asm("btg LATB,#5");
    _T3IF = 0;
}
 
int main(void) {
    setup();
    while(1){
    }
return 0;
}
 
=========================================================================
#1

15 Replies Related Threads

    MBedder
    Circuit breaker
    • Total Posts : 6901
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    Re: Timer Interrupt do not work when trying to increment a variable. 2020/07/07 12:39:04 (permalink)
    +2 (4)
    1. Wrap your source text in [ code] [ /code] tags.
    2. Google "volatile" qualifier.
    #2
    laserline
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2010/04/16 15:44:11
    • Location: Brazil
    • Status: offline
    Re: Timer Interrupt do not work when trying to increment a variable. 2020/07/07 12:50:13 (permalink)
    +1 (1)
    Hi MBedder,
    OK, I read about volatile and add it to the code, thanks for the tips, but this not solved the problem. The T3 INT keep stopping when the "f++;" or "f += 1;" or "f = f + 1;" is active on the code.
    #3
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11936
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Timer Interrupt do not work when trying to increment a variable. 2020/07/07 13:16:59 (permalink)
    0
    What does the debugger tell you is happening?
    #4
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Timer Interrupt do not work when trying to increment a variable. 2020/07/07 13:19:40 (permalink)
    +2 (2)
    No, "volatile" is not required if the non-interrupt code doesn't access the variable.
    Do you really want to have the timer interrupt that quickly?
    Setting PR3 to "1", means it only counts from 0 to 1, then interrupts again.
    That means the interrupt will have re-triggered before your ISR exits.
    Adding one more instruction probably moves the "_T3IF = 0" to AFTER the point where it retriggers.

       
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    laserline
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2010/04/16 15:44:11
    • Location: Brazil
    • Status: offline
    Re: Timer Interrupt do not work when trying to increment a variable. 2020/07/07 13:58:27 (permalink)
    0
    Hi ric,
    No I dont need it so fast, it's just to test the top speed that I can run the interrupt. I set the count to 1000, what dropped the freq. to 20KHz., but the problem persists. Any other idea? I am going crazy with this problem. : )
     
    #6
    PStechPaul
    Super Member
    • Total Posts : 2814
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Timer Interrupt do not work when trying to increment a variable. 2020/07/07 14:53:59 (permalink)
    0
    Please repost the corrected code, or attach a complete project zipfile (right-click on project and choose "package").
     
    You should also post the specific PIC as well as the IDE and compiler versions.

     
    #7
    PStechPaul
    Super Member
    • Total Posts : 2814
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Timer Interrupt do not work when trying to increment a variable. 2020/07/07 15:19:47 (permalink)
    0
    I'm not familiar with XC16, but maybe you should use this for the assembler BTG instruction:
    __builtin_btgDescription:
    This function will generate a btg machine instruction.
    Some examples include:
    int i; /* near by default */
    int l __attribute__((far));
    struct foo { int bit1:1;} barbits;
    int bar;
    void some_bittoggles() {
    register int j asm("w9");
    int k; k = i;
    __builtin_btg(&bar,barbits.bit1);
    __builtin_btg(&i,1);
    __builtin_btg(&j,3);
    __builtin_btg(&k,4);
    __builtin_btg(&l,11);
    return j+k;}


     
    #8
    laserline
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2010/04/16 15:44:11
    • Location: Brazil
    • Status: offline
    Re: Timer Interrupt do not work when trying to increment a variable. 2020/07/07 15:45:55 (permalink)
    0
    Processor: dsPIC33FJ128GP802
    XC16 Compiler v1.50
    __________________________

     
    #include "xc.h"
    volatile unsigned char f = 0;
     
    // FBS
    #pragma config BWRP = WRPROTECT_OFF
    #pragma config BSS = NO_FLASH
    #pragma config RBS = NO_RAM
    // FSS
    #pragma config SWRP = WRPROTECT_OFF
    #pragma config SSS = NO_FLASH
    #pragma config RSS = NO_RAM
    // FGS
    #pragma config GWRP = OFF
    #pragma config GSS = OFF
    // FOSCSEL
    #pragma config FNOSC = PRIPLL
    #pragma config IESO = ON
    // FOSC
    #pragma config POSCMD = HS // Primary Oscillator Source (HS Oscillator Mode)
    #pragma config OSCIOFNC = OFF // OSC2 Pin Function (OSC2 pin has clock out function)
    #pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow Only One Re-configuration)
    #pragma config FCKSM = CSDCMD // Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are disabled)
    // FWDT
    #pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler (1:32,768)
    #pragma config WDTPRE = PR128 // WDT Prescaler (1:128)
    #pragma config WINDIS = OFF // Watchdog Timer Window (Watchdog Timer in Non-Window mode)
    #pragma config FWDTEN = OFF // Watchdog Timer Enable (Watchdog timer enabled/disabled by user software)
    // FPOR
    #pragma config FPWRT = PWR128 // POR Timer Value (128ms)
    #pragma config ALTI2C = OFF // Alternate I2C pins (I2C mapped to SDA1/SCL1 pins)
    // FICD
    #pragma config JTAGEN = OFF // JTAG Port Enable (JTAG is Disabled)
     
    // ======================================================================

    int setup(void) {
        __builtin_write_OSCCONH(0b00000011);
        __builtin_write_OSCCONL(0b00000001);

        CLKDIVbits.PLLPRE = 0b00010; // Setting N1 to 4 (N1 = n+2)
        PLLFBDbits.PLLDIV = 0b000011110; // Setting M to 32 (M = m+2)
        CLKDIVbits.PLLPOST = 0b00; // Setting N2 to 2 (Output/2)

        T2CONbits.T32 = 0; // Setup T32 register to 2x 16-bit Timers
        T3CONbits.TCS = 0; // Internal Clock (Fosc/2)
        T3CONbits.TCKPS = 0b00; // Prescaler (00=1:1 / 01=1:8 / 10=1:64 / 11=1:256)
        PR3 = 1000; // Set Timer3 count
        IPC2bits.T3IP = 1; // Set Timer3 priority
        IEC0bits.T3IE = 1; // Enable TMR3 Interrupt
        IFS0bits.T3IF = 0; // Interrupt request has not occurred

        TRISBbits.TRISB5 = 0;
        T3CONbits.TON = 1;
    }
     
    // ======================================================================
     
    void __attribute__((interrupt,auto_psv)) _T3Interrupt(void) {
        f++;
        __asm("btg LATB,#5");
        _T3IF = 0;
    }
     
    // ======================================================================
     
    int main(void) {
        setup();
        while(1){
        }
        return 0;
    }
     

    #9
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3984
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Timer Interrupt do not work when trying to increment a variable. 2020/07/07 18:36:06 (permalink)
    -1 (3)
    Go back and read the other thread.
    You have removed NOSC and clock lock.
     
    (NOSC = NEW OSC)
     
    You have it all round the wrong way.
     
    I don't think you know what this line does?
    (That is why it is wrong)
    _builtin_write_OSCCONL(0b00000001);


     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #10
    laserline
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2010/04/16 15:44:11
    • Location: Brazil
    • Status: offline
    Re: Timer Interrupt do not work when trying to increment a variable. 2020/07/07 19:21:51 (permalink)
    0
    Thanks Gort2015!
    I will read with more attention and try again with these corrections on the code. I only removed them because it shown up a better performance on the first test.
    I am very new on XC16 and I've only been beaten so far... hehee.
    Any other suggestions will be very welcome!
    #11
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3984
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Timer Interrupt do not work when trying to increment a variable. 2020/07/07 19:41:57 (permalink)
    0 (4)
    Some-one thinks that I am wrrong.
    Post your code and I will pick through it bit by bit.
     
     
    post edited by Gort2015 - 2020/07/07 19:44:00

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #12
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3984
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Timer Interrupt do not work when trying to increment a variable. 2020/07/07 20:16:59 (permalink)
    -1 (3)
    I've put it round the correct way for you.
           CLKDIVbits.PLLPRE = 0b00010; // Setting N1 to 4 (N1 = n+2)
        PLLFBDbits.PLLDIV = 0b000011110; // Setting M to 32 (M = m+2)
        CLKDIVbits.PLLPOST = 0b00; // Setting N2 to 2 (Output/2)
        // OSWEN   request new osc after filling in new values above
        __builtin_write_OSCCONL(0b00000001);


    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #13
    JPortici
    Super Member
    • Total Posts : 1114
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: Timer Interrupt do not work when trying to increment a variable. 2020/07/07 22:15:46 (permalink)
    0 (2)
    Not sure if related, but abstain from using inline assembly.
    the C equivalent of
    __asm("btg LATB,#5");

    is
    LATB ^= (1u << 5);

    and if it's not going to use the bit toggle instruction at ANY optimization level, open a ticket indicating that there is a compiler bug for that specific pic
    #14
    JPortici
    Super Member
    • Total Posts : 1114
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: Timer Interrupt do not work when trying to increment a variable. 2020/07/07 22:17:58 (permalink)
    0
    Gort2015
    Go back and read the other thread.
    You have removed NOSC and clock lock.
     
    (NOSC = NEW OSC)
     
    You have it all round the wrong way.
     
    I don't think you know what this line does?
    (That is why it is wrong)
    _builtin_write_OSCCONL(0b00000001);



     




    besides, how is he going to change the clock if clock switching is disabled? (check the configuration bits!)
    #15
    laserline
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2010/04/16 15:44:11
    • Location: Brazil
    • Status: offline
    Re: Timer Interrupt do not work when trying to increment a variable. 2020/07/08 10:45:09 (permalink)
    +1 (1)
    Problem solved guys... my proto-board could be with some bad connections and the oscillator was presenting a unstable operation. I've changed the proto-board and now it's working.
    In anycase this makes me study the dsPIC Oscillator with more attention to the details and now I can say I trully understood it's operation. THANKS again to all.
    The next phase is to have the DAC operating and later the DMA transfer in order to implement a DDS generator.
    Best!



    #16
    Jump to:
    © 2020 APG vNext Commercial Version 4.5