Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
doctek
PostPosted: Mar 18, 2009 - 11:39 PM
Rookie


Joined: Jun 10, 2007
Posts: 39


Using AVR Timer/Counters: Pulse Width Modulation Modes


OK Freaks, thanks for your responses and suggestions after my plea (below). I've reposted the whole enchilada as a pdf with the code and makefile in a zip.

Please note: You have to log in if you want to be able to download the files and read the tutorial!

Does this work for everyone??


I'll wait a few days for comments and either repost (with a complete tutorial, or delete this and go to Instructables.


Last edited by doctek on Mar 27, 2009 - 05:06 PM; edited 4 times in total
 
 View user's profile Send private message  
Reply with quote Back to top
zphere
PostPosted: Mar 20, 2009 - 04:02 AM
Newbie


Joined: May 31, 2008
Posts: 1


Hi,
Thanks for the write up.

To answer your question, I would like to see a well commented, reasonably monolithic code example showing the usage of the pwm hardware.

Such an example could include the initialization of the pwm hardware, selecting prescalers (and the like) based on appropriate criteria (F_CPU being the most obvious), mode selection, output pin, duty cycle, frequency, hooking interrupts at various times in the cycle.. and so on and so forth.

ex; One large code block with many of the commonly used features represented, comments describing what each one does, and where features are mutually exclusive, only one is uncommented. The comments could also include datasheet page numbers to review for additional details.

I've always found it easier to start with something that is working, modify it, pick it apart, and learn from it while referencing a tutorial or the datasheet. The datasheet is horribly lacking a code segment pulling together and example of the usage of the hardware it is describing in the section - unless I'm just missing it. 350 pages, and very little example code.


Oh, and whatever you do, don't waste your time at Instructables. They've really gone down hill lately. So many things are turning into "Membership Benefit!"s. First we lost viewing all steps on one page, now you cannot view the other images posted for a step w/o logging in. Before long, all a person will be able to do (w/o logging in,) will be viewing the abstract of the instructable.

I'd rather not read a paragraph or two, then click on the Next button and wait for another page load just to read the next two paragraphs. Put the whole thing on one page, and remove the clutter from the rest of the page.

-Thanks.
 
 View user's profile Send private message  
Reply with quote Back to top
doctek
PostPosted: Mar 20, 2009 - 09:31 PM
Rookie


Joined: Jun 10, 2007
Posts: 39


Thanks for the excellent response, zphere. Hopefully, I'll get a few more opinions on this as well.

The sort of tutorial you're asking for is exactly what I have prepared and am trying to post. The first few parts that make up the posting so far are admittedly wordy, but provide the necessary background to understand the PWM modes. Not only do I have more detailed info on the PWM modes, but I have an example program that shows the modes in action and can be easily plagiarized for your own purposes. ("Show me a good programmer and I'll show you a good plagiarizer!")

I think you're a little harsh with Instructables. Your points are well taken, but here're a few counterpoints.

What's wrong with logging in? After all, we have to login in to Freaks to post, to see the Projects, and even to see the Figures that I included with my first post.

Yes, you have to click to see the next section, but you can always download the pdf version.

Finally, for the poster, Instructables handles uploaded code, figures, and files much better than whatever Forum Software it is that Freaks uses.

So here's what I'm thinking: I will actually post the Tutorial as several separate posts - one section (and no more than 3 figures or files each). I think this will work.

An alternative is to put the code, figures and diagrams in the Projects section. So a reader could download them, then read the Tutorial itself.

Any opinions or suggestions??

Thanks.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Mar 21, 2009 - 12:37 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 28280
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

An alternative is to put the code, figures and diagrams in the Projects section. So a reader could download them, then read the Tutorial itself.

Too complex - the best bet is to put the whole thing into a .PDF and attach it to a post here. (use CutePDF is you need a free PDF "printer")

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
doctek
PostPosted: Mar 21, 2009 - 06:00 PM
Rookie


Joined: Jun 10, 2007
Posts: 39


Sounds like a good idea, I'll try it. It'll take a few hours to get the file all put together, then I'll post it.

