Go Down

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

Jimbowat

Hi thanks for a great library i find it works well on the uno. I am varying the frequency and pulse width by reading 2 pots on the analog inputs and mapping that reading to suit the registers for frequency and pulse width. I am using pin 9 and 10 and invert 10 also control its pulse width with another pot.  Great library.

alegorosito

Hi, a simple question. Can I have 4 outputs with high resolution on a Arduino UNO?
Excellent library runnerup. Thank you!

patericko88

Anyone know why pin 11 on timer2 doesn't work?

All the rest work great.

I'm using the Uno.

Cheers

liooon03

hallo !
i'm using the arduno uno r3
if iwant to run the sevro motor (hg-kr053) and driver is Mitsubishi mr-j4-10a
how do i run this motor by arduino ?
      _________   __20K Hz
     /              \
    /                \
   /                  \
  /                    \
 rising time 0.2s and run 1s (20K Hz) down time 0.2s
and reten is the same.

i need the code!

ross4435897

#79
Nov 25, 2014, 09:26 am Last Edit: Nov 25, 2014, 09:27 am by ross4435897

saptarshi007

I used PWM library to generate PWM signals of desired frequency. The problem is i am not able to give a phase shift to the PWM. For example i am generating a PWM of frequency of 25Khz on PIN 9 and i need a phase shift of 90 degrees(i.e a phase shift of 40 microseconds) of the same PWM on PIN10. 'delay' command is not working. Please suggest a solution and thank you.   

ewholz

Thanks for this library!

luxornet

Thank you runnerup for this nice piece of work!

I have some questions for you, since I really need to expand this library for the Micro/Leonardo.
So that is what I will do, but maybe you can help me in the right direction.

  • In your estimation, can I better build of of ATimerDefs or BTimerDefs?
  • Do you have any idea how I could implement the difference in structure of the new 8 - 10 - 16 bit pins and fast PWM?
  • I want to change some of the timer--pin assignments for my project. Is that something that you think will work correctly with the construction you used for the library?
  • I will find my way in the end, but can you give me some places to start looking for the registers and stuff?


Any hint is welcome.

Many thanks in advance!
Christophe aka Petit Jean
Any luck with Arduino Leonardo?
Thank you

CrossRoads

@Talhaamjad,
Please don't crosspost.  I see this same request in Project Guidance. Duplicate is being deleted.
Moderator.
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.

wcbagley

Anyone have any ideas how to get this library to work with the 1.6.0 version of the IDE? I am having trouble getting it to recognize the library if it is in the "libraries" directory. I tried moving the files to a src directory but that did not help.

I finally gave up and put the files into the same directory as my sketch. That way I can get it to compile, but the output pin no longer has any output.

For reference, this sketch worked in the older version if the IDE. The sketch only stopped working when I upgrade to 1.6.0.

lestofante

i think is the new library system, it need to be updated
Guida per principianti http://playground.arduino.cc/Italiano/newbie
Unoffical Telegram group https://t.me/genuino

z7852424120

