ERC1602-4 i2c Text LCD library for East Rising COG display in i2c mode using the ST7032i controller IC.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TextLCD_ERC1602_4.h Source File

TextLCD_ERC1602_4.h

00001 #include    <stdarg.h>
00002 #include    "mbed.h"
00003 #include    "I2cBusDevice.h"
00004 
00005 //EastRising ERC1602-4 i2c display using ST7032i
00006 
00007 
00008 //  i2c address
00009 const char    ERC1602_4_addr = 0x7C;
00010 
00011 //  initialization command sequence
00012 const char    Comm_FunctionSet_Normal      = 0x38;
00013 const char    Comm_FunctionSet_Extended    = 0x39;
00014 const char    Comm_InternalOscFrequency    = 0x1C;
00015 const char    Comm_ContrastSet             = 0x70;
00016 const char    Comm_PwrIconContrast         = 0x57;
00017 const char    Comm_FollowerCtrl            = 0x6C;
00018 const char    Comm_DisplayOnOff            = 0x0C;
00019 const char    Comm_ClearDisplay            = 0x01;
00020 const char    Comm_EntryModeSet            = 0x04;
00021 const char    Comm_ReturnHome              = 0x02;
00022 
00023 //  general commands
00024 const char    Comm_SetDDRAMAddress        = 0x80;
00025 const char    DDRAMAddress_Ofst[]         = {0x00, 0x40};
00026 const char    Comm_SetCGRAM               = 0x40;
00027 
00028 //  setting values
00029 const char    default_Contrast            = 0x35;
00030 const char    COMMAND                     = 0x00;
00031 const char    DATA                        = 0x40;
00032 const char    MaxCharsInALine             = 0x10;    //buffer depth for one line (no scroll function used)
00033 const char    init_seq0_length  = 7;
00034 const char    init_seq0[ init_seq0_length ]
00035 =    {
00036     Comm_FunctionSet_Normal,
00037     Comm_ReturnHome,             //This may be required to reset the scroll function
00038     Comm_FunctionSet_Extended,
00039     Comm_InternalOscFrequency,
00040     Comm_ContrastSet            | ( default_Contrast       & 0xF),
00041     Comm_PwrIconContrast        | ((default_Contrast >> 4) & 0x3),
00042     Comm_FollowerCtrl           | 0x0A,
00043 
00044 };
00045 
00046 const char    init_seq1_length  = 3;
00047 const char    init_seq1[ init_seq1_length ]
00048 =    {
00049     Comm_DisplayOnOff,
00050     Comm_ClearDisplay,
00051     Comm_EntryModeSet,
00052 };
00053 
00054 class TextLCD_ERC1602_4 : I2cBusDevice {
00055 public:
00056 
00057     explicit TextLCD_ERC1602_4( I2C *LCD, char dev_address = ERC1602_4_addr, char *init_massage = NULL ) : I2cBusDevice( LCD, dev_address ) {
00058     restart();
00059     }
00060     ~TextLCD_ERC1602_4() {
00061     }
00062 
00063     void restart( void ) {      
00064        char *init_massage = NULL; 
00065         wait_ms(1);    //    interval after hardware reset
00066         for ( int i = 0; i < init_seq0_length; i++ ) {
00067             lcd_command( init_seq0[ i ] );
00068             wait_us(30);}
00069         for ( int i = 0; i < init_seq1_length; i++ ) {
00070             lcd_command( init_seq1[ i ] );}
00071         if ( init_massage )
00072             puts( 0, init_massage );
00073         set_CGRAM( 7, '\x1F' );
00074         curs[ 0 ]    = 0;
00075         curs[ 1 ]    = 0;
00076 }
00077 
00078     void cls( void ) {
00079         lcd_command( Comm_ClearDisplay );
00080         wait_ms(2);
00081         curs[ 0 ]    = 0;
00082         curs[ 1 ]    = 0;
00083     }
00084     void put_custom_char( char c_code, const char *cg, char x, char y ) {
00085         for ( int i = 0; i < 5; i++ ) {
00086             set_CGRAM( c_code, cg );
00087             putcxy( c_code, x, y );
00088         }
00089     }
00090     
00091     void contrast( char contrast ) {
00092         lcd_command( Comm_FunctionSet_Extended );
00093         lcd_command( Comm_ContrastSet         |  (contrast     & 0x0f) );
00094         lcd_command( Comm_PwrIconContrast     | ((contrast>>4) & 0x03) );
00095         lcd_command( Comm_FunctionSet_Normal   );
00096     }
00097 
00098     void set_CGRAM( char char_code, const char* cg ) {
00099         for ( int i = 0; i < 8; i++ ) {
00100             lcd_command( (Comm_SetCGRAM | (char_code << 3) | i) );
00101             lcd_data( *cg++ );
00102         }
00103     }
00104 
00105     void set_CGRAM( char char_code, char v ) {
00106         char    c[ 8 ];
00107         for ( int i = 0; i < 8; i++ )
00108             c[ i ]    = v;
00109         set_CGRAM( char_code, c );
00110     }
00111 
00112     void putcxy( char c, char x, char y ) {
00113         if ( (x >= MaxCharsInALine) || (y >= 2) )
00114             return;
00115 
00116         lcd_command( (Comm_SetDDRAMAddress | DDRAMAddress_Ofst[ y ]) + x );
00117         lcd_data( c );
00118     }
00119 
00120     void putc( char line, char c ) {
00121         if ( (c == '\n') || (c == '\r') ) {
00122             clear_rest_of_line( line );
00123             curs[ line ]    = 0;
00124             return;
00125         }
00126         putcxy( c, curs[ line ]++, line );
00127     }
00128 
00129     void puts( char line, char *s ) {
00130         while ( char c    = *s++ )
00131             putc( line, c );
00132     }
00133 
00134     void printf( char line, char *format, ... ) {
00135         char        s[ 32 ];
00136         va_list        args;
00137         va_start( args, format );
00138         vsnprintf( s, 32, format, args );
00139         va_end( args );
00140         puts( line, s );
00141     }
00142 
00143 private:
00144     char    curs[2];
00145     void clear_rest_of_line( char line ) {
00146         for ( int i = curs[ line ]; i < MaxCharsInALine; i++ )
00147             putcxy( ' ', i, line );
00148     }
00149 
00150     int lcd_write( char first, char second ) {
00151         char cmd[2];
00152         cmd[ 0 ]    = first;
00153         cmd[ 1 ]    = second;          
00154         return ( write( cmd, 2 ) );        
00155     }
00156 
00157     int lcd_command( char command ) {    
00158         return ( lcd_write( COMMAND, command ) );
00159     }
00160     int lcd_data( char data ) {
00161         return ( lcd_write( DATA, data ) );
00162     }
00163 }
00164 ;