Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to use Timer0 on ATMEGA128?

Status
Not open for further replies.

bianchi77

Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Activity points
9,442
Guys,

Does anyone of you have experience on how to init timer0 on ATMEGA128 ?
Any shares or links will be appreciated,

Thanks
 

ATMega128, 8 MHz Fosc, Timer0 10 ms interrupt, mikroC Code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Timer0 Prescaler = 1024; Preload = 78; Actual Interrupt Time = 9.984 ms
 
//Place/Copy this part in declaration section
void InitTimer0(){
  SREG_I_bit = 1; 
  OCR0 = 78; 
  TCCR0 = 0x28;
  TCCR0 |= 0x05;
  OCIE0_bit = 1; 
}
 
void Timer0Overflow_ISR() org IVT_ADDR_TIMER0_COMP {
  //Enter your code here 
}

 

ATMega128, 8 MHz Fosc, Timer0 10 ms interrupt, mikroC Code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Timer0 Prescaler = 1024; Preload = 78; Actual Interrupt Time = 9.984 ms
 
//Place/Copy this part in declaration section
void InitTimer0(){
  SREG_I_bit = 1; 
  OCR0 = 78; 
  TCCR0 = 0x28;
  TCCR0 |= 0x05;
  OCIE0_bit = 1; 
}
 
void Timer0Overflow_ISR() org IVT_ADDR_TIMER0_COMP {
  //Enter your code here 
}


Thank you my friend

- - - Updated - - -

How can I init it on AVR Studio ?
 

I created :
Code:
/*
 * Timer_simple_v1.0
 *
 * Created: 29/05/2013 11:52:36 AM
 *  Author: Antonius
 */ 
 #include <avr/io.h>
 #define LEDOFF  PORTA |= (1<<1)
 #define LEDON   PORTA &= ~(1<<1)


void init_Ex1(void)
{
	TCCR0 = (1<<CS02)|(1<<CS00); //Timer clock =
	//system clock /
	//1024
	TIFR = 1<<TOV0; //Clear TOV0 / clear
	//pending interrupts
	TIMSK = 1<<TOIE0; //Enable Timer0
	//Overflow Interrupt
	DDRC = 0xFF; //Set Port B as
	//output
}

ISR(TIM0_OVF_vect)
{
		// Add code here to derive other timebases
		PORTC = ~PORTC;
	
}

 
int main (void)
{
       init_Ex1();
	  
}

How can I call it on main() ?

thanks

- - - Updated - - -

Am I writing it correctly ? or do I miss something here ?
Code:
/*
 * Timer_simple_v1.0
 *
 * Created: 29/05/2013 11:52:36 AM
 *  Author: Antonius
 */ 
 #include <avr/io.h>



void init_Ex1(void)
{
	TCCR0 = (1<<CS02)|(1<<CS00); //Timer clock =
	//system clock /
	//1024
	TIFR = 1<<TOV0; //Clear TOV0 / clear
	//pending interrupts
	TIMSK = 1<<TOIE0; //Enable Timer0
	//Overflow Interrupt
	DDRC = 0xFF; //Set Port B as
	//output
}

ISR(TIM0_OVF_vect)
{
		// Add code here to derive other timebases
		PORTC = ~PORTC;
	
}

 
int main (void)
{
       DDRC = 0xFF;
	   PORTC = 0xFF;
	   init_Ex1();
	   ISR();
	  
}
 

You have to call the timer initialization or start code once in the main(). If calling from inside while(1) loop then make sure you just call it once.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top