PWM using TImer

In Mega 2560, I use timer 5 to control pin 45 and timer 1 to control pin 12 for the red led .

When I start the timer togher, pin 12 (timer 1 ) just bright ones and always off. On the other side, timer 5 (pin 45 ) is fine.

The tricky part is when I only using one timer, its work.

What is the problem here? Is the timer conflict to each other? Thanks for your help

#include <math.h> 
void setup() {
  // put your setup code here, to run once:
  Serial.begin (19200);
  Serial.setTimeout(40);
  pinMode(11,OUTPUT);
  pinMode(12, OUTPUT);  // Pin 12 is the PWM output of Timer 1.
  pinMode(45, OUTPUT);
  pinMode(13,OUTPUT);
  startTimer1();
  startTimer5();
}

void loop() {
  // put your main code here, to run repeatedly:
}

void startTimer1() {  // Start timer 1 for X-dircection PWM steps. 

  TIMSK1 = bit (OCIE1A); // Enable ISR TIMER1_COMPA_vect
  TCCR1A =  _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11) | _BV(WGM10);
  TCCR1B = _BV(WGM12) | _BV(WGM13)  |  _BV(CS12) | _BV(CS10); 
  OCR1A = 31250; // 
  OCR1B = 15625 ; //
}
void startTimer5(){
  TIMSK5 = bit (OCIE5A); 
  TCCR5A = _BV(COM5A1) | _BV(COM5B1) | _BV(WGM51) | _BV(WGM50);
  TCCR5B = _BV(WGM52) | _BV(WGM53)  |  _BV(CS52) | _BV(CS50); 
  OCR5A = 15625; 
  OCR5B = 7800 ; 
  }

Does that mean if you comment out the call of startTimer5() in setup() timer 1 is working correctly? As the two timers are independent that's almost impossible.

You turned on the OCR1A and OCR5A interrupts without defining an ISR for either one. I think that may be resetting your processor.

Try:

void startTimer1()    // Start timer 1 for X-dircection PWM steps.
{
  TIMSK1 = 0;
  // Enable PWM on OC1B
  // WGM 15: FastPWM, TOP in OCR1A
  // Prescale = 1024
  TCCR1A = _BV(COM1B1) | _BV(WGM11) | _BV(WGM10);
  TCCR1B = _BV(WGM12) | _BV(WGM13)  |  _BV(CS12) | _BV(CS10);
  OCR1A = 31250; // TOP (frequency = 16 MHz / 1024 / (TOP+1) = 0.499984000511984 Hz
  OCR1B = 15625 ; //
}
void startTimer5()
{
  TIMSK5 = 0;
  // Enable PWM on OC5B
  // WGM 15: FastPWM, TOP in OCR5A
  // Prescale = 1024
  TCCR5A = _BV(COM5B1) | _BV(WGM51) | _BV(WGM50);
  TCCR5B = _BV(WGM52) | _BV(WGM53)  |  _BV(CS52) | _BV(CS50);
  OCR5A = 15625; // TOP (frequency = 16 MHz / 1024 / (TOP+1) = 0.999936004095738 Hz
  OCR5B = 7800 ;
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.