I want to change frequency PWM frequency of atmega32

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

I want to change atmega32 pwm frequency. Please suggest me how to change frequency of pwm signal.

Thanks in advance.

Last Edited: Thu. Nov 28, 2019 - 08:40 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Mega 32 has modes in which "TOP" (which is the period/frequency) can be varied. I think most folks would choose mode 14 which is the one where TOP (frequency) is set by the ICR register.

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

clawson wrote:
mode 14 which is the one where TOP (frequency) is set by the ICR register.

Sir, i found that datasheet but can you please give me an example of changing frequency because i am not able to do it my self.

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

Just write a new TOP value to ICR. BEWARE: this will also change the duty cycle. 

 

Now, we come to a crucial point. Often, it makes no sense to change the TOP value  too quickly. You might want to ramp the period from one value to another, for example. To do that, you need to wait an appropriate time between the changes ("appropriate" depends on the ramp rate). In any case, you should really wait at least an entire period of the counter you are changing, from one setting until you change it.

 

On the cautionary side: how are you determining that you are not able to "do it"?

 

Jim 

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

Last Edited: Fri. Nov 29, 2019 - 05:13 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


The key thing in the datasheet (I'm assuming 16 bit timer 1) is this table:

 

Any mode that does not have a fixed value in the "TOP" column is a mode in which the frequency can be varied. If you stuck to just "Fast PWM" then modes 5, 6 and 7 don't help as they have a fixed TOP (fixed frequency) set as 0x00FF, 0x01FF, 0x03FF. The two modes of interest are modes 14 and 15. The number you place in either ICR1 or OCR1A will set their frequency. If you opt for mode 15 then the frequency is set by OCR1A but the problem with this is that if OCR1A is being used to set frequency it cannot be sued to set duty cycle at the same time. So in mode 15 you can only use channel B (OCR1B) not channel A. 

 

Mode 14 is the "best" choice as it is using ICR1 to set the frequency which leave OCR1A and OCR1B free to set the duty on both channels A and B.

 

In action it might look something like:

uint16_t freq, duty;

int main(void) {
    DDRB = (1 << 5); // OC1A pin is output
    ICR1 = 12345; // Set a frequency
    OCR1A = 619; // some ininitial duty value (619 / 12345)
    // mode 14, clear OC1A on match and start timer
    TCCR1A = (1 << WGM11) | (1 << COM1A1);
    TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS10);
    while(1) {
        freq = rand();
        duty = freq / rand();
        OCR1A = duty;
        ICR1 = freq;
        _delay_ms(200);
    }
}