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
 
154 Kevin 43
    LED_Blink_Rate(HT16K33_BLINK_OFF);
44
    LED_Set_Brightness(15);  // Max brightness
45
    LED_Clear();
46
    LED_Write_Display();
147 Kevin 47
}
48
 
154 Kevin 49
void LED_Set_Brightness(unsigned char c) {
147 Kevin 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
 
154 Kevin 62
void LED_Blink_Rate(unsigned char c) {
147 Kevin 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
 
154 Kevin 76
void LED_Write_Display() {
147 Kevin 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
 
154 Kevin 88
void LED_Clear() {
147 Kevin 89
    unsigned char c;
90
    for (c = 0; c < 17; c++) {
91
        led_data_p->display_buffer[c] = 0;
92
    }
93
}
94
 
154 Kevin 95
void LED_Draw_Colon(unsigned char c) {
147 Kevin 96
    if (c) {
97
        led_data_p->display_buffer[5] = 0xFF;
98
    } else {
99
        led_data_p->display_buffer[5] = 0;
100
    }
101
}
102
 
154 Kevin 103
void LED_Write_Digit_Raw(unsigned char loc, unsigned char bitmask) {
147 Kevin 104
    if (loc > 4) return;
105
    led_data_p->display_buffer[(loc<<1)+1] = bitmask;
106
}
107
 
154 Kevin 108
void LED_Write_Digit_Num(unsigned char loc, unsigned char num, unsigned char dot) {
147 Kevin 109
    if (loc > 4) return;
110
    if (loc > 1) loc++;
154 Kevin 111
    LED_Write_Digit_Raw(loc, numbertable[num] | dot << 7);
147 Kevin 112
}
113
 
154 Kevin 114
void LED_Write_Digit_Alpha(unsigned char loc, unsigned char alpha, unsigned char dot) {
147 Kevin 115
    if (loc > 4) return;
116
    if (loc > 1) loc++;
154 Kevin 117
    LED_Write_Digit_Raw(loc, alphatable[alpha] | dot << 7);
147 Kevin 118
}
119
 
154 Kevin 120
void LED_Write_Num(unsigned int i) {
121
    LED_Write_Digit_Num(0, (i%10000)/1000, 0);
122
    LED_Write_Digit_Num(1, (i%1000)/100, 0);
123
    LED_Write_Digit_Num(2, (i%100)/10, 0);
124
    LED_Write_Digit_Num(3, i%10, 0);
147 Kevin 125
 
126
    if (i < 10) {
154 Kevin 127
        LED_Write_Digit_Raw(0, 0);
128
        LED_Write_Digit_Raw(1, 0);
129
        LED_Write_Digit_Raw(3, 0);
147 Kevin 130
    } else if (i < 100) {
154 Kevin 131
        LED_Write_Digit_Raw(0, 0);
132
        LED_Write_Digit_Raw(1, 0);
147 Kevin 133
    } else if (i < 1000) {
154 Kevin 134
        LED_Write_Digit_Raw(0, 0);
147 Kevin 135
    }
154 Kevin 136
    LED_Write_Display();
147 Kevin 137
}