Control frequency and duty cycle by AVR ??

Go To Last Post
75 posts / 0 new

Pages

Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

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

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.

Regards,
Steve A.

The Board helps those that help themselves.

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

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

Imagecraft compiler user

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

Man that Canuck is Fast and Smart.

Imagecraft compiler user

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

Koshchi wrote:
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.

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

bobgardner wrote:
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.

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

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.

Imagecraft compiler user

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

bobgardner wrote:
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:

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

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?

Imagecraft compiler user

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

Is this a class assignment?

What board are you developing on?

JC

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

Quote:
in this mode how i change the duty cycle.
In which mode? The one with OCRxA as TOP? For that you must use OCRxB (or OCRxC if there is one).
Quote:
but ICR1 for input how i use it to generate variable output frequency and duty cycle.
So you want to use the same timer for input capture simultaneously with using it for PWM with a variable frequency? While this may be doable, it would be difficult.

Regards,
Steve A.

The Board helps those that help themselves.

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

Remember "Suffragette City", circa '78, David Bowie and the Spiders From Mars, where he sang "Wham Bam Thank You Maam!"?

Imagecraft compiler user

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

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.

It all starts with a mental vision.

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

bobgardner wrote:
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.

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

KitCarlson wrote:
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.

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

You have a virus bitten datasheet. Order a replacement one from the Atmel store ASAP :lol:

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

MBedder wrote:
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:

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

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?

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

Last Edited: Sun. May 6, 2012 - 03:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

MBedder wrote:
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.

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

JohanEkdahl wrote:
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. :)

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

#MAAM# wrote:
Please be respectable when you talking with men.
I am respectable when I'm talking with men unless/until they prove the opposite :lol:

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

MBedder wrote:
#MAAM# wrote:
Please be respectable when you talking with men.
I am respectable when I'm talking with men unless/until they prove the opposite :lol:

:lol:

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

#MAAM# wrote:
MBedder wrote:
#MAAM# wrote:
Please be respectable when you talking with men.
I am respectable when I'm talking with men unless/until they prove the opposite :lol:

:lol:

what is Language you understand. You are not welcome in me thread. please go go away.

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

Who the f.ck are you to dictate me where to go? Go read the datasheet, dude!

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

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.

Imagecraft compiler user

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

MBedder wrote:
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.

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

I will kill myself now :lol:

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

You both can redeem yourselves to me by adhereing to the Bob Gardner Criteria for getting AVRfreaks help.

Imagecraft compiler user

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

MBedder wrote:
I will kill myself now :lol:

it's not funny

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

Programmers are more finicky than musicians and other artists. Just suck it up and take it.

Imagecraft compiler user

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

BTW I'm much more musician rather than programmer :lol:

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

Lets see your face and name. Are you really Michail Gorbachev?

Imagecraft compiler user

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

No, he is neither a programmer nor a musician :lol:

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

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.

Imagecraft compiler user

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

#MAAM#, read the tutorials about Timers / PWM and you'll fully understand, start with abcminiuser's and doctek's .

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

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! :) :)

bobgardner wrote:
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.

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

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.

Imagecraft compiler user

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

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!

bobgardner wrote:
we ought to be able to talk about electronics and programming without getting tangled up in religion and politics.

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

bobgardner wrote:
I bet you program electronic countermeasures at Mikoyan, Sukhoi, Yakolev, Tupolev, or one of the the other aircraft design bureaus I cant remember.
No, I'm 120% civilian engineer and have nothing to do with any aircraft design bureaus, electronic countermeasures etc. And since I also am 100% anti-social, I do not see any reason to spread my face/events/achievments/projects/hobbies etc. over the Net.

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

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.

Imagecraft compiler user

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

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
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 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

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

javawantabe wrote:
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.

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

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)

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

And how do you expect us to help you when you don't show us a single line of code.

Regards,
Steve A.

The Board helps those that help themselves.

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

Quote:

I think I'm just misunderstanding how to EXACTLY set duty cycle in mode 14.


The code, man, the code! Ideally a simple complete test program. Tell your AVR's clock frequency. Tell how you are testing, and what the results are. Tell how you have connected your LED(s) and to what pin(s).

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Quote:

I'm afraid to ask. I'll try anyway...

;) Now, explain why half-a-dozen or so frequencies using the prescaler aren't sufficient for LED dimming? :twisted:

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Because the driver IC specifies 400Hz.

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

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.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

bobgardner wrote:
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

Pages