gitignore
This commit is contained in:
parent
3c68a5c9ba
commit
24c079663d
|
@ -0,0 +1 @@
|
|||
.DS_Store
|
172
wwvb/wwvb.ino
172
wwvb/wwvb.ino
|
@ -12,11 +12,10 @@ LiquidCrystal_I2C lcd(0x27, 16, 2);
|
|||
|
||||
#define DEBUG false
|
||||
|
||||
// we sample once per ms:
|
||||
#define NUM_SAMPLES_PER_FRAME 700
|
||||
|
||||
#define CLOCKS_PER_MS 10
|
||||
|
||||
#define PPS_PULSEWIDTH_MS 100
|
||||
|
||||
char statusString[25] = "LOS";
|
||||
|
||||
volatile unsigned int lowLatencyInState;
|
||||
|
@ -31,28 +30,22 @@ volatile unsigned int secondCounter = 0;
|
|||
volatile unsigned int milliCounter = 0;
|
||||
volatile unsigned int clockCounter = 0;
|
||||
|
||||
volatile unsigned int highFor = 0;
|
||||
volatile unsigned int lowFor = 0;
|
||||
|
||||
volatile unsigned int lastBitHighMS = 0;
|
||||
volatile unsigned int timeToTick = 0;
|
||||
volatile unsigned int oldLastLowMillis = 0;
|
||||
volatile unsigned int lastLowMillis = 0;
|
||||
volatile unsigned int oldLastHighMillis = 0;
|
||||
volatile unsigned int lastHighMillis = 0;
|
||||
volatile unsigned int frameReadyToStart = 0;
|
||||
volatile unsigned int beginFrameSearch = 0;
|
||||
volatile unsigned int frameSamples = 0;
|
||||
volatile unsigned int lossOfSignal = 1;
|
||||
volatile unsigned int displayUpdateRequired = 1;
|
||||
volatile unsigned int frameSearch = 0;
|
||||
volatile unsigned int waitingForSecond = 0;
|
||||
volatile unsigned int frameStart = 0;
|
||||
volatile unsigned int frameStartTime = 0;
|
||||
volatile unsigned int frameHigh = 0;
|
||||
volatile unsigned int frameHighReadOut = 0;
|
||||
volatile unsigned int frameLow = 0;
|
||||
volatile unsigned int frameLowReadOut = 0;
|
||||
volatile unsigned int frameCounter = 0;
|
||||
volatile unsigned int frameReadyForRead = 0;
|
||||
volatile unsigned int bitReadyForRead = 0;
|
||||
volatile unsigned int lastBitReceived = 0;
|
||||
volatile unsigned int millisSinceBoot = 0;
|
||||
volatile unsigned int ppsActivationTime;
|
||||
volatile unsigned int ppsActivationTime = 0;
|
||||
volatile unsigned int millisSinceSignalStateChange = 0;
|
||||
volatile unsigned int minuteSync = 0;
|
||||
|
||||
|
@ -74,10 +67,12 @@ void setup() {
|
|||
Serial.println("************************************************");
|
||||
Serial.println("*** ESP8266 Ready.");
|
||||
Serial.println("************************************************");
|
||||
Serial.print(F("\nStarting WWVB on "));
|
||||
Serial.println("************************************************");
|
||||
Serial.print("\n\n\n");
|
||||
Serial.print(F("*** Starting WWVB on "));
|
||||
Serial.println(ARDUINO_BOARD);
|
||||
Serial.println(ESP8266_TIMER_INTERRUPT_VERSION);
|
||||
Serial.print(F("CPU Frequency = "));
|
||||
Serial.print(F("*** CPU Frequency = "));
|
||||
Serial.print(F_CPU / 1000000);
|
||||
Serial.println(F(" MHz"));
|
||||
|
||||
|
@ -88,18 +83,19 @@ void setup() {
|
|||
|
||||
// 100us timer:
|
||||
if (ITimer.attachInterruptInterval(100, TimerHandler)) {
|
||||
Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis());
|
||||
Serial.print(F("Starting ITimer OK, millis() = "));
|
||||
Serial.println(millis());
|
||||
} else {
|
||||
Serial.println(F("Can't set ITimer. Select another freq. or timer"));
|
||||
Serial.println(
|
||||
F("Can't set ITimer. Select another freq. or timer")
|
||||
);
|
||||
}
|
||||
|
||||
//attachInterrupt(digitalPinToInterrupt(LOSC_INPUT_PIN), OSCEdge, RISING);
|
||||
lcd.init();
|
||||
lcd.backlight();
|
||||
lcd.setCursor(0, 0);
|
||||
lcd.print("booting");
|
||||
|
||||
ppsActivationTime = millis();
|
||||
}
|
||||
|
||||
void TimerHandler() {
|
||||
|
@ -111,18 +107,17 @@ void TimerHandler() {
|
|||
// *****************************************************************************
|
||||
// LOW LATENCY HACK to respond in 100us to a falling
|
||||
// start-of-second edge
|
||||
// respond really fast to a falling edge if in the frameReadyToStart
|
||||
// respond really fast to a falling edge if in the waitingForSecond
|
||||
// state
|
||||
lowLatencyInState = digitalRead(WWV_SIGNAL_PIN);
|
||||
if(!lowLatencyInState && frameReadyToStart && !lossOfSignal && !frameStart) {
|
||||
if(waitingForSecond && !lossOfSignal && !lowLatencyInState) {
|
||||
// TICK!
|
||||
// falling edge, beginning of a new frame and second
|
||||
digitalWrite(PPS_OUTPUT_PIN, 1);
|
||||
waitingForSecond = 0;
|
||||
timeToTick = 1;
|
||||
frameStart = 1;
|
||||
frameStartTime = millisSinceBoot;
|
||||
frameReadyToStart = 0;
|
||||
frameSearch = 0;
|
||||
lastBitHighMS = highFor;
|
||||
bitReadyForRead = 1;
|
||||
}
|
||||
// *****************************************************************************
|
||||
MilliEdge();
|
||||
|
@ -146,85 +141,52 @@ void MilliEdge() {
|
|||
SecondEdge();
|
||||
}
|
||||
|
||||
if(wwvbInState != lastWWVBInState) {
|
||||
// any edge indicates we have some signal
|
||||
lossOfSignal = 0;
|
||||
}
|
||||
|
||||
if(wwvbInState && (wwvbInState != lastWWVBInState)) {
|
||||
// we are on the rising edge, last low was 1ms ago
|
||||
oldLastLowMillis = lastLowMillis;
|
||||
lastLowMillis = millisSinceBoot - 1;
|
||||
highFor = 0;
|
||||
}
|
||||
|
||||
if(!wwvbInState && (wwvbInState != lastWWVBInState)) {
|
||||
// we are on the falling edge, last high was 1ms ago
|
||||
oldLastHighMillis = lastHighMillis;
|
||||
lastHighMillis = millisSinceBoot - 1;
|
||||
lowFor = 0;
|
||||
}
|
||||
|
||||
|
||||
if(wwvbInState != lastWWVBInState) {
|
||||
// we are on an edge
|
||||
stateStableMillis = 0;
|
||||
} else {
|
||||
// nothing happening
|
||||
stateStableMillis++;
|
||||
if(wwvbInState == lastWWVBInState) {
|
||||
if(wwvbInState) {
|
||||
highFor++;
|
||||
} else {
|
||||
lowFor++;
|
||||
}
|
||||
}
|
||||
|
||||
if(wwvbInState && (stateStableMillis > 180) && (stateStableMillis < 2000)) {
|
||||
// if we are high but for less than 2s
|
||||
if(wwvbInState && (highFor > 80) && !lossOfSignal) {
|
||||
// if we are high for at least 80ms but no LOS
|
||||
// main screen turn on
|
||||
lossOfSignal = 0;
|
||||
frameSearch = 1;
|
||||
// this enables the falling edge low latency detector
|
||||
// that happens on a tick
|
||||
waitingForSecond = 1;
|
||||
}
|
||||
|
||||
if(wwvbInState && (highFor > 2000) && !lossOfSignal) {
|
||||
// we have received nothing for 2 seconds, loss of signal:
|
||||
lossOfSignal = 1;
|
||||
waitingForSecond = 1;
|
||||
frameCounter = 0;
|
||||
minuteSync = 0;
|
||||
}
|
||||
|
||||
lastWWVBInState = wwvbInState; // copy/save for next loop
|
||||
|
||||
if((stateStableMillis > 2000) && !lossOfSignal) {
|
||||
// we have received nothing for 2 seconds, loss of signal:
|
||||
lossOfSignal = 1;
|
||||
frameStart = 0;
|
||||
frameCounter = 0;
|
||||
minuteSync = 0;
|
||||
digitalWrite(DEBUG1_OUTPUT_PIN, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
if(!frameStart && frameSearch && wwvbInState) {
|
||||
// if we have been high for 180ms (frameSearch) we are ready to start a new frame on the mark
|
||||
frameHigh = 0;
|
||||
frameLow = 0;
|
||||
frameReadyToStart = 1;
|
||||
digitalWrite(DEBUG1_OUTPUT_PIN, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
// frameReadyToStart -> frameStart (and PPS) happens in a 100us
|
||||
// ISR above.
|
||||
|
||||
if (frameStart && (frameSamples < NUM_SAMPLES_PER_FRAME)) {
|
||||
frameSearch = 0;
|
||||
//begin sampling
|
||||
if (wwvbInState) {
|
||||
frameHigh++;
|
||||
} else {
|
||||
frameLow++;
|
||||
}
|
||||
frameSamples++;
|
||||
digitalWrite(DEBUG1_OUTPUT_PIN, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
if(frameStart && (frameSamples >= NUM_SAMPLES_PER_FRAME)) {
|
||||
frameReadyForRead = 1;
|
||||
frameHighReadOut = frameHigh;
|
||||
frameLowReadOut = frameLow;
|
||||
frameStart = 0;
|
||||
frameHigh = 0;
|
||||
frameLow = 0;
|
||||
frameSamples = 0;
|
||||
}
|
||||
digitalWrite(DEBUG1_OUTPUT_PIN, 0);
|
||||
}
|
||||
|
||||
char pb[255];
|
||||
|
||||
void loop() {
|
||||
|
||||
digitalWrite(LED_BUILTIN, !wwvbInState);
|
||||
|
||||
if(timeToTick) {
|
||||
|
@ -234,9 +196,9 @@ void loop() {
|
|||
|
||||
yield();
|
||||
|
||||
if(frameReadyForRead) {
|
||||
frameReadyForRead = 0;
|
||||
processFrame();
|
||||
if(bitReadyForRead) {
|
||||
bitReadyForRead = 0;
|
||||
readBit();
|
||||
}
|
||||
|
||||
yield();
|
||||
|
@ -273,30 +235,22 @@ void PPSLowIfRequired() {
|
|||
|
||||
void TickSecond() {
|
||||
char buf[255];
|
||||
sprintf(buf, "*** TICK(%d): WWVB going low after %d ms high (EDGE)\n", frameCounter, millisSinceBoot - lastLowMillis);
|
||||
sprintf(buf, "*** TICK(%d): WWVB going low after %d ms high (EDGE)\n",
|
||||
frameCounter, lastBitHighMS);
|
||||
SendPPS();
|
||||
Serial.print(buf);
|
||||
}
|
||||
|
||||
void processFrame() {
|
||||
void readBit() {
|
||||
char buf[255];
|
||||
sprintf(buf, "end of frame summary: frameHigh: %d, frameLow: %d\n", frameHighReadOut, frameLowReadOut);
|
||||
unsigned int ms = lastBitHighMS;
|
||||
sprintf(buf, "*** carrier was high for %sms befor \n", ms);
|
||||
Serial.print(buf);
|
||||
float rawVal =
|
||||
(float)frameHighReadOut
|
||||
/
|
||||
( (float)frameHighReadOut + (float)frameLowReadOut );
|
||||
|
||||
rawVal *= 1000;
|
||||
unsigned int intRawVal = (int)rawVal;
|
||||
if (intRawVal > 100000) {
|
||||
intRawVal = 100000;
|
||||
}
|
||||
displayUpdateRequired++;
|
||||
registerBit(convertDutyCycleToBit(intRawVal));
|
||||
registerBit(convertBit(ms));
|
||||
}
|
||||
|
||||
int convertDutyCycleToBit(unsigned int rawVal) {
|
||||
int convertBit(unsigned int ms) {
|
||||
char buf[255];
|
||||
/*
|
||||
|
||||
|
@ -312,6 +266,8 @@ int convertDutyCycleToBit(unsigned int rawVal) {
|
|||
output = ZEROBIT;
|
||||
sprintf(bitbuf, "ZERO");
|
||||
|
||||
return output;
|
||||
/*
|
||||
if (rawVal < 800) {
|
||||
output = ONEBIT;
|
||||
sprintf(bitbuf, "ONE");
|
||||
|
@ -325,6 +281,7 @@ int convertDutyCycleToBit(unsigned int rawVal) {
|
|||
sprintf(buf, "frame rawVal=%d, bit=%s\n", rawVal, bitbuf);
|
||||
Serial.print(buf);
|
||||
return output;
|
||||
*/
|
||||
}
|
||||
|
||||
void registerBit(int doot) {
|
||||
|
@ -361,7 +318,6 @@ void lossOfSync(int errorFrame) {
|
|||
displayUpdateRequired++;
|
||||
}
|
||||
|
||||
|
||||
void sanityCheckFrame(int doot) {
|
||||
if (
|
||||
(
|
||||
|
|
Loading…
Reference in New Issue