Created
February 25, 2020 15:05
-
-
Save Beherith/9d090f64437d6ca721a76e70a097a664 to your computer and use it in GitHub Desktop.
Test the timing of indivisual ones and zeros on the DCC protocol.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ESP8266WiFi.h" | |
#include "WiFiClient.h" | |
const int baudrate = 250000; | |
const int dccpin = 0; //D3 on wemos board | |
int olddcc = 0; | |
unsigned long t = 0; | |
unsigned long lastchangeus = 0; | |
byte ones[16384]; | |
byte zeros[16384]; | |
unsigned int ptr = 0; | |
unsigned long loops = 0; | |
unsigned long loopstime = 0; | |
void setup() { | |
Serial.begin(baudrate); | |
pinMode(dccpin, INPUT_PULLUP); | |
Serial.println("Welcome to the DCC tester, set the serial to baud rate 2,000,000"); | |
Serial.println("first number is is low usecs, second is high usecs"); | |
//Serial.println(2000000); | |
loopstime = millis(); | |
WiFi.mode( WIFI_OFF ); | |
WiFi.forceSleepBegin(); | |
//ESP.wdtDisable(); | |
delay(10); | |
} | |
void loop() { //an ESP loop has an overhead of 5us! | |
unsigned long steppedmicros = micros(); //as loading millis takes nearly 5 usecs! | |
for (int i = 0; i < 100; i++) { | |
loops++; | |
int newdcc = digitalRead(dccpin); | |
if (newdcc != olddcc) { | |
unsigned long newt = micros(); | |
lastchangeus = newt; | |
steppedmicros = newt; | |
if (newdcc == 0) { | |
ones[ptr] = newt - t; | |
ptr++; | |
} else { | |
zeros[ptr] = newt - t; | |
} | |
//Serial.print('.'); | |
t = newt; | |
} | |
olddcc = newdcc; | |
if ((steppedmicros - lastchangeus > 2000000) || (ptr > 16000)) { | |
lastchangeus = micros(); | |
steppedmicros = micros(); | |
Serial.print(steppedmicros); | |
Serial.print(' '); | |
Serial.print(lastchangeus); | |
Serial.print(" Total number of samples:"); | |
Serial.println(ptr); | |
for (unsigned int i = 0; i < ptr; i++) { | |
Serial.print(zeros[i]); | |
Serial.print('\t'); | |
Serial.println(ones[i]); | |
yield(); | |
} | |
ptr = 0; | |
if (loops > 1000000) { | |
Serial.print(loops); | |
Serial.print(" loops done in ms "); | |
Serial.println(millis() - loopstime); | |
loopstime = millis(); | |
loops = 0; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment