#include #include #include #include #include "main.h" #include "clock.h" #include "uartdrv.h" #include "stepper.h" #include "validLed.h" receivedData_t rd; extern volatile clock_t clock; volatile uint8_t tick; volatile uint16_t captValue = 0; volatile uint16_t gapValue = 0; volatile uint8_t valid = 0; uint16_t validCnt = 0; uint16_t timeValidCnt = 0; ISR(INT0_vect) { uint16_t tmpCnt = TCNT1; gapValue = tmpCnt - captValue; TCNT1 = 0; } ISR(INT1_vect) { captValue = TCNT1; tick = 1; } void setValid(uint8_t x) { valid = x; if (valid == 1) { validCnt++; clockSetUseLocalClock(1); } else if (valid == 2) { timeValidCnt++; clockSetUseLocalClock(0); stepperEnable(); } else { clockSetUseLocalClock(1); } } uint8_t getValid() { return valid; } int main() { // signal measurment TCCR1A = 0; TCCR1B = (1 << CS12) | (1 << CS10); DDRD &= ~(1 << PD2); DDRD &= ~(1 << PD3); PORTD |= (1 << PD2) | (1 << PD3); MCUCR |= (1 << ISC11) | (1 << ISC10) | (1 << ISC01); GICR |= (1 << INT0) | (1 << INT1); // esd prot DDRD |= (1 << PD5) | (1 << PD6); uartdrvInit(); clockInit(); stepperInit(); validLedInit(); setValid(0); sei(); printf("MyClock started\n"); uint8_t bit = 0; uint8_t state = 0; while (1) { if (tick != 0) { tick = 0; uint16_t pulse = (((uint32_t) captValue) * 1000) / 15625; uint16_t gap = (((uint32_t) gapValue) * 1000) / 15625; if (gap > START_GAP_MIN) { if (valid == 0) { setValid(1);; } state = 0; clockClearSecond(); } if (pulse > ZERO_WIDTH_MIN && pulse < ZERO_WIDTH_MAX) { bit = 0; } else if (pulse > ONE_WIDTH_MIN && pulse < ONE_WIDTH_MAX) { bit = 1; } else { bit = 2; setValid(0);; } if (valid > 0) { switch (state) { case 0: if (bit != 0) setValid(0); break; case 1: break; case 2: break; case 3: break; case 4: break; case 5: break; case 6: break; case 7: break; case 8: break; case 9: break; case 10: break; case 11: break; case 12: break; case 13: break; case 14: break; case 15: break; case 16: break; case 17: break; case 18: break; case 19: break; case 20: if (bit != 1) setValid(0); break; case 21: rd.minuteOneCnt = 0; rd.minuteDigit0 = 0; rd.minuteDigit0 |= bit; if (bit == 1) rd.minuteOneCnt++; break; case 22: rd.minuteDigit0 |= (bit << 1); if (bit == 1) rd.minuteOneCnt++; break; case 23: rd.minuteDigit0 |= (bit << 2); if (bit == 1) rd.minuteOneCnt++; break; case 24: rd.minuteDigit0 |= (bit << 3); if (bit == 1) rd.minuteOneCnt++; break; case 25: rd.minuteDigit1 = 0; rd.minuteDigit1 |= bit; if (bit == 1) rd.minuteOneCnt++; break; case 26: rd.minuteDigit1 |= (bit << 1); if (bit == 1) rd.minuteOneCnt++; break; case 27: rd.minuteDigit1 |= (bit << 2); if (bit == 1) rd.minuteOneCnt++; break; case 28: rd.minuteOneCnt += bit; if (rd.minuteOneCnt % 2 != 0) { printf("MINUTE INVALID\n"); setValid(0); } else { printf("MINUTE %u %u\n", rd.minuteDigit0, rd.minuteDigit1); } break; case 29: rd.hourOneCnt = 0; rd.hourDigit0 = 0; rd.hourDigit0 |= bit; if (bit == 1) rd.hourOneCnt++; break; case 30: rd.hourDigit0 |= (bit << 1); if (bit == 1) rd.hourOneCnt++; break; case 31: rd.hourDigit0 |= (bit << 2); if (bit == 1) rd.hourOneCnt++; break; case 32: rd.hourDigit0 |= (bit << 3); if (bit == 1) rd.hourOneCnt++; break; case 33: rd.hourDigit1 = 0; rd.hourDigit1 |= bit; if (bit == 1) rd.hourOneCnt++; break; case 34: rd.hourDigit1 |= (bit << 1); if (bit == 1) rd.hourOneCnt++; break; case 35: rd.hourOneCnt += bit; if (rd.hourOneCnt % 2 != 0) { printf("HOUR INVALID\n"); setValid(0); } else { printf("HOUR %u %u\n", rd.hourDigit0, rd.hourDigit1); } break; case 36: rd.dateOneCnt = 0; rd.dayDigit0 = 0; rd.dayDigit0 |= bit; if (bit == 1) rd.dateOneCnt++; break; case 37: rd.dayDigit0 |= (bit << 1); if (bit == 1) rd.dateOneCnt++; break; case 38: rd.dayDigit0 |= (bit << 2); if (bit == 1) rd.dateOneCnt++; break; case 39: rd.dayDigit0 |= (bit << 3); if (bit == 1) rd.dateOneCnt++; break; case 40: rd.dayDigit1 = 0; rd.dayDigit1 |= bit; if (bit == 1) rd.dateOneCnt++; break; case 41: rd.dayDigit1 |= (bit << 1); if (bit == 1) rd.dateOneCnt++; break; case 42: rd.weekdayDigit0 = 0; rd.weekdayDigit0 |= bit; if (bit == 1) rd.dateOneCnt++; break; case 43: rd.weekdayDigit0 |= (bit << 1); if (bit == 1) rd.dateOneCnt++; break; case 44: rd.weekdayDigit0 |= (bit << 2); if (bit == 1) rd.dateOneCnt++; break; case 45: rd.monthDigit0 = 0; rd.monthDigit0 |= bit; if (bit == 1) rd.dateOneCnt++; break; case 46: rd.monthDigit0 |= (bit << 1); if (bit == 1) rd.dateOneCnt++; break; case 47: rd.monthDigit0 |= (bit << 2); if (bit == 1) rd.dateOneCnt++; break; case 48: rd.monthDigit0 |= (bit << 3); if (bit == 1) rd.dateOneCnt++; break; case 49: rd.monthDigit1 = 0; rd.monthDigit1 |= bit; if (bit == 1) rd.dateOneCnt++; break; case 50: rd.yearDigit0 = 0; rd.yearDigit0 |= bit; if (bit == 1) rd.dateOneCnt++; break; case 51: rd.yearDigit0 |= (bit << 1); if (bit == 1) rd.dateOneCnt++; break; case 52: rd.yearDigit0 |= (bit << 2); if (bit == 1) rd.dateOneCnt++; break; case 53: rd.yearDigit0 |= (bit << 3); if (bit == 1) rd.dateOneCnt++; break; case 54: rd.yearDigit1 = 0; rd.yearDigit1 |= bit; if (bit == 1) rd.dateOneCnt++; break; case 55: rd.yearDigit1 |= (bit << 1); if (bit == 1) rd.dateOneCnt++; break; case 56: rd.yearDigit1 |= (bit << 2); if (bit == 1) rd.dateOneCnt++; break; case 57: rd.yearDigit1 |= (bit << 3); if (bit == 1) rd.dateOneCnt++; break; case 58: rd.dateOneCnt += bit; if (rd.dateOneCnt % 2 != 0) { printf("DATE INVALID\n"); setValid(0); } else { printf("DAY %u %u\n", rd.dayDigit0, rd.dayDigit1); printf("WEEKDAY %u\n", rd.weekdayDigit0); printf("MONTH %u %u\n", rd.monthDigit0, rd.monthDigit1); printf("YEAR %u %u\n", rd.yearDigit0, rd.yearDigit1); clockSetClock(rd.yearDigit0 + rd.yearDigit1 * 10, rd.monthDigit0 + rd.monthDigit1 * 10, rd.dayDigit0 + rd.dayDigit1 * 10, rd.weekdayDigit0, rd.hourDigit0 + rd.hourDigit1 * 10, rd.minuteDigit0 + rd.minuteDigit1 * 10); setValid(2); } break; case 59: break; default: valid = 0; break; } printf("%02d:%02d:%02d %02d.%02d.%02d %d %d %02d %d %d %d %d %d\n", clock.hour, clock.minute, clock.second, clock.day, clock.month, clock.year, clock.weekday, valid, state, bit, pulse, gap, validCnt, timeValidCnt); state++; } else { printf("%02d:%02d:%02d %02d.%02d.%02d %d %d %02d %d %d %d %d %d\n", clock.hour, clock.minute, clock.second, clock.day, clock.month, clock.year, clock.weekday, valid, state, bit, pulse, gap, validCnt, timeValidCnt); } } if (clockNextMinute()) { opTime_t opTime = clockGetOpTime(); printf("% 4d:%02d:%02d\n", opTime.hour, opTime.minute, opTime.second); } if (clockNextStep()) { stepperEngine(); } if (clockNextTack()) { validLedEngine(valid); } } }