Can someone explain to me the significance of COM1A1/COM1A0?

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

I was taking notes from this post and stumbled upon a member's response : https://www.avrfreaks.net/forum/help-using-pwm-generate-40-khz-carrier-atmega644p?skey=generate%20pwm  where, he/she had listed the steps on generating a pwm of a specific frequency. I was looking at the datasheet, for CTC mode as I want to generate a PWM of a specific frequency with 50% duty cycle and was wondering how to set the COM1A1/COM1A0 bits? How do they affect the OCR1A/OCR1B ?

Amateur programmer.
Believe when I tell you that my struggle on here is real.

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

Well you didn't say which model of AVR but I guess they have similar operation in most so here, for example, is what the 328P datasheet says about them:

 

 

Is there some part of that which is not clear to you then?

 

(oh and if it's a fixed 50% duty it's not really PWM - the M in PWM suggests the duty will be varying over time)

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

Like what bits need to be set and cleared? What is the logic behind that?

For example: If I need to generate a PWM of 125kHz.... based on the member's response on that post --> ICR1  = 128, OCR1A = 50%of128 = 64, what about OCR1B ..will that be zero? I am quiet confused with the register settings. And am I restricted to a specific mode if I want to generate the same? How does one decide on a mode?

Amateur programmer.
Believe when I tell you that my struggle on here is real.

Last Edited: Fri. May 4, 2018 - 04:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Have you read the chapter(s) about timers in your AVR datasheet?

 

In very brief summary: most timers have one or more output pins they can drive when a compare event occurs. You can have it so that at the compare it simply toggles the pin state from it's previous level or you can have it so that at the compare it sets it high or clears it low. You also have it set the other way at the overflow so you can control whether the generated signal is "the right way up" or "upside down".

 

I assume you have read the whole of:

 

https://www.avrfreaks.net/forum/...

https://www.avrfreaks.net/forum/...

 

Perhaps draw a picture of the signal you wan to create? But if it's really:

 

Image result for square wave

where T = 8us (that is 125kHz) then that should be trivial to set up. Just set the bits for toggle on compare.

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

Have you read and understood this tutorial?

https://www.avrfreaks.net/forum/...

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

Why the hell did I not see this before? The datasheet was hard to follow. One more question.. to generate a 125kHz PWM, should the target frequency be 125 or 62.5khz? And when does one choose ICR1 and OCR? I see that for mode 5 CTC in 328P, the TOP value is OCR1A -- similarly mode 12, the TOP value is ICR1. Why?

Amateur programmer.
Believe when I tell you that my struggle on here is real.

Last Edited: Fri. May 4, 2018 - 06:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for sharing those links. The datasheets were hard to follow and I have issues with retaining content in my head after a read.

Amateur programmer.
Believe when I tell you that my struggle on here is real.

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

clawson, this maybe a dumb question --> can I see the PWM generated on the serial plotter (ARDUINO IDE)?

Amateur programmer.
Believe when I tell you that my struggle on here is real.

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

azimmali wrote:
I see that for mode 5 CTC in 328P, the TOP value is OCR1A -- similarly mode 12, the TOP value is ICR1. Why?

Different modes for different outcomes.....   If you need two outputs (both OCR1A & OCR1B), you would choose 12 as it uses ICR1 to set top and the two OCR regs to set each compare output,

you can not use mode 5 because it uses one of the OCR's to set top (can not be used for compare output).

 

The timers are VERY flexible, so as a result are can be complex, but once you see what they can do (or not do) you will appreciate them!

Its worth the time to understand them!

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

Last Edited: Fri. May 4, 2018 - 07:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

azimmali wrote:
can I see the PWM generated on the serial plotter (ARDUINO IDE)?

The only dumb questions are the ones not asked! 

 

The serial plotter will plot numeric data sent to it via the serial.print() function, so will not display the pwm state, it good to visualize stuff like sensor data.

On a solar controller I have, I use it display Solar panel voltage, current, power, and battery voltage and charging current (over time).

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

So, the only way I will be able to know that is hook it to a scope. I was tried measuring the duration of the pwm by storing it in a variable. On serial printing it...this is what the serial plotter gave me.

 

Amateur programmer.
Believe when I tell you that my struggle on here is real.

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

azimmali wrote:
So, the only way I will be able to know that is hook it to a scope

 

Yes or Logic Analyzer!

 

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

If you don't have a scope & feel "stuck", you can use the poor man's method (not really the best).. for 125KHz take a 1000 ohm* and 0.1uf to perhaps 1uf cap & hook up as an RC filter.  Then you can use your multimeter to read the avg voltage & figure out if the pwm is roughly set to the desired percentage.  So if you set a 10% PWM you should measure about 5Vx10% = 0.5V (roughly).   If you change the PWM (say double it) you will see a precise change in your meter reading (it will exactly double).  Of course, this doesn't measure the frequency of the PWM (you could output 111KHz & get same results).

Note, some high-end meters can measure a 125KHz signal directly without the filter.

 

* for lower freqs increase the value of these parts

 

 

 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!