Hello, i have the problem with using this library: GitHub - khoih-prog/AVR_PWM: This library enables you to use Hardware-based PWM channels on AVR-based boards, such as Nano, UNO, Mega, Leonardo, 32u4, etc., to create and output PWM. Using the same functions as other FastPWM libraries to enable you to port PWM code easily between platforms.
I need to generate a variable frequency in steps (in this example, the PWM frequency is variable from 100 kHz to 128 kHz)
#define _PWM_LOGLEVEL_ 4
#include "AVR_PWM.h"
#define pinToUse 10 // Timer1B on UNO, Nano, etc
#endif
AVR_PWM* PWM_Instance;
float frequency;
char dashLine[] = "=====================================================================================";
void printPWMInfo(AVR_PWM* PWM_Instance)
{
Serial.println(dashLine);
Serial.print("Actual data: pin = ");
Serial.print(PWM_Instance->getPin());
Serial.print(", PWM DC = ");
Serial.print(PWM_Instance->getActualDutyCycle());
Serial.print(", PWMPeriod = ");
Serial.print(PWM_Instance->getPWMPeriod());
Serial.print(", PWM Freq (Hz) = ");
Serial.println(PWM_Instance->getActualFreq(), 4);
Serial.println(dashLine);
}
void setup()
{
Serial.begin(115200);
while (!Serial);
delay(100);
Serial.print(F("\nStarting PWM_DynamicFreq on "));
Serial.println(BOARD_NAME);
Serial.println(AVR_PWM_VERSION);
frequency = 100000.0f;
PWM_Instance = new AVR_PWM(pinToUse, frequency, 50.0f);
if (PWM_Instance)
{
PWM_Instance->setPWM();
}
Serial.println(dashLine);
}
void loop()
{
delay(5000);
frequency = 100000.0f;
Serial.print(F("Change PWM Freq to "));
Serial.println(frequency);
PWM_Instance->setPWM(pinToUse, frequency, 50.0f);
printPWMInfo(PWM_Instance);
delay(5000);
frequency = 128000.0f;
Serial.print(F("Change PWM Freq to "));
Serial.println(frequency);
PWM_Instance->setPWM(pinToUse, frequency, 50.0f);
printPWMInfo(PWM_Instance);
}
And the problem is I get frequencies equal 100 and approximately 142 kHz on my oscilloscope. What could be the cause?