diff --git a/Code/SerialTestPattern/README.md b/Code/SerialTestPattern/README.md new file mode 100644 index 00000000..25cefded --- /dev/null +++ b/Code/SerialTestPattern/README.md @@ -0,0 +1,7 @@ + +This is a simple Arduino sketch that can be used to generate a +serial test pattern (input to the m-bus master for instance). + +It will send one byte at the time, changing one bit compared +to previous at a fixed interval. + diff --git a/Code/SerialTestPattern/SerialTestPattern.ino b/Code/SerialTestPattern/SerialTestPattern.ino new file mode 100644 index 00000000..aef5bec0 --- /dev/null +++ b/Code/SerialTestPattern/SerialTestPattern.ino @@ -0,0 +1,36 @@ +#include + +#define RX_PIN 10 +#define TX_PIN 11 + +SoftwareSerial mySerial(RX_PIN, TX_PIN); + +// https://en.wikipedia.org/wiki/Binary_Gray_sequence +static unsigned int binary_to_gray(unsigned int num) +{ + return num ^ (num >> 1); +} + +void setup() { + mySerial.begin(9600); +} + +void loop() { + static unsigned int i = 0; + static unsigned int ch = 0; + + // Change the character more seldom than each loop entry. + if (i++ > 20) { + i = 0; + // 127 => leave the last bit sent always zero. + if (++ch > 127) { + ch = 0; + } + } + + mySerial.write(binary_to_gray(ch)); + + // 9600 bps => 8 bit = 8/9600s ~ 0.8ms + delay(50); +} +