-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpi_hd44780.c
159 lines (119 loc) · 2.58 KB
/
rpi_hd44780.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
/*
* =====================================================================================
*
* Filename: rpi_hd44780.c
*
* Description: Raspberry Pi GPIO HD44780 Driver
* Requires:
* C library for Broadcom BCM 2835 as used in Raspberry Pi
* available at: http://www.open.com.au/mikem/bcm2835/
*
* Version: 0.2
* Created: 09/27/2012 22:12:11
* Compiler: gcc
*
* Author: Vincent Franco
*
* =====================================================================================
*/
#include "rpi_hd44780.h"
int lcd_set_gpio_pins( void )
{
int i;
// bcm could not initalize return status
if ( ! bcm2835_init() )
{
return 1;
}
for ( i=0; i<6; ++i )
{
// Set all pins as outputs
bcm2835_gpio_fsel(HD_PINS[i], BCM2835_GPIO_FSEL_OUTP);
usleep(1);
// Set all pins LOW
bcm2835_gpio_write(HD_PINS[i], 0);
}
return 0;
}
void lcd_send_byte( const unsigned char u8_byte, int mode )
{
int i;
bcm2835_gpio_write(H_RS, mode);
for ( i=0; i<8; ++i )
{
int p = i%4, b = (i>>2)? i-4 : i+4;
bcm2835_gpio_write(HD_DATAPINS[p], ((u8_byte>>b) & 0x01));
if ( i == 3 )
{
lcd_epulse(mode);
}
}
lcd_epulse(mode);
}
void lcd_send_nibble( const unsigned char u8_byte, int mode )
{
int i, b;
for ( i=0; i<4; ++i )
{
b = i+4;
bcm2835_gpio_write(HD_DATAPINS[i], ((u8_byte>>b) & 0x01));
}
lcd_epulse(mode);
}
void lcd_epulse ( int narrow )
{
bcm2835_gpio_write(H_E, 1);
usleep(1);
bcm2835_gpio_write(H_E, 0);
(narrow)? usleep(P_NARROW) : usleep(P_WIDE);
}
int lcd_init( void )
{
int i;
// Init commands
unsigned char u8_cmds[5] = {
0x28, 0x08, 0x01, 0x06, 0x0C
};
// Set all pins low;
lcd_set_gpio_pins();
// Wait more than 15ms after VCC rises to 4.5V
usleep(P_15MS);
lcd_send_nibble(0x30, 0);
// Wait for more than 4.1ms
for ( i=0; i<2; ++i )
{
lcd_epulse(1); // Pulse twice
}
// Interface to 4-bit
lcd_send_nibble(0x20, 0);
for ( i=0; i<5; ++i )
{
lcd_send_byte(u8_cmds[i], 0);
}
}
void lcd_clear_screen( void )
{
lcd_send_byte(0x01, 0);
}
void lcd_set_cursor( unsigned char u8_line )
{
lcd_send_byte(u8_line, 0);
}
void lcd_send_char( const char u8_char )
{
lcd_send_byte(u8_char, 1); // Send char
bcm2835_gpio_write(H_RS, 0); // Set RS LOW
}
int lcd_send_string( const char *str )
{
if ( str == CNULL_TERM )
{
return 1; // Nothing was passed
}
while ( *str )
{
lcd_send_byte( *str++, 1 );
}
bcm2835_gpio_write(H_RS, 0);
return 0;
}