Go Down

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

randumbhero

Does anyone know of a similar library that works with ATTINY45/85?  Anybody want to help me port this one over?

yatin

can we change mode of PWM to fast instead of phase corrected?( as we can get double resolution in fast mode)
Yatin
Hard work is only solution if you are not smart!!

DrJones

Having issues using this library, probably some annoying oversight but if anyone could help me get it to run it'd be great. Seems like the compiler is not getting the functions properly somehow..

Code: [Select]
void setup() {
#include <C:\Users\Jonas\Documents\Arduino\Fancontrol\PWM\PWM.h>

InitTimersSafe();
SetPinFrequency(1, 25000);

}

void loop() {

}


Error message:
Quote
C:\Users\Jonas\AppData\Local\Temp\ccWpBMvO.ltrans0.ltrans.o: In function `setup':

C:\Users\Jonas\Documents\Arduino\Fancontrol\main/main.ino:4: undefined reference to `InitTimersSafe()'

C:\Users\Jonas\Documents\Arduino\Fancontrol\main/main.ino:5: undefined reference to `SetPinFrequency(signed char, unsigned long)'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.

Lior_K

Hi,

I have problem to use this library. I'm using Arduino 1.8.1
I copy the PWM folder to the correct location as mentioned above
In the Library manager i get the following note: "PWM Version unknown INSTALLED"
What i did wrong?

Thx,
Lior


pert

Hi,

I have problem to use this library. I'm using Arduino 1.8.1
I copy the PWM folder to the correct location as mentioned above
In the Library manager i get the following note: "PWM Version unknown INSTALLED"
What i did wrong?

Thx,
Lior


I don't think your error has anything to do with this library. The "PWM Version unknown INSTALLED" entry you see in Library Manager just means you have the PWM library installed and it doesn't define the version. That doesn't indicate any problem and is exactly what would be expected after you install the library. The true error you are encountering is shown in the red text on the black console window at the bottom of the Arduino IDE window. I don't think that has anything to do with the PWM library. I just installed it and opened Library Manager in Arduino IDE 1.8.1 and didn't encounter that error.

When you encounter error messages you should always post them on the forum as text, using code tags (</> button on the toolbar). screenshots are almost never useful and will make it much less likely for you to get help here.

Lior_K

Hi,

Thanks for your reply. Sorry, I'm new Arduino user (-:
I assumed that it's the PWM library since the PWM function are not working while running the program
Could it be connect to the Java version on my PC?

The message that i get is:
Code: [Select]
[/
http://downloads.arduino.cc/packages/package_index.json file signature verification failed. File ignored.
java.lang.NullPointerException
java.lang.RuntimeException: java.lang.NullPointerException
at cc.arduino.contributions.libraries.ui.LibraryManagerUI.lambda$onUpdatePressed$0(LibraryManagerUI.java:212)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at cc.arduino.contributions.libraries.LibrariesIndexer.rescanLibraries(LibrariesIndexer.java:114)
at cc.arduino.contributions.libraries.LibrariesIndexer.setLibrariesFolders(LibrariesIndexer.java:106)
at processing.app.BaseNoGui.onBoardOrPortChange(BaseNoGui.java:665)
at processing.app.Base.onBoardOrPortChange(Base.java:1295)
at processing.app.Base$12.actionPerformed(Base.java:1522)
at processing.app.Base.filterVisibilityOfSubsequentBoardMenus(Base.java:1561)
at processing.app.Base.access$000(Base.java:82)
at processing.app.Base$11.actionPerformed(Base.java:1494)
at processing.app.Base.rebuildBoardsMenu(Base.java:1472)
at processing.app.Base$8.onIndexesUpdated(Base.java:1327)
at cc.arduino.contributions.libraries.ui.LibraryManagerUI.lambda$onUpdatePressed$0(LibraryManagerUI.java:210)
... 1 more
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at cc.arduino.contributions.libraries.LibrariesIndexer.rescanLibraries(LibrariesIndexer.java:114)
at cc.arduino.contributions.libraries.LibrariesIndexer.setLibrariesFolders(LibrariesIndexer.java:106)
at processing.app.BaseNoGui.onBoardOrPortChange(BaseNoGui.java:665)
at processing.app.Base.onBoardOrPortChange(Base.java:1295)
at processing.app.Base.openLibraryManager(Base.java:1346)
at processing.app.Base.lambda$rebuildImportMenu$2(Base.java:1060)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
]

Thanks,
Lior

pert

Thanks, that's much better to be able to read the full error output in text. That is quite strange. I don't think it's related to your Java version but I'm not sure of the cause. Do you get that error only when you do Sketch > Include library > Manage libraries or do you also get it when you're trying to compile or upload your sketch?

Lior_K

Hi,

it was while Sketch > Include library > Manage libraries.
the message that i got while compiling is too long to be send here but part of it is:

Code: [Select]
[/avrdude: Version 6.3, compiled on Dec 16 2016 at 13:33:19
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"

         Using Port                    : COM9
         Using Programmer              : wiring
         Overriding Baud Rate          : 115200
         AVR Part                      : ATmega2560
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    10     8    0 no       4096    8      0  9000  9000 0x00 0x00
           flash         65    10   256    0 yes    262144  256   1024  4500  4500 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : Wiring
         Description     : Wiring
         Programmer Model: AVRISP
         Hardware Version: 15
         Firmware Version Master : 2.10
         Vtarget         : 0.0 V
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: reading input file "C:\Users\KAYKOV~1.AUT\AppData\Local\Temp\arduino_build_56302/Testing_the_motor_driver_Using_Timer1_for_Generate_CLK.ino.hex"
avrdude: writing flash (4662 bytes):

Writing | ################################################## | 100% 0.78s

avrdude: 4662 bytes of flash written
avrdude: verifying flash memory against C:\Users\KAYKOV~1.AUT\AppData\Local\Temp\arduino_build_56302/Testing_the_motor_driver_Using_Timer1_for_Generate_CLK.ino.hex:
avrdude: load data flash data from input file C:\Users\KAYKOV~1.AUT\AppData\Local\Temp\arduino_build_56302/Testing_the_motor_driver_Using_Timer1_for_Generate_CLK.ino.hex:
avrdude: input file C:\Users\KAYKOV~1.AUT\AppData\Local\Temp\arduino_build_56302/Testing_the_motor_driver_Using_Timer1_for_Generate_CLK.ino.hex contains 4662 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.62s

avrdude: verifying ...
avrdude: 4662 bytes of flash verified

avrdude done.  Thank you.

http://downloads.arduino.cc/packages/package_index.json file signature verification failed. File ignored.]

pert

The output shows that the sketch has successfully compiled and uploaded. The error you're seeing:
Code: [Select]
http://downloads.arduino.cc/packages/package_index.json file signature verification failed. File ignored.]
will not cause any problems for compiling or uploading the sketch. If the code isn't working correctly that has nothing to do with that error. That error does indicate a problem with Boards Manager and possibly Library Manager this may cause you to be unable to use those features to get updatable boards/libraries notifications or install boards/libraries. Often it's possible to do those processes manually if needed so you may be able to just ignore that. I suspect it's some sort of a network issue. It's trying to verify the downloaded package index file and the verification is failing.

ghost0007

#159
Apr 06, 2017, 10:40 am Last Edit: Apr 08, 2017, 08:49 am by ghost0007
Edit:

I found out there is an example how to widen resolution and use 16 bit register fully , with pwmWriteHR ....

sorry for OT

Gojr

I'm curious what you fixed.  I've noticed that occasionally the PWM doesn't start immediately.  The PWM output will be high or low for 5-10mS before the pulses start.

I am using an Arduino Uno with this library.   Does anyone know what causes these errors or how to fix them?  Attached is an image showing the significant error periodically occurring at the beginning of the PWM signal of an output when viewed on a logic analyzer.   

fred555

Hi,

I have to make a project where PWM frequency should be around 8-9kHz on one pin and also servo library is involved. To test how PWM frequency library works with servo library I whipped together simple sketch with a few lines borrowed from examples. I used Arduino Nano.

When servo is attached to the pin 9 and SetPinFrequencySafe to pins 10 or 11, there is no PWM on these pins; servo works OK
When servo is attached to the pin 9 and SetPinFrequencySafe to pin 3, there is 9kHz PWM on pin 3, but when I turn the pot to change the duty cycle, the actual change happens with delay of 3-4 seconds, very weird indeed; servo works OK

So the question is, can PWM frequency library and servo library work reliably together and what pins should I use. I need 1 pin for PWM and 1 pin for servo.

Thank you beforehand,
Fred

Here is the code I used for testing

Code: [Select]

#include <PWM.h>
#include <Servo.h> 

int32_t frequency = 9000; //frequency (in Hz)

int servoPin = 9;
Servo servo;
int angle = 0; // servo position in degrees

void setup()
{

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

  //sets the frequency for the specified pin
  bool success = SetPinFrequencySafe(10, frequency);
 
  //if the pin frequency was set successfully, turn pin 13 on
  if(success) {
    pinMode(13, OUTPUT);
    digitalWrite(13, HIGH);   

  }

 
  servo.attach(servoPin);
 
}

void loop()
{
 
  int sensorValue = analogRead(A0);
  pwmWrite (10, sensorValue / 4);
 
  delay(30);   
 
// scan from 0 to 180 degrees
  for(angle = 0; angle < 180; angle++) 
  {                                 
    servo.write(angle);               
    delay(15);                   
  }
  // now scan back from 180 to 0 degrees
  for(angle = 180; angle > 0; angle--)   
  {                               
    servo.write(angle);           
    delay(15);       
  }

 
}

Pennypicker

Please Help! I got the code below to work. I sent the PWM signal into  Motor Driver BTS7960 43A. The motor speeds up and slows down as expected. But I want to control the DUTY CYCLE to change speed NOT the frequency. PWM Noise must not be heard so I have set the PWM frequency to 21,000hz.

Thanks




 Mimics the fade example but with an extra parameter for frequency. It should dim but with a flicker
 because the frequency has been set low enough for the human eye to detect. This flicker is easiest to see when
 the LED is moving with respect to the eye and when it is between about 20% - 60% brighness. The library
 allows for a frequency range from 1Hz - 2MHz on 16 bit timers and 31Hz - 2 MHz on 8 bit timers. When
 SetPinFrequency()/SetPinFrequencySafe() is called, a bool is returned which can be tested to verify the
 frequency was actually changed.
 
 This example runs on mega and uno.
 */

#include <PWM.h>

//use pin 11 on the Mega instead, otherwise there is a frequency cap at 31 Hz
int led = 9;                // the pin that the LED is attached to
int brightness = 0;         // how bright the LED is
int fadeAmount = 5;         // how many points to fade the LED by
int32_t frequency = 35; //frequency (in Hz)

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

  //sets the frequency for the specified pin
  bool success = SetPinFrequencySafe(led, frequency);
 
  //if the pin frequency was set successfully, turn pin 13 on
  if(success) {
    pinMode(13, OUTPUT);
    digitalWrite(13, HIGH);   
  }
}

void loop()
{
  //use this functions instead of analogWrite on 'initialized' pins
  pwmWrite(led, brightness);

  brightness = brightness + fadeAmount;

  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }     
 
  delay(30);     
}


gonn

hi,
I have read every post of every page but can not able to compile library with ide 1.8
getting, 'InitTimersSafe' was not declared in this scope
can someone run it in latest ides ?

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);

}

Go Up