-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCD.c
More file actions
260 lines (190 loc) · 6.53 KB
/
Copy pathLCD.c
File metadata and controls
260 lines (190 loc) · 6.53 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*----------------------------------------------------------------------------
* RL-ARM - Library
*----------------------------------------------------------------------------
* Name: LCD.C
* Purpose: LCD module 2x16 driver for ST7066 controller
* Rev.: V4.20
*----------------------------------------------------------------------------
* This code is part of the RealView Run-Time Library.
* Copyright (c) 2004-2011 KEIL - An ARM Company. All rights reserved.
*---------------------------------------------------------------------------*/
#include <RTL.h>
#include <91x_lib.H>
#include "LCD.h"
/* LCD IO definitions */
#define LCD_DATA GPIO8->DR[0xFF<<2] /* Data bits D0 = P8.0 .. DB7 = P8.7 */
#define LCD_E GPIO9->DR[0x01<<2] /* Enable control */
#define LCD_RW GPIO9->DR[0x02<<2] /* Read/Write control */
#define LCD_RS GPIO9->DR[0x04<<2] /* Data/Instruction control */
#define LCD_CTRL GPIO9->DR[0x07<<2] /* All 3 control lines (E/RW/RS) */
#define E_ENA 0x01
#define E_DIS 0x00
#define RW_READ 0x02
#define RW_WRITE 0x00
#define RS_DATA 0x04
#define RS_INST 0x00
/* Local variables */
static U32 lcd_ptr;
/* 8 user defined characters to be loaded into CGRAM (used for bargraph) */
static const U8 UserFont[8][8] = {
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
{ 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 },
{ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 },
{ 0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C },
{ 0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E },
{ 0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F },
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }
};
/* Local Function Prototypes */
static void delay (U32 cnt);
static void lcd_write (U32 c);
static U32 lcd_rd_stat (void);
static void lcd_wr_cmd (U32 c);
static void lcd_wr_data (U32 d);
static void lcd_wait_busy (void);
/*----------------------------------------------------------------------------
* LCD Driver Interface Functions
*---------------------------------------------------------------------------*/
/*--------------------------- delay -----------------------------------------*/
static void delay (U32 cnt) {
/* Delay in while loop cycles. */
while (cnt--);
}
/*--------------------------- lcd_write -------------------------------------*/
static void lcd_write (U32 c) {
/* Write data/command to LCD controller. */
GPIO8->DDR = 0xFF;
LCD_RW = RW_WRITE;
LCD_DATA = c;
LCD_E = E_ENA;
delay (10);
LCD_E = E_DIS;
delay (10);
}
/*--------------------------- lcd_rd_stat -----------------------------------*/
static U32 lcd_rd_stat (void) {
/* Read status of LCD controller (ST7066) */
U32 stat;
GPIO8->DDR = 0x00;
LCD_CTRL = RW_READ;
delay (10);
LCD_E = E_ENA;
delay (10);
stat = LCD_DATA;
LCD_E = E_DIS;
return (stat);
}
/*--------------------------- lcd_wait_busy ---------------------------------*/
static void lcd_wait_busy (void) {
/* Wait until LCD controller (ST7066) is busy. */
U32 stat;
do {
stat = lcd_rd_stat ();
} while (stat & 0x80); /* Wait for busy flag */
}
/*--------------------------- lcd_wr_cmd ------------------------------------*/
static void lcd_wr_cmd (U32 c) {
/* Write command to LCD controller. */
lcd_wait_busy ();
LCD_RS = RS_INST;
lcd_write (c);
}
/*--------------------------- lcd_wr_data -----------------------------------*/
static void lcd_wr_data (U32 d) {
/* Write data to LCD controller. */
lcd_wait_busy ();
LCD_RS = RS_DATA;
lcd_write (d);
}
/*--------------------------- LCD_init --------------------------------------*/
void LCD_init (void) {
/* Initialize the ST7066 LCD controller to 4-bit mode. */
GPIO8->DDR = 0xFF;
GPIO9->DDR |= 0x07;
LCD_CTRL = RS_INST;
lcd_write (0x38); /* Select 8-bit interface */
delay (100000);
lcd_write (0x38);
delay (10000);
lcd_write (0x38);
lcd_write (0x38); /* 2 lines, 5x8 character matrix */
lcd_wr_cmd (0x0e); /* Display ctrl:Disp/Curs/Blnk=ON */
lcd_wr_cmd (0x06); /* Entry mode: Move right, no shift */
LCD_load ((U8 *)&UserFont, sizeof (UserFont));
LCD_cls ();
LCD_puts ("Poten Value = ");
}
/*--------------------------- LCD_load --------------------------------------*/
void LCD_load (U8 *fp, U32 cnt) {
/* Load user-specific characters into CGRAM */
U32 i;
lcd_wr_cmd (0x40); /* Set CGRAM address counter to 0 */
for (i = 0; i < cnt; i++, fp++) {
lcd_wr_data (*fp);
}
}
/*--------------------------- LCD_gotoxy ------------------------------------*/
void LCD_gotoxy (U32 x, U32 y) {
/* Set cursor position on LCD display. Left corner: 1,1, right: 16,2 */
U32 c;
c = --x;
if (--y) {
c |= 0x40;
}
lcd_wr_cmd (c | 0x80);
lcd_ptr = y*16 + x;
}
/*--------------------------- LCD_cls ---------------------------------------*/
void LCD_cls (void) {
/* Clear LCD display, move cursor to home position. */
lcd_wr_cmd (0x01);
LCD_gotoxy (1,1);
}
/*--------------------------- LCD_cur_off------------------------------------*/
void LCD_cur_off (void) {
/* Switch off LCD cursor. */
lcd_wr_cmd (0x0c);
}
/*--------------------------- LCD_on ------ ---------------------------------*/
void LCD_on (void) {
/* Switch on LCD and enable cursor. */
lcd_wr_cmd (0x0e);
}
/*--------------------------- LCD_putc --------------------------------------*/
void LCD_putc (U8 c) {
/* Print a character to LCD at current cursor position. */
if (lcd_ptr == 16) {
lcd_wr_cmd (0xc0);
}
lcd_wr_data (c);
lcd_ptr++;
}
/*--------------------------- LCD_puts --------------------------------------*/
void LCD_puts (U8 *sp) {
/* Print a string to LCD display. */
while (*sp) {
LCD_putc (*sp++);
}
}
/*--------------------------- LCD_bargraph ----------------------------------*/
void LCD_bargraph (U32 val, U32 size) {
/* Print a bargraph to LCD display. */
/* - val: value 0..100 % */
/* - size: size of bargraph 1..16 */
U32 i;
val = val * size / 20; /* Display matrix 5 x 8 pixels */
for (i = 0; i < size; i++) {
if (val > 5) {
LCD_putc (5);
val -= 5;
}
else {
LCD_putc (val);
break;
}
}
}
/*----------------------------------------------------------------------------
* end of file
*---------------------------------------------------------------------------*/