#include <Esp.h>
#include <esp8266_peri.h>
#include <eagle_soc.h>
#define REG_WDT_BASE 0x60000900
#define WDT_CTL (REG_WDT_BASE + 0x0)
#define WDT_CTL_ENABLE (BIT(0))
#define PINOUT 5
double freq; // Hz
double offset; // percent
double width; // percent
// unit: microsecond
unsigned long cycle_time;
unsigned long raising_edge;
unsigned long falling_edge;
unsigned long prev_micros;
// compare 2 unsigned value
// true if X > Y while for all possible (X, Y), X - Y < Z
#define TIME_CMP(X, Y, Z) (((X) - (Y)) < (Z))
inline void setHigh() {
GPOS = (1 << PINOUT);
}
inline void setLow() {
GPOC = (1 << PINOUT);
}
void setup() {
CLEAR_PERI_REG_MASK(WDT_CTL, WDT_CTL_ENABLE);
ESP.wdtDisable();
pinMode(PINOUT, OUTPUT);
// calculate arguments
freq = 1;
width = 0.5;
offset = 0.0;
cycle_time = 1000000 / freq;
raising_edge = (unsigned long)(offset * cycle_time) % cycle_time;
falling_edge = (unsigned long)((offset + width) * cycle_time) % cycle_time;
prev_micros = micros();
// do pinout shifting
while(1) {
if (width + offset < 1) {
// raising edge should appear earlier
while (TIME_CMP(micros(), prev_micros + raising_edge, cycle_time)); setHigh();
while (TIME_CMP(micros(), prev_micros + falling_edge, cycle_time)); setLow();
} else {
// falling edge should appear earlier
while (TIME_CMP(micros(), prev_micros + falling_edge, cycle_time)); setLow();
while (TIME_CMP(micros(), prev_micros + raising_edge, cycle_time)); setHigh();
}
prev_micros += cycle_time;
}
}
void loop() {
}
谢谢博主分享的经验。 我想问下你用的是什么型号的示波器?
文中截图用的是梦源实验室的 DScope https://www.dreamsourcelab.com/product/dscope-u2p20/