Flipklok
 
Loading...
Searching...
No Matches
dcfReceive.c
Go to the documentation of this file.
1/*
2 * dcfReceive.c
3 *
4 * Created on: 21 mrt. 2025
5 */
10
11#include "dcfReceive.h"
12#include "timeKeeping.h"
13#include "uart.h"
14
18
27{
28 static unsigned int count;
29 static bool syncingStatus = 1;
30 unsigned int timeDiv;
31 if (1 == syncingStatus)
32 {
33 if (interruptEdgeToggleDcf == 0) // Triggered on falling edge, change edge interrupt
34 {
35 startTime = TA1R;
36 countingDirectionStart = toggleTimer1Direction; // store counting direction when start time is taken
37
38 P1IES &= ~BIT5; //Toggle edge interrupt low to high
40 }
41 else // Triggered on falling edge, change edge interrupt and time can be calculated
42 {
43 endTime = TA1R; // Store current time
44 countingDirectionEnd = toggleTimer1Direction; // store counting direction when end time is taken
45
46 P1IES |= BIT5; //Toggle edge interrupt high to low
49
50
51 __no_operation();
53 {
54 __no_operation();
55 syncingStatus = 0; //Sync bit is detected start receiving data bits
56 #ifdef UART_ENABLED
57 UART_SendString("SD\r\n");
58 #endif
59 //This is actually bit 59 not 0
60// storeBit(0, 0); // Currently on top of bit 0, this bit is always 0, so store it directly
61// count = 1; // First bit is already stored so start at the second bit, bit 1.
62 count = 0;
63 P1IES &= ~BIT5; //Toggle edge interrupt low to high
64 }
65 }
66 }
67 else //Sync is detected so readout data bits
68 {
69 if (interruptEdgeToggleDcf == 0) // Triggered on rising edge, change edge interrupt
70 {
71 startTime = TA1R;
72 countingDirectionStart = toggleTimer1Direction; // store counting direction when start time is taken
73
74 P1IES |= BIT5; //Toggle edge interrupt high to low
76// P1OUT |= BIT7; //test pin
77 }
78 else // Triggered on falling edge, change edge interrupt and time can be calculated
79 {
80 endTime = TA1R; // Store current time
81 countingDirectionEnd = toggleTimer1Direction; // store counting direction when end time is taken
82
83 P1IES &= ~BIT5; //Toggle edge interrupt low to high
85// P1OUT &= ~BIT7; //test pin
87 currentBit = bitValue(timeDiv); // Determine current bit value
88 storeBit(currentBit, count);
89 __no_operation();
90 count++;
91 if (bitArrayLength <= count) // Reset count if all bits of a message are received
92 {
93 __no_operation();
94 bool errors = checkBitStream();
95 if (0 == errors)
96 {
97 __no_operation();
99 }
100 __no_operation();
101 P1IES |= BIT5; //Sync timing starts form high to low edge so change edge interrupt high to low
102 syncingStatus = 1; // Start looking for sync again, count will be reseted there
103 }
104 }
105 }
106}
107
120unsigned int calculateBitTransmitTime(bool checkSyncStatus, unsigned int start, bool startDir, unsigned int stop, bool stopDir)
121{
122 __no_operation();
123 if (1 == startDir && 1 == stopDir) //Both on up direction
124 {
125 __no_operation();
126 return stop - start;
127 }
128 else if (0 == startDir && 0 == stopDir) //Both on down direction
129 {
130 __no_operation();
131 return start - stop;
132 }
133 else if (1 == startDir && 0 == stopDir) //start on up and stop on down direction
134 {
135 __no_operation();
136 return (capacityTimer - start) + (capacityTimer - stop);
137 }
138 else if (0 == startDir && 1 == stopDir) //start on down and stop on up direction
139 {
140 __no_operation();
141 return start + stop;
142 }
143
144 __no_operation(); //fallen trough
145 return 0;
146}
147
154bool bitValue(unsigned int bitTime)
155{
156 if (bitTime > bitTimeCycles1Lower && bitTime < bitTimeCycles1Upper)
157 {
158 __no_operation();
159 return 1;
160 }
161 else if (bitTime > bitTimeCycles0Lower && bitTime < bitTimeCycles0Upper)
162 {
163 __no_operation();
164 return 0;
165 }
166
167 __no_operation(); //fall trough
168 return 0;
169
170}
171
178void storeBit(bool bit, unsigned int location)
179{
180 bitArray[location] = bit;
181}
182
189{
190 __no_operation();
191 // First bit needs to be 0, bit 20 needs to be 1, bit 59 needs to be 0
192 if (1 == bitArray[0] || 0 == bitArray[20] || 1 == bitArray[59])
193 {
194 __no_operation();
195 #ifdef UART_ENABLED
196 UART_SendString("RX bit fault\r\n");
198 #endif
199 return 1; //transmission fault,
200 }
201
202 __no_operation();
203 //Check parity minutes
204 unsigned int i;
205 unsigned int onesMinute = 0;
206 for(i = 21; i <= 27; i++) //minutes
207 {
208 if(1 == bitArray[i])
209 {
210 onesMinute++;
211 }
212 }
213
214 onesMinute = onesMinute + (int)bitArray[28]; // Add parity bit to even count, if the count is uneven the pariybit should make it even.
215
216 __no_operation();
217 if (onesMinute % 2) // if odd then transmission fault
218 {
219 __no_operation();
220 #ifdef UART_ENABLED
221 UART_SendString("RX fault no parity minute\r\n");
223 #endif
224 return 1; //transmission fault no even parity minute
225 }
226
227 __no_operation();
228 //Check parity hour
229 unsigned int j;
230 unsigned int onesHour = 0;
231 for(j = 29; j <= 34; j++) //hour
232 {
233 if(1 == bitArray[j])
234 {
235 onesHour++;
236 }
237 }
238
239 onesHour = onesHour + (int)bitArray[35]; // Add parity bit to even count, if the count is uneven the pariybit should make it even.
240
241 __no_operation();
242 if (onesHour % 2) // if odd then transmission fault
243 {
244
245 #ifdef UART_ENABLED
246 UART_SendString("RX fault no parity hour\r\n");
248 #endif
249 return 1; //transmission fault no even parity hour
250 }
251
252 __no_operation();
253 return 0; // No errors found
254 //other parity for date needs no checking because it is not used
255
256}
257
263{
264 static const unsigned int bcdValues[] = {1, 2, 4, 8, 10, 20, 40, 80};
265
266 volatile unsigned int minuteTemp = 0;
267 volatile unsigned int hourTemp = 0;
268
269 bool error = 0;
270
271 unsigned int i;
272
273 //minute
274 for(i = 21; i <= 27; i++) //convert to if else instead of multiply probaly faster
275 {
276 minuteTemp = minuteTemp + (bcdValues[i-21] * bitArray[i]); // Convert from 7 bit BCD to a integer
277 }
278
279 __no_operation();
280 if (minuteTemp > 60) // there can't be more than 60 minutes in a hour
281 {
282 __no_operation();
283 #ifdef UART_ENABLED
284 UART_SendString("More than 60 minutes fault\r\n");
285 #endif
286 error = 1;
287 }
288
289 //hour
290 for(i = 29; i <= 34; i++)
291 {
292 hourTemp = hourTemp + (bcdValues[i-29] * bitArray[i]); // Convert from 6 bit BCD to a integer
293 }
294
295 __no_operation();
296 if (hourTemp > 24) // there can't be more than 24 hours in a day
297 {
298 __no_operation();
299 #ifdef UART_ENABLED
300 UART_SendString("More than 24 hours fault\r\n");
301 #endif
302 error = 1;
303 }
304
305 if (0 == error) // No errors
306 {
307 minuteDcfLast = minuteTemp;
308 hourDcfLast = hourTemp;
309 // Convert to seconds passed midnight
311
312
313 countDcf77Messages++; // Increment counter, used for checking if there was ever a message received, also for statistics
314 timeSinceLastCompleteDcfMessage = 0; // Reset time since last message received, current DCF message syncs the clock
315 #ifdef UART_ENABLED
316 UART_SendString("DCF: ");
318 UART_SendString(":");
320 UART_SendString(", Mech:");
322 UART_SendString("\r\n");
323 #endif
324 }
325 else
326 {
327 #ifdef UART_ENABLED
329 #endif
330 }
331 __no_operation();
332}
unsigned int testbitTimeCyclesStart
Definition dcfReceive.c:15
bool bitValue(unsigned int bitTime)
Determine the bit value of the transmitted DCF77 bit and return it, gets called from interruptDcf()
Definition dcfReceive.c:154
void storeBit(bool bit, unsigned int location)
Store the received DCF77 bit in the array, gets called from interruptDcf()
Definition dcfReceive.c:178
bool checkBitStream()
Checks if certain predetermined bits are 1 or 0 and if parity of minute and hour is correct,...
Definition dcfReceive.c:188
unsigned int calculateBitTransmitTime(bool checkSyncStatus, unsigned int start, bool startDir, unsigned int stop, bool stopDir)
Calculate the bit time of the DCF77 pulses, gets called from interruptDcf()
Definition dcfReceive.c:120
void decodeBitStream2Seconds()
Decode the received DCF77 bitstream to a time, gets called from interruptDcf() Also ensure that the r...
Definition dcfReceive.c:262
unsigned int testbitTimeCyclesStartLower
Definition dcfReceive.c:17
void interruptDcf()
Time the DCF77 pulses gets called from pin interrupt port1ISR(void)
Definition dcfReceive.c:26
unsigned int testbitTimeCyclesStartUpper
Definition dcfReceive.c:16
volatile unsigned int hourDcfLast
Definition dcfReceive.h:47
bool countingDirectionEnd
Definition dcfReceive.h:19
#define bitTimeCyclesSync
Definition dcfReceive.h:42
bool countingDirectionStart
Definition dcfReceive.h:17
unsigned int startTime
Definition dcfReceive.h:16
uint32_t countDcf77Messages
Definition dcfReceive.h:49
volatile unsigned int minuteDcfLast
Definition dcfReceive.h:46
#define bitTimeCycles1Upper
Definition dcfReceive.h:39
bool bitArray[60]
Definition dcfReceive.h:22
bool currentBit
Definition dcfReceive.h:20
unsigned int endTime
Definition dcfReceive.h:18
#define bitTimeCycles0Upper
Definition dcfReceive.h:35
bool interruptEdgeToggleDcf
Definition dcfReceive.h:15
#define bitTimeCyclesSyncLower
Definition dcfReceive.h:44
#define bitArrayLength
Definition dcfReceive.h:21
#define bitTimeCycles1Lower
Definition dcfReceive.h:40
#define bitTimeCyclesSyncUpper
Definition dcfReceive.h:43
#define bitTimeCycles0Lower
Definition dcfReceive.h:36
uint32_t timeOfLastDcfMessage
Definition timeKeeping.h:21
uint32_t timeSinceLastCompleteDcfMessage
Definition timeKeeping.h:22
double mechanicalTimeFloat
Definition timeKeeping.h:40
volatile bool toggleTimer1Direction
Definition timeKeeping.h:46
#define capacityTimer
Definition timer.h:14
void UART_SendInt(int32_t num)
send a 16 bit integer to host system
Definition uart.c:63
void UART_Bitstream(const bool *arr, unsigned int length)
Sends a the received DCF bitstream to host system.
Definition uart.c:154
void UART_SendTime(uint32_t seconds)
Sends a time formated in hours and minutes to host system.
Definition uart.c:131
void UART_SendString(char *str)
send a full string to host system
Definition uart.c:49