Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

PWM question with ATMega128

PWM question with ATMega128

2007-11-30 by Philippe Habib

I'm new to using the AVR processors and I'm trying to use the built in PWM
function.

 

I have basic PWM working and I've got 2 different pwm signals going from two
independent timers.  So far so good.

 

Using the pre-scaler, I can get close to the desired PWM frequency, and by
setting the compare register appropriately, I can set the duty cycle.

 

When I use the timer as a timer, I know how to set the exact time I need by
using the timer register to some value and having it count to overflow.  Is
there some similar mechanism to allow me to adjust the PWM frequency to
exactly what I need instead of the nearest pre-scaler value?

 

Thanks.



[Non-text portions of this message have been removed]

Re: [AVR-Chat] PWM question with ATMega128

2007-11-30 by Jim Wagner

On Thu, 29 Nov 2007 16:44:33 -0800
 "Philippe Habib" <phabib@well.com> wrote:
> I'm new to using the AVR processors and I'm trying to use
> the built in PWM
> function.
> 
>  
> 
> I have basic PWM working and I've got 2 different pwm
> signals going from two
> independent timers.  So far so good.
> 
>  
> 
> Using the pre-scaler, I can get close to the desired PWM
> frequency, and by
> setting the compare register appropriately, I can set the
> duty cycle.
> 
>  
> 
> When I use the timer as a timer, I know how to set the
> exact time I need by
> using the timer register to some value and having it
> count to overflow.  Is
> there some similar mechanism to allow me to adjust the
> PWM frequency to
> exactly what I need instead of the nearest pre-scaler
> value?

With timers that have TWO compare registers, you can use
one to set the value at which it resets; you can set this
to anything from 1 to the maximum count value of the timer.

The prescaler and the sysem clock will always set the timer
interval for one count.

Example: Clock = 10MHz, prescale = 1, one count is worth
0.1us. But, if prescale = 16, one count is worth 1.6us.
This sets the TIME RESOLUTION of the counter.

Jim Wagner
Oregon Research Electronics
Tangent, OR, USA
---------------------------------------------------------------
The Think Different Store
http://www.thinkdifferentstore.com/
For All Your Mac Gear
---------------------------------------------------------------

Re: PWM question with ATMega128

2007-11-30 by Don Kinzer

--- In AVR-Chat@yahoogroups.com, "Philippe Habib" <phabib@...> wrote:
>I looked through the data sheet [...] but I wasn't
>able to figure out what I need.

There is a good application note on setting up PWM at 
http://www.zbasic.net/appnotes.php.  See AN-216.  Although the app 
note is oriented toward ZBasic, most people should be able to easily 
figure out how to code it in any other language.

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

Re: PWM question with ATMega128

2007-11-30 by Rick

--- In AVR-Chat@yahoogroups.com, "Philippe Habib" <phabib@...> wrote:
>
> Thanks for the reply.  I looked through the data sheet for guidance
about
> the various counters but I wasn't able to figure out what I need.  Maybe
> someone here can help.
> 
> I am using an ATMega 128 clocked at 16 mhz.  What I'm trying to get is a
> 10khz PWM from timer 1 and a 2 khz pwm from timer2.  I am using the
Bascom
> BASIC.
> 
> I expect that what I need to do is to use these like I use timer0 to
set up
> my 10ms timer tick. For that, I set the prescale to 1024 and by
reloading
> the timer to 99 each time my timer ISR executes, I get a 10ms tick.
> 
> Since this is a PWM and I don't have an ISR, I'm looking for the way
to set
> that reload value one time when I configure the PWM.  That's what
I've been
> unable to find.
> 
> I have experimented with trying to set various timer related things but
> haven't gotten it to work.
> 
> Here are my declarations:
> 
> Config Timer0 = Timer , Prescale = 1024
> Config Timer1 = Pwm , Pwm = 10 , Compare A Pwm = Clear Up , Prescale = 1
> Config Timer2 = Pwm , Pwm = On , Compare Pwm = Clear Up , Prescale = 8
> 
> 
> I'd appreciate any help from someone who knows the answer on this one.
> 
> Thanks.
> 

Philippe:

I believe you need to use a PWM wave generation MODE that allows you
to specify the "TOP" value.  This value is specified in the OCRnA or
ICRn register.

While I haven't used an ATmega128, I did something similar with an
ATmega8 to generate a PWM signal for RC servo control.  I used timer1
in "fast PWM" mode, set the "TOP" value in the ICR1 register, and
adjusted the duty cycle by loading different values in the OCR1A
register.  I assume something similar can be accomplished with the
ATmega128.

