Timer Interrupt.

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

I'm try to make a delay with Timer.It's build fine but some times its gonna hang and delay time changes.code is following:

/************************************************************************/
//Timer1 Overflow interrupt subroutine
/************************************************************************/
volatile char	Timer1_OverFlow_Flag = 0;
uint16_t		Timer1_i		     = 0;
#define			Time1_Delay		      0.001					
//1 = 1S,100 ms = 0.1 sec = 10Khz,500 ms = 0.5 sec,
//50 ms = 0.05 sec,1 ms = 0.001 sec,0.0005 = 900Hz
ISR(TIMER1_OVF_vect)
{
	
	if(Timer1_i>(Time1_Delay/0.000185))
	{
		Timer1_OverFlow_Flag = 1;
		Timer1_i = 0;
		
		//PORTD ^= (1<<PORTD1);
	}
	
	Timer1_i++;
}



/************************************************************************/
//Configure Timer
/************************************************************************/
void set_Timer1()
{
	TCCR1A  = 0x00;							//Normal Mode
	TCCR1B |= (1<<CS11);// | (1<<CS00);		//clk I/O/8
	//TIMSK1 |= (1<<TOIE1);					//Enable Timer1 Overflow interrupt

	TCNT1   = 65280;						//256
}


//////////////////////////////////////////////////////////////////////////////////
void EnableTimer1()
{
	TIMSK1 |=  (1<<TOIE1);					//Enable Timer1 Overflow interrupt
}

void DisableTimer1()
{
	TIMSK1 &=~ (1<<TOIE1);					//Disable Timer1 Overflow interrupt

}
//////////////////////////////////////////////////////////////////////////////////


int main()
{

	set_Timer1();
	
	sei();						//Enable global interrupt
    DDRD |= 1<<DDRD1;
    
    while(1)
    {
        PORTD ^= (1<<PORTD1);
        
        EnableTimer1();
 			while(!Timer1_OverFlow_Flag);
 			Timer1_OverFlow_Flag = 0;
 		DisableTimer1();
    }
    
 return 0;   
    
}

 

শূন্য  - The ZeRo

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

Your code is quite messy to read. This forum is not very advanced. It seems that you can change the "tab" settings in the sourcecode post window to 4. That may help, Or use your IDE to use consistent spacing with tabs or spaces.

 

I hope you know that:

>> .001/0.000185
ans =  5.4054

does not fit nicely int an integer, and it will be truncated to 5.

 

If you want regular timing you should not use EnableTimer1() and DisableTimer1().

When posting code, please make an small and clear example. Remove the commented out code.

For what processor are you compiling? There are a lot of small differences between AVR timers.

What happens if you toggle a led in the ISR? Does it run regularly then?

Apart from that I see no obvious mistakes.

Your checking of the flagand led blinking should all be so fast that slight timing irregularities thare should not matter.

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR's: http://www.hoevendesign.com