Go Down

Topic: PWM.h (Read 6772 times) previous topic - next topic

Terryjmyers

I've modified the original PWM.h from Sam Knight in order to conform with a more "install friendly" (library.properties, examples in their own folders, etc).  I've added an additional example that would have helped me greatly when utilizing this library to simply get more resolution for an LED driver. Note that I made NO functional code changes.

The library:
https://github.com/terryjmyers/PWM.git




Original Forum post:
https://forum.arduino.cc/index.php?topic=117425.0

Original Repository:
https://code.google.com/archive/p/arduino-pwm-frequency-library/downloads
https://github.com/terryjmyers

randumbhero

What's the chance you could port this to work on ATTINY45/85?

fliesskomma

Hi Terry,
hope you're still looking here.

Thanks for your PWM.h. It works nice for timer1, but there's an issue if using timer1 and timer2.

Im writing software to an Arduino output 8bit PWM on all 6 PWM-Pins, using the ...Safe() functions.
So the timer0-Pins 5 an6 should output PWM with default frequency and timer1-pins 9 an 10, as well as timer2-pins 3 and 11 should output PWM with 25kHz.

It shows, Pins 5 an 6 work as expected.
Pin9 begins PWM with a value of 2 and not at one
Pin3 begins PWM with a value of 7, or 8
an finally Pin11 stays to low from 0...127 and goes to high at 128 (2-Bit-PWM?)

Do you have any idea to fix this?

Here's a straight-forward sketch to show the issue
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <PWM.h> //https://github.com/terryjmyers/PWM

const int pwmPin0 = 5;
const int pwmPin1 = 6;
const int pwmPin2 = 9;
const int pwmPin3 = 10;
const int pwmPin4 = 3;
const int pwmPin5 = 11;
int ledPin = 13;

int32_t pwmFrequency = 25000;  //pwmFrequency (in Hz) for pins 9, 10, 3, 11

