Go Down

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

Affirmative

Hi RunnerUp,

Great Library!   Works like a charm on the Nano.   Even verified the accuracy of both the frequency and the DutyCycle.

Now the big question (could go out to everyone as well).   My script is getting bigger by the day and I still need to add a rotary pot&button User Interface.    The infamous 10lbs of stuff in a 5lb bag!!  Not enough space.   I believe all the code is transferable to the Every except the PWM.h libary.

I've done the search for PWM & Every and there are attempts  - some good, some wild.   But, none are as elegant and simple to use as yours.

Would it be possible to update (or new version) to run on the 4809 Every?   Could call it PWM-Every.h.

Thanks,
Dave

bergstar

#226
Aug 30, 2020, 11:08 pm Last Edit: Aug 30, 2020, 11:09 pm by bergstar
I can't compile this on any Arduino SAMD:

Code: [Select]
*: undefined reference to `InitTimersSafe()'
*: undefined reference to `SetPinFrequencySafe(signed char, unsigned long)'
collect2.exe: error: ld returned 1 exit status
Using library pwm at version 5.0 in folder: *\Documents\Arduino\libraries\pwm

This is the same on all SAMD boards.

Same sketch for AVR compiles without issues. 

Any suggestions?

J-M-L

#227
Aug 30, 2020, 11:12 pm Last Edit: Aug 30, 2020, 11:14 pm by J-M-L
Any suggestions?
it's meant for AVR only

may be you can read this thread for ideas (or here too)
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

ocrdu

Any suggestions?
The timer hardware on the SAMD21 is different; you need a library written/suitable for the SAMD21. I have made one, but it is not as easy to use as the one described in this thread: https://github.com/ocrdu/Arduino_SAMD21_turbo_PWM

It should be available in the Library Manager; set filter your search to "turbo".

There may be others out there.

Muplex

#229
Sep 07, 2020, 07:56 pm Last Edit: Sep 07, 2020, 08:58 pm by Muplex
Hello, I'm trying a sketch to generate waveforms on my arduino UNO r3
When I use timer 1 associated with pin 9 everything goes OK.

Same identical sketch,with pin 11 does not work at all.

const int signal_pin = 9; is OK
const int signal_pin = 11; do not work!!!

Many other users said the same issue, anyone discover the solution or the reason of these behaviuor ?


Code: [Select]
#include <PWM.h> //PWM librarey for controlling freq. of PWM signal
#include <ShiftLCD.h> //LCD with Shift-Register IC circuit

//#define LOCALDEBUG 1

ShiftLCD lcd(10, 13, 12);

const int Encoder_OuputA  = 7;
const int Encoder_OuputB  = 4;
const int Encoder_Switch = 8;

const int Interrupt_pin = 2;
const int signal_pin = 9;
const int Sine_pin = 5;

int Previous_Output;
int multiplier = 1;

volatile double angle = 0;
volatile double increment = 0.2;

int32_t frequency; //frequency to be set
int32_t lower_level_freq = 1; //Lowest possible freq value is 1Hz
int32_t upper_level_freq = 100000; //Maximum possible freq is 100KHz

void setup()
{
  //pin Mode declaration
  pinMode (Encoder_OuputA, INPUT);
  pinMode (Encoder_OuputB, INPUT);
  pinMode(Encoder_Switch, INPUT_PULLUP);

  lcd.begin(16, 2); //Initialise 16*2 LCD

  lcd.print("Signal Generator"); //Intro Message line 1
  lcd.setCursor(0, 1);
  lcd.print("- Muplex "); //Intro Message line 2
  delay(2000);
  lcd.clear();
  lcd.print("Freq:0    Hz");
  lcd.setCursor(0, 1);
  lcd.print("Inc. by: 1 ");

#ifdef LOCALDEBUG
  Serial.begin(9600);
#endif

  Timer1_Initialize();
  Timer2_Initialize();

  Previous_Output = digitalRead(Encoder_OuputA); //Read the inital value of Output A

  attachInterrupt(digitalPinToInterrupt(Interrupt_pin), generate_sine, CHANGE);
}

