Generate 5 pwms with single timer

Greetings,
I am using arduino Uno which has 3 timers, namely:

timer 0: 8 bit
timer 1: 16 bit
timer 2: 8 bit

I am currently using timer 1 to generate a 50Hz wave with variable duty ratio/cycle or PWM.

Is there a way to generate 5 PWMs with varying duty ratio from the same timer

Thanks!

Sure. You will have to set your timer to interrupt at some frequency. In your ISR, you will have to check an array of your 5 PWMs to see if it is time for each of them to toggle.

The Uno has 6 PWM pins, using all the hardware timers. Why must it be a single timer?

@MorganS - Well timer2 cannot be used because it has maximum prescalar value of 64 thus with 50 Hz frequency the OCR value goes beyond 255.
And using timer0 with prescalar of 256 or 1024 gives me ICR value in decimals but I am preferring to use integer values though I guess float or double would also work.

@blh64 - can you give me a sample code..

//storage variables
boolean toggle1 = 0;

void setup(){

//set pins as outputs

pinMode(13, OUTPUT);

cli();//stop interrupts

//set timer1 interrupt at 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
OCR1A = 15624;// = (1610^6) / (11024) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12 and CS10 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);

sei();//allow interrupts

}//end setup

ISR(TIMER1_COMPA_vect){//timer1 interrupt 1Hz toggles pin 13 (LED)
//generates pulse wave of frequency 1Hz/2 = 0.5kHz (takes two cycles for full wave- toggle high then toggle low)
if (toggle1){
digitalWrite(13,HIGH);
toggle1 = 0;
}
else{
digitalWrite(13,LOW);
toggle1 = 1;
}
}

This is just a simple code to generate pulse wave with frequency of 1hz and duty ratio=50%

How do I adjust it to make 3 pulse waves with duty ratio 25%, 50% and 75%

50Hz is SLOW. 1Hz is positively glacial for an Arduino. Even the slow ones can do 16 million things in one second.

I would do this with simple millis() timing but since you are well on the path to making the timer work, let's keep going with that...

What is the smallest timing interval you need? If you never need to go smaller than 25% of 50Hz then you need your timer to interrupt you at periods of 25 milliseconds. Then count to three (from zero). At zero, turn them all on. At one, turn off the first one, at two, the second and at three they are all off.

Here's some code that will probably do what you want. You can have as many pins as you need

const int pins[] = { 10, 11, 12, 13 };
const int pinCount = sizeof(pins) / sizeof(pins[0]);
const byte dutyCycles[pinCount] = {  1, 5, 25, 99 };   // expressed from 1 to 99

void setup() {

  //set pins as outputs
  for ( int i = 0; i < pinCount; ++i ) {
    pinMode( pins[i], OUTPUT );
  }

  /*
     frequency is 50Hz with 1% increments in duty cycle
     so set timer1 to interrupt at 5kHz (200 usec)
     Actual calculation left as an exercise
  */

  // TIMER 1 for interrupt frequency 5000 Hz:
  cli(); // stop interrupts
  TCCR1A = 0; // set entire TCCR1A register to 0
  TCCR1B = 0; // same for TCCR1B
  TCNT1  = 0; // initialize counter value to 0
  // set compare match register for 5000 Hz increments
  OCR1A = 3199; // = 16000000 / (1 * 5000) - 1 (must be <65536)
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // Set CS12, CS11 and CS10 bits for 1 prescaler
  TCCR1B |= (0 << CS12) | (0 << CS11) | (1 << CS10);
  // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);
  sei(); // allow interrupts
}

ISR(TIMER1_COMPA_vect) {
  //timer1 interrupt

  static byte tickCount = 0;

  for ( int i = 0; i < pinCount; ++i ) {
    if ( tickCount == dutyCycles[i] ) digitalWrite(pins[i], LOW);
    if ( tickCount == 0 ) digitalWrite(pins[i], HIGH);
  }
  tickCount = (tickCount + 1) % 100;
}

void loop() {

}