Это упрощённая версия нашего гибридного статического/динамического генератора импульсов. На платформе Arduino Mega2560 он позволяет генерировать импульсы от 750нс до почти произвольной длины (ограниченной только шириной целого числа) с разрешением примерно 437.5нс
Для управления шириной импульса откройте последовательный интерфейс и введите + для увеличения ширины импульса или - для уменьшения ширины импульса.
dynamic_pulse_serial.ino
#include <Arduino.h>
#include <avr/io.h>
#include <ArduinoJson.h>
const int pulseAPin = 11;
#define PORT11 PORTB
#define PIN11 5
#define PIN11_MASK (1 << PIN11)
int pulseLength = 0;
/**
* Pulse the output pin, for very short pulses.
*/
void PulseDynamic(int n) {
cli();
PORT11 |= PIN11_MASK;
// Dynamic for loop
for (;n >= 0; n--) {
_NOP();
}
PORT11 &= ~PIN11_MASK;
sei();
}
void setup()
{
Serial.begin(115200);
Serial.setTimeout(25);
pinMode(pulseAPin, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
PulseDynamic(pulseLength);
// Process serial
while(Serial.available() > 0) {
int c = Serial.read();
if(c == '+') {
pulseLength++;
Serial.println(pulseLength);
} else if(c == '-') {
pulseLength--;
if(pulseLength < 0) {pulseLength = 0;}
Serial.println(pulseLength);
}
}
delay(50);
}If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow