T0 interrupt Problem

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

Atmega16,The code is as follows:

void Timer0_Init(void)
{
	#if 1
	TCCR0 = 0x00;
	TCNT0 = 0x00;	//
	OCR0  = 200;//100 ;	//输出比较寄存器
	
	TIMSK = 0x02;	//中断屏蔽寄存器	//输出比较匹配中断使能
	TCCR0 = 0x02;	//控制寄存器	8分频
	#endif
}
void TIMER0_COMP_vect (void)
{
	time_100us_cnt++;
	TCNT0 = 0x00;
	//sei();
}

Every time I found in the interrupt service routine,SREG--The global interrupt is clear,and next will not come in the interrupt.Why the global interrupt was clear when the TIMER0 interrupt is conming?

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

By design, the cpu clears the global interrupt bit when servicing an interrupt.

Normally, the interrupt service routine will finish with the 'return from interrupt' instruction, reti. This restores the global interrupt bit, allowing the next interrupt to be serviced.

However, you have implemented the vector as a normal function, so it finishes with the 'return from subroutine' instruction ret, which does not restore the global interrupt bit.

Implementing the vector as

ISR(TIMER0_COMP_vect)

will cause the correct return instruction to be used, as well as inserting proper code to prevent the ISR from clobbering registers which may have been in use by the main thread.

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

mikericetga wrote:
By design, the cpu clears the global interrupt bit when servicing an interrupt.

Normally, the interrupt service routine will finish with the 'return from interrupt' instruction, reti. This restores the global interrupt bit, allowing the next interrupt to be serviced.

However, you have implemented the vector as a normal function, so it finishes with the 'return from subroutine' instruction ret, which does not restore the global interrupt bit.

Implementing the vector as

ISR(TIMER0_COMP_vect)

will cause the correct return instruction to be used, as well as inserting proper code to prevent the ISR from clobbering registers which may have been in use by the main thread.

Thank you for you advice! And now it's ok!

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

Unless I'm missing something, isn't the real problem that there was no code for the interrupt to vector to? Just declaring some function with an interrupt vector name doesn't mean that code becomes the ISR for that interrupt. Of course, the solution is the same - properly define the ISR function, which both gets its address inserted into the interrupt vector table, and assures that the correct entry and exit code is generated for an ISR.