void loop()
{
  if (digitalRead(Encoder_OuputA) != Previous_Output)
  {
    if (digitalRead(Encoder_OuputB) != Previous_Output)
    {
      frequency = frequency - multiplier;
      // Serial.println(frequency);
      pwmWriteHR(signal_pin, 32768); //Set duty cycle to 50% by default -> for 16-bit 65536/2 = 32768
      SetPinFrequencySafe(signal_pin, frequency);
      lcd.setCursor(0, 0);
      lcd.print("Freq:     Hz");
      lcd.setCursor(5, 0);
      lcd.print(frequency);
    }
    else
    {
      frequency = frequency + multiplier;
      // Serial.println(frequency);
      pwmWriteHR(signal_pin, 32768); //Set duty cycle to 50% by default -> for 16-bit 65536/2 = 32768
      SetPinFrequencySafe(signal_pin, frequency);
      lcd.setCursor(0, 0);
      lcd.print("Freq:     Hz");
      lcd.setCursor(5, 0);
      lcd.print(frequency);
    }
    delay(10);
  }

  if (digitalRead(Encoder_Switch) == 0)
  {
    multiplier = multiplier * 10;

    if (multiplier > 1000)
      multiplier = 1;

    // Serial.println(multiplier);
    lcd.setCursor(0, 1);
    lcd.print("Cng. by:     ");
    lcd.setCursor(8, 1);
    lcd.print(multiplier);

    delay(200);
    while (digitalRead(Encoder_Switch) == 0);
  }

  Previous_Output = digitalRead(Encoder_OuputA);

}

void generate_sine()
{
  double sineValue = sin(angle);
  sineValue *= 255;
  int plot = map(sineValue, -255, +255, 0, 255);
#ifdef LOCALDEBUG
  Serial.println(plot);
#endif
  analogWrite(Sine_pin, plot);
  angle += increment;

  if (angle > 2 * PI)
    angle = 0;
}

J-M-L

Hello, I'm trying a sketch to generate waveforms on my arduino UNO r3
When I use timer 1 associated with pin 9 everything goes OK.

Same identical sketch,with pin 11 does not work at all.

const int signal_pin = 9; is OK
const int signal_pin = 11; do not work!!!

Many other users said the same issue, anyone discover the solution or the reason of these behaviuor ?

May be read this
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Muplex

#231
Sep 07, 2020, 10:23 pm Last Edit: Sep 07, 2020, 10:28 pm by Muplex
May be read this
Ok, for the next generations... =)
If I well understand is because Timer1 is the only internal clock 16 bit vs Timer0 and Timer1 who have 8 bits.

OCRxA is the OutputCompareRegister right ?

Can you explain me a little deeper the link between them ?


My goal is to have two sinusoid waves on two arduino uno pins.
SPWM due to the previous limitation don't seems the correct way. (i was working to implement  this project )
R2R DAC requires a lot of pins for a 8bit wave + 1 LCD and 1 rotary encoder = Not enought Pins....

@J-M-L do you see a way to achieve my goal without IC components or I have to study some OpAmps + Mosfet configuration ?
Maybe ArduinoMega is suitable due to the increased numer of pins and timers?


Thank you a lot, you help me to achieve these point!

jparekh68

Hi!!

I want to use this PWM library. Downloaded it. But unable to add it on my IDE. Can somebody help? I am new to Arduino.

sitas

hi, is it possible to adapt this library for servo ?

BetaCarotin

Is there a way to invert the PWM on a second pin? I am running in 16-bit mode on Pin 9 and would like to have the inverted signal on Pin 10 (thats the second pin of counter 1 isn't it?) I am running it on a nano.
There is some example about changing registers to get an inverted write- but I am not sure how/if it affects the 16bit mode and other library functions.

Go Up