Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TextLCD_SR4_TestProgram ServoInterfaceBoardExample1
TextLCD_SR4.cpp
00001 /* mbed TextLCD_SR4 Library, for a 4-bit LCD based on HD44780 00002 * Copyright (c) 2007-2010, sford 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 * THE SOFTWARE. 00021 */ 00022 00023 /* 00024 * Modified for http://mbed.org/users/shintamainjp/notebook/textlcd_sr4_en/ 00025 * Shinichiro Nakamura 00026 */ 00027 00028 #include "TextLCD_SR4.h" 00029 #include "mbed.h" 00030 00031 TextLCD_SR4::TextLCD_SR4(PinName rs, PinName e, PinName dat, PinName clk, LCDType type) 00032 : _rs(rs), _e(e), _dat(dat), _clk(clk), _type(type) { 00033 00034 _e = 1; 00035 _rs = 0; // command mode 00036 00037 wait(0.015); // Wait 15ms to ensure powered up 00038 00039 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) 00040 for (int i = 0; i < 3; i++) { 00041 writeByte(0x3); 00042 wait(0.00164); // this command takes 1.64ms, so wait for it 00043 } 00044 writeByte(0x2); // 4-bit mode 00045 wait(0.000040f); // most instructions take 40us 00046 00047 writeCommand(0x28); // Function set 001 BW N F - - 00048 writeCommand(0x0C); 00049 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes 00050 cls(); 00051 } 00052 00053 void TextLCD_SR4::character(int column, int row, int c) { 00054 int a = address(column, row); 00055 writeCommand(a); 00056 writeData(c); 00057 } 00058 00059 void TextLCD_SR4::cls() { 00060 writeCommand(0x01); // cls, and set cursor to 0 00061 wait(0.00164f); // This command takes 1.64 ms 00062 locate(0, 0); 00063 } 00064 00065 void TextLCD_SR4::locate(int column, int row) { 00066 _column = column; 00067 _row = row; 00068 } 00069 00070 int TextLCD_SR4::_putc(int value) { 00071 if (value == '\n') { 00072 _column = 0; 00073 _row++; 00074 if (_row >= rows()) { 00075 _row = 0; 00076 } 00077 } else { 00078 character(_column, _row, value); 00079 _column++; 00080 if (_column >= columns()) { 00081 _column = 0; 00082 _row++; 00083 if (_row >= rows()) { 00084 _row = 0; 00085 } 00086 } 00087 } 00088 return value; 00089 } 00090 00091 int TextLCD_SR4::_getc() { 00092 return -1; 00093 } 00094 00095 void TextLCD_SR4::writeByte(int value) { 00096 writeToShiftRegister(value << 0); 00097 wait(0.000040f); // most instructions take 40us 00098 _e = 0; 00099 wait(0.000040f); 00100 _e = 1; 00101 writeToShiftRegister(value << 4); 00102 wait(0.000040f); 00103 _e = 0; 00104 wait(0.000040f); // most instructions take 40us 00105 _e = 1; 00106 } 00107 00108 void TextLCD_SR4::writeToShiftRegister(int value) { 00109 /* 00110 * MSB first. 00111 */ 00112 for (int i = 0; i < 8; i++) { 00113 if (value & (1 << (7 - i))) { 00114 _dat = 1; 00115 } else { 00116 _dat = 0; 00117 } 00118 _clk = 1; 00119 wait_us(1); 00120 _clk = 0; 00121 wait_us(1); 00122 } 00123 } 00124 00125 void TextLCD_SR4::writeCommand(int command) { 00126 _rs = 0; 00127 writeByte(command); 00128 } 00129 00130 void TextLCD_SR4::writeData(int data) { 00131 _rs = 1; 00132 writeByte(data); 00133 } 00134 00135 int TextLCD_SR4::address(int column, int row) { 00136 switch (_type) { 00137 case LCD20x4: 00138 switch (row) { 00139 case 0: 00140 return 0x80 + column; 00141 case 1: 00142 return 0xc0 + column; 00143 case 2: 00144 return 0x94 + column; 00145 case 3: 00146 return 0xd4 + column; 00147 } 00148 case LCD16x2B: 00149 return 0x80 + (row * 40) + column; 00150 case LCD16x2: 00151 case LCD20x2: 00152 default: 00153 return 0x80 + (row * 0x40) + column; 00154 } 00155 } 00156 00157 int TextLCD_SR4::columns() { 00158 switch (_type) { 00159 case LCD20x4: 00160 case LCD20x2: 00161 return 20; 00162 case LCD16x2: 00163 case LCD16x2B: 00164 default: 00165 return 16; 00166 } 00167 } 00168 00169 int TextLCD_SR4::rows() { 00170 switch (_type) { 00171 case LCD20x4: 00172 return 4; 00173 case LCD16x2: 00174 case LCD16x2B: 00175 case LCD20x2: 00176 default: 00177 return 2; 00178 } 00179 }
Generated on Tue Jul 19 2022 05:34:49 by
1.7.2