1 sec timer using internal 8MHZ osc of ATMEGA-8

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

I usually use the internal 8MHZ oscillator of ATMEGA-8.

How can I create appropriate 1sec or 250mS or any integer accurate mS timer by using internal 8MHZ osc [not any external osc]?

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

Oh come on! Surely you will need a divisor to scale down your main clock. How you set up such a divisor is clearly documented in the manual as well as in thousands of tutorials on the web. Then you need to define the mode of the timer you're gonna use. Again, just read up the timer section in your particular AVR's manual.

Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."

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

Dear DO1THL,

Thanks for your comments.

I know all the information are clearly documented in the manual.

But if you help me to write the code(mS/Sec delay using 8MHZ internal osc) then that will be more helpful for me.

Thanks

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

Quote:
I know all the information are clearly documented in the manual.

But if you help me to write the code(mS/Sec delay using 8MHZ internal osc) then that will be more helpful for me.


Can you imagine that the statement above is like a slap in the face? Basically you are saying: "Well, although i do know that everything i need to know is written down in large manuals, i don't care about wasting my time reading them. I much more prefer to ask others who did spend their time to read those large manuals to do even more work for me and write the code i need for my project, my home work, my school assignment, or the task my boss has just assigned to me."

Evaluate your chances of getting help right now... (that is: until uncle Bob chimes in, he might be your last resort)

Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."

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

The code is exactly the same if you use internal or external oscillator! So if you are too lazy to figure it out yourself you should at least be able to find some example code?

Precision however might not be totally accurate with the internal oscillator, you can trim your internal oscillator though to achieve better precision! Accurate timing is on of the main reasons to use an external crystal.

- Brian

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

Gents,

Let's try to be helpful and not scare away recently joined users of this site.

harunduet, to give you some pointers (though you will have to actually write the code) I'd start by pointing out that the AVR timers have a useful mode called Clear Tmer on Compare (CTC). In this mode you can have a timer count up to a certain number then trigger some kind of action (an interrupt) and then reset the counter to 0 and start all over again. So the trick to making a 1 second timer is either to arrange for the timer to count all the way to 1 second and then trigger or maybe (if the count would be too high) have it trigger something like every 1/0th of a second and when it does you simply increment your own counter variable and when that reaches 10 you do the stuff that has to happen every second (and then set the counter back to 0 to start over).

Now you are using a mega8. It has two 8bit timers (that can count to 256) and one 16bit timer (that can count to 65536). If you ran any of these timers at the same speed as the main CPU frequency (8MHz) then 256/8000000 or even 65536/8000000 is a very short time. Even the latter is only 8 one thousandths of a second so you'd then have to count yourself a lot of such events to reach a whole second.

However timers also have a "prescaler" which means they can count at some fraction of the main CPU speed. You can divide the count rate by 8, 64, 256 or 1024 to make it run that much slower. So take that 16bit timer that counts to 65535 in 0.008s if you could make it run about 1000 times slower it could be in the right ball-park for what you want to achieve. so consider running the 16bit timer at the /1024 rate. This means it counts to 65536 in (65536 * 1024)/8000000 which is 8.388608 seconds. So that's actually about 8 times too long. So this suggests we don't want it to count all the way from 0 to 65535 but stop and reset back to 0 at about 1/8th of that count.

In fact there is a utility called avrcalc.exe (google/search) that can work out exactly the count you'd need so put into that 8MHz and /1024 and it says count 0x1E84 = 7,812 steps and you will get an even every 1000.064 seconds. The .064 in that is because it's very difficult with binary division to get an 8MHz clock to count an exact number of seconds and this may suggest that using a crystal with a slightly different frequency may be a better choice to achieve your 1000ms goal. But 1000.064s is pretty close and may be OK for you.

So bottom line:

1) 16 bit timer
2) CTC mode
3) /1024 prescaler
4) 0x1E84 count (which means putting 0x1E83 in the CTC comparison register because 0x0000 itself is one count).

Now you try to write that code using the datasheet and show us what you've got if you hit problems.

Attachment(s): 

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

In this case you could set the prescaler to divide by 256, since 8000000 / 256 = 31250. Set the CTC value to 31249.

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

clawson wrote:
Gents,

Let's try to be helpful and not scare away recently joined users of this site.

harunduet, to give you some pointers (though you will have to actually write the code) I'd start by pointing out that the AVR timers have a useful mode called Clear Tmer on Compare (CTC). In this mode you can have a timer count up to a certain number then trigger some kind of action (an interrupt) and then reset the counter to 0 and start all over again. So the trick to making a 1 second timer is either to arrange for the timer to count all the way to 1 second and then trigger or maybe (if the count would be too high) have it trigger something like every 1/0th of a second and when it does you simply increment your own counter variable and when that reaches 10 you do the stuff that has to happen every second (and then set the counter back to 0 to start over).

