PWM på "godtyckliga" frekvenser på arduino
Man kan lätt slita sitt hår här. Hur fixar man en analog-out med en godtycklig frekvens på enklast sätt?
1. Ladda ner och installera
pwm.h
#include <PWM.h>
//use pin 11 on the Mega instead, otherwise there is a frequency cap at 31 Hz
int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int32_t frequency = 35; //frequency (in Hz)
void setup()
{
//initialize all timers except for 0, to save time keeping functions
InitTimersSafe();
//sets the frequency for the specified pin
bool success = SetPinFrequencySafe(led, frequency);
//if the pin frequency was set successfully, turn pin 13 on
if(success) {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
}
void loop()
{
//use this functions instead of analogWrite on 'initialized' pins
pwmWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(30);
}
Ovan exempel medföljer pwm.h och sätter frekvensen till 35 Hz. Men det går att sätta den till t.ex. 25 KHz eller 16 KHz eller 30 KHz också om du vill.
Denna höga upplösning fungerar enbart för vissa pinnar, t.ex. pinne 9.
https://arduino-info.wikispaces.com/Arduino-PWM-Frequency