Hi guys,
I want to control frequency and duty cycle by using built in PWM with ATMEGA8. I read already a lot of tutorials but confused about the mode of PWM. I need sample program or any advice on this problem. thanks in advance.
Hi guys,
I want to control frequency and duty cycle by using built in PWM with ATMEGA8. I read already a lot of tutorials but confused about the mode of PWM. I need sample program or any advice on this problem. thanks in advance.
To change the frequency, change the TOP value (you will need a mode that uses either ICRx or OCRxA as TOP). To change the frequency, change the compare value (OCRxx). Note that in modes where OCRxA is used as TOP, it can not be used as the duty cycle.
I'll send you an example that compiles on imagecraft c and runs on a mega328 and uses the serial port if you think that would help. You have any specs on the range of frequency adjust needed on the pwm, and what the minimum number of duty steps that are acceptable at the high frequencies? You can set the TOP in the ICR1A register to control the frequency, and set the OCR1A compare register to set the duty cycle, but it must always be <= to the ICR1A
Man that Canuck is Fast and Smart.
To change the frequency, change the TOP value (you will need a mode that uses either ICRx or OCRxA as TOP). To change the frequency, change the compare value (OCRxx). Note that in modes where OCRxA is used as TOP, it can not be used as the duty cycle.
ok, in this mode how i change the duty cycle.
I'll send you an example that compiles on imagecraft c and runs on a mega328 and uses the serial port if you think that would help. You have any specs on the range of frequency adjust needed on the pwm, and what the minimum number of duty steps that are acceptable at the high frequencies? You can set the TOP in the ICR1A register to control the frequency, and set the OCR1A compare register to set the duty cycle, but it must always be <= to the ICR1A
no i haven't any restrictions in frequency range. thanks in advance.
I think Me & Koshchi both recommend timer1 with any prescale, set pwm mode for TOP=ICR1 to set freq, OCR1A sets duty, must be <= ICR1.
I think Me & Koshchi both recommend timer1 with any prescale, set pwm mode for TOP=ICR1 to set freq, OCR1A sets duty, must be <= ICR1.
but ICR1 for input how i use it to generate variable output frequency and duty cycle. I'm sorry i still confused. :oops:
This is the moment that all we all start wondering if you have read the chapter on the 16 bit timer in the wonderful datasheet. I'd like to ask a question: can you edit, compile, burn and run any simple avr c program? Like make an output pin go on and off in a loop? Which avr c compiler do you use?
Is this a class assignment?
What board are you developing on?
JC
in this mode how i change the duty cycle.
but ICR1 for input how i use it to generate variable output frequency and duty cycle.
Remember "Suffragette City", circa '78, David Bowie and the Spiders From Mars, where he sang "Wham Bam Thank You Maam!"?
If you use ICR1 as input, then use an output compare ISR with software to generate pmw. There will be upper limit on the frequency, however the requirement is unknown.
This is the moment that all we all start wondering if you have read the chapter on the 16 bit timer in the wonderful datasheet. I'd like to ask a question: can you edit, compile, burn and run any simple avr c program? Like make an output pin go on and off in a loop? Which avr c compiler do you use?
you are wrong. i read the chapter of timer1 and i didn't find my answer or not understanding. i using codevision avr. i made a lot of projects before. thanks for your helping.
If you use ICR1 as input, then use an output compare ISR with software to generate pmw. There will be upper limit on the frequency, however the requirement is unknown.
i want to generate pwm with variable frequency and duty cycle on the same pin of atmega8 pin15 (OC1A). i need the mode of operation and equation of duty cycle. i read datasheet in this section i find only the equation of frequency. thanks in advance.
You have a virus bitten datasheet. Order a replacement one from the Atmel store ASAP :lol:
You have a virus bitten datasheet. Order a replacement one from the Atmel store ASAP :lol:
it is not a funny thing to add this comment. if you can post reply to help man it's OK. If you play you are not welcome. :mrgreen:
It's not a funny thing to play fool unless you're really the one. The datasheet chews up the variable frequency (and duty cycle of course) PWM in all details and even gives the code examples - do you want us to chew over the datasheet for you?
i need the mode of operation
Look in Table 39 and find that you have a choice between mode 8, 10, 12 and 14. You want a PWM mode so mode 12 is out.
For any of the remaining modes (8, 10 or 14) you control the duty cycle by varying OCR1A.
It's not a funny thing to play fool unless you're really the one. The datasheet chews up the variable frequency (and duty cycle of course) PWM in all details and even gives the code examples - do you want us to chew over the datasheet for you?
Please be respectable when you talking with men. you are not compressed to add post in my thread. you are not welcome to my thread.
Quote:
i need the mode of operation
Bob told you to "set pwm mode for TOP=ICR1". This means that the frequency is controlled by the value in ICR1 (and of course by the CPU frequency and the prescaler).Look in Table 39 and find that you have a choice between mode 8, 10, 12 and 14. You want a PWM mode so mode 12 is out.
For any of the remaining modes (8, 10 or 14) you control the duty cycle by varying OCR1A.
thank you. i understood what you say. this is a great post. thanks again. :)
Please be respectable when you talking with men.
#MAAM# wrote:I am respectable when I'm talking with men unless/until they prove the opposite :lol:Please be respectable when you talking with men.
:lol:
MBedder wrote:#MAAM# wrote:I am respectable when I'm talking with men unless/until they prove the opposite :lol:Please be respectable when you talking with men.:lol:
what is Language you understand. You are not welcome in me thread. please go go away.
Who the f.ck are you to dictate me where to go? Go read the datasheet, dude!
All forum members should be compelled to use their Real Name, Real Picture, Real City and Real Country before asking their question. If you aint got the gumption to show your face, I dont want to quess whats going on.
Who the f.ck are you to dictate me where to go? Go read the datasheet, dude!
You are proof that you are not be respectable before any more.
I will kill myself now :lol:
You both can redeem yourselves to me by adhereing to the Bob Gardner Criteria for getting AVRfreaks help.
I will kill myself now :lol:
it's not funny
Programmers are more finicky than musicians and other artists. Just suck it up and take it.
BTW I'm much more musician rather than programmer :lol:
Lets see your face and name. Are you really Michail Gorbachev?
No, he is neither a programmer nor a musician :lol:
You're a Tuff Nut to Crack. I bet you program electronic countermeasures at Mikoyan, Sukhoi, Yakolev, Tupolev, or one of the the other aircraft design bureaus I cant remember.
#MAAM#, read the tutorials about Timers / PWM and you'll fully understand, start with abcminiuser's and doctek's .
No matter who really is he, at least, he is not respectable! It looks like he is acting in an ASIC Fashion. His mission here is only restricted to reply with sarcasm, LOLs, and nasty language. Even if you are a big scientist, you don't have the right to throw crap on people and make them non-humans and curse them like you did with me and with many other members over here! Thank god, your posts are still there for reference!
Mr. Bob, I have just uploaded my picture! Perhaps you will find out what's going on! :) :)
You're a Tuff Nut to Crack. I bet you program electronic countermeasures at Mikoyan, Sukhoi, Yakolev, Tupolev, or one of the the other aircraft design bureaus I cant remember.
I find it much easier to 'talk' to someone willing to chat face to face, man to man. We all have our social customs and differences, but just for the sake of commerce and civility, we ought to be able to talk about electronics and programming without getting tangled up in religion and politics. Good to 'see' both of you new AVRfreaks. I hope we learn from each other.
I totally agree with you and thank you for your "DECENT" post!
People like you are those who make this forum a place to learn and share, and not a place to make jokes out of others.
God Bless!
we ought to be able to talk about electronics and programming without getting tangled up in religion and politics.
I bet you program electronic countermeasures at Mikoyan, Sukhoi, Yakolev, Tupolev, or one of the the other aircraft design bureaus I cant remember.
OK, OK. I only 'tease' you about your shyness because because I Like You. Like I kibbitz my friends that smoke. They hate it, but I only do it for their own good. Just don't be so hard on the new guys.
Hello #MAAM#,
First off, before I set out with frequency and duty numbers, I set the registers that control output and interrupts.
I know its been suggested to start with Timer1, but if you are new to timers and PWM, I find Timer 0 or 2 a little easier to understand for newbies. This following code should get you up and going. You'll need to change OCR2 to a value from 0 to 255 to vary the duty cycle. Your PWM signal will be output on PB3.
This "should" get you started on Timer2:
/* Setup timer2 register TCCR2 / WGM20=1, COM21=1, CS22=1 */ TCCR2 = 0x62; // Timer2=PWM Phase correct, Noninverted, prescale 64 TCNT2 = 0; // Start counter at 0 OCR2 = 0; // Duty set to 0
Thanks for any person try to help me. Sorry for confused or not fully understanding timers. Thanks for patient. I think i get the solution. I will share my project after finishing. Thank you
Hello #MAAM#,First off, before I set out with frequency and duty numbers, I set the registers that control output and interrupts.
I know its been suggested to start with Timer1, but if you are new to timers and PWM, I find Timer 0 or 2 a little easier to understand for newbies. This following code should get you up and going. You'll need to change OCR2 to a value from 0 to 255 to vary the duty cycle. Your PWM signal will be output on PB3.
This "should" get you started on Timer2:
/* Setup timer2 register TCCR2 / WGM20=1, COM21=1, CS22=1 */ TCCR2 = 0x62; // Timer2=PWM Phase correct, Noninverted, prescale 64 TCNT2 = 0; // Start counter at 0 OCR2 = 0; // Duty set to 0
thank you for your advice but timer2 is not suitable for my application.
Well, I have the same question as the OP... but since this "discussion" digressed to slightly better than crap... I'm afraid to ask. I'll try anyway...
I'm using an Atmega32U4. Timer3. I'm trying to simply set the brightness of an LED via PWM on OC3A, specifically at 400Hz. Following tutorials, I was successful using mode 5 but then you have very little control over frequency (limited to results determined by prescale). So I am attempting to change it to mode 14 and use ICR3 to control the frequency... which I've done, but now I seem to have no control over duty cycle (beyond full on and full off).
I think I'm just misunderstanding how to EXACTLY set duty cycle in mode 14.
Thank you.
- Steven
(ps. I don't post my ugly face ANYWHERE... pretend you're blind and feel your screen if it makes you like me more)
And how do you expect us to help you when you don't show us a single line of code.
I think I'm just misunderstanding how to EXACTLY set duty cycle in mode 14.
I'm afraid to ask. I'll try anyway...
Because the driver IC specifies 400Hz.
Because the driver IC specifies 400Hz.
set the OCR1A compare register to set the duty cycle, but it must always be <= to the ICR1A
OK, I got it now... it was that which must have been throwing me off. I probably wasn't keeping that bit of logic in mind.
From all the tutorials I followed, OCR1A was a value between 255 (full off) and 0 (full on). I wasn't grasping that this is ONLY because TOP is an 8-bit figure. When I change TOP to be 624 (unless i've screwed the math, that should be 400Hz with a 16Mhz clock and 64 prescaler) that I've also adjusted the resolution.
I know this is all "duh" to you that are familiar... some of us struggle.
- Steven
Quote:Because the driver IC specifies 400Hz.
One trick to get good answers is to supply pertinent information. Tell us what driver this is - preferably a link to a page where we can d/l and read the specs and/or data sheet.
We disagree there. What that would do is result in comments like "use a different IC" or "why don't you hook up a piece of peanut butter to pin 12 and smack the donkey" blah blah.
I specified I wanted 400Hz... what "pertinent information" was missing? The "why" of my 400Hz is rather irrelevant, no? theusch needed his curiosity satisfied and I obliged, but it certainly wasn't necessary for answering the question. I'm constantly perplexed with this forum how people seem to need every bit of history and reasoning leading up to a question... as though the question itself is never the proper one. I'm not knocking it (too much)... this is probably the most helpful forum I've found for this stuff... but at the same time, there's a real ego complex going on. Instead of "I don't know" or "here's the answer" its constantly "why would you want to do that?" and "do this instead". Which CAN be helpful depending on the context... but more often than not, its a result of the answerer making presumptions. This was the case here.... theusch obviously presumed I had no good reason for needing a specific frequency.
Often, presumptions lead to a LOT of blah blah and wasted time. See, now I'm having to explain my explanation.
- Steven
I specified I wanted 400Hz... what "pertinent information" was missing?
I'm trying to simply set the brightness of an LED via PWM
Seriously, if you pose your question accurately and carefully, you can get an answer immediately. Or within one or two replies.
Let's face it. You initialise TCCR3A, TCCR3B, ICR3 and the appropriate DDR bit for the OC3A pin. Then set the duty cycle with OCR3A register.
I make this 4 init statements and one programmable statement.
Try the Secret Service method and the thread goes on for weeks. It is hardly worth all the invisible ink and ray guns!
David.
I've no idea what you're going on about with your "Secret Service" talk. If you're presumption is that I have something to hide... well, that's one more presumption that's way off base. Not every question requires a complete background to answer. This one certainly didn't and neither did the OP's.
Don't you get sick of reading people's entire code and schematics and life story just to answer a "how do I" question?
Quote:
I specified I wanted 400Hz... what "pertinent information" was missing?
Because >>you<< "simply" said ...
Quote:
I'm trying to simply set the brightness of an LED via PWM
... and nothing about a driver chip interface.
Completely irrelevant. I clearly stated "specifically at 400Hz". Whether its for a driver chip interface or just because the hair on my toes really really like the hum of 400Hz... its entirely irrelevant.
Officer asks: Sir, do you know how fast you were going?
Logical person answers: [no | yes | 62 mph]
Avrfreak answers: Is your wife satisfied at home?
Which one gets the ticket and which one gets a slap up the side of the head?
:lol:
No, the experienced engineer immediately realizes the question has insufficient info to have a good engineering answer made regarding tradeoffs in price, speed, power consumption, size of package, heat dissipation in the (non)specified usage environment, etc etc. So far everyone has described using a timer register for pwm frequency. If I can get 62500 pwms a sec with 16MHz and an 8 bit timer, I could get 31250 by just setting the a compare reg to 127 instead of the full scale 255, and using another compare reg from 0 to 127 for duty cycle. You trade off bits of duty as the period gets smaller, (pwm freq higher).
OK, I got it now.
I've no idea what you're going on about with your "Secret Service" talk. If you're presumption is that I have something to hide... well, that's one more presumption that's way off base. Not every question requires a complete background to answer. This one certainly didn't and neither did the OP's.Don't you get sick of reading people's entire code and schematics and life story just to answer a "how do I" question?
Yes. Of course I do.
My point was that "5 minutes of your time composing a question wisely" gets an accurate and speedy response.
Describing a problem in clear English may often prompt you to spot the solution. It certainly helps others to give a sensible answer. Otherwise you just get speculation.
Yes. You may even get suggestions for a better approach in the first place. You will probably resent this, but a third party will often benefit.
David.
My point was that "5 minutes of your time composing a question wisely" gets an accurate and speedy response.
Describing a problem in clear English may often prompt you to spot the solution. It certainly helps others to give a sensible answer. Otherwise you just get speculation.
Yes. You may even get suggestions for a better approach in the first place. You will probably resent this, but a third party will often benefit.
Anyway... this discussion probably should have ended a long time ago :) There has to be limit to the total number of characters available on the Internet. And I'm using more than my share.
- Steven
IMO it's not about someone having to justify things. At least that is not why I asked in this thread. It is all about the evolution of threads we see here quite often:
We get the minimal Q but as answers and follow ups get in, it is evident that the person asking is on the wrong track and we've all wasted time. You could have started out "I am using LED driver so-and-so which needs a 400 Hz PWM signal. ..." and all would have been well.
Bottom line: The more info you supply in the original question the more likelihood there is of a solution to the problem. Even quote details you don't think are relevant - they may be.
Programming help by forum messages might be worse than computer support through international longdistance calls through translators. Seen those 'humorous' support transcriptions where the back and forth gets off track and hilarity ensues? Never seen a mouse. Cant tell if the computer is on or off. Never pressed the start button before. You think a couple of these guys that think they have seen EVERY gremlin that could possibly surface during the last couple of decades don't have a LOOOONG list of possible problems to check for when someone asks how to init their uart? Here's proof that there is a Devil that is allowed to mess with us: Test the product thoroughly three days before the selloff demo. Works great. Test the product thoroughly two days before the selloff. Works great. Test the product the day before the selloff. Works great. Run the demo the evening before the morning of the selloff. Demo computer wont boot. Demo wont run. Dev system with only copy of source code has a blue screen.
Never seen a mouse.
Dev system with only copy of source code
The Friendly Freak was walking his bulldog 'Mega' (image below) down the access road along the freeway in his neighborhood. A delivery truck came down the road (image below), and caught his eye as the hood ornament was the spitting image of Mega.
The truck pulled to a stop when abreast. Stenciled on the side was "PWM Delivery" along with the company slogan "You'll be amazed at our exact frequency".
"Excuse me", said the driver of the Mack. "Do you know where the palace of King Samperi is located?"
The Friendly Freak looked across the freeway and pointed to the turrets of the palace poking into the sky a short distance away. "You can see it there", he said. "You'd best ..."
The driver cut him off. "I just asked where it was; I don't need any extra chit chat." He put the truck into gear and roared off.
"Sheesh", thought the Freak. "I was just going to tell him to turn around in the big parking lot ahead and go back to the underpass behind. The underpasses ahead are closed for construction and the access road dead-ends in a small cul-de-sac".
Sure enough, some time later back came the Mack. The driver again stopped and berated our hero for not advising him of the road situation and ranted about the difficulty of turning around.
Before the truck took off angrily, Mega lifted his leg on the chromed front wheel. "Good dog" said the Friendly Freak.
[Author's note: Just as with University of Georgia mascots http://en.wikipedia.org/wiki/Uga... , 'Mega' is actually the eighth in a long line all going by the same name, so technically it is 'Mega8' for short, and 'ATMega8-16PU' on the AKC documents.
Also in the household is the fourth in a line of purse dogs, 'Tiny4'. ]
Quote:Never seen a mouse.
Well, I thought that would be "Never seen an Enter key", Bob... :wink:
That, is funny!!! :wink:
Yuk
Yuk
Yuk
Hello guys,,,,
I am following this post and sharing my issue with you all...looking for some solution....Thanks in advance.
well, I am able to generate pwm signal (20kHz) on Atmega8, while set ICR1 as TOP and OCR1A as its duty. But later I want to convert this pwm signal of 4ms period while keeping the ICR1 value same. I follow this configuration to generate 20kHz pwm signal.
TCNT1 = 0x00FF;
ICR1 = 0x00C7;
OCR1A = 100;
Please suggest the changes to achieve 4ms pwm, (keeping ICR1 same).
Thanks..
achieve 4ms pwm, (keeping ICR1 same).
Impossible with an AVR8 timer1 or equivalent. 20kHz => 250Hz is 1/80. There are no timer prescalers that are /80
As with Mr. Mack at the top of the thread you hijacked, you haven't given us any reasons for the requirement of not adjusting ICR1. But re-reading the thread from 2012, Mr. Mack taught me to not ask questions like "Why must ICR1 remain unchanged?"
Hello Mr theusch,
Thanks for your reply~
well, here is the explanation about my requirement.....
I am getting PWM(20kHz) as the output of MCU; while configured ICR1 and OCR1A, OCR1B, TCCR1A, TCCR1B, TCNT1, TIMSK, TIFR..
Now, this PWM is applied to MOTOR and motor is running. In parallel, when motor is running, we have photo interrupt is enable which is directed the motor output pulse (Period: 4ms), which is applied to external interrupt pin of MCU.
So, I am looking for the solution to generate 4ms pulse after applying PWM (50us) to Motor.
Please suggest. Thanks.
Please suggest.
1) Use another timer.
2) Count 80 times the 50us period of the 20kHz, using overflow. Realize that that apparently simple task may take up to 50% of your AVR's processor power and would be subject to jitter if other interrupt sources enabled.
I fail to see how this relates to
later I want to convert this pwm signal of 4ms period while keeping the ICR1 value same.
Hello,
Thanks for your reply~
1) I cant use another timer as I am already using OC1A/OC1B for generating PWM (forward/reverse).
When the motor is running, then only interrupt signal is generated else NO. Generated same time when motor runs. I am trying to use it as an External Interrupt.
Please suggest~
1) I cant use another timer as I am already using OC1A/OC1B for generating PWM (forward/reverse).
??? Timer0? Timer2? What are the accuracy requirements of the 4ms period?
I am not using other timer bcoz, the motor n drivers are already configured using PB0~PB4 and external interrupt pulse is at PD3.
I didnt understand the "accuracy requirement"? Please clear.
Thanks
I am not using other timer bcoz,
That makes absolutely no sense. A 4ms interrupt interval for a timer is not a big burden on the system, and in an appropriate ISR any pin could be set or cleared.
If you give a complete picture of the situation, instead of the vague question in your first post in this thread, then suggestions can be made.
What does the external interrupt pin have to do with generating a pulse every 4ms?
Re accuracy: How long is the pulse? How close must it be to exactly 4ms?