Atmega 32 PWM

Discussion in 'Homework Help' started by helloeveryone, Dec 14, 2013.

  1. helloeveryone

    Thread Starter Active Member

    Apr 8, 2011
    51
    0
    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
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,375
    494
    What is TOP value?
    What is OCR value?
     
  3. helloeveryone

    Thread Starter Active Member

    Apr 8, 2011
    51
    0
    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.
     
  4. Jony130

    AAC Fanatic!

    Feb 17, 2009
    3,957
    1,097
  5. helloeveryone

    Thread Starter Active Member

    Apr 8, 2011
    51
    0
    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?
     
  6. Jony130

    AAC Fanatic!

    Feb 17, 2009
    3,957
    1,097
    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: Dec 23, 2013
  7. donpetru

    Active Member

    Nov 14, 2008
    186
    25
    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.
     
  8. helloeveryone

    Thread Starter Active Member

    Apr 8, 2011
    51
    0
    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: Dec 23, 2013
  9. Jony130

    AAC Fanatic!

    Feb 17, 2009
    3,957
    1,097
    What is T1 and T2 ?
     
  10. helloeveryone

    Thread Starter Active Member

    Apr 8, 2011
    51
    0
    Hmm, there doesn't seem to be a T2 pin, only T0 and T1.
     
  11. Jony130

    AAC Fanatic!

    Feb 17, 2009
    3,957
    1,097
    But this pins T0 and T1 are external input for the timer0 and timer1.
     
  12. helloeveryone

    Thread Starter Active Member

    Apr 8, 2011
    51
    0
    So, they cannot be configured as output pins?
     
  13. Jony130

    AAC Fanatic!

    Feb 17, 2009
    3,957
    1,097
    Pins PB0 and PB1 can be configured as output but T0 and T1 can only work as a input.
     
  14. helloeveryone

    Thread Starter Active Member

    Apr 8, 2011
    51
    0
    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: Dec 23, 2013
  15. Jony130

    AAC Fanatic!

    Feb 17, 2009
    3,957
    1,097
    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.
     
  16. helloeveryone

    Thread Starter Active Member

    Apr 8, 2011
    51
    0
    How would I setup the registers to toggle oc1a on off every second?
     
  17. Jony130

    AAC Fanatic!

    Feb 17, 2009
    3,957
    1,097
  18. helloeveryone

    Thread Starter Active Member

    Apr 8, 2011
    51
    0
    FCPU = 8MHz
     
  19. Jony130

    AAC Fanatic!

    Feb 17, 2009
    3,957
    1,097
    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

    [​IMG]

    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: Jan 1, 2014
  20. helloeveryone

    Thread Starter Active Member

    Apr 8, 2011
    51
    0
    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?
     
Loading...