Pages: [1]   Go Down
Author Topic: pwm with phase correct  (Read 353 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
    {
    }
}
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 209
Posts: 13028
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

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

Why do you need to set TOP?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?)
Logged

Pages: [1]   Go Up
Jump to: