Version 2.0 of TextLCD_SB1602E. The old class driver has been rewritten in more common manner of I2C devices.

Dependents:   SB1602E_Hello BME280_LCD PreHeater PreHeater ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SB1602E.cpp Source File

SB1602E.cpp

00001 /** Text LCD module "SB1602E" class library
00002  *
00003  *  @author  Tedd OKANO, Masato YAMANISHI & Toyomasa Watarai
00004  *  @version 2.1
00005  *  @date    07-April-2015
00006  *
00007  *  SB1602E is an I2C based low voltage text LCD panel (based Sitronix ST7032 chip)
00008  *  The module by StrawberryLinux
00009  *  http://strawberry-linux.com/catalog/items?code=27002 (Online shop page (Japanese))
00010  *  http://strawberry-linux.com/pub/ST7032i.pdf (datasheet of the chip)
00011  *
00012  *  This is a library to operate this module easy.
00013  *
00014  *  Released under the Apache 2 license License
00015  *
00016  *  revision history (class lib name was "TextLCD_SB1602E")
00017  *    revision 1.0  22-Jan-2010   a. 1st release
00018  *    revision 1.1  23-Jan-2010   a. class name has been changed from lcd_SB1602E to TextLCD_SB1602E
00019  *                                b. printf() added
00020  *                                c. copyright notice added
00021  *    revision 1.3  02-May-2014   a. puticon() added (for SB1602B) by Masato YAMANISHI san
00022  *    revision 2.0  20-Oct-2014   a. class name is changed and published as "SB1602E"
00023  *                                b. re-written for better usability
00024  *    revision 2.1  07-Apl-2015   a. add printf() with X and Y position
00025  *                                b. add setter for number of chars in a line (e.g. 8x2 LCD support)
00026  */
00027 
00028 #include    <stdarg.h>
00029 #include    "mbed.h"
00030 #include    "SB1602E.h"
00031 
00032 
00033 SB1602E::SB1602E( PinName I2C_sda, PinName I2C_scl, char *init_massage ) : i2c_p( new I2C( I2C_sda, I2C_scl ) ), i2c( *i2c_p ), charsInLine( MaxCharsInALine )
00034 {
00035     init( init_massage );
00036 }
00037 
00038 SB1602E::SB1602E( I2C &i2c_, char *init_massage ) : i2c_p( NULL ), i2c( i2c_ ), charsInLine( MaxCharsInALine )
00039 {
00040     init( init_massage );
00041 }
00042 
00043 SB1602E::~SB1602E()
00044 {
00045     if ( NULL != i2c_p )
00046         delete  i2c_p;
00047 }
00048 
00049 #define     DEFAULT_CONTRAST    0x35
00050 
00051 void SB1602E::init( char *init_massage )
00052 {
00053     const char init_seq0[]  = {
00054         Comm_FunctionSet_Normal,
00055         Comm_ReturnHome,             //    This may be required to reset the scroll function
00056         Comm_FunctionSet_Extended,
00057         Comm_InternalOscFrequency,
00058         Comm_ContrastSet            | ( DEFAULT_CONTRAST       & 0xF),
00059         Comm_PwrIconContrast        | ((DEFAULT_CONTRAST >> 4) & 0x3),
00060         Comm_FollowerCtrl           | 0x0A,
00061     };
00062     const char init_seq1[]  = {
00063         Comm_DisplayOnOff,
00064         Comm_ClearDisplay,
00065         Comm_EntryModeSet,
00066     };
00067     
00068     i2c_addr    = 0x7C;
00069 
00070     wait( 0.04 );    //    interval after hardware reset
00071 
00072     for ( int i = 0; i < sizeof( init_seq0 ); i++ ) {
00073         lcd_command( init_seq0[ i ] );
00074         wait( 30e-6 );
00075     }
00076 
00077     wait( 0.2 );
00078 
00079     for ( int i = 0; i < sizeof( init_seq1 ); i++ ) {
00080         lcd_command( init_seq1[ i ] );
00081         wait( 2e-3 );
00082     }
00083 
00084     set_CGRAM( 7, '\x1F' );
00085 
00086     curs[ 0 ]    = 0;
00087     curs[ 1 ]    = 0;
00088 
00089     if ( init_massage )
00090     {
00091         puts( 0, init_massage );
00092         curs[ 0 ]    = 0;
00093     }
00094 }
00095 
00096 void SB1602E::printf( char line, char *format, ... )
00097 {
00098     char    s[ 32 ];
00099     va_list args;
00100 
00101     va_start( args, format );
00102     vsnprintf( s, 32, format, args );
00103     va_end( args );
00104 
00105     puts( line, s );
00106 }
00107 
00108 void SB1602E::printf( char x, char y, char *format, ... )
00109 {
00110     char    s[ 32 ];
00111     va_list args;
00112 
00113     va_start( args, format );
00114     vsnprintf( s, 32, format, args );
00115     va_end( args );
00116 
00117     curs[ y ] = x;
00118     puts( y, s );
00119 }
00120 
00121 void SB1602E::putc( char line, char c )
00122 {
00123     if ( (c == '\n') || (c == '\r') ) {
00124         clear_lest_of_line( line );
00125         curs[ line ]    = 0;
00126         return;
00127     }
00128 
00129     putcxy( c, curs[ line ]++, line );
00130 }
00131 
00132 void SB1602E::puts( char line, char *s )
00133 {
00134     while ( char c    = *s++ )
00135         putc( line, c );
00136 }
00137 
00138 void SB1602E::putcxy( char c, char x, char y )
00139 {
00140     const char    Comm_SetDDRAMAddress        = 0x80;
00141     const char    DDRAMAddress_Ofst[]         = { 0x00, 0x40 };
00142 
00143     if ( (x >= charsInLine) || (y >= 2) )
00144         return;
00145 
00146     lcd_command( (Comm_SetDDRAMAddress | DDRAMAddress_Ofst[ y ]) + x );
00147     lcd_data( c );
00148 }
00149 
00150 void SB1602E::clear( void )
00151 {
00152     lcd_command( Comm_ClearDisplay );
00153     wait( 2e-3 );
00154     curs[ 0 ]    = 0;
00155     curs[ 1 ]    = 0;
00156 }
00157 
00158 void SB1602E::contrast( char contrast )
00159 {
00160     lcd_command( Comm_FunctionSet_Extended );
00161     lcd_command( Comm_ContrastSet         |  (contrast     & 0x0f) );
00162     lcd_command( Comm_PwrIconContrast     | ((contrast>>4) & 0x03) );
00163     lcd_command( Comm_FunctionSet_Normal   );
00164 }
00165 
00166 void SB1602E::put_custom_char( char c_code, const char *cg, char x, char y )
00167 {
00168     for ( int i = 0; i < 5; i++ ) {
00169         set_CGRAM( c_code, cg );
00170         putcxy( c_code, x, y );
00171     }
00172 }
00173 
00174 void SB1602E::set_CGRAM( char char_code, const char* cg )
00175 {
00176     for ( int i = 0; i < 8; i++ ) {
00177         lcd_command( (Comm_SetCGRAM | (char_code << 3) | i) );
00178         lcd_data( *cg++ );
00179     }
00180 }
00181 
00182 void SB1602E::set_CGRAM( char char_code, char v )
00183 {
00184     char    c[ 8 ];
00185 
00186     for ( int i = 0; i < 8; i++ )
00187         c[ i ]    = v;
00188 
00189     set_CGRAM( char_code, c );
00190 }
00191 
00192 void SB1602E::clear_lest_of_line( char line )
00193 {
00194     for ( int i = curs[ line ]; i < charsInLine; i++ )
00195         putcxy( ' ', i, line );
00196 }
00197 
00198 int SB1602E::lcd_write( char first, char second )
00199 {
00200     char cmd[2];
00201 
00202     cmd[ 0 ]    = first;
00203     cmd[ 1 ]    = second;
00204 
00205     return ( i2c.write( i2c_addr, cmd, 2 ) );
00206 
00207 }
00208 
00209 int SB1602E::lcd_command( char command )
00210 {
00211     return ( lcd_write( COMMAND, command ) );
00212 }
00213 
00214 int SB1602E::lcd_data( char data )
00215 {
00216     return ( lcd_write( DATA, data ) );
00217 }
00218 
00219 //  Following function has been imported from Masato YAMANISHI san's code.
00220 //  Thank you!
00221 //  http://developer.mbed.org/users/masato/code/TextLCD_SB1602E/file/39110c58e55c/TextLCD_SB1602E.h
00222 
00223 const unsigned char icon_data[]= {
00224     // アイコンアドレス, 該当ビット
00225     0x00, 0x10, // 0b10000,
00226     0x02, 0x10, // 0b10000,
00227     0x04, 0x10, // 0b10000,
00228     0x06, 0x10, // 0b10000,
00229 
00230     0x07, 0x10, // 0b10000,
00231     0x07, 0x08, // 0b01000,
00232     0x09, 0x10, // 0b10000,
00233     0x0B, 0x10, // 0b10000,
00234 
00235     0x0D, 0x08, // 0b01000,
00236     0x0D, 0x04, // 0b00100,
00237     0x0D, 0x02, // 0b00010,
00238     0x0D, 0x10, // 0b10000,
00239 
00240     0x0F, 0x10, // 0b10000, // アンテナマーク
00241 };
00242 
00243 void SB1602E::puticon(unsigned short flg)
00244 {
00245     static unsigned char icon_buff[16]; // アイコンの編集用
00246     unsigned char i;
00247 
00248     for(i=0; i<sizeof(icon_data)/2; i++) {
00249         if(flg & (0x1000>>i)) { // 該当ビットが立っていたら
00250             icon_buff[icon_data[i*2]] |= icon_data[i*2+1];  // バッファを立てます。
00251         } else {
00252             icon_buff[icon_data[i*2]] &= ~icon_data[i*2+1]; // バッファをクリアします。
00253         }
00254     }
00255     // 一括でLCDに書き込みます。
00256     for(i=0; i<16; i++) {
00257         lcd_command(Comm_FunctionSet_Extended); // 0b00111001); // コマンド
00258         lcd_command(Comm_SetCGRAM + i); // 0b01000000+i);       // アイコン領域のアドレスを設定
00259         lcd_data(icon_buff[i]); // アイコンデータ
00260     }
00261 }
00262 
00263 
00264 
00265 
00266 
00267