一関 Aチーム / ArduinoUsbHostShield
Committer:
kotakku
Date:
Sat Jan 18 15:06:35 2020 +0000
Revision:
0:b1ce54272580
1.0.0 first commit

Who changed what in which revision?

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