Getting 10 kHz PWM
can tell me how close 10 khz (can off few hz) on digital pin 9 on nano? looked @ prescalars , don't allow me close number doing research appears possible through other means. confused heavy amounts of c++ code though if can post dummy-proof syntax set , unset pin 9 10khz immensely grateful. last resort, if can't decipher code, i'd able copy/paste code block sketch. thank you.
here's 1 way, blink without delay style.
adjust duration period want.
adjust duration period want.
code: [select]
byte triggerout = 2; // port d, bit 2 on arduino
unsigned long currentmicros;
unsigned long nextmicros;
unsigned long duration = 25ul; // flip every 50us = 20khz pulse
unsigned long elapsedmicros;
void setup() {
pinmode (triggerout, output);
nextmicros = micros();
}
void loop() {
currentmicros = micros();
elapsedmicros = currentmicros - nextmicros;
if (elapsedmicros >= duration) {
nextmicros = nextmicros + duration;
pind = pind | 0b00000100; // toggle d2 output writing input port.
}
// other stuff while time goes by
// ...
// end of other stuff, go check time again @ top of loop
}
Arduino Forum > Using Arduino > Programming Questions > Getting 10 kHz PWM
arduino
Comments
Post a Comment