Rick

RE: [AVR-Chat] PWM question with ATMega128

2007-11-30 by Philippe Habib

Thanks for the reply.  I looked through the data sheet for guidance about
the various counters but I wasn't able to figure out what I need.  Maybe
someone here can help.

I am using an ATMega 128 clocked at 16 mhz.  What I'm trying to get is a
10khz PWM from timer 1 and a 2 khz pwm from timer2.  I am using the Bascom
BASIC.

I expect that what I need to do is to use these like I use timer0 to set up
my 10ms timer tick. For that, I set the prescale to 1024 and by reloading
the timer to 99 each time my timer ISR executes, I get a 10ms tick.

Since this is a PWM and I don't have an ISR, I'm looking for the way to set
that reload value one time when I configure the PWM.  That's what I've been
unable to find.

I have experimented with trying to set various timer related things but
haven't gotten it to work.

Here are my declarations:

Config Timer0 = Timer , Prescale = 1024
Config Timer1 = Pwm , Pwm = 10 , Compare A Pwm = Clear Up , Prescale = 1
Config Timer2 = Pwm , Pwm = On , Compare Pwm = Clear Up , Prescale = 8


I'd appreciate any help from someone who knows the answer on this one.

Thanks.
Show quoted textHide quoted text
-----Original Message-----
From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On Behalf
Of Jim Wagner
Sent: Thursday, November 29, 2007 10:11 PM
To: AVR-Chat@yahoogroups.com
Subject: Re: [AVR-Chat] PWM question with ATMega128

On Thu, 29 Nov 2007 16:44:33 -0800
 "Philippe Habib" <phabib@well.com> wrote:
> I'm new to using the AVR processors and I'm trying to use
> the built in PWM
> function.
> 
>  
> 
> I have basic PWM working and I've got 2 different pwm
> signals going from two
> independent timers.  So far so good.
> 
>  
> 
> Using the pre-scaler, I can get close to the desired PWM
> frequency, and by
> setting the compare register appropriately, I can set the
> duty cycle.
> 
>  
> 
> When I use the timer as a timer, I know how to set the
> exact time I need by
> using the timer register to some value and having it
> count to overflow.  Is
> there some similar mechanism to allow me to adjust the
> PWM frequency to
> exactly what I need instead of the nearest pre-scaler
> value?

With timers that have TWO compare registers, you can use
one to set the value at which it resets; you can set this
to anything from 1 to the maximum count value of the timer.

The prescaler and the sysem clock will always set the timer
interval for one count.

Example: Clock = 10MHz, prescale = 1, one count is worth
0.1us. But, if prescale = 16, one count is worth 1.6us.
This sets the TIME RESOLUTION of the counter.

Jim Wagner
Oregon Research Electronics
Tangent, OR, USA
---------------------------------------------------------------
The Think Different Store
http://www.thinkdifferentstore.com/
For All Your Mac Gear
---------------------------------------------------------------


 
Yahoo! Groups Links






-- 
No virus found in this incoming message.
Checked by AVG Free Edition. 
Version: 7.5.503 / Virus Database: 269.16.10/1159 - Release Date: 11/29/2007
11:10 AM

Re: [AVR-Chat] Re: PWM question with ATMega128

2007-11-30 by Jim Wagner

On Fri, 30 Nov 2007 22:42:04 -0000
 "Rick" <bit.tricks@gmail.com> wrote:
> --- In AVR-Chat@yahoogroups.com, "Philippe Habib"
> <phabib@...> wrote:
> >
> > Thanks for the reply.  I looked through the data sheet
> for guidance
> about
> > the various counters but I wasn't able to figure out
> what I need.  Maybe
> > someone here can help.
> > 
> > I am using an ATMega 128 clocked at 16 mhz.  What I'm
> trying to get is a
> > 10khz PWM from timer 1 and a 2 khz pwm from timer2.  I
> am using the
> Bascom
> > BASIC.
> > 
> > I expect that what I need to do is to use these like I
> use timer0 to
> set up
> > my 10ms timer tick. For that, I set the prescale to
> 1024 and by
> reloading
> > the timer to 99 each time my timer ISR executes, I get
> a 10ms tick.
> > 
> > Since this is a PWM and I don't have an ISR, I'm
> looking for the way
> to set
> > that reload value one time when I configure the PWM.
>  That's what
> I've been
> > unable to find.
> > 
> > I have experimented with trying to set various timer
> related things but
> > haven't gotten it to work.
> > 
> > Here are my declarations:
> > 
> > Config Timer0 = Timer , Prescale = 1024
> > Config Timer1 = Pwm , Pwm = 10 , Compare A Pwm = Clear
> Up , Prescale = 1
> > Config Timer2 = Pwm , Pwm = On , Compare Pwm = Clear Up
> , Prescale = 8
> > 
> > 
> > I'd appreciate any help from someone who knows the
> answer on this one.
> > 
> > Thanks.
> > 
> 
> Philippe:
> 
I think there is more than a little confusion here.

