1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
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
14 Web : http://www.circuitsathome.com
15 e-mail : support@circuitsathome.com
20 // pin definition and set/clear
22 #define RS 0x04 // RS pin
23 #define E 0x08 // E pin
25 #define SET_RS lcdPins |= RS
26 #define CLR_RS lcdPins &= ~RS
27 #define SET_E lcdPins |= E
28 #define CLR_E lcdPins &= ~E
30 #define SENDlcdPins() pUsb->gpioWr( lcdPins )
32 #define LCD_sendcmd(a) { CLR_RS; \
36 #define LCD_sendchar(a) { SET_RS; \
40 static byte lcdPins; //copy of LCD pins
42 Max_LCD::Max_LCD(USB *pusb) : pUsb(pusb) {
46 void Max_LCD::init() {
47 _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
49 // MAX3421E::gpioWr(0x55);
54 void Max_LCD::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
56 _displayfunction |= LCD_2LINE;
61 // for some 1 line displays you can select a 10 pixel high font
62 if((dotsize != 0) && (lines == 1)) {
63 _displayfunction |= LCD_5x10DOTS;
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);
75 delayMicroseconds(10000); // wait min 4.1ms
81 delayMicroseconds(10000); // wait min 4.1ms
87 delayMicroseconds(10000);
88 // finally, set to 4-bit interface
96 delayMicroseconds(10000);
97 // finally, set # lines, font size, etc.
98 command(LCD_FUNCTIONSET | _displayfunction);
100 // turn the display on with no cursor or blinking default
101 _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
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);
113 /********** high level commands, for the user! */
114 void Max_LCD::clear() {
115 command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
116 delayMicroseconds(2000); // this command takes a long time!
119 void Max_LCD::home() {
120 command(LCD_RETURNHOME); // set cursor position to zero
121 delayMicroseconds(2000); // this command takes a long time!
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
130 command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
133 // Turn the display on/off (quickly)
135 void Max_LCD::noDisplay() {
136 _displaycontrol &= ~LCD_DISPLAYON;
137 command(LCD_DISPLAYCONTROL | _displaycontrol);
140 void Max_LCD::display() {
141 _displaycontrol |= LCD_DISPLAYON;
142 command(LCD_DISPLAYCONTROL | _displaycontrol);
145 // Turns the underline cursor on/off
147 void Max_LCD::noCursor() {
148 _displaycontrol &= ~LCD_CURSORON;
149 command(LCD_DISPLAYCONTROL | _displaycontrol);
152 void Max_LCD::cursor() {
153 _displaycontrol |= LCD_CURSORON;
154 command(LCD_DISPLAYCONTROL | _displaycontrol);
158 // Turn on and off the blinking cursor
160 void Max_LCD::noBlink() {
161 _displaycontrol &= ~LCD_BLINKON;
162 command(LCD_DISPLAYCONTROL | _displaycontrol);
165 void Max_LCD::blink() {
166 _displaycontrol |= LCD_BLINKON;
167 command(LCD_DISPLAYCONTROL | _displaycontrol);
170 // These commands scroll the display without changing the RAM
172 void Max_LCD::scrollDisplayLeft(void) {
173 command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
176 void Max_LCD::scrollDisplayRight(void) {
177 command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
180 // This is for text that flows Left to Right
182 void Max_LCD::leftToRight(void) {
183 _displaymode |= LCD_ENTRYLEFT;
184 command(LCD_ENTRYMODESET | _displaymode);
187 // This is for text that flows Right to Left
189 void Max_LCD::rightToLeft(void) {
190 _displaymode &= ~LCD_ENTRYLEFT;
191 command(LCD_ENTRYMODESET | _displaymode);
194 // This will 'right justify' text from the cursor
196 void Max_LCD::autoscroll(void) {
197 _displaymode |= LCD_ENTRYSHIFTINCREMENT;
198 command(LCD_ENTRYMODESET | _displaymode);
201 // This will 'left justify' text from the cursor
203 void Max_LCD::noAutoscroll(void) {
204 _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
205 command(LCD_ENTRYMODESET | _displaymode);
208 // Allows us to fill the first 8 CGRAM locations
209 // with custom characters
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++) {
219 /*********** mid level commands, for sending data/cmds */
221 inline void Max_LCD::command(uint8_t value) {
223 delayMicroseconds(100);
226 #if defined(ARDUINO) && ARDUINO >=100
228 inline size_t Max_LCD::write(uint8_t value) {
230 return 1; // Assume success
234 inline void Max_LCD::write(uint8_t value) {
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
244 delayMicroseconds(2);
246 delayMicroseconds(2);
248 lcdPins &= 0x0f; //prepare place for the lower nibble
249 lcdPins |= (val << 4) & 0xf0; //copy lower nibble to LCD variable
254 delayMicroseconds(100);