TMR0 works properly, TMR1 - 3 starts immediately after releasing

Thread Starter

PIC-Programming

Joined Feb 15, 2016
9
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!
 

jpanhalt

Joined Jan 18, 2008
11,087
Code:
INTCONbits.PEIE = 1; // Enable peripheral interrupts
INTCONbits.GIEH = 1; // Enable all Interrupts

PIR1bits.TMR1IF = 0;
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
 

Thread Starter

PIC-Programming

Joined Feb 15, 2016
9
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:

jpanhalt

Joined Jan 18, 2008
11,087
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
 

Dodgydave

Joined Jun 22, 2012
11,285
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.
 

MMcLaren

Joined Feb 14, 2010
861
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:

Picbuster

Joined Dec 2, 2013
1,047
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!
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.
 

Thread Starter

PIC-Programming

Joined Feb 15, 2016
9
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.
How/Why could the TMR1IF be set without an Interrupt Event?
 

ErnieM

Joined Apr 24, 2011
8,377
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.
 

Picbuster

Joined Dec 2, 2013
1,047
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.
}
 
Top