Now you are using a mega8. It has two 8bit timers (that can count to 256) and one 16bit timer (that can count to 65536). If you ran any of these timers at the same speed as the main CPU frequency (8MHz) then 256/8000000 or even 65536/8000000 is a very short time. Even the latter is only 8 one thousandths of a second so you'd then have to count yourself a lot of such events to reach a whole second.

However timers also have a "prescaler" which means they can count at some fraction of the main CPU speed. You can divide the count rate by 8, 64, 256 or 1024 to make it run that much slower. So take that 16bit timer that counts to 65535 in 0.008s if you could make it run about 1000 times slower it could be in the right ball-park for what you want to achieve. so consider running the 16bit timer at the /1024 rate. This means it counts to 65536 in (65536 * 1024)/8000000 which is 8.388608 seconds. So that's actually about 8 times too long. So this suggests we don't want it to count all the way from 0 to 65535 but stop and reset back to 0 at about 1/8th of that count.

In fact there is a utility called avrcalc.exe (google/search) that can work out exactly the count you'd need so put into that 8MHz and /1024 and it says count 0x1E84 = 7,812 steps and you will get an even every 1000.064 seconds. The .064 in that is because it's very difficult with binary division to get an 8MHz clock to count an exact number of seconds and this may suggest that using a crystal with a slightly different frequency may be a better choice to achieve your 1000ms goal. But 1000.064s is pretty close and may be OK for you.

So bottom line:

1) 16 bit timer
2) CTC mode
3) /1024 prescaler
4) 0x1E84 count (which means putting 0x1E83 in the CTC comparison register because 0x0000 itself is one count).

Now you try to write that code using the datasheet and show us what you've got if you hit problems.

If it wasn't for people like you I'd have lost all faith entirely in the world.
Sometimes, some of us can read a data sheet ten times and not understand a single word of it until some kind sir comes along and explains it (the same exact way as the data sheet) but with a human voice, and everything clicks like a lego set.

Much appreciated.
your post may have helped determine a positive outcome for my current project.
Love the positivity flowing from your direction...
Well played.

if (Learning_AVRs)
{
DOH();
}

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

Thanks Clawson for setting the example on how to respond to a technical question. 8)

I'll believe corporations
are people when Texas executes one.

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

Quote:
Sometimes, some of us can read a data sheet ten times and not understand a single word of it until some kind sir comes along and explains it

I guess you missed this part:

Quote:
I know all the information are clearly documented in the manual.


THATS what irritated D01THL and I would not blame him. I took it the same way as the OP does not want to do his/her own homework of code it, run it, debug it.

The OP in their opening post says they usually use the internal 8mhz oscillator. I would gather that the OP has a clue to some element.

Quote:
Let's try to be helpful and not scare away recently joined users of this site.

March 30, 2011 is considered 'Just joined' ;)

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

A common approach to getting "long" delays (and 1 second @ 8MHz clock is pretty long!) is to generate faster ticks and count these ticks in software to get long delays. It is common to choose a tick interval around 10ms, for a variety of reasons.

You can get 10ms from an 8MHz clock by e.g. using a prescaler of 4 and a CTC count of 20000 (load 19999 into the compare register). Or, if you need your 16-bit timer for other purposes, you can choose a tick of 8ms, a prescale of 256 and a CTC count of 250 (load 249, and since this is <=255, you can use an 8-bit timer).

Once you have your 10ms or 8ms tick set up and it's configured to generate an interrupt each tick, in your interrupt handler you can do many useful timing-related functions. Regarding your 1-second timer, you simply count to 100 (if 10ms ticks) or 125 (if 8ms ticks) and when you reach that count you do your 1-second action or set a flag so the background code can do the 1-second action, and start the tick counter back at 0.

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

jgmdesign wrote:
Quote:
Sometimes, some of us can read a data sheet ten times and not understand a single word of it until some kind sir comes along and explains it

I guess you missed this part:

Quote:
I know all the information are clearly documented in the manual.


THATS what irritated D01THL and I would not blame him. I took it the same way as the OP does not want to do his/her own homework of code it, run it, debug it.

The OP in their opening post says they usually use the internal 8mhz oscillator. I would gather that the OP has a clue to some element.

Quote:
Let's try to be helpful and not scare away recently joined users of this site.

March 30, 2011 is considered 'Just joined' ;)

OK so what do you tell somebody who's been over the timer data sheet 20 times an still doesn't "get it"?
you tell him "oh well man, your project is toast".
That's not right.
I realize how things work around here after several years, you don't just hand out info etc...
but people who spend the time to HELP people learn are awesome. Much more so than people who are here to complain and not help at all.

