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.
LCD.cpp
00001 /* mbed LCD Library, for a 4-bit LCD based on HD44780 00002 * Copyright (c) 2007-2010, hb9gaa 00003 * 00004 */ 00005 00006 #include "LCD.h" 00007 #include "mbed.h" 00008 00009 TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1, 00010 PinName d2, PinName d3, LCDType type) : _rs(rs), _rw(rw), 00011 _e(e), _d(d0, d1, d2, d3), 00012 _type(type) { 00013 00014 _e = 1; 00015 _rw = 0; 00016 _rs = 0; // command mode 00017 00018 wait(0.015); // Wait 15ms to ensure powered up 00019 00020 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) 00021 for (int i=0; i<3; i++) { 00022 writeByte(0x3); 00023 wait(0.00164); // this command takes 1.64ms, so wait for it 00024 } 00025 writeByte(0x2); // 4-bit mode 00026 wait(0.0001f); // most instructions take 100us 00027 00028 writeCommand(0x28); // Function set 001 BW N F - - 00029 writeCommand(0x0C); 00030 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes 00031 cls(); 00032 } 00033 00034 void TextLCD::character(int column, int row, int c) { 00035 int a = address(column, row); 00036 writeCommand(a); 00037 writeData(c); 00038 } 00039 00040 void TextLCD::cls() { 00041 writeCommand(0x01); // cls, and set cursor to 0 00042 wait(0.004100f); // This command takes 4.1 ms 00043 locate(0, 0); 00044 } 00045 00046 void TextLCD::locate(int row, int column) { 00047 _row = row; 00048 _column = column; 00049 } 00050 00051 int TextLCD::_putc(int value) { 00052 if (value == '\n') { 00053 _column = 0; 00054 _row++; 00055 if (_row >= rows()) { 00056 _row = 0; 00057 } 00058 } else { 00059 character(_column, _row, value); 00060 _column++; 00061 if (_column >= columns()) { 00062 _column = 0; 00063 _row++; 00064 if (_row >= rows()) { 00065 _row = 0; 00066 } 00067 } 00068 } 00069 return value; 00070 } 00071 00072 int TextLCD::_getc() { 00073 return -1; 00074 } 00075 00076 void TextLCD::writeByte(int value) { 00077 _d = value >> 4; 00078 wait(0.0001); // most instructions take 100us 00079 _e = 0; 00080 wait(0.0001); 00081 _e = 1; 00082 _d = value >> 0; 00083 wait(0.0001); 00084 _e = 0; 00085 wait(0.0001); // most instructions take 100us 00086 _e = 1; 00087 } 00088 00089 void TextLCD::writeCommand(int command) { 00090 _rs = 0; 00091 writeByte(command); 00092 } 00093 00094 void TextLCD::writeData(int data) { 00095 _rs = 1; 00096 writeByte(data); 00097 } 00098 00099 int TextLCD::address(int column, int row) { 00100 switch (_type) { 00101 case LCD20x4: 00102 switch (row) { 00103 case 0: 00104 return 0x80 + column; 00105 case 1: 00106 return 0xc0 + column; 00107 case 2: 00108 return 0x94 + column; 00109 case 3: 00110 return 0xd4 + column; 00111 } 00112 case LCD16x2B: 00113 return 0x80 + (row * 40) + column; 00114 case LCD16x2: 00115 case LCD20x2: 00116 default: 00117 return 0x80 + (row * 0x40) + column; 00118 } 00119 } 00120 00121 int TextLCD::columns() { 00122 switch (_type) { 00123 case LCD20x4: 00124 case LCD20x2: 00125 return 20; 00126 case LCD16x2: 00127 case LCD16x2B: 00128 default: 00129 return 16; 00130 } 00131 } 00132 00133 int TextLCD::rows() { 00134 switch (_type) { 00135 case LCD20x4: 00136 return 4; 00137 case LCD16x2: 00138 case LCD16x2B: 00139 case LCD20x2: 00140 default: 00141 return 2; 00142 } 00143 }
Generated on Tue Jul 12 2022 20:54:01 by
1.7.2