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