# TMR0 works properly, TMR1 - 3 starts immediately after releasing

#### 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!

Joined Jul 18, 2013
26,291
I don't use C but can't you just turn TMR1 off?
TMR1ON = 0.
Max.

#### 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

#### 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.

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
10,591
why dont you enable the timer, then clrf timer1, then start the programme, instead of enabling it in setup.

#### PIC-Programming

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

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

#### dannyf

Joined Sep 13, 2015
2,197
TIMER_INT_ON
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.

#### Dodgydave

Joined Jun 22, 2012
10,591
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.

#### PIC-Programming

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

#### dannyf

Joined Sep 13, 2015
2,197
he clock Speed of the internal osc is 1MHz and the timer should run/runs for 0,5 sec
I can confirm for you that your code works just fine. The problem is somewhere else.

#### PIC-Programming

Joined Feb 15, 2016
9
I can confirm for you that your code works just fine. The problem is somewhere else.
Thank you!

#### MMcLaren

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

#### PIC-Programming

Joined Feb 15, 2016
9
I wonder if enabling the Timer 1 Oscillator may be causing a problem? That's normally intended for use with a 32768-Hz crystal...
Tried, still same Problem..

#### Picbuster

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

#### 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?

#### Picbuster

Joined Dec 2, 2013
1,040
How/Why could the TMR1IF be set without an Interrupt Event?
Please read pic manual in section interrupts ' the flag will be set regardless the status of its correspondence enable bit'

#### ErnieM

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

#### PIC-Programming

Joined Feb 15, 2016
9
Please read pic manual in section interrupts ' the flag will be set regardless the status of its correspondence enable bit'
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".

#### Picbuster

Joined Dec 2, 2013
1,040
use this

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.
}