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.
Fork of SwitchSciencembedLPC824_test by
TextLCD_SB1602E.h
00001 /* 00002 * LCD module "SB1602E" library 00003 * 00004 * SB1602E is an I2C based low voltage text LCD panel (based Sitronix ST7032 chip) 00005 * The module by StrawberryLinux 00006 * http://strawberry-linux.com/catalog/items?code=27002 (Online shop page (Japanese)) 00007 * http://strawberry-linux.com/pub/ST7032i.pdf (datasheet of the chip) 00008 * 00009 * This is a library to operate this module easy. 00010 * 00011 * Copyright (c) 2010 Tedd OKANO 00012 * Released under the MIT License: http://mbed.org/license/mit 00013 * 00014 * revision 1.0 22-Jan-2010 a. 1st release 00015 * revision 1.1 23-Jan-2010 a. class name has been changed from lcd_SB1602E to TextLCD_SB1602E 00016 * b. printf() added 00017 * c. copyright notice added 00018 */ 00019 00020 #ifndef MBED_TextLCD_SB1602E 00021 #define MBED_TextLCD_SB1602E 00022 00023 #include <stdarg.h> 00024 #include "mbed.h" 00025 #include "I2cBusDevice.h" 00026 00027 // SB1602E IIC address 00028 00029 const char SB1602E_addr = 0x7C; 00030 00031 // SB1602E initialization command sequence 00032 00033 #ifdef INIT_VALUE_DATASHEET_ORIGINAL 00034 00035 const char Comm_FunctionSet_Normal = 0x38; 00036 const char Comm_FunctionSet_Extended = 0x39; 00037 const char Comm_InternalOscFrequency = 0x14; 00038 const char Comm_ContrastSet = 0x78; 00039 const char Comm_PwrIconContrast = 0x5E; 00040 const char Comm_FollowerCtrl = 0x6A; 00041 const char Comm_DisplayOnOff = 0x0C; 00042 const char Comm_ClearDisplay = 0x01; 00043 const char Comm_EntryModeSet = 0x06; 00044 00045 #else 00046 00047 const char Comm_FunctionSet_Normal = 0x38; 00048 const char Comm_FunctionSet_Extended = 0x39; 00049 const char Comm_InternalOscFrequency = 0x14; 00050 const char Comm_ContrastSet = 0x70; 00051 const char Comm_PwrIconContrast = 0x5C; 00052 const char Comm_FollowerCtrl = 0x60; 00053 const char Comm_DisplayOnOff = 0x0C; 00054 const char Comm_ClearDisplay = 0x01; 00055 const char Comm_EntryModeSet = 0x04; 00056 const char Comm_ReturnHome = 0x02; 00057 00058 #endif 00059 00060 // SB1602E general commands 00061 00062 const char Comm_SetDDRAMAddress = 0x80; 00063 const char DDRAMAddress_Ofst[] = { 0x00, 0x40 }; 00064 00065 const char Comm_SetCGRAM = 0x40; 00066 00067 // SB1602E setting values 00068 00069 const char default_Contrast = 0x35; 00070 00071 const char COMMAND = 0x00; 00072 const char DATA = 0x40; 00073 00074 const char MaxCharsInALine = 0x10; // buffer deoth for one line (no scroll function used) 00075 00076 const char init_seq0_length = 7; 00077 const char init_seq0[ init_seq0_length ] 00078 = { 00079 Comm_FunctionSet_Normal, 00080 Comm_ReturnHome, // This may be required to reset the scroll function 00081 Comm_FunctionSet_Extended, 00082 Comm_InternalOscFrequency, 00083 Comm_ContrastSet | ( default_Contrast & 0xF), 00084 Comm_PwrIconContrast | ((default_Contrast >> 4) & 0x3), 00085 Comm_FollowerCtrl | 0x0A, 00086 00087 }; 00088 // required 30us interval 00089 00090 const char init_seq1_length = 3; 00091 const char init_seq1[ init_seq1_length ] 00092 = { 00093 Comm_DisplayOnOff, 00094 Comm_ClearDisplay, 00095 Comm_EntryModeSet, 00096 }; 00097 // required 30us, 2ms interval 00098 00099 00100 class TextLCD_SB1602E : I2cBusDevice { 00101 public: 00102 00103 explicit TextLCD_SB1602E( I2C *i2c, char dev_address = SB1602E_addr, char *init_massage = NULL ) : I2cBusDevice( i2c, dev_address ) { 00104 wait( 0.04 ); // interval after hardware reset 00105 00106 for ( int i = 0; i < init_seq0_length; i++ ) { 00107 lcd_command( init_seq0[ i ] ); 00108 wait( 30e-6 ); 00109 } 00110 00111 wait( 0.2 ); 00112 00113 for ( int i = 0; i < init_seq1_length; i++ ) { 00114 lcd_command( init_seq1[ i ] ); 00115 wait( 2e-3 ); 00116 } 00117 00118 if ( init_massage ) 00119 puts( 0, init_massage ); 00120 00121 set_CGRAM( 7, '\x1F' ); 00122 00123 curs[ 0 ] = 0; 00124 curs[ 1 ] = 0; 00125 } 00126 00127 00128 ~TextLCD_SB1602E() { 00129 } 00130 00131 void clear( void ) { 00132 lcd_command( Comm_ClearDisplay ); 00133 wait( 2e-3 ); 00134 curs[ 0 ] = 0; 00135 curs[ 1 ] = 0; 00136 } 00137 00138 00139 void put_custom_char( char c_code, const char *cg, char x, char y ) { 00140 for ( int i = 0; i < 5; i++ ) { 00141 set_CGRAM( c_code, cg ); 00142 putcxy( c_code, x, y ); 00143 } 00144 } 00145 00146 void contrast( char contrast ) { 00147 lcd_command( Comm_FunctionSet_Extended ); 00148 lcd_command( Comm_ContrastSet | (contrast & 0x0f) ); 00149 lcd_command( Comm_PwrIconContrast | ((contrast>>4) & 0x03) ); 00150 lcd_command( Comm_FunctionSet_Normal ); 00151 } 00152 00153 void set_CGRAM( char char_code, const char* cg ) { 00154 for ( int i = 0; i < 8; i++ ) { 00155 lcd_command( (Comm_SetCGRAM | (char_code << 3) | i) ); 00156 lcd_data( *cg++ ); 00157 } 00158 } 00159 00160 void set_CGRAM( char char_code, char v ) { 00161 char c[ 8 ]; 00162 00163 for ( int i = 0; i < 8; i++ ) 00164 c[ i ] = v; 00165 00166 set_CGRAM( char_code, c ); 00167 } 00168 00169 void putcxy( char c, char x, char y ) { 00170 if ( (x >= MaxCharsInALine) || (y >= 2) ) 00171 return; 00172 00173 lcd_command( (Comm_SetDDRAMAddress | DDRAMAddress_Ofst[ y ]) + x ); 00174 lcd_data( c ); 00175 } 00176 00177 void putc( char line, char c ) { 00178 if ( (c == '\n') || (c == '\r') ) { 00179 clear_lest_of_line( line ); 00180 curs[ line ] = 0; 00181 return; 00182 } 00183 00184 putcxy( c, curs[ line ]++, line ); 00185 } 00186 00187 void puts( char line, char *s ) { 00188 while ( char c = *s++ ) 00189 putc( line, c ); 00190 } 00191 00192 void printf( char line, char *format, ... ) { 00193 char s[ 32 ]; 00194 va_list args; 00195 00196 va_start( args, format ); 00197 vsnprintf( s, 32, format, args ); 00198 va_end( args ); 00199 00200 puts( line, s ); 00201 } 00202 00203 private: 00204 char curs[2]; 00205 00206 void clear_lest_of_line( char line ) { 00207 for ( int i = curs[ line ]; i < MaxCharsInALine; i++ ) 00208 putcxy( ' ', i, line ); 00209 } 00210 00211 int lcd_write( char first, char second ) { 00212 char cmd[2]; 00213 00214 cmd[ 0 ] = first; 00215 cmd[ 1 ] = second; 00216 00217 return ( write( cmd, 2 ) ); 00218 00219 } 00220 00221 int lcd_command( char command ) { 00222 return ( lcd_write( COMMAND, command ) ); 00223 } 00224 00225 int lcd_data( char data ) { 00226 return ( lcd_write( DATA, data ) ); 00227 } 00228 } 00229 ; 00230 00231 #endif 00232 00233 00234 00235 00236 00237 00238 00239
Generated on Fri Jul 15 2022 13:53:23 by
1.7.2