I want six pins pwm output.
I use your library.
The pin6 and pin11 always duty=100%.
Why they didn`t work correctly like other pin?
Here is my code,please give me an answer,thanks.


#include <PWM.h>
void setup()
{
     InitTimers();
     Serial.begin(115200);
     Serial.println();
 
   
     Timer0_SetFrequency(400000);
     Timer0_SetPrescaler(ps_8);
     
     Timer1_SetFrequency(50000);
   
     Timer2_SetFrequency(400000);
     Timer2_SetPrescaler(psalt_8);

     


}

void loop()
{
   int x=32768;
   pwmWrite(5,128);//timer0
   pwmWrite(6,128);
   
   pwmWriteHR(9,x);//timer1
   pwmWriteHR(10,x);
   
   pwmWrite(3,128);//timer2
   pwmWrite(11,128);
   Serial.println("High Resolution PWM");
}

adio7292

I am trying to get the library to work and am very new to programming. I am receiving these errors. Does anyone know how to remedy these problems?

1)Arduino\libraries\PWM/ATimerDefs.h:179:3 error: #error "ATimerDefs.h only supports ATMega640, ATMega1280, ATMega1281, ATMega2560, and ATMega2561"

2)Arduino\libraries\PWM/PWM.h:36:33: error: utility/BTimerDefs.h: No such file or directory

3)Arduino\libraries\PWM/BTimerDefs.h:56: error: 'uint16_t' does not name a type

4)Arduino\libraries\PWM/BTimerDefs.h:57: error: 'uint16_t' does not name a type

Much appreciated!

jim_beam

Thank you for the great library!

I have a beginner question, how do I invert one of the outputs e.g. on Timer1 (Pin12 non-invertet, and pinPin11 inverted) on a MEGA.

And 2nd, how do I add an lokking so that Pin 12 and 11 never be HIGH at the same time.

So far I was playing with code from, with limited Frequenzy up to 32kHz:
https://www.youtube.com/watch?v=D826h-YQun4&feature=youtu.be

My Code so far:
Code: [Select]
/*  
Phase correct inverted 2CH (HS and LS) PWM, 32kHz, 0-255 DutyCycle

http://playground.arduino.cc/Main/TimerPWMCheatsheet
https://www.youtube.com/watch?v=D826h-YQun4&feature=youtu.be
http://arduino.cc/en/Tutorial/SecretsOfArduinoPWM

lock   ns Verriegelung
-----------------
1       34        
2       95
3      162
4      225
5      284
6      342
7      411
8      470
9      535
10     600
20    1220
30    1840

FRQ: 32,377kHz

Mega:
Timer 0 - Pin 13,4  - 64kHz - NO lock! - millis()!
Timer 1 - Pin 12,11 - 32kHz
Timer 2 - Pin 10(HighSide) ,9(LowSide) - 32kHz
Timer 3 - Pin 5,3,2 - ?
Timer 4 - Pin 8,7,6 - ?

x = % / 2.55;
% = x * 2.55

*/


//--- Global Variables ---------------------
byte x = 0;                     // Aussteuerung 0-255 (0 LS OFF; 1 HS ON
const byte lock = 10;           // Verriegelung (s.o.)
int delta = -1;                 // Duty Cycle verschieben


//--- FCN Declarations ----------------------
void setInvPWM(byte x=127, byte lock=10);  


//--- Setup ---------------------------------
void setup() {  
TCCR2A = TCCR2A | 0x30;         // Set Timer 2 inverted
TCCR2B = TCCR2B & 0xF8 | 0x01;  // Set Timer 2 Prescaler FRQ
//Serial.begin(115200);         // Debug
}


//--- Main loop -----------------------------
void loop() {  

  setInvPWM(x,lock);            // Set Inverted HalfBridge PWM and lock on Mega Timer2
    
  if(x==255 || x==0){
    delay(2000);                // extra Pause
    delta = -delta;
  }  
  
  x +=delta;;                   // Tastverhältniss durchzaehlen, autorollover
  
  delay(50);
}


//--- set Inverted PWM Chanels -------------
void setInvPWM(byte x, byte lock){
  int y;                        // Helper for LS
  
  if(x<1) y = 255;              // Set LS to 0
  else if(x>(254-lock)) y = 0;  // Set LS to 1
  else y = x+lock;              // Verrieglung
  
  if(y<0) y=0;                  // limt to 0
  else if(y>255) y=255;         // limt to 255
  
  // Timer 2 on Mega Pins: 10,9
  analogWrite (10, (x));        // write PWM HS
  analogWrite (9, (y));         // write PWM LS
}


And thats what I want it to look like, but with higer frequency:


What do I need to do if I use this nice lib?
Thanks a lot!


nadeemqandeel

I got a question, I used your library to generate PWM signals on my arduino uno. I used the code you had written but added to it pin3, pin11 and pin10 along with pin9. so that all of them would generate a PWM signal of frequency 108KHz. Now the problem is that pin 11 can not generate that PWM signal while the rest of the pins can. I don't know why. I thought that pin 11 is broken but I tried another code on it to light an LED and it worked fine. I attached the code below. Thank you very much for your help and for posting that library.

#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 led2 = 10;              //added
int led3 = 11;              //added
int led4 = 3;              //added
int brightness = 0;         // how bright the LED is
int fadeAmount = 5;         // how many points to fade the LED by
int32_t frequency = 108000; //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);
  bool success2 = SetPinFrequencySafe(led2, frequency); //added
  bool success3 = SetPinFrequencySafe(led3, frequency); //added
  bool success4 = SetPinFrequencySafe(led4, frequency); //added
  //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);
 
pwmWrite(led2, brightness);

  brightness = brightness + fadeAmount;

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

pwmWrite(led3, brightness);

  brightness = brightness + fadeAmount;

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

  brightness = brightness + fadeAmount;

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

Go Up