forked from unfrozen/stm8_libs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib_lcd.c
174 lines (154 loc) · 3.52 KB
/
lib_lcd.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* File name: lib_lcd.c
* Date first: 12/19/2017
* Date last: 12/31/2018
*
* Description: Library for Hitachi HD44780 LCDs on STM8 architecture.
*
* Author: Richard Hodges
*
* Copyright (C) 2017, 2018 Richard Hodges. All rights reserved.
* Permission is hereby granted for any use.
*
******************************************************************************
*
* Pinouts:
*
* C4..C7 LCD D4..D7 (pins 11-14, fast)
* C3 LCD Enable (pin 6, fast)
* A3 LCD RS (pin 4) on STM8S_103 (fast pin)
* F4 LCD RS (pin 4) on STM8S_105 (slow pin)
* Vss LCD ground (pin 1)
* Vdd LCD +5V (pin 2)
* Vo LCD contrast (pin 3)
* W* LCD R/W wired low (pin 5)
*/
#include "stm8s_header.h"
#include "lib_delay.h"
#include "lib_lcd.h"
static void lcd_comd(char);
static void lcd_data(char);
static void lcd_nybble(char);
#ifdef STM8103
#define BUSY_COUNT 100 /* RC clock at 16mhz */
#define RS_PORT _PA_ODR
#define RS_PIN 3
#endif
#ifdef STM8105
#define RS_PORT _PF_ODR
#define RS_PIN 4
#warning "Pins C1 & C2 clobbered. Use as inputs."
#define BUSY_COUNT 50 /* crystal oscillator at 8 mhz */
#endif
char busy_count = BUSY_COUNT; /* 100 uSecs */
/******************************************************************************
*
* Set LCD cursor by line and column
*
* in: line (0-3), column (0-19)
*/
void lcd_curs(char line, char col)
{
char val;
val = col;
if (line & 1)
val += 0x40; /* lines 1 & 3 have 0x40 offset */
if (line & 2)
val += 20; /* lines 2 & 3 have 20 offset */
lcd_comd(val | 0x80);
}
/******************************************************************************
*
* Write data byte to lcd
*/
void lcd_putc(char val)
{
val;
__asm
bset RS_PORT, #RS_PIN
push _busy_count
ld a, (4, sp)
and a, #0xf0
ld _PC_ODR, a
bset _PC_ODR, #3
call _delay_500ns
bres _PC_ODR, #3
ld a, (4, sp)
swap a
and a, #0xf0
ld _PC_ODR, a
bset _PC_ODR, #3
call _delay_500ns
bres _PC_ODR, #3
call _delay_usecs
pop a
__endasm;
}
void lcd_puts(char *s)
{
while (1) {
if (*s == 0)
return;
lcd_putc(*s);
s++;
}
}
/******************************************************************************
*
* Write command byte to lcd
*/
void lcd_comd(char val)
{
lcd_nybble(val);
lcd_nybble(val << 4);
delay_50us();
}
/******************************************************************************
*
* Write command nybble to lcd
*/
void lcd_nybble(char val)
{
val;
__asm
bres RS_PORT, #RS_PIN
ld a, (3, sp)
and a, #0xf0
ld _PC_ODR, a
call _delay_500ns
bset _PC_ODR, #3
call _delay_500ns
bres _PC_ODR, #3
__endasm;
}
/******************************************************************************
*
* Initialize LCD
*/
void lcd_init(void)
{
PC_DDR |= 0xf8; /* C3, C4-C7 outputs */
PC_CR1 |= 0xf8;
PC_CR2 |= 0xf8; /* fast outputs */
#ifdef STM8103
PA_DDR |= 0x08; /* A3 output */
PA_CR1 |= 0x08;
PA_CR2 |= 0x08;
#endif
#ifdef STM8105
PF_DDR = 0x10;
PF_CR1 = 0x10;
#endif
delay_ms(50);
lcd_nybble(0x30); /* start with 8-bit mode */
delay_ms(5); /* more than 4.1 ms */
lcd_nybble(0x30);
delay_usecs(120); /* more than 100 us */
lcd_nybble(0x30);
delay_usecs(50); /* more than 37 us */
lcd_nybble(0x20); /* set to 4-bit mode */
delay_usecs(50);
lcd_comd(0x28); /* 4-bit mode, 2 lines */
lcd_comd(0x0e); /* turn on diplay and cursor */
lcd_comd(0x06); /* auto increment and move cursor */
}