In the mean time, I've noticed an error in the Makefile lines to enable the Clock Output. Don't use the code in my first post - I'll correct it in the new one.

Thanks again for the suggestion.
 
 View user's profile Send private message  
Reply with quote Back to top
zbaird
PostPosted: Mar 22, 2009 - 03:42 AM
Raving lunatic


Joined: Aug 13, 2006
Posts: 4146
Location: Bellingham, WA - USA

Quote:
Don't use the code in my first post - I'll correct it in the new one.

Edit any changes into earlier posts, too, since not everyone will read through the entire thread.

_________________
Chuck Baird
What would Scooby do?
http://www.zbaird.com
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
doctek
PostPosted: Mar 22, 2009 - 04:21 AM
Rookie


Joined: Jun 10, 2007
Posts: 39


Done - Corrections are in the original posting.
 
 View user's profile Send private message  
Reply with quote Back to top
doctek
PostPosted: Mar 24, 2009 - 06:16 PM
Rookie


Joined: Jun 10, 2007
Posts: 39


All comments prior to this one were to the original (partial)post. Comments after this should be to the new pdf version and to the code that goes with it.

Hopefully, the pdf approach will be satisfactory! Thanks for your patience.
 
 View user's profile Send private message  
Reply with quote Back to top
MatrixIPN
PostPosted: Mar 29, 2009 - 07:55 PM
Newbie


Joined: Aug 30, 2008
Posts: 1


Thank's man now eat your whole enchilada and a few taquitos, thanks a lot
 
 View user's profile Send private message  
Reply with quote Back to top
simpsonss
PostPosted: Apr 21, 2009 - 08:37 AM
Rookie


Joined: Aug 11, 2008
Posts: 42


Thousand of thanks to u.great job man.
 
 View user's profile Send private message  
Reply with quote Back to top
ichionid
PostPosted: May 07, 2009 - 11:12 AM
Rookie


Joined: Aug 24, 2007
Posts: 32


Your tutorial along with deans, are the best recipe for PWM!Very HappyVery Happy
 
 View user's profile Send private message  
Reply with quote Back to top
lautman
PostPosted: May 09, 2009 - 07:17 AM
Wannabe


Joined: Apr 01, 2003
Posts: 71


