Flipklok
 
Loading...
Searching...
No Matches
timeKeeping.c
Go to the documentation of this file.
1/*
2 * timekeeping.c
3 *
4 * Created on: 21 mrt. 2025
5 */
10
11#include "timeKeeping.h"
12#include "dcfReceive.h"
13#include "uart.h"
14#include "pinInterrupts.h"
15
17
18
25
27{
28 if(countDcf77Messages > 0)
29 {
30 __no_operation();
31 volatile int testing = 0;
32 }
33 volatile uint32_t timeDiff;
34 __no_operation();
35 toggleInterruptDcf = 1; // Enable dcfReceive interrupt
36// unsigned int state = __get_SR_register() & GIE; __disable_interrupt(); // Save current interrupt state and disable interrupts
37 if (0 == toggleFwdInterrupt) //fwdButton is not pressed otherwise skip time keeping
38 {
39 if(0 == countDcf77Messages) // When no DCF77 message has been received work only on internal clock
40 {
42 P1DIR |= BIT7; // set as output enables indicator LED, indicating clock is off time
43 }
44 else if(countDcf77Messages > 0) // One or more DCF77 messages has been received use the time of DCF to set clock
45 {
46 if((timeOfLastDcfMessage + timeSinceLastCompleteDcfMessage) > mechanicalTimeFloat) // Clock runs behind need to be advanced to current time
47 {
48 timeDiff = (timeOfLastDcfMessage + timeSinceLastCompleteDcfMessage) - mechanicalTimeFloat; // Calculate how much the clock runs behind
49 }
51 {
52 if((mechanicalTimeFloat - (timeOfLastDcfMessage + timeSinceLastCompleteDcfMessage)) > clockWraparoundThresholdSec) // When the time that the clock runs ahead is greater than the threshold than rotate to 00:00 so it can sync from there
53 {
54 timeDiff = secondsInDay + 60 - mechanicalTimeFloat; // Calculate the amount of time between clock current state and the time until 00:01
55 }
56 else if((mechanicalTimeFloat - (timeOfLastDcfMessage + timeSinceLastCompleteDcfMessage)) <= clockWraparoundThresholdSec) // If the time that the clock runs ahead is less or equal to the threshold it is faster to just wait until the clock is on time
57 {
58 timeDiff = 0;
59 }
60 else
61 {
62 __no_operation(); //?
63 }
64 }
65 else
66 {
67 __no_operation(); //?
68 }
69 }
70 else
71 {
72 __no_operation(); //?
73 }
74
75 //Checking if calculated time is plausible
76 if (timeDiff > secondsInDay)
77 {
78 __no_operation(); //Clock will rotate more than a full turn, disable stepper and recalculate
79 }
80 else // Calculated time is plausible convert to needed steps
81 {
82 if (timeDiff > 60) // If the time difference is large enough stop receiving dcf messages to prevent crosstalk between stepper and receiver, is enabled again at start of this function
83 {
84 toggleInterruptDcf = 0; // Disable dcfReceive interrupt until clock is done syncing
85 P1DIR |= BIT7; // set as output enables indicator LED, indicating clock is off time
86 toggleFwdInterruptISR = 0; // Disable fwdButton when calculating time and adjusting the clock (enabled again in stepperAdvance function)
87 timerCompareStepperSpeedToggle = 1; //set stepper speed to fast
88 }
89 else
90 {
91 timerCompareStepperSpeedToggle = 0; //set stepper speed to slow
92 if (0 < countDcf77Messages)
93 {
94 P1DIR &= ~BIT7; // set as input disables indicator LED, indicating clock runs on time
95 }
96 }
98// #ifdef UART_ENABLED
99// UART_SendInt(timeDiff);
100// #endif
101
102 if (stepsRemaining >= stepsToggleThreshold && stepsRemaining < stepsInDay) //Check if the needed steps to take is larger than the movement threshold, this prevents excessive use of the motor
103 {
104 P1OUT |= BIT0; //Turn 5v on
105 toggleCalculateTimeDifference = 0; // Disable triggering time difference function in timer interrupt (this function), will be enabled again in stepperAdvance function
106 TA0CCTL2 |= CCIE; // TACCR2 interrupt stepperAdvance enable
107 }
108 }
109 }
110
111// if (state) __enable_interrupt(); // Restore previous interrupt state
112}
113
114
115
122uint32_t calculateStepsToTake(uint32_t time)
123{
124 if (time <= 0) //time difference is negative or 0
125 {
126 __no_operation();
127 return 0;
128 }
129 else
130 {
131 double steps = (double)time * stepsClockSecond;
132 uint32_t stepsRounded = (uint32_t)steps;
133 __no_operation();
134 return stepsRounded;
135 }
136}
137
uint32_t countDcf77Messages
Definition dcfReceive.h:49
bool toggleInterruptDcf
Definition dcfReceive.h:24
bool toggleFwdInterruptISR
bool toggleFwdInterrupt
bool timerCompareStepperSpeedToggle
void calculateTimeDifference()
calculate the difference between the mechanical time and the current time to have the time difference...
Definition timeKeeping.c:26
void resetTimeKeeping()
Reset the time settings on startup, gets called from main()
uint32_t stepsRemaining
Definition timeKeeping.h:43
uint32_t timeOfLastDcfMessage
Definition timeKeeping.h:21
uint32_t timeSinceLastCompleteDcfMessage
Definition timeKeeping.h:22
#define stepsClockSecond
Definition timeKeeping.h:31
double mechanicalTimeFloat
Definition timeKeeping.h:40
#define secondsInDay
Definition timeKeeping.h:36
#define clockWraparoundThresholdSec
Definition timeKeeping.h:34
bool toggleCalculateTimeDifference
Definition timeKeeping.c:16
#define stepsInDay
Definition timeKeeping.h:37
uint32_t calculateStepsToTake()
#define stepsToggleThreshold
Definition timeKeeping.h:33