hacks/arduino/wwvb/wwvb.ino

111 lines
3.2 KiB
C++

/*
ESP8266 Blink by Simon Peter
Blink the blue LED on the ESP-01 module
This example code is in the public domain
The blue LED on the ESP-01 module is connected to GPIO1
(which is also the TXD pin; so we cannot use Serial.print() at the same time)
Note that this sketch uses LED_BUILTIN to find the pin with the internal LED
*/
#define WWV_SIGNAL_PIN 14
void ICACHE_RAM_ATTR readLevel();
volatile byte wwvbInState; // store receiver signal level
byte prevWwvbInState; // store previous signal level
unsigned int prevEdgeMillis; // store time signal was read
byte bitVal; // bit decoded 0, 1 or Mark
byte badBit; // bad bit, noise detected
byte prevMark; // store previous mark bit
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
pinMode(WWV_SIGNAL_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(WWV_SIGNAL_PIN), readLevel, CHANGE); // fire interrupt on edge detected
Serial.begin(9600);
Serial.print("herro booted\n");
}
// the loop function runs over and over again forever
void loop() {
if (wwvbInState != prevWwvbInState) {
pulseValue();
prevWwvbInState = wwvbInState;
}
yield();
}
void pulseValue() {
unsigned int edgeMillis = millis(); // save current time
badBit = 0; // set noise counter to zero
if (wwvbInState == 1) { // rising edge
prevEdgeMillis = edgeMillis; // set previous time to current
}
else { // falling edge
int pulseLength = edgeMillis - prevEdgeMillis; // calculate pulse length millis
if (pulseLength < 100) { // less than 100ms, noise pulses
badBit = 1;
}
else if (pulseLength < 400) { // 800ms carrier drop mark
bitVal = 2;
}
else if (pulseLength < 700) { // 500ms carrier drop one
bitVal = 1;
}
else { // 200ms carrier drop zero
bitVal = 0;
}
if (badBit == 0) {
printBitVal();
}
}
}
void readLevel() {
wwvbInState = digitalRead(WWV_SIGNAL_PIN); // read signal level
digitalWrite(LED_BUILTIN, !wwvbInState); // flash WWVB receiver indicator pin
yield();
}
void printBitVal() {
if ((bitVal == 2) && (prevMark == 0)) {
Serial.print(" : ");
prevMark = 1;
}
else if ((bitVal == 2) && (prevMark == 1)) {
Serial.print("\nBit Value: ");
Serial.print("| ");
prevMark = 0;
}
else {
Serial.print(bitVal, DEC);
prevMark = 0;
}
}
/*****************************************************************************
* Time display functions
*****************************************************************************/
void printTime() {
Serial.print("?x00?y0?f"); // movie cursor to line 1 char 1, clear screen
}
// LCD routines to initialize LCD and clear screen
void lcdInit() { // using P H Anderson Serial LCD driver board
Serial.print("?G216"); // configure driver for 2 x 16 LCD
delay(300);
Serial.print("?BDD"); // set backlight brightness
delay(300);
Serial.print("?f"); // clear screen
Serial.print("?c0"); // set cursor off
}