We aren't all college educated geniuses.
Some of us came from programming basic stamps, unfortunately. A little "nudge" here and there in the right direction helps quite a bit.

Personally,
no matter how many times I've been over the Mega8 data sheet, I still have no idea how to use a timer or even perform basic math.
I've been trying to figure it out for years.
I don't have people here helping me.
The one simple patient post by the kind person above helped tons.

Now I know how to set one up and trigger an interrupt accordingly.
If you actually helped somebody I bet you'd be appreciated also.

if (Learning_AVRs)
{
DOH();
}

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

Quote:
Now I know how to set one up and trigger an interrupt accordingly.
If you actually helped somebody I bet you'd be appreciated also.

Go through my 4500+ posts. I think there are quite a few helps in there and thank yous to go with them.

YOu also cannot have your cake and eat it too. :)

Enough of this. Before this thread goes to hell.

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

Quote:
Before this thread goes to hell.

Of for $DEITY's sake (to coin a phrase). Why are we now trying to drag this thing off topic again? If someone has something to say to help the OP program a timer then say, otherwise don't.
Quote:
Go through my 4500+ posts.

Boy that is tempting.

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

Tutorials forum has quite a few tuts. that cover all this about timers and PWM, too. That's why they're there...

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

I found this page yesterday that helped me complete my learning of some of the functions of Timer2.

http://members.shaw.ca/climber/a...

Very informative article.
funny thing is this page here on this site is what popped up when I typed in mega8 timer codevision into yahoo search engine.

I was able to have an LED blink during my main while(1). this keeps going obviously..
until I read ADC input from a potentiometer.
I told it to
if(ADCH >= 0x7F)
{
TCCR1B=0x03; ////enable timer1 at 125khz
}

What this did on my Mega8Lwas create a six second timer.

I set it to interrupt on overflow for practice.
During the interrupt here's what I did.

interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
#asm("cli") //global iterrupt disable
TCCR1B = 0x00; //disable timer/counter1
TCNT1 = 0; //clear tc1 byte
rLED = 0; //off red led
gLED = 1; //on green
pFET = 0; //turn on pfet
delay_ms(300);
pFET = 1; //turnoff pfet
gLED = 0;
//delay_ms(300); //use for brake before count
#asm("sei") // global int enable
}

Which seems to work great!

I know how to use timers now!

Now on to PWM and serial.

There may be hope for me after all!

Sorry to get so cranky with you guys but small little posts like this tend to help this AVR wannabe out quite a bit!

I really needed to understand the prescaler, which is not labeled in codevision as a prescaler.

Quote:

Tutorials forum has quite a few tuts. that cover all this about timers and PWM, too. That's why they're there...

agreed, but I've had quite a bit of trouble understanding them. For some reason this post and the link above made it easier for me to understand. 8)

Enabling and disabling the timer once it's configured was a tough one to figure out for me but somehow I managed to figure out the bit to set and how to set it with TCCR1B and the correct hex value to select the binary bits to send to TCCR1B.

For some reason when you are reading a data sheet things get very confusing and intimidating!

if (Learning_AVRs)
{
DOH();
}

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

Cakemonster, have a read of my tutorial on multitasking. It might give you some ideas on the use of a timer. As for the code you posted, there are a couple of things that could be improved. First up, what happens when the timer overflows? It goes back to 0. So setting it back to 0 in the isr is not only superfluous but affects the timing. Putting long delays in the isr also defeats the purpose of using a timer and interrupts. If you want to do general timing, consider the timer as a metronome and all timing is based on the ticks that you count off for longer intervals. In my multitasking tutorial i demonstrate using this method.
Lastly, disabling and enabling interrupts in the isr is not required in many cases. As part of the interrupt sequence, interrupts are disabled autonatically and re- enabled when you exit the isr, so for the most part, you don't need to do anything.

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

Ah yes,
The global enable/disable ints was put there because I had an error in my program. I thought this might help fix it.

I was adjusting the LEd's high when the interrupt triggered. back to low in the main loop.
This made a predictable off time but it decremented the on time of the LED each blink!
Couldn't figure it out.

Then moved everything to the ISR and it fixed the code.

Indeed, I need lots more work on cleaning things up and leaving unnecessary things out. Thanks for the help/input. I'll go check out your documents asap.

Remember when I did the shift gauge/rpm gauge?
My programming is already improved quite a bit!

Can't wait until I understand all this stuff.
I have too many ideas for AVR MCU's...

Quote:

Lastly, disabling and enabling interrupts in the isr is not required in many cases. As part of the interrupt sequence, interrupts are disabled autonatically and re- enabled when you exit the isr, so for the most part, you don't need to do anything.

