Re: PWM frequency library

Hi runnerup
Thanks for sharing this PWM Library, I am a new Arduino user of MEGA2560, I found your library useful for my project but found some road blocks like why my analog input changed the resolution from 16 to 10 bit while using a PWM frequency of 100 Hz?, and if there is anyway of delaying a PWM signal pin from another pin output.

Any comments are Welcome.

Regards

Peter

#include <Event.h>
#include <Timer.h>

#include <PWM.h>

//use pin 11 on the mega for this example to work
int led = 11; // the pin that the LED is attached to
unsigned long TimerX;
int sensorPin = A0;
int sensorValue = 0;
unsigned long PWMX;
void setup()
{
InitTimersSafe(); //initialize all timers except for 0, to save time keeping functions
Serial.begin(115200);
Serial.println();
TimerX = millis();

demonstrateFrequencysEffectOnResolution();
settingHighResolutionDuty();
}

void demonstrateFrequencysEffectOnResolution()
{
Serial.println("As frequency increases, resolution will decrease...");
for(int i = 1; i < 100; i+=10)
{
SetPinFrequency(led, i); //setting the frequency

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("...Finished");
}

void settingHighResolutionDuty()
{
SetPinFrequency(2, 100); //setting the frequency to 100 Hz
SetPinFrequency(6, 100);

while(true)
{

sensorValue = analogRead(sensorPin);

PWMX = sensorValue*62;

pwmWriteHR(2, PWMX);
pwmWriteHR(6, PWMX);

Serial.println(sensorValue);

}

}

void loop()
{
}

I'm sorry, your questions are not clear to me. As far as analog input goes, this library does not touch it. Analog input registers at 10 bit resolution for all Arduinos, so naturally setting PWM output as a function analog input will decrease your resolution to about 10 bits.
I don't know what you mean by, "if there is anyway of delaying a PWM signal pin from another pin output?" You will have to restate the question for me to answer it.

Also, you might want to change:

PWMX = sensorValue*62;

to:

PWMX = sensorValue*64;

since 65536 / 1024 = 64.

One last thing, what is the purpose of TimerX in your code? You declare it and initialize it but never use it.

Good luck on your project.

Hi runnerup
thanks for replying, I was not aware fot he 10 bit resolution of the analog input, What I meant to say on delaying PWM outputs at different pins is that, I need to start a pulse let's say at pin # PWM2, then 2.5 mSec later start the same pulse- same frequency at pin # PWM3, delay() and millis() functions are not desired to be used. As it is now, both pulses start at the same time. I have fixed it using a digital write to a digital pin disabling interrupts before and enabling them after the pulse is generated and using micros() function but for any reason I can not get lower than 15 Hz. The TimerX variable was used for timing on previous code.

I will update the code once I tested succesfully.

Regards