Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
147 Kevin 1
#include "led_HT16K33.h"
2
#include "i2c.h"
3
 
4
static const char numbertable[] = {
5
    0x3F /* 0 */,
6
    0x06 /* 1 */,
7
    0x5B /* 2 */,
8
    0x4F /* 3 */,
9
    0x66 /* 4 */,
10
    0x6D /* 5 */,
11
    0x7D, /* 6 */
12
    0x07, /* 7 */
13
    0x7F, /* 8 */
14
    0x6F, /* 9 */
15
};
16
 
17
static const char alphatable[] = {
18
    0x77, /* a */
19
    0x7C, /* b */
20
    0x39, /* C */
21
    0x5E, /* d */
22
    0x79, /* E */
23
    0x71, /* F */
24
};
25
 
26
static LED_DATA led_data;
27
static LED_DATA *led_data_p = &led_data;
28
 
29
void LED_Init() {
30
    led_data_p->i2c_address = 0x70;
31
}
32
 
33
void LED_Start() {
34
    unsigned char result;
35
    unsigned char c = 0x21;     // Cmd to turn on oscillator
36
 
37
    I2C_Master_Send(led_data_p->i2c_address, 1, &c);
38
    result = I2C_Get_Status();
39
    while (!result) {
40
        result = I2C_Get_Status();
41
    }
42
 
43
    LED_blinkRate(HT16K33_BLINK_OFF);
44
    LED_setBrightness(15);  // Max brightness
45
    LED_clear();
46
    LED_writeDisplay();
47
}
48
 
49
void LED_setBrightness(unsigned char c) {
50
    unsigned char result;
51
 
52
    if (c > 15) c = 15;
53
    c |= 0xE0;
54
 
55
    I2C_Master_Send(led_data_p->i2c_address, 1, &c);
56
    result = I2C_Get_Status();
57
    while (!result) {
58
        result = I2C_Get_Status();
59
    }
60
}
61
 
62
void LED_blinkRate(unsigned char c) {
63
    unsigned char buffer;
64
 
65
    if (c > 3) c = 0;
66
 
67
    buffer = HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (c << 1);
68
 
69
    I2C_Master_Send(led_data_p->i2c_address, 1, &buffer);
70
    buffer = I2C_Get_Status();
71
    while (!buffer) {
72
        buffer = I2C_Get_Status();
73
    }
74
}
75
 
76
void LED_writeDisplay() {
77
    unsigned char result;
78
 
79
    led_data_p->display_buffer[0] = 0x00;  // Start at address 0x00
80
    I2C_Master_Send(led_data_p->i2c_address, 17, led_data_p->display_buffer);
81
 
82
    result = I2C_Get_Status();
83
    while (!result) {
84
        result = I2C_Get_Status();
85
    }
86
}
87
 
88
void LED_clear() {
89
    unsigned char c;
90
    for (c = 0; c < 17; c++) {
91
        led_data_p->display_buffer[c] = 0;
92
    }
93
}
94
 
95
void LED_drawColon(unsigned char c) {
96
    if (c) {
97
        led_data_p->display_buffer[5] = 0xFF;
98
    } else {
99
        led_data_p->display_buffer[5] = 0;
100
    }
101
}
102
 
103
void LED_writeDigitRaw(unsigned char loc, unsigned char bitmask) {
104
    if (loc > 4) return;
105
    led_data_p->display_buffer[(loc<<1)+1] = bitmask;
106
}
107
 
108
void LED_writeDigitNum(unsigned char loc, unsigned char num, unsigned char dot) {
109
    if (loc > 4) return;
110
    if (loc > 1) loc++;
111
    LED_writeDigitRaw(loc, numbertable[num] | dot << 7);
112
}
113
 
114
void LED_writeDigitAlpha(unsigned char loc, unsigned char alpha, unsigned char dot) {
115
    if (loc > 4) return;
116
    if (loc > 1) loc++;
117
    LED_writeDigitRaw(loc, alphatable[alpha] | dot << 7);
118
}
119
 
120
void LED_writeNum(unsigned int i) {
121
    LED_writeDigitNum(0, (i%10000)/1000, 0);
122
    LED_writeDigitNum(1, (i%1000)/100, 0);
123
    LED_writeDigitNum(2, (i%100)/10, 0);
124
    LED_writeDigitNum(3, i%10, 0);
125
 
126
    if (i < 10) {
127
        LED_writeDigitRaw(0, 0);
128
        LED_writeDigitRaw(1, 0);
129
        LED_writeDigitRaw(3, 0);
130
    } else if (i < 100) {
131
        LED_writeDigitRaw(0, 0);
132
        LED_writeDigitRaw(1, 0);
133
    } else if (i < 1000) {
134
        LED_writeDigitRaw(0, 0);
135
    }
136
    LED_writeDisplay();
137
}