...and that's the kinds of stuff I fail to catch in the Data Sheet.

AVR's are hard to understand!
Many thanks as per usual goes out to the AVR Freaks.

if (Learning_AVRs)
{
DOH();
}

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

Datasheets are a bit like the booklet that comes with your car- it tells you what everything does but it doesnt tell you how to drive the car. In comparison with other micros, the AVRs are probably the most straightforward. Their peripherals have most of what you need. I can't recall ever having to struggle trying to figure out what something does unlike, say the timers on a STM32.

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

With enough toying around with code in codevision I can get basic things done...

but not understanding it all is mostly the issue.
It's like not being able to perfectly mix the clutch and gas even though you can see the clutch and gas right there at your feet.

I just read through this...
https://www.avrfreaks.net/index.p...

and unfortunately don't understand any of it.
It doesn't say...
"hey, if you want to add something, do this!"
..in terms I can clearly understand.

No,
it says
"
| bit OR
& bit AND
~ bit NOT
^ bit EXLUSIVE OR (XOR)
<< bit LEFT SHIFT
>> bit RIGHT SHIFT
"

Which basically doesn't help me "mix the clutch and gas" very well.

#define led PORTB.0

LED = 1

is much more manageable for people like me to understand.

It's hard for me to understand why some people don't understand that this is hard for me to understand.
I guess some people just get it and others sit there spinning, mulling over the same concepts for years trying to understand.

I don't have teachers and it annoys people on this site to ask questions, so I'm left to myself to figure it all out, which is not easy for me.

Then they start adding in all this (()}{;;} stuff
and I'm like "where did all that come from?"
I didn't see that in the car manual.

It appears I'm missing several key things I need
for comprehension of these AVR chips.

Yes, I agree they are easier to learn than say my
PICKIT2 or my Cygnal 8051F300's.
But once I understand (and I don't plan to give up)I will be golden.

Like I said previously,
no matter how many times I read the data sheets and tutorials, I still have trouble understanding where half of this stuff comes from and why.
I wish it was another way.. :P

Until then guys like me are left with too many questions and on a study mission that seems hopeless..
Until some nice guy comes along and spells it all out in "human" terms.

For example:
I would have never understood that prescaler thing on my own. I still don't understand it FULLY but I am able to use it now and work with it and get it working to my advantage because of the explanations shown in this thread.

To be honest, I had no idea what a prescaler bit was or how to set it before all this. I had no idea how to use a timer two days ago and last night wrote code using a timer that works because of this humane example.

Yet I've been playing with Mega8 since 2003..
using If and For and highs and lows, Variable manipulation and code examples I can fathom.

I know I'm not alone,
otherwise everyone I know would be automating their lives.

I believe the terms "pimpin' ain't easy" fit well here.

if (Learning_AVRs)
{
DOH();
}

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

cakemonster wrote:
...and it annoys people on this site to ask questions, so I'm left to myself to figure it all out, which is not easy for me.
Hmmm...there are 100s of people on this site who are here to answer questions great and small. I don't know of ANY other site that's this active. Your complaining and FALSE characterizations about people on this site when there IS so much help here isn't appreciated, by me anyway.

You don't have ONE thread title that indicates need for help on timers, there are tuts. here that are as good as or even better/ more extensive than what Clawson did and they've been there for YEARS and you have a code generator in CV ( and of course this thread is the FIRST one to show someone about timers, of all things :roll: :roll: ) ! The link you gave has been around for years and his stuff is excellent, but you're just now finding it... Put the blame on your not getting timers on yourself, where it obviously belongs, not on the 'freaks.I'm not interested in going back and forth AT ALL, nothing to go back & forth about... but statements like that ought to be dealt with. New folks think they can say whatever they want about this site but it's the veterans ONLY who get the hand slap...lately.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

Quote:
...and it annoys people on this site to ask questions, so I'm left to myself to figure it all out, which is not easy for me.
Quote:

Hmmm...there are 100s of people on this site who are here to answer questions great and small. I don't know of ANY other site that's this active. Your complaining and FALSE characterizations about people on this site when there IS so much help here isn't appreciated, by me anyway.

This is exactly what I mean.
How many times have I read people here shove people under the rug? I didn't need to ask any questions to know you guys do this left and right.

Sorry I don't perfectly understand AVR's the first time I read through a data sheet or tutorial..
At least I usually provide examples and educated guesses when I do ask for help or add input.
Your comments don't seem to be helping anyone.

Uhm, thanks. I guess..

if (Learning_AVRs)
{
DOH();
}

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

Quote:
Your comments don't seem to be helping anyone.

Yup it's a rather sad facet of this site that people cannot seem to just stick to the question asked and simply answer it. Every thread also needs $0.05 of philosophy no one asked for.