-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclock.c
More file actions
368 lines (321 loc) · 10.2 KB
/
clock.c
File metadata and controls
368 lines (321 loc) · 10.2 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "ch32fun.h"
/*=============================================================================
* TM1638 Clock Display - Shows HH:MM:SS time with 1PPS sync
*
* Hardware connections:
* - TM1638 Module: PD0 (CLK), PD3 (DIO), PD2 (STB)
* - 1PPS: PC1 (rising edge interrupt)
* - Buttons: SW1/2 hours, SW3/6 brightness, SW4/5 mins, SW7/8 secs
*===========================================================================*/
// ============= TM1638 Configuration =============
#define TM1638_CLK_PIN 0
#define TM1638_DIO_PIN 3
#define TM1638_STB_PIN 2
#define TM1638_CLK_DELAY_US 5
#define TM1638_CMD_DATA 0x40
#define TM1638_CMD_DISPLAY 0x80
#define TM1638_CMD_ADDRESS 0xC0
// ============= 1PPS Configuration =============
#define PPS_PIN 1 // PC1
// ============= Timing Configuration =============
#define MAIN_LOOP_DELAY_MS 1
// ============= Global State Variables =============
uint8_t hours = 12;
uint8_t minutes = 0;
uint8_t seconds = 0;
volatile uint8_t pps_count = 0;
uint8_t last_buttons = 0;
uint32_t button_hold_counter = 0;
uint8_t display_buffer[16];
uint8_t brightness = 2; // 0-7, where 0 is dimmest
uint8_t led_pos = 0; // Current LED position (0-7)
int8_t led_dir = 1; // LED direction: 1 = right, -1 = left
// ============= Function Prototypes =============
void tm1638_send_command(uint8_t cmd);
void tm1638_clear_display(void);
void tm1638_send_byte(uint8_t data);
uint8_t tm1638_read_byte(void);
void tm1638_update_clock_display(void);
void tm1638_set_brightness(uint8_t level);
void advance_led(void);
// 7-segment digit patterns (0-9)
const uint8_t digit_segments[] = {
0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F, // 9
};
// Delay functions
void delay_us(uint32_t us) {
uint32_t i;
for (i = 0; i < us * 8; i++) {
__asm__("nop");
}
}
void delay_ms(uint32_t ms) {
while (ms--) {
delay_us(1000);
}
}
// 1PPS interrupt handler
void EXTI7_0_IRQHandler(void) __attribute__((interrupt));
void EXTI7_0_IRQHandler(void) {
if (EXTI->INTFR & (1 << PPS_PIN)) {
EXTI->INTFR = (1 << PPS_PIN); // Clear interrupt flag
pps_count++;
}
}
// Initialize 1PPS input with interrupt
void pps_init(void) {
// Enable GPIOC and AFIO clocks
RCC->APB2PCENR |= RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO;
// Configure PC1 as input with pull-down
GPIOC->CFGLR &= ~(0xF << (PPS_PIN * 4));
GPIOC->CFGLR |= (GPIO_CFGLR_IN_PUPD << (PPS_PIN * 4));
GPIOC->BCR = (1 << PPS_PIN); // Pull-down
// Configure EXTI1 for PC1 (Port C)
AFIO->EXTICR = (AFIO->EXTICR & ~(0x3 << (PPS_PIN * 2))) |
(0x2 << (PPS_PIN * 2));
// Configure EXTI for rising edge
EXTI->INTENR |= (1 << PPS_PIN); // Enable interrupt
EXTI->RTENR |= (1 << PPS_PIN); // Rising edge trigger
EXTI->FTENR &= ~(1 << PPS_PIN); // Disable falling edge
// Enable EXTI interrupt in NVIC
NVIC_EnableIRQ(EXTI7_0_IRQn);
}
// TM1638 functions
void tm1638_init(void) {
RCC->APB2PCENR |= RCC_APB2Periph_GPIOD;
// Configure pins as outputs
GPIOD->CFGLR &= ~(0xF << (TM1638_CLK_PIN*4) |
0xF << (TM1638_DIO_PIN*4) |
0xF << (TM1638_STB_PIN*4));
GPIOD->CFGLR |= (GPIO_CFGLR_OUT_10Mhz_PP << (TM1638_CLK_PIN*4)) |
(GPIO_CFGLR_OUT_10Mhz_PP << (TM1638_DIO_PIN*4)) |
(GPIO_CFGLR_OUT_10Mhz_PP << (TM1638_STB_PIN*4));
// Set initial states
GPIOD->BSHR = (1 << TM1638_STB_PIN) | (1 << TM1638_CLK_PIN);
delay_us(10);
// Configure display
tm1638_send_command(TM1638_CMD_DATA | 0x00); // Normal mode, auto address
tm1638_set_brightness(brightness);
tm1638_clear_display();
}
void tm1638_send_byte(uint8_t data) {
uint8_t i;
for (i = 0; i < 8; i++) {
GPIOD->BCR = (1 << TM1638_CLK_PIN);
if (data & 0x01) {
GPIOD->BSHR = (1 << TM1638_DIO_PIN);
} else {
GPIOD->BCR = (1 << TM1638_DIO_PIN);
}
data >>= 1;
delay_us(TM1638_CLK_DELAY_US);
GPIOD->BSHR = (1 << TM1638_CLK_PIN);
delay_us(TM1638_CLK_DELAY_US);
}
}
uint8_t tm1638_read_byte(void) {
uint8_t i, data = 0;
// Configure DIO as input
GPIOD->CFGLR &= ~(0xF << (TM1638_DIO_PIN*4));
GPIOD->CFGLR |= (GPIO_CFGLR_IN_FLOAT << (TM1638_DIO_PIN*4));
for (i = 0; i < 8; i++) {
GPIOD->BCR = (1 << TM1638_CLK_PIN);
delay_us(TM1638_CLK_DELAY_US);
data >>= 1;
if (GPIOD->INDR & (1 << TM1638_DIO_PIN)) {
data |= 0x80;
}
GPIOD->BSHR = (1 << TM1638_CLK_PIN);
delay_us(TM1638_CLK_DELAY_US);
}
// Configure DIO back to output
GPIOD->CFGLR &= ~(0xF << (TM1638_DIO_PIN*4));
GPIOD->CFGLR |= (GPIO_CFGLR_OUT_10Mhz_PP << (TM1638_DIO_PIN*4));
return data;
}
void tm1638_send_command(uint8_t cmd) {
GPIOD->BCR = (1 << TM1638_STB_PIN);
tm1638_send_byte(cmd);
GPIOD->BSHR = (1 << TM1638_STB_PIN);
}
void tm1638_set_brightness(uint8_t level) {
if (level > 7) {
level = 7;
}
brightness = level;
tm1638_send_command(TM1638_CMD_DISPLAY | 0x08 | brightness);
}
void tm1638_clear_display(void) {
uint8_t i;
GPIOD->BCR = (1 << TM1638_STB_PIN);
tm1638_send_byte(TM1638_CMD_ADDRESS);
for (i = 0; i < 16; i++) {
tm1638_send_byte(0x00);
}
GPIOD->BSHR = (1 << TM1638_STB_PIN);
}
uint8_t tm1638_read_buttons(void) {
uint8_t i, buttons = 0;
GPIOD->BCR = (1 << TM1638_STB_PIN);
tm1638_send_byte(0x42); // Read key scan data
for (i = 0; i < 4; i++) {
uint8_t keys = tm1638_read_byte();
if (keys & 0x01) {
buttons |= (1 << (i*2));
}
if (keys & 0x10) {
buttons |= (1 << (i*2 + 1));
}
}
GPIOD->BSHR = (1 << TM1638_STB_PIN);
return buttons;
}
void tm1638_update_clock_display(void) {
uint8_t i;
// Display format: HH. MM. SS (dots and spaces as separators)
// Positions: 0=H1, 1=H2, 2=space, 3=M1, 4=M2, 5=space, 6=S1, 7=S2
display_buffer[0] = digit_segments[hours / 10];
display_buffer[2] = digit_segments[hours % 10];
display_buffer[4] = 0x00; // Space
display_buffer[6] = digit_segments[minutes / 10];
display_buffer[8] = digit_segments[minutes % 10];
display_buffer[10] = 0x00; // Space
display_buffer[12] = digit_segments[seconds / 10];
display_buffer[14] = digit_segments[seconds % 10];
// LED addresses (odd bytes) - bouncing light using persistent state
for (i = 0; i < 8; i++) {
display_buffer[i*2 + 1] = (i == led_pos) ? 0x01 : 0x00;
}
// Send all data
GPIOD->BCR = (1 << TM1638_STB_PIN);
tm1638_send_byte(TM1638_CMD_ADDRESS);
for (i = 0; i < 16; i++) {
tm1638_send_byte(display_buffer[i]);
}
GPIOD->BSHR = (1 << TM1638_STB_PIN);
}
void advance_led(void) {
led_pos += led_dir;
if (led_pos >= 7) {
led_pos = 7;
led_dir = -1;
} else if (led_pos == 0) {
led_dir = 1;
}
}
void process_buttons(uint8_t buttons) {
// Button mapping:
// SW1 (0x01): hours -1 SW2 (0x04): hours +1
// SW3 (0x10): brightness - SW4 (0x40): minutes -1
// SW5 (0x02): minutes +1 SW6 (0x08): brightness +
// SW7 (0x20): seconds -1 SW8 (0x80): seconds +1
if (buttons & 0x01) { // SW1: hours -1
if (hours > 0) {
hours--;
} else {
hours = 23;
}
}
if (buttons & 0x04) { // SW2: hours +1
hours = (hours + 1) % 24;
}
if (buttons & 0x10) { // SW3: brightness -
if (brightness > 0) {
tm1638_set_brightness(brightness - 1);
}
}
if (buttons & 0x40) { // SW4: minutes -1
if (minutes > 0) {
minutes--;
} else {
minutes = 59;
}
}
if (buttons & 0x02) { // SW5: minutes +1
minutes = (minutes + 1) % 60;
}
if (buttons & 0x08) { // SW6: brightness +
if (brightness < 7) {
tm1638_set_brightness(brightness + 1);
}
}
if (buttons & 0x20) { // SW7: seconds -1
if (seconds > 0) {
seconds--;
} else {
seconds = 59;
}
}
if (buttons & 0x80) { // SW8: seconds +1
seconds = (seconds + 1) % 60;
}
}
int main(void) {
SystemInit();
delay_us(10000);
pps_init();
tm1638_init();
tm1638_update_clock_display();
// Main loop
uint8_t button_poll_counter = 0;
while (1) {
// Read buttons every 10ms to reduce I/O
button_poll_counter++;
if (button_poll_counter >= 10) {
button_poll_counter = 0;
uint8_t buttons = tm1638_read_buttons();
// Process button presses
if (buttons != last_buttons) {
if (buttons) {
process_buttons(buttons);
tm1638_update_clock_display();
button_hold_counter = 0;
}
last_buttons = buttons;
} else if (buttons) {
// Button held - repeat action
button_hold_counter++;
if (button_hold_counter > 50) { // 500ms delay (50 * 10ms)
// Repeat every 100ms
if ((button_hold_counter % 10) == 0) {
process_buttons(buttons);
tm1638_update_clock_display();
}
}
}
}
// Check for 1PPS pulse(s) with IRQ protection
__disable_irq();
uint8_t pending = pps_count;
pps_count = 0;
__enable_irq();
if (pending) {
while (pending--) {
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
if (hours >= 24) {
hours = 0;
}
}
}
advance_led();
}
tm1638_update_clock_display();
}
delay_ms(MAIN_LOOP_DELAY_MS);
}
}