generating few delays using timer 1

Thread Starter

abo zaynab

Joined May 1, 2018
35
void Interrupt(){
if (TMR1IF_bit){
TMR1IF_bit = 0;
TMR1H = 0x0B;
TMR1L = 0xDC;
sec2++;
sec3++;
sec4++;
}
}

whenever i use those variables to make a different delays just 1 delay works only,ex.if i made sec2 for 10 seconds n sec3 for 15 sec just the 10seconds only works for both of them
 

danadak

Joined Mar 10, 2018
4,057
Is this Microchip ?

Just a thought does timer, when period register updated, need a reset as
well to start it using new period ?

Regards, Dana.
 
Last edited:

Thread Starter

abo zaynab

Joined May 1, 2018
35
Is this Microchip ?

Just a thought does timer, one period register updated, need a reset as
well to start it using new period ?

Regards, Dana.
thanks forward but i reset both variables before using them again but still the same problem only 1 of them works
 

JohnInTX

Joined Jun 26, 2012
4,787
Assuming that TIMER1 is interrupting at 1 sec intervals, consider loading the derived timers and counting down to zero then stopping. That way, whatever is waiting on the timer doesn’t have to worry about rollover or missing a specific value. Like this:

C:
// every second in interrupt routine
if(sec2) sec2–; // decrement if not zero else leave at zero
if(sec3) sec3–; // and so on..

//then to use the timer
sec2 = 10; // set timer;

while(1){
   if(sec2 == 0) // test for timeout when able
     do_timeout_2();
   else
     do_something_else();
   }
Keep in mind that if your timers are more than one byte, you have to temporarily disable the interrupt when you set or read the timer.
Good luck!
 

Picbuster

Joined Dec 2, 2013
1,057
Simple solution
in interrupt (Timer like TMR1 set speed either by divider or count to a certain value)

Counter--;
if (Counter<1) { Counter=0;}

//in Program
//Set counter to value
//When zero time is past
//Example
Counter = 123;
while (Counter){}

It is also possible to write a macro or use or create a delay(nnn) function ( see microchip's Lib).

Picbuster
 

Thread Starter

abo zaynab

Joined May 1, 2018
35
Simple solution
in interrupt (Timer like TMR1 set speed either by divider or count to a certain value)

Counter--;
if (Counter<1) { Counter=0;}

//in Program
//Set counter to value
//When zero time is past
//Example
Counter = 123;
while (Counter){}

It is also possible to write a macro or use or create a delay(nnn) function ( see microchip's Lib).

Picbuster
thanks but i dont want to use delay() routine as i dont know how to interrupt the routine while its working to do something else
 

Thread Starter

abo zaynab

Joined May 1, 2018
35
Assuming that TIMER1 is interrupting at 1 sec intervals, consider loading the derived timers and counting down to zero then stopping. That way, whatever is waiting on the timer doesn’t have to worry about rollover or missing a specific value. Like this:

C:
// every second in interrupt routine
if(sec2) sec2–; // decrement if not zero else leave at zero
if(sec3) sec3–; // and so on..

//then to use the timer
sec2 = 10; // set timer;

while(1){
   if(sec2 == 0) // test for timeout when able
     do_timeout_2();
   else
     do_something_else();
   }
Keep in mind that if your timers are more than one byte, you have to temporarily disable the interrupt when you set or read the timer.
Good luck!
thanks ur solution was helpfull
 
Top