TMR0 works properly, TMR1 - 3 starts immediately after releasing

Discussion in 'Embedded Systems and Microcontrollers' started by PIC-Programming, Feb 15, 2016.

  1. PIC-Programming

    Thread Starter New Member

    Feb 15, 2016
    9
    0
    Hello everybody!

    I'm currently working on a project which is about regulation. To assure that, I am using a timer to control the stats of my sensors every five minutes.
    The problem is, that all timers (except TMR0 which is already in use) start immediately after releasing/starting the program.

    I'm using MPLAB IDE v8.92 with C18 Compiler and the PIC18F14K22.

    At first scan I'm clearing the IF but it seems to turn back high immediately.

    First scan Code:

    OpenTimer1( TIMER_INT_ON & // enable timerinterrupt
    T1_16BIT_RW & // registersize = 16Bit
    T1_SOURCE_INT & // internal Osz.
    T1_PS_1_8 & // prescaler 1/8
    T1_OSC1EN_ON & // Enable Timer1 oscillator
    T1_SYNC_EXT_OFF); // Don’t sync external clock input

    RCONbits.IPEN = 1; // Prioritätslevel aktivieren
    RCONbits.BOR = 0; // brown -Reset off

    INTCONbits.PEIE = 1; // Enable peripheral interrupts
    INTCONbits.GIEH = 1; // Enable all Interrupts

    PIR1bits.TMR1IF = 0;

    I hope somebody can help me.
    Thank you up front!
     
  2. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,509
    2,369
    I don't use C but can't you just turn TMR1 off?
    TMR1ON = 0.
    Max.
     
  3. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,675
    899
    Code (Text):
    1.  
    2. INTCONbits.PEIE = 1; // Enable peripheral interrupts
    3. INTCONbits.GIEH = 1; // Enable all Interrupts
    4.  
    5. PIR1bits.TMR1IF = 0;
    6.  
    You are enabling global interrupts before clearing TMR1IF. Clear the IF first; otherwise, you will get an immediate interrupt.

    You may also need to clear a register or two, but that depends on your code. Unanswered, do you want TMR1 to run continuously?

    John
     
  4. PIC-Programming

    Thread Starter New Member

    Feb 15, 2016
    9
    0
    Of course I can but I need a second timer, thats why...

    I use TMR0 for an adjustable intervall to open a window.

    I also cleared the IF before enabling global Interrupts. Still starts immediately...

    These are all registers associated with TMR1, I would't know which one could be the problem.


    TMR1_Registers.JPG

    Still thank you all!
     
    Last edited: Feb 15, 2016
  5. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,675
    899
    Assuming you only have 2 interrupts enabled, TMR0 and TMR1, what happens just for testing if you disable TMR0 and clear its IF? (not that that is necessary if the interrupt is disabled...just belt & suspenders).

    Logically, if only TMR1 is enabled, it interrupts on rollover, i.e., the registers will be close to empty, unless your ISR is really long. . Clearing TMR1's IF last should give some time before another interrupts.

    Unfortunately, I am only familiar with the 16F chips, but TMR1 is not something new in the 18F series, except for its priority setting, which shouldn't affect the experiment just mentioned.

    John
     
  6. Dodgydave

    Distinguished Member

    Jun 22, 2012
    4,969
    744
    why dont you enable the timer, then clrf timer1, then start the programme, instead of enabling it in setup.
     
  7. PIC-Programming

    Thread Starter New Member

    Feb 15, 2016
    9
    0
    John:
    Tried ... still same.

    Dodgydave:
    Actually I don't know how to do that but it shouldn't change anything right?
     
  8. dannyf

    Well-Known Member

    Sep 13, 2015
    1,771
    359
    I would check the manual to see if that's how you configure timer interrupts - the rest of the code seems to be TxCON related, except this one.
     
  9. Dodgydave

    Distinguished Member

    Jun 22, 2012
    4,969
    744
    Your timer is probably overflowing as soon as its enabled, which is causing an interupt if its set to internal osc, whats its clock speed, and whats the precaler division, that should give you its overflow time in microseconds.
     
  10. PIC-Programming

    Thread Starter New Member

    Feb 15, 2016
    9
    0
    The clock Speed of the internal osc is 1MHz and the timer should run/runs for 0,5 sec


    C18_OpenTimer1.JPG
     
  11. dannyf

    Well-Known Member

    Sep 13, 2015
    1,771
    359
    I can confirm for you that your code works just fine. The problem is somewhere else.
     
  12. PIC-Programming

    Thread Starter New Member

    Feb 15, 2016
    9
    0
    Thank you!
     
  13. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    I wonder if enabling the Timer 1 Oscillator may be causing a problem? That's normally intended for use with a 32768-Hz crystal...
     
    Last edited: Feb 15, 2016
  14. PIC-Programming

    Thread Starter New Member

    Feb 15, 2016
    9
    0
    Tried, still same Problem..
     
  15. Picbuster

    Member

    Dec 2, 2013
    373
    50
    The timer1 when activated ( TMR1IE=1;TMR1IF=0;) is always running even in sleep mode
    disable the timer TMR1IE=0; and make sure that the flag is also null TMRIF=0;
    This will avoid a timer startup.
    In an other place(main) you do:
    TMR1IE=1;
    TMR1IF=0; // a waiting int is deleted remember that TMR1IF could be set without an interrupt event.
     
  16. PIC-Programming

    Thread Starter New Member

    Feb 15, 2016
    9
    0
    How/Why could the TMR1IF be set without an Interrupt Event?
     
  17. Picbuster

    Member

    Dec 2, 2013
    373
    50
    Please read pic manual in section interrupts ' the flag will be set regardless the status of its correspondence enable bit'
     
  18. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Yep, what Pixcbuster said. The flags get set irregardless of the interrupt enable flag. That allows you to poll the flag bit for those using the timer without an interrupt.
     
  19. PIC-Programming

    Thread Starter New Member

    Feb 15, 2016
    9
    0
    Now I understand.

    However I just use a counter that counts up every overflow Interrupt and set it to zero if I want to "start it".
     
  20. Picbuster

    Member

    Dec 2, 2013
    373
    50
    use this

    //--------------- in your interrupt -----------------------
    if(INTCONbits.TMR0IF)
    {
    INTCONbits.TMR0IF=0; //TMR0 interrupt flag
    timer++ ;
    Other0=0;
    }
    //---------------------------------------------------------

    if (TMR1IF)
    {
    TMR1IF=0;
    Sec=Sec+16;
    Other1=0;
    }
    //=============== end isr =================================


    //-------------------- in Main -------------------------------


    while(1);
    {
    if ( your_thingy)
    {
    TMR1IE=1; // use counter 1 when your_thingy = 1
    TMR1IF=0;

    TMR0IE=0;
    TMR0IF=0;
    }

    If (!your_thingy)
    {
    TMR0IE=1; // use counter0 when your_thingy = 0
    TMR0IF=0;
    TMR1IE=1;
    TMR1IF=0;
    }

    // better to do is;

    TMR1IE= your_thingy;
    TMR0IE= !your_thingy;
    TMR0IF=0;
    TMR1IF=0;

    // clear or preset the counters before you use them that's up to you.
    }
     
Loading...