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 get exact 1 ms Timer0 OVF Interrupt for ATMega8A ?

Status
Not open for further replies.

baileychic

Advanced Member level 3
Joined
Aug 2, 2017
Messages
728
Helped
56
Reputation
112
Reaction score
57
Trophy points
28
Activity points
7,033
How to get exact 1 ms Timer0 OVF Interrupt for ATMega8A ?

This is my code. I am not getting exact 1 ms interrupt. My OSC is 4 MHz Internal RC Oscillator. Fuses are correct.

Code:
void InitTimer0() {
    TCCR0 = 0x02; 
    SREG_I_bit = 1; 
    TOIE0_bit = 1;
}
 
void Timer0Overflow_ISR() org IVT_ADDR_TIMER0_OVF {
    TCNT0 = 0x06;
}
 

Hi,

How exact do you want the 1ms?
Give a tolerance.
--> internal RC isn't very precise

Do you want a one shot 1ms, or continously 1ms?

I am not getting exact 1 ms interrupt.
This tells nothing about what you see.
Is it 1.00001ms or do you see 0.1ms, or do you see 100ms or do you see varying timing?

Fuses are correct.
So why do you hide them as secret?

--> The more valuable informations you give the better is the response.

Klaus
 

ТССR0 = 3; // prescaler 64
//TCNT0 reload value:
TCNT0 = 4E6/64/1000-1; //interrupt freq. approx. 1008Hz
 

Hi,

You want exact 1.000kHz interrupt frequency?
Then use another timer/counrt with ctc mode.

Klaus
 

Is this correct ?

Code:
TclkT1 = 64/fclkI/O = 64/4 MHz = 1.6us
1ms/1.6us = 62.5
= ~ 63
65536 - 63 = 65473 = 0xFFC1

TCNT1H = 0xFF
TCNT1L = 0xC1

Prescalar = 64

TCCR1A = 0x00
TCCR1B = 0x03

void InitTimer1() {
	SREG_I_bit = 1;	
	TCCR1A = 0x00;
	TCCR1B = 0x03;
	TCNT1H = 0xFF;
	TCNT1L = 0xC1;
	TOIE1_bit = 1;
}


void Timer1Overflow_ISR() org IVT_ADDR_TIMER1_OVF {

	TCNT1H = 0xFF;
	TCNT1L = 0xC1;

	PORTB.B1 = ~PORTB.B1;
}
 

Hi,

Now you switched to timer1
* it is a 16 bit timer, now you could use divider of 8 to get exact 1.000ms --> you are loosing accuracy
* now you have ctc available, but you don't use it --> you are loosing precision

Klaus
 

I don't know about CTC. I was writing code for an existing hardware. If Timer1 COMPA is used then PORTB.B1 was not working. So, I switched to OVF. I tried with Timer0 but didn't get exact 1ms and hence tried with Timer1 OVF.
 

Hi,

I don't know about CTC
Before I read the datasheet I didn't know about CTC, too.
But lucky us, they have written the datasheet.

Klaus
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top