-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTM1637.c
112 lines (102 loc) · 2.3 KB
/
TM1637.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
/* FILENAME TM1637.c
* TARGET ATMEL ATMEGA328P
* DATE CREATED 5/14/2020
* AUTHOR NICK FRANGOS
*/
#include "TM1637.h"
// Segment Mapping
const uint8_t segmap[] = {
0x3F, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, // 0-7
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71 // 8-9, A-F
};
void TM_init(void)
{
TM_DDR &= ~(1<<TM_DIO) | (1<<TM_CLK); // set pins to input
TM_PORT |= (1<<TM_DIO) | (1<<TM_CLK); // enable pullup resistors in pull pins to logic HIGH
_delay_us(TM_DELAY);
}
void TM_start(void)
{
TM_diolow();
}
void TM_stop(void)
{
TM_clklow();
TM_diolow();
TM_clkhigh();
TM_diohigh();
}
void TM_send(uint8_t byte)
{
uint8_t data = byte;
for(uint8_t i = 0; i < 8; i++)
{
TM_clklow();
if(data & 0x01)
{
TM_diohigh();
}
else
{
TM_diolow();
}
_delay_us(TM_DELAY);
data = data >> 1;
TM_clkhigh();
}
// Wait for ACK
TM_clklow();
TM_clkhigh();
}
void TM_diolow()
{
TM_DDR |= (1<<TM_DIO); // set DIO pin to output
TM_PORT &= ~(1<<TM_DIO); // set DIO pin to logic LOW
_delay_us(TM_DELAY);
}
void TM_diohigh()
{
TM_DDR &= ~(1<<TM_DIO); // set DIO pin to input to allow pullup resistors to pull to logic HIGH
TM_PORT |= (1<<TM_DIO); // set DIO pin to logic HIGH to enable pull-up resistor
_delay_us(TM_DELAY);
}
void TM_clklow()
{
TM_DDR |= (1<<TM_CLK); // set CLK pin to output
TM_PORT &= ~(1<<TM_CLK); // set CLK pin to logic LOW
_delay_us(TM_DELAY);
}
void TM_clkhigh()
{
TM_DDR &= ~(1<<TM_CLK); // set CLK pin to input to allow pullup resistors to pull to logic HIGH
TM_PORT |= (1<<TM_CLK); // set CLK pin to logic HIGH to enable pull-up resistor
_delay_us(TM_DELAY);
}
void TM_clearDisplay()
{
TM_start();
TM_send(TM_AUTOADDR);
TM_stop();
TM_start();
TM_send(TM_GRID1);
TM_send(0x00);
TM_send(0x00);
TM_send(0x00);
TM_send(0x00);
TM_send(0x00);
TM_send(0x00);
TM_stop();
}
void TM_printValue(uint8_t grid1, uint8_t grid2, uint8_t grid3, uint8_t grid4)
{
TM_start();
TM_send(TM_AUTOADDR);
TM_stop();
TM_start();
TM_send(TM_GRID1);
TM_send(grid1);
TM_send(grid2);
TM_send(grid3);
TM_send(grid4);
TM_stop();
}