Flipklok
 
Loading...
Searching...
No Matches
timeKeeping.c File Reference

handles the keeping and calculating of time of the system More...

#include "timeKeeping.h"
#include "dcfReceive.h"
#include "uart.h"
#include "pinInterrupts.h"

Go to the source code of this file.

Functions

void calculateTimeDifference ()
 calculate the difference between the mechanical time and the current time to have the time difference which the stepper needs to be advanced with, gets called from end interrupt of ISR_TA0(void).
 
uint32_t calculateStepsToTake (uint32_t time)
 Calculate the steps to take for a given time difference, gets called by calculateTimeDifference()
 
void resetTimeKeeping ()
 Reset the time settings on startup, gets called from main()
 

Variables

bool toggleCalculateTimeDifference = 1
 

Detailed Description

handles the keeping and calculating of time of the system

Definition in file timeKeeping.c.

Function Documentation

◆ calculateStepsToTake()

uint32_t calculateStepsToTake ( uint32_t time)

Calculate the steps to take for a given time difference, gets called by calculateTimeDifference()

Parameters
timethe difference between time to advance the stepper with
Returns
uint32_t amount of steps that the stepper needs to be rotated with

Definition at line 122 of file timeKeeping.c.

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}
#define stepsClockSecond
Definition timeKeeping.h:31

References stepsClockSecond.

◆ calculateTimeDifference()

void calculateTimeDifference ( )

calculate the difference between the mechanical time and the current time to have the time difference which the stepper needs to be advanced with, gets called from end interrupt of ISR_TA0(void).

It then calculates the amount of steps that are needed to advance the clock to display the current time. The function take in to account which way it is the fastest to fully wrap the clock or to wait until the time synchronizes

Definition at line 26 of file timeKeeping.c.

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}
uint32_t countDcf77Messages
Definition dcfReceive.h:49
bool toggleInterruptDcf
Definition dcfReceive.h:24
bool toggleFwdInterruptISR
bool toggleFwdInterrupt
bool timerCompareStepperSpeedToggle
uint32_t stepsRemaining
Definition timeKeeping.h:43
uint32_t timeOfLastDcfMessage
Definition timeKeeping.h:21
uint32_t timeSinceLastCompleteDcfMessage
Definition timeKeeping.h:22
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

References calculateStepsToTake(), clockWraparoundThresholdSec, countDcf77Messages, mechanicalTimeFloat, secondsInDay, stepsInDay, stepsRemaining, stepsToggleThreshold, timeOfLastDcfMessage, timerCompareStepperSpeedToggle, timeSinceLastCompleteDcfMessage, toggleCalculateTimeDifference, toggleFwdInterrupt, toggleFwdInterruptISR, and toggleInterruptDcf.

Referenced by ISR_TA0().

◆ resetTimeKeeping()

void resetTimeKeeping ( )

Reset the time settings on startup, gets called from main()

Definition at line 142 of file timeKeeping.c.

References mechanicalTimeFloat, timeOfLastDcfMessage, and timeSinceLastCompleteDcfMessage.

Referenced by main().

Variable Documentation

◆ toggleCalculateTimeDifference

bool toggleCalculateTimeDifference = 1

Definition at line 16 of file timeKeeping.c.

Referenced by calculateTimeDifference(), ISR_TA0(), and stepperAdvance().