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

DCF77 receiving and decoding implementation. More...

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

Go to the source code of this file.

Functions

void interruptDcf ()
 Time the DCF77 pulses gets called from pin interrupt port1ISR(void)
 
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()
 
bool bitValue (unsigned int bitTime)
 Determine the bit value of the transmitted DCF77 bit and return it, gets called from interruptDcf()
 
void storeBit (bool bit, unsigned int location)
 Store the received DCF77 bit in the array, gets called from interruptDcf()
 
bool checkBitStream ()
 Checks if certain predetermined bits are 1 or 0 and if parity of minute and hour is correct, gets called from interruptDcf()
 
void decodeBitStream2Seconds ()
 Decode the received DCF77 bitstream to a time, gets called from interruptDcf() Also ensure that the received time is plausible by a couple of checks.
 

Variables

unsigned int testbitTimeCyclesStart = bitTimeCyclesSync
 
unsigned int testbitTimeCyclesStartUpper = bitTimeCyclesSyncUpper
 
unsigned int testbitTimeCyclesStartLower = bitTimeCyclesSyncLower
 

Detailed Description

DCF77 receiving and decoding implementation.

Definition in file dcfReceive.c.

Function Documentation

◆ bitValue()

bool bitValue ( unsigned int bitTime)

Determine the bit value of the transmitted DCF77 bit and return it, gets called from interruptDcf()

Parameters
bitTimeTime of the bit signal in timer cycles
Returns
true (1) if bit is 1, false (0) if bit is 0

Definition at line 154 of file dcfReceive.c.

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}
#define bitTimeCycles1Upper
Definition dcfReceive.h:39
#define bitTimeCycles0Upper
Definition dcfReceive.h:35
#define bitTimeCycles1Lower
Definition dcfReceive.h:40
#define bitTimeCycles0Lower
Definition dcfReceive.h:36

References bitTimeCycles0Lower, bitTimeCycles0Upper, bitTimeCycles1Lower, and bitTimeCycles1Upper.

Referenced by interruptDcf().

◆ calculateBitTransmitTime()

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

Calculate the time it took between the rising and falling edge of the DCF77 pulse Take in to account the up and down behavior of the timer

Parameters
checkSyncStatus(unused in this function).
startThe start point of the transmission on the timer up/down counter.
startDirThe direction of the start point (1 for up, 0 for down).
stopThe stop point of the transmission.
stopDirThe direction of the stop point (1 for up, 0 for down).
Returns
unsigned int of the time

Definition at line 120 of file dcfReceive.c.

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}
#define capacityTimer
Definition timer.h:14

References capacityTimer.

Referenced by interruptDcf().

◆ checkBitStream()

bool checkBitStream ( )

Checks if certain predetermined bits are 1 or 0 and if parity of minute and hour is correct, gets called from interruptDcf()

Returns
bool of the transmission fault, 0 is no fault

Definition at line 188 of file dcfReceive.c.

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}
bool bitArray[60]
Definition dcfReceive.h:22
void UART_Bitstream(const bool *arr, unsigned int length)
Sends a the received DCF bitstream to host system.
Definition uart.c:154
void UART_SendString(char *str)
send a full string to host system
Definition uart.c:49

References bitArray, UART_Bitstream(), and UART_SendString().

Referenced by interruptDcf().

◆ decodeBitStream2Seconds()

void decodeBitStream2Seconds ( )

Decode the received DCF77 bitstream to a time, gets called from interruptDcf() Also ensure that the received time is plausible by a couple of checks.

Definition at line 262 of file dcfReceive.c.

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}
volatile unsigned int hourDcfLast
Definition dcfReceive.h:47
uint32_t countDcf77Messages
Definition dcfReceive.h:49
volatile unsigned int minuteDcfLast
Definition dcfReceive.h:46
uint32_t timeOfLastDcfMessage
Definition timeKeeping.h:21
uint32_t timeSinceLastCompleteDcfMessage
Definition timeKeeping.h:22
double mechanicalTimeFloat
Definition timeKeeping.h:40
void UART_SendInt(int32_t num)
send a 16 bit integer to host system
Definition uart.c:63
void UART_SendTime(uint32_t seconds)
Sends a time formated in hours and minutes to host system.
Definition uart.c:131

References bitArray, countDcf77Messages, hourDcfLast, mechanicalTimeFloat, minuteDcfLast, timeOfLastDcfMessage, timeSinceLastCompleteDcfMessage, UART_Bitstream(), UART_SendInt(), UART_SendString(), and UART_SendTime().

Referenced by interruptDcf().

◆ interruptDcf()

void interruptDcf ( )

Time the DCF77 pulses gets called from pin interrupt port1ISR(void)

Wait first for sync pulse than read out each of 59 bits Determine the value of the bit with: bool bitValue() based on the calculated transmit time with: unsigned int calculateBitTransmitTime() Store the result via: storeBit(), when all 59 bits are received check with: checkBitStream(), and decode to time: decodeBitStream2Seconds()

Definition at line 26 of file dcfReceive.c.

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}
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
unsigned int testbitTimeCyclesStartUpper
Definition dcfReceive.c:16
bool countingDirectionEnd
Definition dcfReceive.h:19
bool countingDirectionStart
Definition dcfReceive.h:17
unsigned int startTime
Definition dcfReceive.h:16
bool currentBit
Definition dcfReceive.h:20
unsigned int endTime
Definition dcfReceive.h:18
bool interruptEdgeToggleDcf
Definition dcfReceive.h:15
#define bitArrayLength
Definition dcfReceive.h:21
volatile bool toggleTimer1Direction
Definition timeKeeping.h:46

References bitArrayLength, bitValue(), calculateBitTransmitTime(), checkBitStream(), countingDirectionEnd, countingDirectionStart, currentBit, decodeBitStream2Seconds(), endTime, interruptEdgeToggleDcf, startTime, storeBit(), testbitTimeCyclesStartLower, testbitTimeCyclesStartUpper, toggleTimer1Direction, and UART_SendString().

Referenced by port1ISR().

◆ storeBit()

void storeBit ( bool bit,
unsigned int location )

Store the received DCF77 bit in the array, gets called from interruptDcf()

Parameters
[in]bitThe value of the bit
[in]locationThe location of the array to store the bit

Definition at line 178 of file dcfReceive.c.

179{
180 bitArray[location] = bit;
181}

References bitArray.

Referenced by interruptDcf().

Variable Documentation

◆ testbitTimeCyclesStart

unsigned int testbitTimeCyclesStart = bitTimeCyclesSync

Definition at line 15 of file dcfReceive.c.

◆ testbitTimeCyclesStartLower

unsigned int testbitTimeCyclesStartLower = bitTimeCyclesSyncLower

Definition at line 17 of file dcfReceive.c.

Referenced by interruptDcf().

◆ testbitTimeCyclesStartUpper

unsigned int testbitTimeCyclesStartUpper = bitTimeCyclesSyncUpper

Definition at line 16 of file dcfReceive.c.

Referenced by interruptDcf().