# TIMER0\_OVF and TIMER2\_OVF

**URL:** <https://forums.freertos.org/t/timer0-ovf-and-timer2-ovf/2164>\
**Category:** Kernel\
**Created:** [February 1, 2008, 2:21pm UTC](https://forums.freertos.org/t/timer0-ovf-and-timer2-ovf/2164 "2008-02-01T14:21:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 1, 2008, 2:21pm UTC](https://forums.freertos.org/t/timer0-ovf-and-timer2-ovf/2164/1 "2008-02-01T14:21:24Z")

</div>

**[brenodee](https://sourceforge.net/u/brenodee/)** wrote on [Friday, February 01, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/61a63ebf/#fa3b):

Hi all, I’m tried to use the interruption from Timer2\_ovf and Timer0\_ovf an atmega128 but it doesn’t work out. The INT0 and INT1 vector work fine. I’m posting my code so that you could figure out.

#include \<stdlib.h\>  
#include \<string.h\>  
#include \<stdio.h\>

#ifdef GCC\_MEGA\_AVR  
&nbsp;&nbsp;&nbsp; /\* EEPROM routines used only with the WinAVR compiler. \*/  
&nbsp;&nbsp;&nbsp; #include \<avr/eeprom.h\>  
#endif

/\* Scheduler include files. \*/  
#include "FreeRTOS.h"  
#include "task.h"  
#include "croutine.h"  
#include "semphr.h"  
#include \<avr/interrupt.h\>  
#include \<avr/sleep.h\>  
#include \<…/…/Source/include/semphr.h\>  
#include \<math.h\>  
/\* Demo file headers. \*/

#include "PollQ.h"  
#include "integer.h"  
#include "serial.h"  
#include "comtest.h"  
#include "crflash.h"  
#include "print.h"  
#include "partest.h"  
#include "regtest.h"

#define SIGNAL&nbsp;&nbsp;&nbsp; \_\_attribute\_\_ ((signal))

/\* Baud rate used by the serial port tasks. \*/  
#define mainCOM\_TEST\_BAUD\_RATE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ( ( unsigned portLONG ) 38400 )

void TIMER0\_OVF(void)\_\_attribute\_\_ ((signal,naked));  
void vApplicationIdleHook( void );  
/\*-----------------------------------------------------------\*/

portSHORT main( void )  
{  
&nbsp;&nbsp;&nbsp; /\* Registers \*/

&nbsp;&nbsp;&nbsp; SREG&nbsp; = 0x80;  
&nbsp;&nbsp;&nbsp; MCUCR = 0x03;  
&nbsp;&nbsp;&nbsp; EICRA = 0x0F;  
&nbsp;&nbsp;&nbsp; EIMSK = 0x03;  
&nbsp;&nbsp;&nbsp; TIMSK = 0x01;  
&nbsp;&nbsp;&nbsp; TCCR0 = 0x03;  
&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp; xSerialPortInitMinimal( mainCOM\_TEST\_BAUD\_RATE, 1 );

&nbsp;&nbsp;&nbsp; vParTestInitialise();  
&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp; while(1);

&nbsp;&nbsp;&nbsp; return 0;  
}

/\*-----------------------------------------------------------\*/  
ISR(TIMER0\_OVF\_vect) // INT\_0 or INT\_1 work out!!!  
{  
&nbsp;&nbsp;&nbsp; vParTestToggleLED( 1);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vParTestInitialise();  
}

void vApplicationIdleHook( void )  
{  
&nbsp;&nbsp;&nbsp; vCoRoutineSchedule();  
}

Thanks, for all!!!!

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 1, 2008, 7:40pm UTC](https://forums.freertos.org/t/timer0-ovf-and-timer2-ovf/2164/2 "2008-02-01T19:40:10Z")

</div>

**[woops\_](https://sourceforge.net/u/woops_/)** wrote on [Friday, February 01, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/61a63ebf/#fa3b/d075):

Dont know the answer but dont think it is anything todo with FreeRTOS so must just be in the register settings. Are there examples in the compiler to copy?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 1, 2008, 8:55pm UTC](https://forums.freertos.org/t/timer0-ovf-and-timer2-ovf/2164/3 "2008-02-01T20:55:49Z")

</div>

**[jaydmdigital](https://sourceforge.net/u/jaydmdigital/)** wrote on [Friday, February 01, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/61a63ebf/#fa3b/7eba):

See my thread here [https://sourceforge.net/forum/forum.php?thread\_id=1882976&forum\_id=382005](https://sourceforge.net/forum/forum.php?thread_id=1882976&forum_id=382005)

The default is for Timer1 not 0 or 2 (8-bit) or 3(second 16-bit timer)

If you want to change it, you need to modify the port.c file  
…\FreeRTOS\Source\portable\GCC\ATMega323

#define portCOMPARE\_MATCH\_A\_INTERRUPT\_ENABLE&nbsp;&nbsp;&nbsp; ( ( unsigned portCHAR ) 0x10 )  
//set it to the correct interrupt mask for TIMER3 if you like

/\*  
\* Setup timer 1 compare match A to generate a tick interrupt.  
\*/  
static void prvSetupTimerInterrupt( void )  
{  
unsigned portLONG ulCompareMatch;  
unsigned portCHAR ucHighByte, ucLowByte;

&nbsp;&nbsp;&nbsp; /\* Using 16bit timer 1 to generate the tick.&nbsp; Correct fuses must be  
&nbsp;&nbsp;&nbsp; selected for the configCPU\_CLOCK\_HZ clock. \*/

&nbsp;&nbsp;&nbsp; ulCompareMatch = configCPU\_CLOCK\_HZ / configTICK\_RATE\_HZ;

&nbsp;&nbsp;&nbsp; /\* We only have 16 bits so have to scale to get our required tick rate. \*/  
&nbsp;&nbsp;&nbsp; ulCompareMatch /= portCLOCK\_PRESCALER;

&nbsp;&nbsp;&nbsp; /\* Adjust for correct value. \*/  
&nbsp;&nbsp;&nbsp; ulCompareMatch -= ( unsigned portLONG ) 1;

&nbsp;&nbsp;&nbsp; /\* Setup compare match value for compare match A.&nbsp; Interrupts are disabled  
&nbsp;&nbsp;&nbsp; before this is called so we need not worry here. \*/  
&nbsp;&nbsp;&nbsp; ucLowByte = ( unsigned portCHAR ) ( ulCompareMatch & ( unsigned portLONG ) 0xff );  
&nbsp;&nbsp;&nbsp; ulCompareMatch \>\>= 8;  
&nbsp;&nbsp;&nbsp; ucHighByte = ( unsigned portCHAR ) ( ulCompareMatch & ( unsigned portLONG ) 0xff );  
&nbsp;&nbsp;&nbsp; OCR1AH = ucHighByte;  
&nbsp;&nbsp;&nbsp; OCR1AL = ucLowByte;  
//Change to OCR3AH and AL

&nbsp;&nbsp;&nbsp; /\* Setup clock source and compare match behaviour. \*/  
&nbsp;&nbsp;&nbsp; ucLowByte = portCLEAR\_COUNTER\_ON\_MATCH | portPRESCALE\_64;  
&nbsp;&nbsp;&nbsp; TCCR1B = ucLowByte;  
// Make sure the correct register for TIMER3

&nbsp;&nbsp;&nbsp; /\* Enable the interrupt - this is okay as interrupt are currently globally  
&nbsp;&nbsp;&nbsp; disabled. \*/  
&nbsp;&nbsp;&nbsp; ucLowByte = TIMSK;  
&nbsp;&nbsp;&nbsp; ucLowByte |= portCOMPARE\_MATCH\_A\_INTERRUPT\_ENABLE;  
&nbsp;&nbsp;&nbsp; TIMSK = ucLowByte;  
}

too bad no way to mark up my comments

Jay
