PWM

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

Could anyone kindly point out why, in my project, OCR1A will not produce any PWM whilst OCR1B works as desired. Here is the code used on an Atmega168 using Winavr.

#include
#define MCU_FREQ 1000000
int main(void)
{
DDRB=(1<<PB1);//set OC1A as output pin
DDRB=(1<<PB2);//set OC1B as output pin
TCCR1A = (1<< COM1A1)|(1<< COM1B1);
TCCR1B =(1<<WGM13) | (1<<CS10);/*set counter1 to mode 8,prescaler = 1*/
//phase and frequency correct PWM, 
//ICR1 equals top with no prescaler
ICR1 = 10000;//set ICR1 to produce 50Hz frequency
//MCU_FREQ = (1000000/(2 x n=1 x 10000)) = 50Hz n=prescaler 

	

while (1)
   {

OCR1B = 3000; /*anticlockwise; example value works as expected*/
OCR1A =3000;/*anticlockwise; no matter what value from 1000 to 3000 this register does not output PWM*/
  
  }
  
  }
  

The fact that the code is pretty simple is even more infuriating :twisted: I have swapped MCU's (44/88/168) checked with different multimeters for PWM. Checked the makefile parameters are correct, but still the same problem with no PWM output from OCR1A.

Thanks in advance for any words of wisdom :lol: .

Lachlan

What we need to learn,
we learn by doing.

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

DDRB=(1<<PB1);//set OC1A as output pin
After this line DDRB holds the value 0x02

DDRB=(1<<PB2);//set OC1B as output pin
After this line DDRB holds the value 0x04

But you want to configure PB1 and PB2 as output. So you should type:

DDRB |= (1 << PB1);
DDRB |= (1 << PB2);

Regards
Sebastian