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()
{
}