USB Host Shield 2.0
max_LCD.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2 
3 This software may be distributed and modified under the terms of the GNU
4 General Public License version 2 (GPL2) as published by the Free Software
5 Foundation and appearing in the file GPL2.TXT included in the packaging of
6 this file. Please note that GPL2 Section 2[b] requires that all works based
7 on this software must also be made publicly available under the terms of
8 the GPL2 ("Copyleft").
9 
10 Contact information
11 -------------------
12 
13 Circuits At Home, LTD
14 Web : http://www.circuitsathome.com
15 e-mail : support@circuitsathome.com
16  */
17 #include "max_LCD.h"
18 #include <string.h>
19 
20 // pin definition and set/clear
21 
22 #define RS 0x04 // RS pin
23 #define E 0x08 // E pin
24 
25 #define SET_RS lcdPins |= RS
26 #define CLR_RS lcdPins &= ~RS
27 #define SET_E lcdPins |= E
28 #define CLR_E lcdPins &= ~E
29 
30 #define SENDlcdPins() pUsb->gpioWr( lcdPins )
31 
32 #define LCD_sendcmd(a) { CLR_RS; \
33  sendbyte(a); \
34  }
35 
36 #define LCD_sendchar(a) { SET_RS; \
37  sendbyte(a); \
38  }
39 
40 static uint8_t lcdPins; //copy of LCD pins
41 
42 Max_LCD::Max_LCD(USB *pusb) : pUsb(pusb) {
43  lcdPins = 0;
44 }
45 
46 void Max_LCD::init() {
47  _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
48 
49  // MAX3421E::gpioWr(0x55);
50 
51  begin(16, 1);
52 }
53 
54 void Max_LCD::begin(uint8_t cols __attribute__((unused)), uint8_t lines, uint8_t dotsize) {
55  if(lines > 1) {
56  _displayfunction |= LCD_2LINE;
57  }
58  _numlines = lines;
59  _currline = 0;
60 
61  // for some 1 line displays you can select a 10 pixel high font
62  if((dotsize != 0) && (lines == 1)) {
63  _displayfunction |= LCD_5x10DOTS;
64  }
65 
66  // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
67  // according to datasheet, we need at least 40ms after power rises above 2.7V
68  // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
69  delayMicroseconds(50000);
70  lcdPins = 0x30;
71  SET_E;
72  SENDlcdPins();
73  CLR_E;
74  SENDlcdPins();
75  delayMicroseconds(10000); // wait min 4.1ms
76  //second try
77  SET_E;
78  SENDlcdPins();
79  CLR_E;
80  SENDlcdPins();
81  delayMicroseconds(10000); // wait min 4.1ms
82  // third go!
83  SET_E;
84  SENDlcdPins();
85  CLR_E;
86  SENDlcdPins();
87  delayMicroseconds(10000);
88  // finally, set to 4-bit interface
89  lcdPins = 0x20;
90  //SET_RS;
91  SET_E;
92  SENDlcdPins();
93  //CLR_RS;
94  CLR_E;
95  SENDlcdPins();
96  delayMicroseconds(10000);
97  // finally, set # lines, font size, etc.
98  command(LCD_FUNCTIONSET | _displayfunction);
99 
100  // turn the display on with no cursor or blinking default
101  _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
102  display();
103 
104  // clear it off
105  clear();
106 
107  // Initialize to default text direction (for romance languages)
108  _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
109  // set the entry mode
110  command(LCD_ENTRYMODESET | _displaymode);
111 }
112 
113 /********** high level commands, for the user! */
115  command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
116  delayMicroseconds(2000); // this command takes a long time!
117 }
118 
120  command(LCD_RETURNHOME); // set cursor position to zero
121  delayMicroseconds(2000); // this command takes a long time!
122 }
123 
124 void Max_LCD::setCursor(uint8_t col, uint8_t row) {
125  int row_offsets[] = {0x00, 0x40, 0x14, 0x54};
126  if(row > _numlines) {
127  row = _numlines - 1; // we count rows starting w/0
128  }
129 
130  command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
131 }
132 
133 // Turn the display on/off (quickly)
134 
136  _displaycontrol &= ~LCD_DISPLAYON;
137  command(LCD_DISPLAYCONTROL | _displaycontrol);
138 }
139 
141  _displaycontrol |= LCD_DISPLAYON;
142  command(LCD_DISPLAYCONTROL | _displaycontrol);
143 }
144 
145 // Turns the underline cursor on/off
146 
148  _displaycontrol &= ~LCD_CURSORON;
149  command(LCD_DISPLAYCONTROL | _displaycontrol);
150 }
151 
153  _displaycontrol |= LCD_CURSORON;
154  command(LCD_DISPLAYCONTROL | _displaycontrol);
155 }
156 
157 
158 // Turn on and off the blinking cursor
159 
161  _displaycontrol &= ~LCD_BLINKON;
162  command(LCD_DISPLAYCONTROL | _displaycontrol);
163 }
164 
166  _displaycontrol |= LCD_BLINKON;
167  command(LCD_DISPLAYCONTROL | _displaycontrol);
168 }
169 
170 // These commands scroll the display without changing the RAM
171 
174 }
175 
178 }
179 
180 // This is for text that flows Left to Right
181 
183  _displaymode |= LCD_ENTRYLEFT;
184  command(LCD_ENTRYMODESET | _displaymode);
185 }
186 
187 // This is for text that flows Right to Left
188 
190  _displaymode &= ~LCD_ENTRYLEFT;
191  command(LCD_ENTRYMODESET | _displaymode);
192 }
193 
194 // This will 'right justify' text from the cursor
195 
197  _displaymode |= LCD_ENTRYSHIFTINCREMENT;
198  command(LCD_ENTRYMODESET | _displaymode);
199 }
200 
201 // This will 'left justify' text from the cursor
202 
204  _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
205  command(LCD_ENTRYMODESET | _displaymode);
206 }
207 
208 // Allows us to fill the first 8 CGRAM locations
209 // with custom characters
210 
211 void Max_LCD::createChar(uint8_t location, uint8_t charmap[]) {
212  location &= 0x7; // we only have 8 locations 0-7
213  command(LCD_SETCGRAMADDR | (location << 3));
214  for(int i = 0; i < 8; i++) {
215  write(charmap[i]);
216  }
217 }
218 
219 /*********** mid level commands, for sending data/cmds */
220 
221 inline void Max_LCD::command(uint8_t value) {
222  LCD_sendcmd(value);
223  delayMicroseconds(100);
224 }
225 
226 #if defined(ARDUINO) && ARDUINO >=100
227 
228 inline size_t Max_LCD::write(uint8_t value) {
229  LCD_sendchar(value);
230  return 1; // Assume success
231 }
232 #else
233 
234 inline void Max_LCD::write(uint8_t value) {
235  LCD_sendchar(value);
236 }
237 #endif
238 
239 void Max_LCD::sendbyte(uint8_t val) {
240  lcdPins &= 0x0f; //prepare place for the upper nibble
241  lcdPins |= (val & 0xf0); //copy upper nibble to LCD variable
242  SET_E; //send
243  SENDlcdPins();
244  delayMicroseconds(2);
245  CLR_E;
246  delayMicroseconds(2);
247  SENDlcdPins();
248  lcdPins &= 0x0f; //prepare place for the lower nibble
249  lcdPins |= (val << 4) & 0xf0; //copy lower nibble to LCD variable
250  SET_E; //send
251  SENDlcdPins();
252  CLR_E;
253  SENDlcdPins();
254  delayMicroseconds(100);
255 }
void command(uint8_t)
Definition: max_LCD.cpp:221
void noBlink()
Definition: max_LCD.cpp:160
void leftToRight()
Definition: max_LCD.cpp:182
void init()
Definition: max_LCD.cpp:46
size_t write(uint8_t)
Definition: max_LCD.cpp:228
void noAutoscroll()
Definition: max_LCD.cpp:203
void autoscroll()
Definition: max_LCD.cpp:196
void cursor()
Definition: max_LCD.cpp:152
void clear()
Definition: max_LCD.cpp:114
Max_LCD(USB *pusb)
Definition: max_LCD.cpp:42
void createChar(uint8_t, uint8_t[])
Definition: max_LCD.cpp:211
void display()
Definition: max_LCD.cpp:140
void setCursor(uint8_t, uint8_t)
Definition: max_LCD.cpp:124
void blink()
Definition: max_LCD.cpp:165
void noDisplay()
Definition: max_LCD.cpp:135
void home()
Definition: max_LCD.cpp:119
void begin(uint8_t cols, uint8_t rows, uint8_t charsize=LCD_5x8DOTS)
Definition: max_LCD.cpp:54
void noCursor()
Definition: max_LCD.cpp:147
void scrollDisplayRight()
Definition: max_LCD.cpp:176
void rightToLeft()
Definition: max_LCD.cpp:189
void scrollDisplayLeft()
Definition: max_LCD.cpp:172
Definition: UsbCore.h:212
#define CLR_E
Definition: max_LCD.cpp:28
#define SET_E
Definition: max_LCD.cpp:27
#define SENDlcdPins()
Definition: max_LCD.cpp:30
#define LCD_sendcmd(a)
Definition: max_LCD.cpp:32
#define LCD_sendchar(a)
Definition: max_LCD.cpp:36
#define LCD_ENTRYSHIFTDECREMENT
Definition: max_LCD.h:41
#define LCD_SETDDRAMADDR
Definition: max_LCD.h:35
#define LCD_RETURNHOME
Definition: max_LCD.h:29
#define LCD_CURSOROFF
Definition: max_LCD.h:47
#define LCD_BLINKOFF
Definition: max_LCD.h:49
#define LCD_ENTRYMODESET
Definition: max_LCD.h:30
#define LCD_CURSORSHIFT
Definition: max_LCD.h:32
#define LCD_DISPLAYON
Definition: max_LCD.h:44
#define LCD_2LINE
Definition: max_LCD.h:60
#define LCD_1LINE
Definition: max_LCD.h:61
#define LCD_5x8DOTS
Definition: max_LCD.h:63
#define LCD_ENTRYSHIFTINCREMENT
Definition: max_LCD.h:40
#define LCD_SETCGRAMADDR
Definition: max_LCD.h:34
#define LCD_FUNCTIONSET
Definition: max_LCD.h:33
#define LCD_MOVELEFT
Definition: max_LCD.h:55
#define LCD_DISPLAYMOVE
Definition: max_LCD.h:52
#define LCD_CURSORON
Definition: max_LCD.h:46
#define LCD_4BITMODE
Definition: max_LCD.h:59
#define LCD_5x10DOTS
Definition: max_LCD.h:62
#define LCD_BLINKON
Definition: max_LCD.h:48
#define LCD_CLEARDISPLAY
Definition: max_LCD.h:28
#define LCD_MOVERIGHT
Definition: max_LCD.h:54
#define LCD_DISPLAYCONTROL
Definition: max_LCD.h:31
#define LCD_ENTRYLEFT
Definition: max_LCD.h:39