Go Down

Topic: PWM frequency library (Read 220732 times) previous topic - next topic

dtr

Hi,
I'm seeing strange behavior of the pwmWriteHR() function on a Mega (not an official Arduino one). I'm not 100% what the board model is. It has an ATmega1280 processor though.

With attached code I see that pin 3 is getting the PWM set for pin 2 and pin 8 gets the output set for pin 7. Though I have to set a PWM value for 3 and 8 or no output will be given. So setting a value for a pin enables output but it gets the value set for the previous pin.

When I use analogWrite() I'm seeing normal behavior.

Arduino IDE 1.8.1 on Win10.

Is it my code or a library bug?

Code: [Select]

#include <PWM.h>

int led = 11;

void setup() {

 Serial.begin(115200);

  InitTimersSafe(); //initialize all timers except for 0, to save time keeping functions

  //SetPinFrequency(led, 15625);  //setting the frequency
  SetPinFrequency(2, 31250);
  SetPinFrequency(3, 31250);
  SetPinFrequency(5, 31250);
  SetPinFrequency(6, 31250);
  SetPinFrequency(7, 31250);
  SetPinFrequency(8, 31250);
  
  Serial.println("TIMER1");
  uint16_t frequency = Timer1_GetFrequency();
  uint16_t decimalResolution = Timer1_GetTop() + 1;
  uint16_t binaryResolution = GetPinResolution(led); //this number will be inaccurately low because the float is being truncated to a int

  char strOut[75];
  sprintf(strOut, "Frequency: %u Hz\r\n Number of Possible Duties: %u\r\n Resolution: %u bit\r\n", frequency, decimalResolution, binaryResolution );

  Serial.println(strOut);

  Serial.println("TIMER2");
   frequency = Timer2_GetFrequency();
   decimalResolution = Timer2_GetTop() + 1;
   binaryResolution = GetPinResolution(led); //this number will be inaccurately low because the float is being truncated to a int

   strOut[75];
  sprintf(strOut, "Frequency: %u Hz\r\n Number of Possible Duties: %u\r\n Resolution: %u bit\r\n", frequency, decimalResolution, binaryResolution );

  Serial.println(strOut);

  Serial.println("TIMER3");
   frequency = Timer3_GetFrequency();
   decimalResolution = Timer3_GetTop() + 1;
   binaryResolution = GetPinResolution(led); //this number will be inaccurately low because the float is being truncated to a int

   strOut[75];
  sprintf(strOut, "Frequency: %u Hz\r\n Number of Possible Duties: %u\r\n Resolution: %u bit\r\n", frequency, decimalResolution, binaryResolution );

  Serial.println(strOut);
  
  Serial.println("TIMER4");
   frequency = Timer4_GetFrequency();
   decimalResolution = Timer4_GetTop() + 1;
   binaryResolution = GetPinResolution(led); //this number will be inaccurately low because the float is being truncated to a int

   strOut[75];
  sprintf(strOut, "Frequency: %u Hz\r\n Number of Possible Duties: %u\r\n Resolution: %u bit\r\n", frequency, decimalResolution, binaryResolution );

  Serial.println(strOut);

 
}


unsigned int value = 0;
 int value2 = 0;
 int value3 = 0;
int dir = 0;


void loop() {


  pwmWriteHR(2, 32000); // ?
  pwmWriteHR(3, 0); // ?

  pwmWriteHR(7, 5000);
  pwmWriteHR(8, value);

  value = (value + 10) % 65536;
  value2 = (value2 + 5) % 65536;

//delay(1);

}

dtr

My problem posted just above is solved by texmit's fix earlier in this thread: http://forum.arduino.cc/index.php?topic=117425.msg2854538#msg2854538

That fix was posted almost a year ago. Too bad the library owner seems to have orphaned it. It's a mighty useful one.

karlinbcn

Hi.
First of all please accept my apologies if I'm not able to explain my doubts in a right way. I'm pretty new to arduino and PWM is hard to understand for me.

First:
Is it possible with this library to generate fractional frequencies? I mean 5.2Hz 34.23Hz and so on.
I ask this because I see in the examples that is always an integer, in fact defined as int32_t

Second:
I don't understand how the instructions works...
SetPinFrequencySafe(led, frequency); Set Led Pin to frequency Hz
Then if success we turn the pin ON and HIGH... At this moment is the signal in the pin?

In  pwmWrite(led, brightness); What is brightness, is it not the frequency? Is it the duty cycle?

Third:
To change the frequency in running time is SetPinFrequencySafe needed or pwmWrite can do the job?



Thanks and best regards




Go Up