Go Down

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

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




EdwardHarry

#167
Jun 07, 2017, 10:23 am Last Edit: Jun 07, 2017, 10:33 am by EdwardHarry
Dear Brothers, firstly sorry for bad english, my arduino is Arduino MEGA 2560, i want to learn how to change frequency and search the library for it, and find the google code link below :

https://code.google.com/p/arduino-pwm-frequency-library/downloads/list

and downloaded the  5th Version of it >> unzip it in the arduino.ide library on my computer, im sure that i follow this step of adding library :

https://www.arduino.cc/en/guide/libraries

btw my arduino.ide version is 1.8.2

why i cannot compile or even upload the example program ?
it said :

Arduino: 1.8.2 (Windows Store 1.8.3.0) (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\*****\Arduino\sketch_jun07a\sketch_jun07a.ino:1:17: fatal error: PWM.h: No such file or directory

 #include <PWM.h>

                 ^

compilation terminated.

exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


my teacher said that i must search the library for arduino mega instead of arduino uno (?), is that right ? is there any solution ?

ballscrewbob

Your teacher is wrong i'm afraid as both the UNO and MEGA use similar AVR architecture.

There is a good comparison link HERE

Clearly The mega has more pins due to the difference in processor but the lib should still be capable of handling that aspect.
It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

CrossRoads

More likely the library has been put in the wrong location.
Using <PWM.h> requires the files to be in the /libraries folder.
Using "PWM.h" requires the files to be in same folder as the sketch.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

oyakodon

Hi Everyone, I'm new here ;]

Just wanted to say that this is an excellent PWM library and you guys are an awesome community!

I was wondering if anyone has tried using interrupts with this library? Let's say to just count the number of positive-edge pulses, then if a threshold is reached, stop the pwm?

Perhaps something along the lines of ISR(Timer1_compa_vect)?

Thanks,
Oyakodon

hugoabreu

Hi there,

Do you guys think that's possible to make a Frequency sweep with this librarie?

I was trying to build one, but I'm not sure of what I am missing, or if it is possible indeed.

please, If you want to take a look in my code bellow:

Code: [Select]

     FPWM = lgf[lx]; // This is the frequency of the PWM that im obtainning from another function.

     Init: InitTimersSafe();
   //sets the frequency for the specified pin
     success = SetPinFrequencySafe(SPWM, FPWM);
   //initialize all timers except for 0, to save time keeping functions
   
   if (success)   //here its where I would change my PWM frequency, if success.
   {
     cdelay = 1;
     VL = 0;
     sumvL = 0;
     if (readFlag == 1)
     {
       readFlag = 0;
     }
     while (VL <= cdelay)
     {
       //use this functions instead of analogWrite on 'initialized' pins
       pwmWrite(SPWM, LPWM);  // more precisely here.
       if (readFlag == 1)
       {
         sumvL = sumvL + analogVal;
         VL++;
         readFlag = 0;
       }
     }
     Vs[lx] = sumvL / VL;
   }
   else
   goto Init; //If success is not ok I would go back to InitTimersSafe() line.
   
Thank you for the attention and sorry for bad english.

ballscrewbob

It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

hugoabreu

Hi.

This code I've put its inside the for loop. I didn't put everything couse, its too long and it doesn't matter.

My main question is if you think that we can change frequencies of PWM inside a for loop.

Ty

ballscrewbob

@hugoabreu

All code matters.
Without it nobody can tell if its good or bad or could be improved in any way or even broken into better segments etc.

And yes it should be possible to call a change inside a loop.
However you should realise that in doing so timers and interrupts could also be changed with unforeseen results unless you account for them.


It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

Prithul

Thanks a lot. This library is AWESOME.

I was able to get 100kHz PWM with 16 bit resolution!

J-M-L

I'm experiencing an issue between a UNO and MEGA - my problem is described in a new post (did not notice this thread was still active, assumed it was too old)

Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

phischmi

Hi all,
this is really a great library - very helpful :)

There's just one thing, that's bugging me. Each time, i include the lib in any of my programs, i get some warnings during the compiling.

Code: [Select]

/Users/philipp/Documents/Arduino/libraries/PWM/utility/BTimerDefs.cpp:26:0: warning: "UINT16_MAX" redefined
 #define UINT16_MAX 65535

 ^
In file included from /Applications/Arduino.app/Contents/Java/hardware/tools/avr/lib/gcc/avr/4.9.2/include/stdint.h:9:0,
                 from /Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/inttypes.h:37,
                 from /Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/avr/sfr_defs.h:126,
                 from /Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/avr/io.h:99,
                 from /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/wiring_private.h:26,
                 from /Users/philipp/Documents/Arduino/libraries/PWM/utility/BTimerDefs.cpp:23:
/Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/stdint.h:346:0: note: this is the location of the previous definition
 #define UINT16_MAX (__CONCAT(INT16_MAX, U) * 2U + 1U)
 ^
/Users/philipp/Documents/Arduino/libraries/PWM/utility/BTimerDefs.cpp:27:0: warning: "UINT8_MAX" redefined
 #define UINT8_MAX 255

 ^
In file included from /Applications/Arduino.app/Contents/Java/hardware/tools/avr/lib/gcc/avr/4.9.2/include/stdint.h:9:0,
                 from /Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/inttypes.h:37,
                 from /Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/avr/sfr_defs.h:126,
                 from /Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/avr/io.h:99,
                 from /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/wiring_private.h:26,
                 from /Users/philipp/Documents/Arduino/libraries/PWM/utility/BTimerDefs.cpp:23:
/Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/stdint.h:331:0: note: this is the location of the previous definition
 #define UINT8_MAX (INT8_MAX * 2 + 1)
 ^
Sketch uses 6508 bytes (20%) of program storage space. Maximum is 32256 bytes.
Global variables use 258 bytes (12%) of dynamic memory, leaving 1790 bytes for local variables. Maximum is 2048 bytes.


Nevertheless the code compiles and works fine.

Is someone else having the same problems and how can i solve this?


Thank you all!


Philipp

J-M-L

#178
Oct 16, 2017, 07:06 am Last Edit: Oct 16, 2017, 07:07 am by J-M-L
looks like two developers got the same name idea for a #define... the second one reinventing the wheel from stdint.h which has been here for more than 18 years and is better written....(it's a header file in the C standard library introduced in the C99 standard library section 7.18 to allow programmers to write more portable code)


you could go modify the library and put a test to see if it's already defined, something like
Code: [Select]
#ifndef UINT8_MAX
#define UINT8_MAX 255
#endif


but you could also  simply remove them (possibly #include <stdint.h> but I think is added automatically hence the compiler barking at the redefinition - to be tested)
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

phischmi

Thank you!
I just altered ATimerDefs.cpp and BTimerDefs.cpp.

Changed
Code: [Select]

#define UINT16_MAX 65535
#define UINT8_MAX 255


to
Code: [Select]

#ifndef UINT16_MAX
#define UINT16_MAX 65535
#endif
#ifndef UINT8_MAX
#define UINT8_MAX 255
#endif


in both files.

Works and the compiler is not barking anymore :)

Go Up