Using OC1A and OC1B

Hello everyone. I am working with Pololu baby orangutan Atmega328p, I wonder if there is a way to work with 2 independent PWM with the Timer1 in both pin PB1[OC1A] and pin PB2[OC1B]. I know that setting TCCR1A, TCCR1B and DDRB make it works, but it doesn’t.

Here is my code for working with PWM on pin PB1 OC1A

void setup() 
{
  pinMode(A5,INPUT);

  pinMode(13,INPUT_PULLUP);
  pinMode(12,INPUT_PULLUP);
  pinMode(10,INPUT_PULLUP);

  //PWMs
//  pinMode(9 , OUTPUT);//9, OC1A, PB1
//  pinMode(8 , OUTPUT);//8, OC1B, PB2

  DDRB  = ((1 << PB3) | (1 << PB1) | (1 << PB2));
  DDRD  = ((1 << PD3) | (1 << PD5) | (1 << PD6));  

  TCCR1A = ((1 << COM1A1) | (0 << COM1A0) | (0 << COM1B1) | (0 << COM1B0) | (1 << WGM11) | (0 << WGM10));
  TCCR1B = ((1 << WGM13) | (1 << WGM12) | (0 << CS12) | (1 << CS11) | (1 << CS10));  


  ICR1 = (uint16_t)2499;
  OCR1A = (uint16_t)0;

  Serial.begin(115200);
}
void loop() 
{
  OCR1A = (uint16_t)outputValue;

  if(outputValue<=2499)
    outputValue++;
  else
    outputValue=0;

  Serial.println(outputValue);
    
  _delay_ms(5);
}

But when I prove it with only pin PB2 [OC1B] it does not work. Here is my code

void setup() 
{
  pinMode(A5,INPUT);

  pinMode(13,INPUT_PULLUP);
  pinMode(12,INPUT_PULLUP);
  pinMode(10,INPUT_PULLUP);

  //PWMs
//  pinMode(9 , OUTPUT);//9, OC1A, PB1
//  pinMode(8 , OUTPUT);//8, OC1B, PB2

  DDRB  = ((1 << PB3) | (1 << PB1) | (1 << PB2));
  DDRD  = ((1 << PD3) | (1 << PD5) | (1 << PD6));  

  TCCR1A = ((0 << COM1A1) | (0 << COM1A0) | (1 << COM1B1) | (0 << COM1B0) | (1 << WGM11) | (0 << WGM10));
  TCCR1B = ((1 << WGM13) | (1 << WGM12) | (0 << CS12) | (1 << CS11) | (1 << CS10));  


  ICR1 = (uint16_t)2499;
  OCR1A = (uint16_t)0;
  OCR1B = (uint16_t)0;

  Serial.begin(115200);
}
void loop() 
{
  OCR1B = (uint16_t)outputValue;

  if(outputValue<=2499)
    outputValue++;
  else
    outputValue=0;

  Serial.println(outputValue);
    
  _delay_ms(5);
}

I would thank any suggestion

Hello, juanjoc.

Your code looks okay to me, but note that unlike what your comments say, PB2/OC1B is on digital pin 10, not pin 8. You are setting pin 10 to INPUT_PULLUP, which should not be a problem because your later DDRB assignment will override that and set pin 10 to be an output, but that makes me think either you might have something else connected to pin 10 or you might be measuring the wrong pin. Could you double-check those and try measuring again?

Kevin

Yes, It works. the soldering get break and i could not get any signal. But now I can

Thanks

1 Like