-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
262 lines (252 loc) · 6.18 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
MIT License
Copyright (c) 2020 Carsten1987
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <xc.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "bsp.h"
#include "device_config.h"
#include "dcf77_decoder.h"
#include "segment_display.h"
#include "print_date.h"
// Print Time via UART
#define PRINT_TIME
// Load Time via SPI to connected Displays
#define SPI_TIME
// Pin for Output-Enable of Shift-Registers
#define PIN_OUTPUT_ENABLE RB1
// Pin for Loading Day-Of-Week-Data
#define PIN_DOF RB2
// Pin for Loading Date-Data
#define PIN_DATE RB3
// Pin for Loading Time-Data
#define PIN_TIME RB4
// Pin for Clear Shift Register
#define PIN_CLAER_SR RB5
// Minimal time for 0 Pulse in ms
#define IMPULS_0_MIN 40u
// Maximal time for 0 Pulse in ms
#define IMPULS_0_MAX 130u
// Minimal time for 1 Pulse in ms
#define IMPULS_1_MIN 140u
// Maximal time for 1 Pulse in ms
#define IMPULS_1_MAX 230u
#ifdef PRINT_TIME
static char buffer[40] = { 0 };
static volatile uint8_t in = 0;
static volatile uint8_t out = 0;
#endif
static volatile uint16_t ms_counter = 0;
static volatile uint16_t new_value = 0;
static volatile uint16_t pause = 0;
static volatile uint8_t bit_counter = 0;
static volatile uint16_t milli_seconds = 0;
static volatile uint8_t seconds = 0;
static time current_time = {
.minutes = 0,
.hours = 0,
.day_of_week = 1,
.day = 1,
.month = 1,
.year = 20,
};
#ifdef SPI_TIME
static uint8_t date_segment = 0;
static uint8_t day_segment = 0;
static uint8_t spi_buffer[11] = { 0 };
static uint8_t out_pos = 0;
#endif
void __interrupt() isr()
{
if (TMR1IF)
{ // 1ms ISR
TMR1IF = 0;
TMR1H = 0xF6;
TMR1L = 0x3C;
ms_counter++;
milli_seconds++;
}
if(RCIF && RCIE)
{ // received UART char
/* code */
}
#ifdef PRINT_TIME
if(TXIF && TXIE)
{ // tx buffer empty. Next char can be send
TXREG = buffer[out++];
if(out == sizeof(buffer))
{
out = 0;
}
if(out == in)
{
TXIE = 0;
}
}
#endif
if(INTF)
{ // RBO Interrupt detected
INTF = 0;
if(INTEDG)
{
INTEDG = 0;
pause = ms_counter;
}
else
{
INTEDG = 1;
new_value = ms_counter;
}
ms_counter = 0;
}
}
void main(void)
{
bool new_time = false;
init();
PIN_OUTPUT_ENABLE = 0; // enable all outputs
PIN_CLAER_SR = 1; // do not clear SRs
#ifdef PRINT_TIME
(void)strcpy(buffer, "Initialization successfully done\n");
in = (uint8_t)strlen(buffer);
TXREG = buffer[out++];
TXIE = 1;
#else
TXREG = 'I';
#endif
while(1)
{
// check if Bit 59 is detected (Bit 59 missing)
if(pause > 1000u)
{ // Start new Sequence
pause = 0u;
minute_gone(bit_counter);
bit_counter = 0u;
milli_seconds = 0u;
seconds = 0u;
new_time = true;
}
if(milli_seconds >= 1000u)
{
milli_seconds = 0u;
seconds++;
}
if(seconds == 60u)
{ // No signal detected. Do Manual Minute counting
seconds = 0u;
minute_gone(0u);
new_time = true;
}
if(new_time)
{ // Read out new Time from DCF77 Module
get_time(¤t_time);
#ifdef PRINT_TIME
in = make_time(buffer, ¤t_time);
out = 0u;
TXREG = buffer[out++];
TXIE = 1u;
#endif
new_time = false;
}
// check length of Pulse and store information
if((new_value >= IMPULS_0_MIN) && (new_value <= IMPULS_0_MAX))
{
new_value = 0u;
new_bit(bit_counter++, 0u);
}
else if((new_value >= IMPULS_1_MIN) && (new_value <= IMPULS_1_MAX))
{
new_value = 0u;
new_bit(bit_counter++, 1u);
}
else
{
// do nothing
}
#ifdef SPI_TIME
if(SSPIF) // wait until transmission completed
{
WCOL = 0u;
SSPIF = 0u;
if(out_pos == 0u)
{
PIN_DOF = 1u;
PIN_TIME = 0u;
}
else if(out_pos == 6u)
{
PIN_TIME = 1;
PIN_DATE = 0;
}
else if(out_pos == 8u)
{
PIN_DATE = 1u;
PIN_DOF = 0u;
}
else
{
// do nothing
}
SSPBUF = spi_buffer[out_pos++];
if(out_pos == sizeof(spi_buffer))
{
out_pos = 0;
}
/*
0-5 = Zeit
6-7 = Datum
8-10 = Tag
*/
if(out_pos == 6u)
{
spi_buffer[0] = get_time_data(¤t_time, 0u);
spi_buffer[1] = get_time_data(¤t_time, 1u);
spi_buffer[2] = get_time_data(¤t_time, 2u);
spi_buffer[3] = get_time_data(¤t_time, 3u);
spi_buffer[4] = get_time_data_seconds(seconds, 4u);
spi_buffer[5] = get_time_data_seconds(seconds, 5u);
}
else if(out_pos == 8u)
{
spi_buffer[6] = (uint8_t)(1u << date_segment);
spi_buffer[7] = get_date_data(¤t_time, date_segment++);
if(date_segment == 8u)
{
date_segment = 0u;
}
}
else if(out_pos == 0u)
{
spi_buffer[8] = (uint8_t)(1u << day_segment);
spi_buffer[9] = get_day_data(¤t_time, day_segment, 0u);
spi_buffer[10] = get_day_data(¤t_time, day_segment++, 1u);
if(day_segment == 3u)
{
day_segment = 0u;
}
}
else
{
// do nothing
}
}
#endif
}
}