mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-04-30 13:42:12 +00:00
Add serial test pattern arduino code
This commit is contained in:
7
Code/SerialTestPattern/README.md
Normal file
7
Code/SerialTestPattern/README.md
Normal file
@@ -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.
|
||||||
|
|
||||||
36
Code/SerialTestPattern/SerialTestPattern.ino
Normal file
36
Code/SerialTestPattern/SerialTestPattern.ino
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#include <SoftwareSerial.h>
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user