Flipklok
 
Loading...
Searching...
No Matches
timeKeeping.h File Reference
#include <msp430.h>
#include <stdbool.h>
#include <stdint.h>

Go to the source code of this file.

Macros

#define timerCountsInDay   60 * 60 * 24 / (capacityTimer / clockFreq)
 
#define toothGearStepper   15.0
 
#define toothGearClock   24.0
 
#define gearRatio   (toothGearClock / toothGearStepper)
 
#define stepsFullRotationStepper   2048.0
 
#define stepsClockHour   (stepsFullRotationStepper * gearRatio)
 
#define stepsClockMinute   (stepsClockHour / 60.0)
 
#define stepsClockSecond   (stepsClockMinute / 60.0)
 
#define timePerStep   (3600.0 / (stepsFullRotationStepper * gearRatio))
 
#define stepsToggleThreshold   20
 
#define clockWraparoundThresholdSec   (10 * 60)
 
#define stepsWrap   (stepsClockHour * 24)
 
#define secondsInDay   86400
 
#define stepsInDay   (stepsClockSecond * secondsInDay)
 

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 ()
 
void storeTimeLastCompleteDcfMessage ()
 
void resetTimeKeeping ()
 Reset the time settings on startup, gets called from main()
 

Variables

bool toggleCalculateTimeDifference
 
uint32_t timeOfLastDcfMessage
 
uint32_t timeSinceLastCompleteDcfMessage
 
double mechanicalTimeFloat
 
uint32_t stepsRemaining
 
volatile bool toggleTimer1Direction
 

Macro Definition Documentation

◆ clockWraparoundThresholdSec

#define clockWraparoundThresholdSec   (10 * 60)

Definition at line 34 of file timeKeeping.h.

Referenced by calculateTimeDifference().

◆ gearRatio

#define gearRatio   (toothGearClock / toothGearStepper)

Definition at line 27 of file timeKeeping.h.

◆ secondsInDay

#define secondsInDay   86400

Definition at line 36 of file timeKeeping.h.

Referenced by calculateTimeDifference().

◆ stepsClockHour

#define stepsClockHour   (stepsFullRotationStepper * gearRatio)

Definition at line 29 of file timeKeeping.h.

◆ stepsClockMinute

#define stepsClockMinute   (stepsClockHour / 60.0)

Definition at line 30 of file timeKeeping.h.

◆ stepsClockSecond

#define stepsClockSecond   (stepsClockMinute / 60.0)

Definition at line 31 of file timeKeeping.h.

Referenced by calculateStepsToTake().

◆ stepsFullRotationStepper

#define stepsFullRotationStepper   2048.0

Definition at line 28 of file timeKeeping.h.

◆ stepsInDay

#define stepsInDay   (stepsClockSecond * secondsInDay)

Definition at line 37 of file timeKeeping.h.

Referenced by calculateTimeDifference().

◆ stepsToggleThreshold

#define stepsToggleThreshold   20

Definition at line 33 of file timeKeeping.h.

Referenced by calculateTimeDifference().

◆ stepsWrap

#define stepsWrap   (stepsClockHour * 24)

Definition at line 35 of file timeKeeping.h.

◆ timePerStep

#define timePerStep   (3600.0 / (stepsFullRotationStepper * gearRatio))

Definition at line 32 of file timeKeeping.h.

Referenced by stepperAdvance().

◆ timerCountsInDay

#define timerCountsInDay   60 * 60 * 24 / (capacityTimer / clockFreq)

Definition at line 23 of file timeKeeping.h.

Referenced by ISR_TA1_CCR0().

◆ toothGearClock

#define toothGearClock   24.0

Definition at line 26 of file timeKeeping.h.

◆ toothGearStepper

#define toothGearStepper   15.0

Definition at line 25 of file timeKeeping.h.

Function Documentation

◆ calculateStepsToTake()

uint32_t calculateStepsToTake ( )

Referenced by calculateTimeDifference().

◆ 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().

◆ storeTimeLastCompleteDcfMessage()

void storeTimeLastCompleteDcfMessage ( )

Variable Documentation

◆ mechanicalTimeFloat

double mechanicalTimeFloat

◆ stepsRemaining

uint32_t stepsRemaining

Definition at line 43 of file timeKeeping.h.

Referenced by calculateTimeDifference(), and stepperAdvance().

◆ timeOfLastDcfMessage

uint32_t timeOfLastDcfMessage

◆ timeSinceLastCompleteDcfMessage

uint32_t timeSinceLastCompleteDcfMessage

◆ toggleCalculateTimeDifference

bool toggleCalculateTimeDifference
extern

Definition at line 16 of file timeKeeping.c.

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

◆ toggleTimer1Direction

volatile bool toggleTimer1Direction

Definition at line 46 of file timeKeeping.h.

Referenced by interruptDcf(), ISR_TA1(), ISR_TA1_CCR0(), and setupTimer1().