Atmega 32 PWM

Thread Starter

helloeveryone

Joined Apr 8, 2011
64
If we change the TOP value and leave the OCR value unchanged what will we change as far as PWM goes:

i) phase and frequency
ii) duty cycle and frequency
iii) only phase
iv) only frequency
 

Thread Starter

helloeveryone

Joined Apr 8, 2011
64
TOP and OCR values can be anything.

Top is the highest value that the Timer/Counter register reaches while counting.
OCR - Ouput on Compare register does something once the timer reaches the value in OCR if I understand correctly. I know that TOP can be equal to OCR. I don't really understand the difference between the two.
 

Thread Starter

helloeveryone

Joined Apr 8, 2011
64
In atmega32 you can change the TOP in PWM mode only for 16 bit timer/counter1.
And in data sheet you can find time diagram figure 46 on page 100 and equation for PMW frequency on page 101
http://www.atmel.com/Images/doc2503.pdf
http://www.embedds.com/8-bit-timercounter0-operation-modes-part2/
Thank you for the useful links.
I'm not really use to reading datasheets so it isn't that clear to me. Could you explain what is the default value of TOP for TCNT1 and how do we change that value, ie what register do we use to configure a different TOP? Is it the TCCR1A or register or OCR and ICR registers? I have figured out that in normal mode the default is 0xFFFF for the TOP value. Must we explicitly choose the TCNT1 mode or will Normal be the default mode?
 

Jony130

Joined Feb 17, 2009
5,488
Default value for TOP is 0xFFFF. Or you can chose one of this TOP values 0x00FF, 0x01FF, 0x03FF or the value in ICR1 also set TOP value.
And the configure register are TCCR1A in TCCR1B.
All information needed to set all this you will find in Table 47 on 109 page.
 
Last edited:

donpetru

Joined Nov 14, 2008
185
OCR value can change the duty cycle.
If OCR is used as TOP, then it can't be used for compare match (or PWM output). So, TOP value stop or start registers functionality.
 

Thread Starter

helloeveryone

Joined Apr 8, 2011
64
Depending on the mode Timer 1 is working in, it can change the state of pins?

I) T1 & T2
II) OC1A & OC1B, if they are configured as output pins
III) T1 & T2, if they are configured as output pins
IV) TOSC1 & TOSC2, if they are configured as output pins
V) none of the above

I don't think it's IV)
 
Last edited:

Thread Starter

helloeveryone

Joined Apr 8, 2011
64
Pins PB0 and PB1 can be configured as output but T0 and T1 can only work as a input.
So, the answer must be none of the above. Unless timer1 can change the state of pins TOSC1 & TOSC2 or OC1A & OC1B. I can't find if TOSC1 & TOSC2 can be set as output pins and controlled by timer1
 
Last edited:

Jony130

Joined Feb 17, 2009
5,488
I give you a tip:

1 - open Atmega32 pdf data sheet
2 - press Ctrl+F to find TOSC1
3 - And start reading this data sheet, because all information you need you will find in data sheet.
 

Jony130

Joined Feb 17, 2009
5,488
First we need to select the mode of operation. For example if we chose mode 4 (CTC) from table 47 on page page 109. So we need to set WGM12 bit in TCCR1B registers.
Next I set CS10 and CS11 to set prescaller (64) bit in TCCR1B.
Next I set COM1A0 to make toggle PD5/OC1A pin on compare match in TCCR1A registered. Next we need to determine OCR1A register to get 1Hz at OC1A output.
In datasheet we find this equation



We need to solve it for OCRnA for a given Fcpu and for prescaller N = 64 value.

So for example if you want high state for every 1 second we have to count up to
Cout = 1/(1/8MHz * 64) = 1/32us = 31250 So this give as OCR1A value 31249
 
Last edited:

Thread Starter

helloeveryone

Joined Apr 8, 2011
64
Excellent explanation. However when I work out the math I get:
1/8000000 = 0.000000125
(1/8000000) * 64 = 0.000008
1/0.000008 = 125000
Cout = 1/(1/8MHz * 64) = 1/0.000008 = 125000

who is correct?
 
Top