-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcd.c
119 lines (106 loc) · 1.63 KB
/
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
#include "avr.h"
#define DDR DDRB
#define PORT PORTB
#define RS_PIN 0
#define RW_PIN 1
#define EN_PIN 2
#define LCD_ROW_SIZE 16
static inline void
set_data(unsigned char x)
{
PORTD = x;
DDRD = 0xff;
}
static inline unsigned char
get_data(void)
{
DDRD = 0x00;
return PIND;
}
static inline void
sleep_700ns(void)
{
NOP();
NOP();
NOP();
}
static unsigned char
input(unsigned char rs)
{
unsigned char d;
if (rs) SET_BIT(PORT, RS_PIN); else CLR_BIT(PORT, RS_PIN);
SET_BIT(PORT, RW_PIN);
get_data();
SET_BIT(PORT, EN_PIN);
sleep_700ns();
d = get_data();
CLR_BIT(PORT, EN_PIN);
return d;
}
static void
output(unsigned char d, unsigned char rs)
{
if (rs) SET_BIT(PORT, RS_PIN); else CLR_BIT(PORT, RS_PIN);
CLR_BIT(PORT, RW_PIN);
set_data(d);
SET_BIT(PORT, EN_PIN);
sleep_700ns();
CLR_BIT(PORT, EN_PIN);
}
static void
write(unsigned char c, unsigned char rs)
{
while (input(0) & 0x80);
output(c, rs);
}
void
lcd_ini(void)
{
SET_BIT(DDR, RS_PIN);
SET_BIT(DDR, RW_PIN);
SET_BIT(DDR, EN_PIN);
wait_avr(16);
output(0x30, 0);
wait_avr(5);
output(0x30, 0);
wait_avr(1);
write(0x3c, 0);
write(0x0c, 0);
write(0x06, 0);
write(0x01, 0);
}
void
clr_lcd(void)
{
write(0x01, 0);
}
void
pos_lcd(unsigned char r, unsigned char c)
{
unsigned char n = r * 40 + c;
write(0x02, 0);
while (n--) {
write(0x14, 0);
}
}
void
put_lcd(char c)
{
write(c, 1);
}
void
puts_lcd(const char *s)
{
char c;
while ((c = pgm_read_byte(s++)) != 0) {
write(c, 1);
}
}
void lcd_print(char * str, int row)
{
for(int i = 0; i <= LCD_ROW_SIZE; i++)
{
pos_lcd(row, i);
put_lcd(str[i]);
}
}