-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbg.c
164 lines (150 loc) · 2.39 KB
/
dbg.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
#include "common.h"
#include <avr/io.h>
#include <util/delay.h>
#define DIO PB6
#define CLK PB7
#define DIO_DDR DDB6
#define CLK_DDR DDB7
#define LOW 0
#define HIGH 1
#define IN 0
#define OUT 1
#define CMD_AUTO_ADDR 0x40
#define NUM_DIGITS 4
#define START_ADDR 0xC0
#define DISPLAY_ON 0x88
#define COLON_FLAG 0x80
void bitDelay(void)
{
_delay_us(100);
}
void setClk(int state)
{
if (state == LOW)
PORTB &= ~(1 << CLK);
if (state == HIGH)
PORTB |= (1 << CLK);
}
void setDio(int state)
{
if (state == LOW)
PORTB &= ~(1 << DIO);
if (state == HIGH)
PORTB |= (1 << DIO);
}
void start(void)
{
/*
* When CLK is high, and DIO goes from high to low, input begins
*/
setClk(HIGH);
setDio(HIGH);
bitDelay();
setDio(LOW);
bitDelay();
setClk(LOW);
bitDelay();
}
void stop(void)
{
/*
* When CLK is high, and DIO goes from low to high, input ends
*/
setClk(LOW);
setDio(LOW);
bitDelay();
setClk(HIGH);
bitDelay();
setDio(HIGH);
bitDelay();
}
void write(char data)
{
/*
*Send each bit of data
*/
for (int i = 0; i < 8; i++) {
//transfer data when clock is low, from low bit to high bit
setClk(LOW);
setDio(data & 0x01);
data >>= 1;
bitDelay();
setClk(HIGH);
bitDelay();
}
/*
* End of 8th clock cycle is the start of ACK from TM1637
*/
setClk(LOW);
bitDelay();
setClk(HIGH);
bitDelay();
setClk(LOW);
}
const char displayDigits[16] = {
//
// AAAA
// F B
// F B
// F B
// F B
// GGGG
// E C
// E C
// E C
// E C
// DDDD H
0x3F,
0x06,
0x5B,
0x4F,
0x66,
0x6D,
0x7D,
0x07,
0x7F,
0x6F,
0x77,
0x7C,
0x39,
0x5E,
0x79,
0x71
};
char inline convertChar(char ch) {
return displayDigits[ch];
}
void fourDigit_display(char* digits, int light, bool colon) {
start();
write(CMD_AUTO_ADDR);
stop();
start();
write(START_ADDR);
for (int i = 0; i < NUM_DIGITS; i++) {
char digit_to_write = digits[i];
write(convertChar(digits[i]) | (COLON_FLAG * colon));
}
stop();
start();
//This sets it to the brightest
write(DISPLAY_ON | (light & 0x07));
stop();
}
void dbg_init() {
DDRB |= 1 << CLK_DDR | 1 << DIO_DDR;
}
void dbg_print(unsigned int number) {
char digits[4];
digits[3] = number % 16;
number /= 16;
digits[2] = number % 16;
number /= 16;
digits[1] = number % 16;
number /= 16;
digits[0] = number % 16;
for (unsigned int i = 0; i < 4; ++i) {
digits[i] %= 0x10;
number /= 0x10;
}
fourDigit_display(digits, 0xFF, false);
}