timer1.pwm() & analogWrite() interference

Hi to everyone,

my situation is the following:

I have to generate a total of 6 pwm outputs on an arduino uno:

-4 at high frequency
for this i use pins 5 6 3 and 11 and the function analogwrite() to generate the pwm

-2 at low frequency = 1Hz
for this i use pins 9 and 10 and the function timer1.pwm() (library timerOne.h) to generate the pwm

as i read in the forum somewhere, this board has three times, the timer1 should affect just pins 9 and 10 so i should not have interference between timers.

The problem:

if i set a value higher than 0 to the slow frequency pwms i see also the other pwms to run at 1Hz.

in the loop function i have created a procedure( adv.communication(); ) that allows to update the duty cycle of the pwms.

here is the arduino code that i use:

#include<advantech2.h>
advantech adv;


void setup() {
  Serial.begin(115200);
  Serial.setTimeout(10);
  Timer1.initialize(1000000);             //1000000 means  1Hz
  Timer1.attachInterrupt(callback);    //callback is calld every second

}   

void loop() {
  int com=adv.communication();        //allows to updare the values of the pwms D.C. stored in an array
  }
  
void callback(){ 
  adv.update_all_outputs();               // every second the analogwrite() and timer1.pwm functions are called to update the pwm  values stored in the array
}

The part of my library code in wich i update the pwms i the following:

every channel is set up using

pinMode(address[position][7],OUTPUT);

but this is done in the loop(), not in the setup()

   switch((int)address[position][6]){
      case 0:
	analogWrite((int)address[position][7],(int)(data_raw[position]*255.00/100.00));
	break;
      case 1:
	Timer1.pwm((int)address[position][7],(int)(data_raw[position]*1023.00/100.00));
	break;
    }
  • (int)address[position][6] is used to refear to the kind of pwm (slow =1 fast =0)
  • (int)address[position][7] is used to specify the pin number
  • data_raw[position] is the duty cycle value, float from 0 to 100

at the beginning every duty cycle is set at 0.
if i "turn on "( i mena i set a value of duty cycle greater than 0 only the fast pwms everything works fine, but as i "turn on" one of the slow pwms also the others start to run at the same frequency of 1Hz.

i can't understand why...
am i mistaking something?
is there anything else that i have to set?
should i make the same thing using timer 0 and timer 2 instead of the analogwrite function?

thanks for reading,
cheers

  • data_raw[position] is the duty cycle value, float from 0 to 100

I don't know the answer to your question, but I have to wonder when you don't make the duty cycle value range from 0.0 to 1.0, and save a divide-by-100.0 step.