forked from QUT-EESS/libRobotDev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRDLCD.h
More file actions
270 lines (225 loc) · 4.69 KB
/
Copy pathRDLCD.h
File metadata and controls
270 lines (225 loc) · 4.69 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
261
262
263
264
265
266
267
268
269
/*
* libRobotDev
* RDLCD.h
* Purpose: Abstracts all LCD functions
* Created: 05/12/2014
* Author(s): Arda Yilmaz, Jeremy Pearson
* Status: UNTESTED
*/
#include <avr/io.h>
// Ascii font data
#include "RDASCIIFont.h"
#ifndef RDLCD_H_
/**
* Robot Development Liquid Crystal Display Header.
*/
#define RDLCD_H_
/**
* Setting for whether the mode will be master (1) or slave (0).
*/
#define SPI_MASTER 1
#include "RDSPI.h"
/**
* LCD Font Width.
*/
#define RDLCD_FONT_W 5
/************
* LCD pins *
************/
/**
* LCD Chip Select pin.
*/
#define RDLCD_CS PC0
/**
* LCD Reset pin.
*/
#define RDLCD_RST PC1
/**
* LCD Data/Command pin.
*/
#define RDLCD_DC PC3
/**
* LCD Port.
*/
#define RDLCD_PORT PORTC
/**
* LCD Command Binary Representation.
*/
#define RDLCD_C 0
/**
* LCD Data Binary Representation.
*/
#define RDLCD_D 1
/****************
* LCD Commands *
****************/
/**
* LCD Basic Functionality Mode.
*/
#define LCD_BSC_FN 0x20
/**
* LCD Extended Functionality Mode.
*/
#define LCD_EXT_FN 0x21
/**
* LCD Set Screen To Blank.
*/
#define LCD_BLANK 0x08
/**
* LCD Normal Mode.
*/
#define LCD_NORM 0x0C
/**
* LCD Set Screen To Black.
*/
#define LCD_BLACK 0x09
/**
* LCD Inverted Screen Mode.
*/
#define LCD_INVRT 0x0D
/**
* LCD Set Contrast.
*/
#define LCD_SET_CONTRAST 0x80
/**
* LCD Set X Position of Cursor.
*/
#define LCD_SET_X 0x80
/**
* LCD Set Y Position of Cursor.
*/
#define LCD_SET_Y 0x40
/******************
* SPI Parameters *
******************/
/**
* LCD Clock Prescaler.
*/
#define LCD_CLK_PS 0
/*******************
* LCD Parameters. *
*******************/
/**
* LCD Width.
*/
#define RDLCD_W 84
/**
* LCD Height.
*/
#define RDLCD_H 48
/**
* LCD X Coordinate of Center.
*/
#define RDLCD_CNTR_X RDLCD_W / 2
/**
* LCD Y Coordinate of Center.
*/
#define RDLCD_CNTR_Y RDLCD_H / 2
/**
* LCD Number of Vertical Rows.
*/
#define RDLCD_ROW_H 8
/**
* LCD Default Contrast.
*/
#define RDLCD_DEFAULT_CONTRAST 0x3F
/**
* LCD High Contrast.
*/
#define RDLCD_HIGH_CONTRAST 0x4F
/******************
* LCD Functions. *
******************/
/**
* Writes a byte of pixel-data or a command to LCD screen.
*
* @param byte
* The byte of pixel-data or a command.
*
* @param dc
* Boolean of whether the byte should be treated as a command (0) or
* pixel-data (1).
*/
void RDLCDWrite(uint8_t byte, uint8_t dc) {
// Set data/command mode
RDLCD_PORT = (RDLCD_PORT & ~(1 << RDLCD_DC)) | (dc << RDLCD_DC);
// Write byte to SPI
RDSPIRWByte(byte, 5, &RDLCD_PORT, RDLCD_CS);
}
/**
* Initialises the LCD screen.
*/
void RDLCDInit(void) {
// Init LCD control pins, pull CS and RST high
DDRC |= (1 << RDLCD_CS) | (1 << RDLCD_RST) | (1 << RDLCD_DC);
RDLCD_PORT |= (1 << RDLCD_CS) | (1 << RDLCD_RST);
// Reset LCD
RDLCD_PORT &= ~(1 << RDLCD_RST);
RDLCD_PORT |= (1 << RDLCD_RST);
// Init SPI - little endian, CPOL = 0, CPHA = 0
RDSPIInit(0,0);
// Use extended instruction set
RDLCDWrite(LCD_EXT_FN, RDLCD_C);
// Set default contrast
RDLCDWrite(LCD_SET_CONTRAST | RDLCD_HIGH_CONTRAST, RDLCD_C);
// Use basic instruction set
RDLCDWrite(LCD_BSC_FN, RDLCD_C);
//Set LCD to normal mode
RDLCDWrite(LCD_NORM, RDLCD_C);
//Reset the LCD position
RDLCDWrite(LCD_SET_X | 0x00, RDLCD_C);
RDLCDWrite(LCD_SET_Y | 0x00, RDLCD_C);
}
/**
* Clears the LCD screen.
*/
void RDLCDClear(void) {
for (int i = 0; i < RDLCD_W * RDLCD_ROW_H; i++){
RDLCDWrite(0x00, RDLCD_D);
}
}
/**
* Writes the given character to the LCD screen at the current cursor position.
*
* @param character
* The character to be written to the LCD screen.
*/
void RDLCDCharacter(unsigned char character) {
// Clear padding between each character
RDLCDWrite(0x00, RDLCD_D);
for (int i = 0; i < RDLCD_FONT_W; i++)
{
RDLCDWrite(ASCII[character - 0x20][i], RDLCD_D);
}
RDLCDWrite(0x00, RDLCD_D);
}
/**
* Writes the given string to the LCD screen, starting from the current cursor
* position.
*
* @param characters
* The string to be written to the LCD screen.
*/
void RDLCDString(unsigned char *characters) {
while (*characters != '\0')
{
RDLCDCharacter(*(characters++));
}
}
/**
* Sets the position of the LCD cursor to the given x- and y-coordinates.
*
* @param x
* The x-coordinate of the desired cursor position.
*
* @param y
* The y-coordinate of the desired cursor position.
*/
void RDLCDPosition(unsigned char x, unsigned char y) {
if((x < RDLCD_W) & (y < (RDLCD_ROW_H)))
{
RDLCDWrite(LCD_SET_X | x, RDLCD_C);
RDLCDWrite(LCD_SET_Y | y, RDLCD_C);
}
}
#endif // RDLCD_H_