• AVR Freaks

Hot!dsPIC33F TMR1 not setting flag

Author
cpohtato
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2019/08/11 05:45:36
  • Location: 0
  • Status: offline
2019/08/12 16:53:28 (permalink)
0

dsPIC33F TMR1 not setting flag

Hi,
 
I'm using a dsPIC33EV128GM102 with the XC16 compiler and trying to get interrupts to work. The electrical contacts are good (I've checked by running other code) and I've done everything in the user manual for timers but I can't figure out why Timer 1 isn't triggering the ISR I've written. The general idea is that the chip lights up and LED, but when a button is pressed, a timer goes for a certain period and then triggers the interrupt, which flips another flag and turns the LED off for two seconds before turning it back on.
 
I've tried varying PR1 (from 0xFFFF to 0x0001) and trying to read the value of the TMR1 register directly to figure out what it's doing (didn't work very well) but I can't seem to get it to work.
 
Also, does TMR1 increment on every clock pulse (after taking into account pre-scaler) or instruction cycle, and if instruction cycle, how many clock pulses is that? I'm just using the internal oscillator (7.37 MHz).
 
/*
 * File: timerMain.c
 * Using a dsPIC33EV128GM102
 */

#define FOSC (7370000ULL) //Set oscillation frequency
#define FCY (FOSC/2) //Set cycle frequency

#include "xc.h"
#include <p33Exxxx.h>
#include <libpic30.h>

volatile int LEDOn = 1; //LED flag
int i; //Input variable

void digPinSetup(void);
void tmrSetup(void);

void main(void) {
    digPinSetup(); //Setup digital pins
    tmrSetup; //Setup timer, but don't start yet
    
    while(1)
    {
        if (LEDOn)
        {
            PORTBbits.RB6 = 1; //Turn LED on
        }
        else
        {
            PORTBbits.RB6 = 0; //Turn LED off
            __delay_ms(2000); //Wait 2 s
            LEDOn = 1; //Then turn LED flag back on
            TMR1 = 0x00; //Reset TMR1 count
            T1CONbits.TON = 1; //Restart timer
        }
        
        i = PORTBbits.RB5;
        
        if (i) //When button pressed
        {
            T1CONbits.TON = 1; //Turn timer on
        }
    }
}

void digPinSetup(void)
{
    TRISBbits.TRISB6 = 0; //RB6 is output
    TRISBbits.TRISB5 = 1; //RB5 is input
}

void tmrSetup(void)
{
    T1CON = 0x00; //Reset TMR1 control register and 1:1 pre-scaler
    T1CONbits.TCKPS = 0b11; //Clock pre-scaler
    TMR1 = 0x0000; //Reset TMR1 count
    PR1 = 0xFFFF; //Set Period Register to 65536 pulses
    //PR1 = 0x0001; //Set Period Register to 1 pulse
    IPC0bits.T1IP = 0x06; //Set TMR1 priority to 6
    IFS0bits.T1IF = 0; //Clear TMR interrupt flag
    IEC0bits.T1IE = 1; //Enable interrupts for TMR1
    INTCON2bits.GIE = 1; //Enable global interrupts
}

void __attribute__((__interrupt__, auto_psv)) _T1Interrupt(void)
{
    //TMR1 ISR
    T1CONbits.TON = 0; //Turn timer off
    TMR1 = 0; //Reset counter
    LEDOn = 0; //LED flag off
    IFS0bits.T1IF = 0; //Reset Timer1 interrupt flag and return from ISR
}

 
Any help is appreciated; cheers
#1

7 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 2997
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: dsPIC33F TMR1 not setting flag 2019/08/14 13:03:05 (permalink)
    4 (1)
    The timers are counting clock cycles.
    But IIRC the peripheral clock and the core clock are somewhat independently selectable.
     
    Some things to check:
    1. Do you ever get i = 1 ? (If PB5 is "analog-capable", you have to set the corresponding ANS bit to 0.)
    2. What's currently happening? LED on all the time?

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    davekw7x
    Entropy++
    • Total Posts : 1804
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Second star on the right, straight on till morning
    • Status: offline
    Re: dsPIC33F TMR1 not setting flag 2019/08/14 14:45:44 (permalink)
    5 (3)
    Have not analyzed your code or even looked very closely.
    One thing jumped right off of the screen and socked me in the jaw. (Ouch!)
     
    cpohtato

    void main(void) {
        digPinSetup(); //Setup digital pins
        tmrSetup; //Setup timer, but don't start yet <---Pow to Dave!


    You did not call the tmrSetup function!  Did you get any kind of compiler warning for that line?
     
    Regards,

    Dave
     

    Sometimes I just can't help myself...
    #3
    nice
    Super Member
    • Total Posts : 1088
    • Reward points : 0
    • Joined: 2004/09/18 11:42:25
    • Location: Germany
    • Status: offline
    Re: dsPIC33F TMR1 not setting flag 2019/08/14 15:07:48 (permalink)
    4.5 (2)
    Good spot! If additional warnings are enabled, XC16 throws a warning about a "statement with no effect". A function name without following braces is just representing the functions program memory address, i.e
    tmrSetup;

    is the same as
    0x1234;

    if the function "tmrSetup" starts at program memory address 0x1234.
    #4
    nice
    Super Member
    • Total Posts : 1088
    • Reward points : 0
    • Joined: 2004/09/18 11:42:25
    • Location: Germany
    • Status: offline
    Re: dsPIC33F TMR1 not setting flag 2019/08/14 15:15:19 (permalink)
    0
    du00000001
    The timers are counting clock cycles.
    But IIRC the peripheral clock and the core clock are somewhat independently selectable.

     
    Despite of 33CH/33CK devices the clock source of TMR1 on all other 16-bit devices is either the clock provided to T1CK or FCY. Which other 16-bit devices than 33CH/33CK do provide an internal peripheral clock frequency other than FCY for TMR1?
    #5
    nice
    Super Member
    • Total Posts : 1088
    • Reward points : 0
    • Joined: 2004/09/18 11:42:25
    • Location: Germany
    • Status: offline
    Re: dsPIC33F TMR1 not setting flag 2019/08/14 15:20:33 (permalink)
    4 (2)
    cpohtato
    and if instruction cycle, how many clock pulses is that?


    Please take a look at equation 9-1 of your devices data sheet. It clearly states that FCY is FOSC/2.
     
    cpohtato
    Any help is appreciated

     
    Further notes not related to your issue:
    • as "xc.h" is a compiler provided include file, you should use "#include <xc.h>"
    • including "p33Exxxx.h" is pointless as including "xc.h" already does the same
    • don’t use PORT registers for writing to outputs, but use LAT registers instead (i.e.replace PORTBbits.RB6 by LATBbits.LATB6)
     
    post edited by nice - 2019/08/14 15:25:03
    #6
    du00000001
    Just Some Member
    • Total Posts : 2997
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: dsPIC33F TMR1 not setting flag 2019/08/14 15:35:15 (permalink)
    0
    @ nice
    While the peripheral clock is Fosc/2, the core could run at a lower clock than that (using the resp. prescaler).

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #7
    cpohtato
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2019/08/11 05:45:36
    • Location: 0
    • Status: offline
    Re: dsPIC33F TMR1 not setting flag 2019/08/14 16:50:08 (permalink)
    0
    Yeah the input i variable was fine and adding the brackets fixed the code; a little embarrassing actually
    I re-compiled the version of the code without brackets and yes the compiler did throw that error but I think I just never noticed it because it wasn't a fatal error
     
    nice
    don’t use PORT registers for writing to outputs, but use LAT registers instead (i.e.replace PORTBbits.RB6 by LATBbits.LATB6)



    I just searched it up and it makes sense; will do in future
    Thanks guys
    #8
    Jump to:
    © 2019 APG vNext Commercial Version 4.5