Timer0 Start/Stop.

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Freakers,

In my code I'm enable "TIMSK |= (1<<TOIE0);" when a button pressed.

After making decision In my "ISR(TIMER0_OVF_vect)" I'm disable timer "TIMSK &=~ (1<<TOIE0);"

 

 

Firs time when i'm press button program goes to ISR.

When second time i'm pressed button program does not go to ISR.

 

How Can i fixed it?

 

in main:

if(PINB & (1 << PB0))
        {
         
            TIMSK |= (1<<TOIE0);
            
        }

 

ISR :

 

ISR(TIMER0_OVF_vect)
{
    PORTB |= (1<<PB6);
    
    if(t > 300)
    {
        PORTB &=~ (1<<PB6);
        TIMSK &=~ (1<<TOIE0);
    }
    
    else
    {
        
        t++;
    }
    
}

 

This topic has a solution.

শূন্য  - The ZeRo

Last Edited: Wed. Oct 26, 2016 - 12:43 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

void set_Timer()
{
    TCCR0 |= (1<<CS01) | (1<<CS00);    //Set Prescaller 64
    TCNT0 = 247;
  
}

 

শূন্য  - The ZeRo

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Where and how is t defined?

There must be some code missing; however, from what is shown, once t increments to 300 where is it cleared?

 

Although, what you are attempting is possible, it seems to be a poor use of the timer.

 

I would probably do something like:

volatile uint16_t t_count;   // System Timer0 Overflow tick counter
uint16_t PortBstartTime;         // Start time for PORTB event 

ISR(TIMER0_OVF_vect)
{
    t_count++;
}

static inline uint16_t getCurrentTime(void) __attribute__((always_inline));
uint16_t getCurrentTIME(void) {
	uint16_t temp;
	ATOMIC_BLOCK(ATOMIC_FORCEON) {
		temp = t_count;
	}
	return (temp);
}

in main() {
  ...
  while (1) {
    ...
    if(PINB & (1 << PB0)) {
            PORTB |= (1 << PB6);  // PB6 high
            PortBstartTime = getCurrentTime();  // Load current time into PortBstartTime
    }

    // The 300 in the following would be the count of Timer0 Overflows that should be delayed
    if ((int16_t)(getCurrentTime() - PortBstartTime) > 300) {
        PORTB &= ~(1 << PB6);   // PB6 low
    }
  ...
  }
}

This minimizes time in the ISR and allows timing for multiple events, just saving a startTime for each event.

Edit: If PB0 goes high (again) while the delay is waiting for 300 ticks, a new PortBstartTime will be generated keeping PB6 high.

David (aka frog_jr)

Last Edited: Wed. Oct 26, 2016 - 04:11 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1
    if(t > 300)
    {
        PORTB &=~ (1<<PB6);
        TIMSK &=~ (1<<TOIE0);
        t = 0; // <<<<<<<<<<<<<<< !!!!!!!!!!
    }

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lots of thanks dear clawson

 

t = 0; // <<<<<<<<<<<<<<< !!!!!!!!!!

Now its work fine :)

শূন্য  - The ZeRo

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks frog_jr

 

actually i'm forget to clear "t"!

শূন্য  - The ZeRo

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

After making decision In my "ISR(TIMER0_OVF_vect)" I'm disable timer "TIMSK &=~ (1<<TOIE0);"

Just to be clear -- that does NOT "disable timer".  Rather, it just disables one of the interrupt sources for that timer's events from firing.  The timer is not stopped.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ummm,

 

Following line may be Disable Timer right?

TCNT0 = 0;

 

শূন্য  - The ZeRo

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

eRony wrote:
Following line may be Disable Timer right?

No.

(and the thread title indeed says Start/Stop)

 

Even if you do actually "stop" the timer from continuing to count (see the datasheet -- it sure would help if you told us which AVR model is involved), there still may be pins attached to the timer if e.g. you were doing CTC or PWM.

 

See the "stopped" in there?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nope there is no stopped!

 

I'm using ATmega8.

শূন্য  - The ZeRo

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

eRony wrote:

Nope there is no stopped!

 

I'm using ATmega8.

Of course there is. In the beginning (as the book says) timers are all stopped. They only start to run when you set the CS bits to something other than 0. To stop you put the CS bits back to 0.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

TCCR0 &=~ (1<<CS02) | (1<<CS01) | (1<<CS00);

Now right?

শূন্য  - The ZeRo

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

eRony wrote:
Nope there is no stopped! I'm using ATmega8.

???

 

No clock source (Timer/Counter stopped)

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So if i "TIMSK &=~ (1<<TOIE0);" do it.

Timer is Still running?

 

Just stop giving OVF Interrupt.

 

শূন্য  - The ZeRo

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

শূন্য  - The ZeRo

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm really learning something new :)

শূন্য  - The ZeRo