When you say "PWM", are you talking abou a PWM signal that
is generted by internal hardware? Or, are you talking about
a software PWM.

This quesion arises because you say that you are using the
timer to generate "10ms tics" and you also talk abou a
10KHz PWM.

Normally, the PWM frequency is the repetition frequency of
the entire PWM waveform. Otherwise, one would talk about
"PWM clock". Yet, I think of "ticks" as the clock applied
as the input of a PWM. So, if the PWM is generating
"ticks", I would think of those as clocks to a software
PWM. 

Further, a 10MHz clock and 1024 prescale will give that
counter a clock of 9.765... KHz. There is no way that you
could get a 10KHz PWM out of that setup.

Thus, we need to make sure what it is that you are talking
about.

Jim Wagner
Oregon Research Electronics
Tangent, OR, USA
---------------------------------------------------------------
The Think Different Store
http://www.thinkdifferentstore.com/
For All Your Mac Gear
---------------------------------------------------------------

Re: [AVR-Chat] Re: PWM question with ATMega128

2007-11-30 by Jim Wagner

On Fri, 30 Nov 2007 18:25:13 -0500
 "Jim Wagner" <jim_d_wagner@applelinks.net> wrote:
> On Fri, 30 Nov 2007 22:42:04 -0000
>  "Rick" <bit.tricks@gmail.com> wrote:
> > --- In AVR-Chat@yahoogroups.com, "Philippe Habib"
> > <phabib@...> wrote:
> > >
> > > Thanks for the reply.  I looked through the data
> sheet
> > for guidance
> > about
> > > the various counters but I wasn't able to figure out
> > what I need.  Maybe
> > > someone here can help.
> > > 
> > > I am using an ATMega 128 clocked at 16 mhz.  What I'm
> > trying to get is a
> > > 10khz PWM from timer 1 and a 2 khz pwm from timer2.
>  I
> > am using the
> > Bascom
> > > BASIC.
> > > 
> > > I expect that what I need to do is to use these like
> I
> > use timer0 to
> > set up
> > > my 10ms timer tick. For that, I set the prescale to
> > 1024 and by
> > reloading
> > > the timer to 99 each time my timer ISR executes, I
> get
> > a 10ms tick.
> > > 
> > > Since this is a PWM and I don't have an ISR, I'm
> > looking for the way
> > to set
> > > that reload value one time when I configure the PWM.
> >  That's what
> > I've been
> > > unable to find.
> > > 
> > > I have experimented with trying to set various timer
> > related things but
> > > haven't gotten it to work.
> > > 
> > > Here are my declarations:
> > > 
> > > Config Timer0 = Timer , Prescale = 1024
> > > Config Timer1 = Pwm , Pwm = 10 , Compare A Pwm =
> Clear
> > Up , Prescale = 1
> > > Config Timer2 = Pwm , Pwm = On , Compare Pwm = Clear
> Up
> > , Prescale = 8
> > > 
> > > 
> > > I'd appreciate any help from someone who knows the
> > answer on this one.
> > > 
> > > Thanks.
> > > 
> > 
> > Philippe:
> > 
> I think there is more than a little confusion here.
> 
> When you say "PWM", are you talking abou a PWM signal
> that
> is generted by internal hardware? Or, are you talking
> about
> a software PWM.
> 
> This quesion arises because you say that you are using
> the
> timer to generate "10ms tics" and you also talk abou a
> 10KHz PWM.
> 
> Normally, the PWM frequency is the repetition frequency
> of
> the entire PWM waveform. Otherwise, one would talk about
> "PWM clock". Yet, I think of "ticks" as the clock applied
> as the input of a PWM. So, if the PWM is generating
> "ticks", I would think of those as clocks to a software
> PWM. 
> 
> Further, a 10MHz clock and 1024 prescale will give that
> counter a clock of 9.765... KHz. There is no way that you
> could get a 10KHz PWM out of that setup.
> 
> Thus, we need to make sure what it is that you are
> talking
> about.
> 
> Jim Wagner
> Oregon Research Electronics
> Tangent, OR, USA

