Subversion Repositories Code-Repo

Rev

Rev 329 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
329 Kevin 1
#include "HT16K33.h"
2
#include "I2C1.h"
3
 
4
static const uint8_t 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 uint8_t 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_p;
27
 
28
void LED_Init(LED_DATA *data) {
29
    led_data_p = data;
30
 
31
    led_data_p->i2c_address = HT16K33_ADDRESS;
32
}
33
 
34
void LED_Start() {
35
    uint8_t c = 0x21;     // Cmd to turn on oscillator
36
 
37
    I2C1_Master_Send(led_data_p->i2c_address, &c, 1);
38
    uint8_t result = I2C1_Get_Status();
39
    while (!result) {
40
        result = I2C1_Get_Status();
41
    }
42
 
43
    LED_Blink_Rate(HT16K33_BLINK_OFF);
44
    LED_Set_Brightness(15);  // Max brightness
45
    LED_Clear();
46
    LED_Write_Display();
47
}
48
 
49
void LED_Set_Brightness(uint8_t c) {
50
    if (c > 15) c = 15;
51
    c |= 0xE0;
52
 
53
    I2C1_Master_Send(led_data_p->i2c_address, &c, 1);
54
    uint8_t result = I2C1_Get_Status();
55
    while (!result) {
56
        result = I2C1_Get_Status();
57
    }
58
}
59
 
60
void LED_Blink_Rate(uint8_t c) {
61
    uint8_t buffer;
62
 
63
    if (c > 3) c = 0;
64
 
65
    buffer = HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (c << 1);
66
 
67
    I2C1_Master_Send(led_data_p->i2c_address, &buffer, 1);
68
    buffer = I2C1_Get_Status();
69
    while (!buffer) {
70
        buffer = I2C1_Get_Status();
71
    }
72
}
73
 
74
void LED_Write_Display() {
75
    led_data_p->display_buffer[0] = 0x00;  // Start at address 0x00
76
    I2C1_Master_Send(led_data_p->i2c_address, led_data_p->display_buffer, 17);
77
 
78
    uint8_t result = I2C1_Get_Status();
79
    while (!result) {
80
        result = I2C1_Get_Status();
81
    }
82
}
83
 
84
void LED_Clear() {
85
    for (uint8_t c = 0; c < 17; c++) {
86
        led_data_p->display_buffer[c] = 0;
87
    }
88
}
89
 
90
void LED_Draw_Colon(uint8_t c) {
91
    if (c) {
92
        led_data_p->display_buffer[5] = 0xFF;
93
    } else {
94
        led_data_p->display_buffer[5] = 0;
95
    }
96
}
97
 
98
void LED_Write_Digit_Raw(uint8_t loc, uint8_t bitmask) {
99
    if (loc > 4) return;
100
    led_data_p->display_buffer[(loc<<1)+1] = bitmask;
101
}
102
 
103
void LED_Write_Digit_Num(uint8_t loc, uint8_t num, uint8_t dot) {
104
    if (loc > 4) return;
105
    if (loc > 1) loc++;
106
    LED_Write_Digit_Raw(loc, numbertable[num] | dot << 7);
107
}
108
 
109
void LED_Write_Digit_Alpha(uint8_t loc, uint8_t alpha, uint8_t dot) {
110
    if (loc > 4) return;
111
    if (loc > 1) loc++;
112
    LED_Write_Digit_Raw(loc, alphatable[alpha] | dot << 7);
113
}
114
 
115
void LED_Write_Num(uint16_t i) {
116
    LED_Write_Digit_Num(0, (i%10000)/1000, 0);
117
    LED_Write_Digit_Num(1, (i%1000)/100, 0);
118
    LED_Write_Digit_Num(2, (i%100)/10, 0);
119
    LED_Write_Digit_Num(3, i%10, 0);
120
 
121
    if (i < 10) {
122
        LED_Write_Digit_Raw(0, 0);
123
        LED_Write_Digit_Raw(1, 0);
124
        LED_Write_Digit_Raw(3, 0);
125
    } else if (i < 100) {
126
        LED_Write_Digit_Raw(0, 0);
127
        LED_Write_Digit_Raw(1, 0);
128
    } else if (i < 1000) {
129
        LED_Write_Digit_Raw(0, 0);
130
    }
131
    LED_Write_Display();
132
}