使用 Arduino Mega2560 和串口控制的动态短脉冲发生器
这是我们混合静态/动态脉冲发生器的简化版本。在 Arduino Mega2560 平台上,它允许生成从 750ns 到几乎任意长度(仅受整数宽度限制)的脉冲,分辨率约为 437.5ns
要控制脉冲宽度,打开串口界面并输入 + 增加脉冲宽度或 - 减小脉冲宽度。
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