/
LCD_lib.c
167 lines (141 loc) · 2.74 KB
/
LCD_lib.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
#include <p18f26k22.h>
#include <string.h>
#include "LCD_lib.h"
#define E_PIN LATCbits.LATC3
#define E_TRIS TRISCbits.TRISC3
#define RW_PIN LATCbits.LATC2
#define RW_TRIS TRISCbits.TRISC2
#define RS_PIN LATCbits.LATC1
#define RS_TRIS TRISCbits.TRISC1
#define OUTPORT LATA
#define BUSY_IN PORTAbits.RA3
#define OUTPORT_TRIS TRISA
void command(unsigned char i);
void toggleClock(void);
void DELAY(int MS);
void usDELAY(unsigned int uS);
void command(unsigned char i)
{
OUTPORT = (i>>4) & 0x0F;
RS_PIN = 0;
RW_PIN = 0;
toggleClock();
OUTPORT = i & 0x0F;
toggleClock();
}
void toggleClock()
{
E_PIN = 1;
Nop();
E_PIN = 0;
}
void DELAY(int MS)
{
// At 4MHZ: 1 ms = 1k cycles:
// MAX = 25ms
int outer;
int i;
for(outer = 0; outer < MS; outer++)
for (i = 0; i < 1000; i++)
;
}
void usDELAY(unsigned int uS)
{
int i;
for(i = 0; i < uS; i++)
;
}
// Goes to character 0, line x
void lcd_setLine(unsigned char line)
{
const unsigned char lineIDs[5] = {0, 0x80, 0xC0, 0x90, 0xD0};
command(lineIDs[line]);
lcd_waitUntilReady();
}
#define DISPLAY_ON 0x0F
#define DISPLAY_OFF 0b00001011
#define CURSOR_ON 0x0F
#define CURSOR_OFF 0b00001101
#define BLINK_ON 0x0F
#define BLINK_OFF 0b00001110
void lcd_setDisplay(unsigned char config)
{
command(config);
lcd_waitUntilReady();
}
#define ID_RIGHT 0b00000111
#define ID_LEFT 0b00000101
#define SHIFT_ON 0b00000111
#define SHIFT_OFF 0b00000110
void lcd_setEntry(unsigned char config)
{
command(config);
lcd_waitUntilReady();
}
void lcd_sendData(unsigned char i)
{
OUTPORT = (i>>4) & 0x0F;
RS_PIN = 1;
RW_PIN = 0;
toggleClock();
OUTPORT = i & 0x0F;
toggleClock();
lcd_waitUntilReady();
}
void lcd_waitUntilReady(void)
{
char breakchar = 0;
// Set R7 as an input
OUTPORT_TRIS = OUTPORT_TRIS | 0b00001000;
// Set Read
RW_PIN = 1;
RS_PIN = 0;
usDELAY(50);
while(!breakchar)
{
toggleClock();
Nop(); //Delay 1us
if(BUSY_IN == 0)
breakchar = 1;
toggleClock();
Nop();
}
OUTPORT_TRIS = OUTPORT_TRIS & 0b11110111;
}
void lcd_write(char *stringIn)
{
while(*stringIn != '\0')
{
lcd_sendData(*stringIn);
stringIn++;
}
}
void lcd_init()
{
OUTPORT_TRIS = 0x00;
RS_TRIS = 0;
RW_TRIS = 0;
E_TRIS = 0;
E_PIN = 0;
RW_PIN = 0;
RS_PIN = 0;
OUTPORT = 0;
DELAY(10);
OUTPORT = 0x03;
DELAY(4);
toggleClock();
DELAY(1);
toggleClock();
DELAY(1);
toggleClock();
DELAY(1);
OUTPORT = 0x02;
toggleClock();
// 4 Bit, 5x8, 2 line display. Won't change.
command(0x28);
lcd_waitUntilReady();
// Setup Display
lcd_setEntry(ID_RIGHT & SHIFT_OFF);
lcd_setDisplay(DISPLAY_ON & CURSOR_OFF & BLINK_OFF);
lcd_setLine(1);
}