Ooops,. clock is 16MHz. So 1024 prescale gives 15.625KHz
clock ticks for that counter. Still, no way you can get a
10KHz PWM from that.

Jim
---------------------------------------------------------------
The Think Different Store
http://www.thinkdifferentstore.com/
For All Your Mac Gear
---------------------------------------------------------------

Re: [AVR-Chat] Re: PWM question with ATMega128

2007-11-30 by Dave Hylands

Hi guys,

> Ooops,. clock is 16MHz. So 1024 prescale gives 15.625KHz
> clock ticks for that counter. Still, no way you can get a
> 10KHz PWM from that.

Use WGM mode 14 which uses the ICR register to set the top.

If we start with a prescalar of 1, we want each cycle of our PWM
output to last 1600 clock cycles (@16 MHz), so you could set ICR to
1600, and vary OCR between 0 and 1600 to control the duty cycle.

Picking a prescalar of 8, and setting the ICR to 200 will give the same effect.

-- 
Dave Hylands
Vancouver, BC, Canada
http://www.DaveHylands.com/

Re: [AVR-Chat] Re: PWM question with ATMega128

2007-12-01 by Philippe Habib

I am using timer 0 for a clock tick.

I am trying to use timer1 for a 2 khz PWM signal generated by the  
internal hardware.

I am trying to use timer2 for a 10 khz PWM signal generated by the  
internal hardware.

I realized the timer tick has nothing to do with the PWM, I just  
copied here because its one of the timers I'm using.
Show quoted textHide quoted text
On Nov 30, 2007, at 3:25 PM, Jim Wagner wrote:

> On Fri, 30 Nov 2007 22:42:04 -0000
>  "Rick" <bit.tricks@gmail.com> wrote:
>> --- In AVR-Chat@yahoogroups.com, "Philippe Habib"
>> <phabib@...> wrote:
>>>
>>> Thanks for the reply.  I looked through the data sheet
>> for guidance
>> about
>>> the various counters but I wasn't able to figure out
>> what I need.  Maybe
>>> someone here can help.
>>>
>>> I am using an ATMega 128 clocked at 16 mhz.  What I'm
>> trying to get is a
>>> 10khz PWM from timer 1 and a 2 khz pwm from timer2.  I
>> am using the
>> Bascom
>>> BASIC.
>>>
>>> I expect that what I need to do is to use these like I
>> use timer0 to
>> set up
>>> my 10ms timer tick. For that, I set the prescale to
>> 1024 and by
>> reloading
>>> the timer to 99 each time my timer ISR executes, I get
>> a 10ms tick.
>>>
>>> Since this is a PWM and I don't have an ISR, I'm
>> looking for the way
>> to set
>>> that reload value one time when I configure the PWM.
>>  That's what
>> I've been
>>> unable to find.
>>>
>>> I have experimented with trying to set various timer
>> related things but
>>> haven't gotten it to work.
>>>
>>> Here are my declarations:
>>>
>>> Config Timer0 = Timer , Prescale = 1024
>>> Config Timer1 = Pwm , Pwm = 10 , Compare A Pwm = Clear
>> Up , Prescale = 1
>>> Config Timer2 = Pwm , Pwm = On , Compare Pwm = Clear Up
>> , Prescale = 8
>>>
>>>
>>> I'd appreciate any help from someone who knows the
>> answer on this one.
>>>
>>> Thanks.
>>>
>>
>> Philippe:
>>
> I think there is more than a little confusion here.
>
> When you say "PWM", are you talking abou a PWM signal that
> is generted by internal hardware? Or, are you talking about
> a software PWM.
>
> This quesion arises because you say that you are using the
> timer to generate "10ms tics" and you also talk abou a
> 10KHz PWM.
>
> Normally, the PWM frequency is the repetition frequency of
> the entire PWM waveform. Otherwise, one would talk about
> "PWM clock". Yet, I think of "ticks" as the clock applied
> as the input of a PWM. So, if the PWM is generating
> "ticks", I would think of those as clocks to a software
> PWM.
>
> Further, a 10MHz clock and 1024 prescale will give that
> counter a clock of 9.765... KHz. There is no way that you
> could get a 10KHz PWM out of that setup.
>
> Thus, we need to make sure what it is that you are talking
> about.
>
> Jim Wagner
> Oregon Research Electronics
> Tangent, OR, USA
> ---------------------------------------------------------------
> The Think Different Store
> http://www.thinkdifferentstore.com/
> For All Your Mac Gear
> ---------------------------------------------------------------
>
>
>
> Yahoo! Groups Links
>
>
>
>

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.