Phase Correct PWM Mode

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

Hi,

I'm trying to use phase-correct PWM (for the first time) to control a motor connected to a tiny44. No matter what I set OCR0A to, duty cycle is always hanging out around 50% but the frequency is varying. What am I doing wrong?

Thanks!

#include 

#define byte(bit) (1 << bit) // byte with bit set

#define IRLedPin PA1
#define motorPin PB2

#define setPortAPinForOutput(pin) (DDRA |= byte(pin))
#define setPortAPin(pin) (PORTA |= byte(pin))
#define clearPortAPin(pin) (PORTA &= ~(byte(pin)))

#define setPortBPinForOutput(pin) (DDRB |= byte(pin))
#define setPortBPin(pin) (PORTB |= byte(pin))
#define clearPortBPin(pin) (PORTB &= ~(byte(pin)))

int main(void)
{
	//set clock divider to /1
	CLKPR = (1 << CLKPCE);
	CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
	
	
	setPortAPinForOutput(IRLedPin);
	setPortAPin(IRLedPin);
	
	setPortBPinForOutput(motorPin);
	
	TCCR0A = _BV(COM0A0) | _BV(WGM00); //Toggle OC0A on Compare Match, Set WGM00 for Phase Correct PWM (Also must set WGM02 in TCCR0B)
	TCCR0B = _BV(WGM02) | _BV(CS00);  //Set WGM02 for Phase Correct PWM as described in previous line, no prescaler
	OCR0A = 200; //Something other than 50% Duty cycle
	
	
	
    for(;;){

    }
    return 0; 
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are never setting the duty cycle anywhere. OCR0A sets the TOP. For mode 5, the duty cycle is set with OCR0B. If you want to use OCRA as the duty cycle (with 0xFF as TOP), use mode 1.

Regards,
Steve A.

The Board helps those that help themselves.

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

Aha! I see. Thank you very much!