This repository has been archived by the owner on Oct 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
ht162.c
129 lines (106 loc) · 3.3 KB
/
ht162.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
/*
* LCD3 firmware
*
* Copyright (C) Casainho, 2018.
*
* Released under the GPL License, Version 3
*/
#include <string.h>
#include "stm8s.h"
#include "stm8s_gpio.h"
#include "gpio.h"
#include "pins.h"
#include "timers.h"
#include "ht162.h"
#include "lcd.h"
void ht1622_send_bits(uint16_t ui16_data, uint8_t ui8_bits);
void ht1622_send_command(uint8_t command);
void ht1622_init (void)
{
GPIO_WriteHigh(GPIOB, GPIO_PIN_4); // enable VDD to HT1622 ??
// GPIO_WriteHigh(GPIOE, GPIO_PIN_3); // only needed for 24V battery pack
GPIO_Init(LCD3_ONOFF_POWER__PORT,
LCD3_ONOFF_POWER__PIN,
GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_WriteHigh(LCD3_ONOFF_POWER__PORT, LCD3_ONOFF_POWER__PIN);
GPIO_WriteHigh(LCD3_ENABLE_BACKLIGHT_POWER__PORT, LCD3_ENABLE_BACKLIGHT_POWER__PIN);
GPIO_WriteLow(LCD3_ENABLE_BACKLIGHT__PORT, LCD3_ENABLE_BACKLIGHT__PIN);
GPIO_Init(LCD3_HT1622_CS__PORT,
LCD3_HT1622_CS__PIN,
GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_WriteHigh(LCD3_HT1622_CS__PORT, LCD3_HT1622_CS__PIN);
GPIO_Init(LCD3_HT1622_WRITE__PORT,
LCD3_HT1622_WRITE__PIN,
GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_WriteHigh(LCD3_HT1622_WRITE__PORT, LCD3_HT1622_WRITE__PIN);
GPIO_Init(LCD3_HT1622_DATA__PORT,
LCD3_HT1622_DATA__PIN,
GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_WriteHigh(LCD3_HT1622_DATA__PORT, LCD3_HT1622_DATA__PIN);
GPIO_Init(LCD3_HT1622_READ__PORT,
LCD3_HT1622_READ__PIN,
GPIO_MODE_IN_PU_NO_IT);
ht1622_send_command(CMD_SYS_EN);
ht1622_send_command(CMD_RC_INT);
ht1622_send_command(CMD_LCD_ON);
}
void ht1622_send_bits(uint16_t ui16_data, uint8_t ui8_bits)
{
static uint16_t ui16_mask;
ui16_mask = 1 << (ui8_bits - 1);
for (uint8_t i = ui8_bits; i > 0; i--)
{
GPIO_WriteLow(LCD3_HT1622_WRITE__PORT, LCD3_HT1622_WRITE__PIN);
if (ui16_data & ui16_mask)
{
GPIO_WriteHigh(LCD3_HT1622_DATA__PORT, LCD3_HT1622_DATA__PIN);
}
else
{
GPIO_WriteLow(LCD3_HT1622_DATA__PORT, LCD3_HT1622_DATA__PIN);
}
GPIO_WriteHigh(LCD3_HT1622_WRITE__PORT, LCD3_HT1622_WRITE__PIN);
ui16_data <<= 1;
}
}
void ht1622_send_command(uint8_t command)
{
GPIO_WriteLow(LCD3_HT1622_CS__PORT, LCD3_HT1622_CS__PIN);
ht1622_send_bits(4, 3);
ht1622_send_bits(command, 8);
ht1622_send_bits(1, 1);
GPIO_WriteHigh(LCD3_HT1622_CS__PORT, LCD3_HT1622_CS__PIN);
}
void ht1622_send_frame_buffer (uint8_t *p_lcd_frame_buffer)
{
uint8_t ui8_len;
uint8_t ui8_counter = 0;
uint8_t ui8_data = 0;
uint8_t ui8_lcd_frame_buffer_index = 0;
// send first address and first 4 bits
ui8_data = p_lcd_frame_buffer[ui8_lcd_frame_buffer_index];
GPIO_WriteLow(LCD3_HT1622_CS__PORT, LCD3_HT1622_CS__PIN);
ht1622_send_bits(5, 3);
ht1622_send_bits(0, 6); // start at 0 address
ht1622_send_bits(ui8_data, 4);
ui8_counter++;
// send the rest of the frame buffer
ui8_len = (LCD_FRAME_BUFFER_SIZE << 1) - 1;
while (ui8_len > 0)
{
ui8_len--;
ui8_counter++;
if (ui8_counter == 2)
{
ui8_counter = 0;
ui8_data = p_lcd_frame_buffer[ui8_lcd_frame_buffer_index] >> 4;
}
else
{
ui8_lcd_frame_buffer_index++;
ui8_data = p_lcd_frame_buffer[ui8_lcd_frame_buffer_index];
}
ht1622_send_bits(ui8_data, 4);
}
GPIO_WriteHigh(LCD3_HT1622_CS__PORT, LCD3_HT1622_CS__PIN);
}