Go Down

Topic: pwm with phase correct (Read 459 times) previous topic - next topic


Hey guyz,
i 'm trying to write a code to drive a dc motor. I don't care about the freq but i want good resolution. So i found that phase correct is a good choice to drive dc motors and timer 2 is my choice. My question is that i cant understand how to set the TOP. I read that you can set it through OCRnA but in another article i read that 255 is the fixed value in phase correct. I am asking this because i want to find the pwm frequency. Isn't PWM_frequency = clock_speed / (2 * Prescaller_value * TOP_value ) the right equation?
here is my code:

Code: [Select]

#include <avr/io.h>

int main(void)
pinMode(3, OUTPUT);
pinMode(11, OUTPUT);

    OCR2A = 128;
    // set PWM for 50% duty cycle

    TCCR2A |= (1 << COM2A1);
    // set none-inverting mode

    TCCR2A |= (0 << WGM21) | (1 << WGM20);
    // set phase-correct PWM Mode

    TCCR2B |= (1 << CS20);
    // set no prescaler and starts PWM

    while (1);

Coding Badly

The Arduino API configures all timers except the one running millis (timer 0) for phase-correct PWM.

My question is that i cant understand how to set the TOP.

Why do you need to set TOP?


because i have to find the correct pwm frequency of this code. I don't have oscilloscope and it's my first time that i m doing this. That's why i asked what's the TOP in this code. (255 or 128?)

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131