void setup() {
  InitTimersSafe(); //initialize all timers except for 0, to save time keeping functions
  Serial.begin(115200);
  delay(100);

  Serial.print("Set pin\t");
  Serial.print(pwmPin0);
  Serial.println(" as standart PWM Output\t; Timer 0");
  pinMode(pwmPin0, OUTPUT);
  Serial.print("Set pin\t");
  Serial.print(pwmPin1);
  Serial.println(" as standart PWM Output\t; Timer 0");
  pinMode(pwmPin1, OUTPUT);
  //No pinMode for PWM.h-Pins 9, 10, 3, 11

  Serial.print("Set pin\t");
  Serial.print(pwmPin2);
  Serial.print(" to PWM frequency\t");
  Serial.println(pwmFrequency);
  SetPinFrequencySafe(pwmPin2, pwmFrequency);
  Serial.print("pinResolution [Bits] = ");
  Serial.println(GetPinResolution(pwmPin2));

  Serial.print("Set pin\t");
  Serial.print(pwmPin3);
  Serial.print(" to PWM frequency\t");
  Serial.println(pwmFrequency);
  SetPinFrequencySafe(pwmPin3, pwmFrequency);
  Serial.print("pinResolution [Bits] = ");
  Serial.println(GetPinResolution(pwmPin3));

  Serial.print("Set pin\t");
  Serial.print(pwmPin4);
  Serial.print(" to PWM frequency\t");
  Serial.println(pwmFrequency);
  SetPinFrequencySafe(pwmPin4, pwmFrequency);
  Serial.print("pinResolution [Bits] = ");
  Serial.println(GetPinResolution(pwmPin4));

  Serial.print("Set pin\t");
  Serial.print(pwmPin5);
  Serial.print(" to PWM frequency\t");
  Serial.println(pwmFrequency);
  SetPinFrequencySafe(pwmPin5, pwmFrequency);
  Serial.print("pinResolution [Bits] = ");
  Serial.println(GetPinResolution(pwmPin5));

  Serial.println();
  Serial.print("Timer0, 1, 2 getFrequency(): ");
  Serial.print(Timer0_GetFrequency());
  Serial.print(", ");
  Serial.print(Timer1_GetFrequency());
  Serial.print(", ");
  Serial.println(Timer2_GetFrequency());
  Serial.print("Timer0, 1, 2 getPrescaler(): ");
  Serial.print(Timer0_GetPrescaler());
  Serial.print(", ");
  Serial.print(Timer1_GetPrescaler());
  Serial.print(", ");
  Serial.println(Timer2_GetPrescaler());
  Serial.print("Timer0, 1, 2 getResolution(): ");
  Serial.print(Timer0_GetResolution());
  Serial.print(", ");
  Serial.print(Timer1_GetResolution());
  Serial.print(", ");
  Serial.println(Timer2_GetResolution());
  Serial.print("Get pinResolution pwmPin 0...5: 5, 6, 9, 10, 3, 11: ");
  Serial.print(GetPinResolution(pwmPin0));
  Serial.print(", ");
  Serial.print(GetPinResolution(pwmPin1));
  Serial.print(", ");
  Serial.print(GetPinResolution(pwmPin2));
  Serial.print(", ");
  Serial.print(GetPinResolution(pwmPin3));
  Serial.print(", ");
  Serial.print(GetPinResolution(pwmPin4));
  Serial.print(", ");
  Serial.println(GetPinResolution(pwmPin5));
  Serial.print("Timer1, 2 GetTop()");
  Serial.print(Timer1_GetTop());
  Serial.print(", ");
  Serial.println(Timer2_GetTop());

  Serial.print("\nStart fading all PWM-outputs from 0...31, 119...135, 239...255");
}
void loop() {
  for (int i = 0; i < 31; i++) {
    Serial.println(i);
    digitalWrite(ledPin, HIGH);
    analogWrite(pwmPin0, 0);
    analogWrite(pwmPin1, 0);
    pwmWrite(pwmPin2, 0);
    pwmWrite(pwmPin3, 0);
    pwmWrite(pwmPin4, 0);
    pwmWrite(pwmPin5, 0);
    delay(50);
    digitalWrite(ledPin, LOW);
    analogWrite(pwmPin0, i);
    analogWrite(pwmPin1, i);
    pwmWrite(pwmPin2, i);
    pwmWrite(pwmPin3, i);
    pwmWrite(pwmPin4, i);
    pwmWrite(pwmPin5, i);
    delay(500);
  }

  analogWrite(pwmPin0, 0);
  analogWrite(pwmPin1, 0);
  pwmWrite(pwmPin2, 0);
  pwmWrite(pwmPin3, 0);
  pwmWrite(pwmPin4, 0);
  pwmWrite(pwmPin5, 0);
  delay(1000);

  for (int i = 119; i < 135; i++) {
    Serial.println(i);
    digitalWrite(ledPin, HIGH);
    analogWrite(pwmPin0, 0);
    analogWrite(pwmPin1, 0);
    pwmWrite(pwmPin2, 0);
    pwmWrite(pwmPin3, 0);
    pwmWrite(pwmPin4, 0);
    pwmWrite(pwmPin5, 0);
    delay(50);
    digitalWrite(ledPin, LOW);
    analogWrite(pwmPin0, i);
    analogWrite(pwmPin1, i);
    pwmWrite(pwmPin2, i);
    pwmWrite(pwmPin3, i);
    pwmWrite(pwmPin4, i);
    pwmWrite(pwmPin5, i);
    delay(500);
  }

  analogWrite(pwmPin0, 0);
  analogWrite(pwmPin1, 0);
  pwmWrite(pwmPin2, 0);
  pwmWrite(pwmPin3, 0);
  pwmWrite(pwmPin4, 0);
  pwmWrite(pwmPin5, 0);
  delay(1000);

  for (int i = 239; i < 255; i++) {
    Serial.println(i);
    digitalWrite(ledPin, HIGH);
    analogWrite(pwmPin0, 0);
    analogWrite(pwmPin1, 0);
    pwmWrite(pwmPin2, 0);
    pwmWrite(pwmPin3, 0);
    pwmWrite(pwmPin4, 0);
    pwmWrite(pwmPin5, 0);
    delay(50);
    digitalWrite(ledPin, LOW);
    analogWrite(pwmPin0, i);
    analogWrite(pwmPin1, i);
    pwmWrite(pwmPin2, i);
    pwmWrite(pwmPin3, i);
    pwmWrite(pwmPin4, i);
    pwmWrite(pwmPin5, i);
    delay(500);
  }

  analogWrite(pwmPin0, 0);
  analogWrite(pwmPin1, 0);
  pwmWrite(pwmPin2, 0);
  pwmWrite(pwmPin3, 0);
  pwmWrite(pwmPin4, 0);
  pwmWrite(pwmPin5, 0);
  delay(1000);
}

Go Up