Excellent tutorial (though I'm still mulling it over; my own app requires timer 0, so no frequency correct mode, making the examples a little less applicable for me). I did notice a mistake in Fig. 4 (p. 8): OC1A goes low when COUNT equals OCR1A and high when COUNT equals ICR1 (TOP). Text on p. 7 says, "When the COUNT is equal to the value in OCR1A, the output on OC1A will go high. As soon as the count reaches TOP, OC1A will be cleared." Sounds like just the opposite of what's shown. I know the output may easily be inverted, but the text should match the diagram.
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
doctek
PostPosted: May 14, 2009 - 05:30 PM
Rookie


Joined: Jun 10, 2007
Posts: 39


If you're using just timer 0, then you have just Phase Correct Mode available. Since this mode offers the main advantages of Phase & Frequency Correct Mode, you'll be just fine and the examples I give should be fully applicable. In fact, I couldn't really come up with a good demo of P&F Mode vs. Phase Correct Mode only. P&F only helps if the behavior during a frequency transition is of great importance - usually it isn't. If you use Phase Correct Mode only in the examples I provide, you'll find the behavior is identical.

As far as Fig. 4 vs. the text, you are correct on both points. The text *should indeed* match the figure (sorry about that); and the behavior can be easily inverted. (Maybe that's what I was trying to illustrate and just didn't realize it? Smile}

HTH.
 
 View user's profile Send private message  
Reply with quote Back to top
munish4u911ex
PostPosted: May 27, 2009 - 12:37 PM
Newbie


Joined: Mar 10, 2009
Posts: 5


Hi all,
seems everybody's using OC1A here... I have also tried to set pwm on OC1B of mega8 but it didn't work.
The (working) code I made for OC1A was:
Code:
void pwm_set_1A(unsigned char pulse_width)
{
DDRB |= (1<<1);         //Port B.1 as o/p (OC1A)
OCR1AL = pulse_width;   //Load Pulse width
OCR1AH = 0;
TCCR1A=((1<<COM1A1)&~(1<<COM1A0)|(1<<WGM10)&(~(1<<WGM11))); //8-bit, Non-Inverted PWM
//just a harder way to write TCCR1A = 0x81 :D
TCCR1B = 1<<CS10;             //Start PWM
}

Now, it works. Problem is with getting OC1B to work:
Code:

void pwm_set_1B(unsigned char pulse_width)
{
DDRB |= (1<<2);         //Port B.2 as o/p (OC1B)
OCR1BL = pulse_width;   //Load Pulse width
OCR1BH = 0;
TCCR1A = ((1<<COM1B1)&~(1<<COM1B0));//|(~(1<<WGM13)&(1<<WGM12))); //8-bit, Non-Inverted PWM
TCCR1B = ((1<<WGM12)&~(1<<WGM13));

TCCR1B = 1<<CS10;             //Start PWM
}
I am using these in:
Code:

while(1)
{      
   for(unsigned char i=0; i<0xff; i++)
   {pwm_set_1B(i); //pwm_set_1A(i) works here
   _delay_ms(5);
   }
   for(unsigned char i=0xff; i>0; i--)
   {pwm_set_1B(i);   
   _delay_ms(5);
   }
}
when I run it in Proteus, nothing comes on OC1B. Am I setting TCCR1A & TCCR1B the correct way??
Kind suggestions please...
-Regards,
Munish Rolling Eyes
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: May 27, 2009 - 12:38 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 28280
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

Kind suggestions please...

How do you know Proteus simulates correctly?

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
munish4u911ex
PostPosted: May 27, 2009 - 12:41 PM
Newbie


Joined: Mar 10, 2009
Posts: 5


Coz it is simulating OC1A correctly! and in hardware also I have connected 2 LEDs to both the OCs. OC1A lights (dims) its respective LED, while OC1B doesnt.
 
 View user's profile Send private message  
Reply with quote Back to top
doctek
PostPosted: May 29, 2009 - 04:16 AM
Rookie


Joined: Jun 10, 2007
Posts: 39


Looks to me like you aren't really doing the same things to make OC1B like OC1A.
- You're making TCCR1A equal to just 1<<COM1B1. The setting of (1<<WGM10)&(~(1<<WGM11) is lost.
- Setting TCCR1B to (1<<WGM12)&(~(1<<WGM13) would select CTC mode. (See the Waveform Generation Mode Bit Description Table.)
- But that doesn't even happen since you then make TCCR1B equal to 1<<CS10 and wipe out the value of the other bits in TCCR1B.

Try this:
Code:

void pwm_set_1B(unsigned char pulse_width)
{
DDRB |= (1<<2);         //Port B.2 as o/p (OC1B)
OCR1BL = pulse_width;   //Load Pulse width
OCR1BH = 0;
TCCR1A = (1<<COM1B1) |(1<<WGM10)); //8-bit, Non-Inverted PWM

TCCR1B = 1<<CS10;             //Start PWM
}


If you OR in the command to enable OC1B, you can keep OC1A running as well.
 
 View user's profile Send private message  
Reply with quote Back to top
limseckhoung
PostPosted: Sep 27, 2009 - 11:22 AM
Newbie


Joined: Sep 09, 2009
Posts: 10
Location: Penang, Malaysia

Thank you for the great+detail+ easy to understand tutorial!! Really helped me!! =)
 
 View user's profile Send private message  
Reply with quote Back to top
aghabro
PostPosted: Sep 27, 2009 - 01:08 PM
Newbie


Joined: Sep 27, 2009
Posts: 1


Dear guyz i m getting a crrupeted zip from the link geven above? wht shld i do now?
 
 View user's profile Send private message  
Reply with quote Back to top
divclonera
PostPosted: Sep 27, 2009 - 11:56 PM
Newbie


Joined: Sep 27, 2009
Posts: 1


is anyone one here writing bascom scripts for atmega48? This looks like all greek to me. I would pay someone to compile me a script.
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits