Rob Keij / LCD_4x20_MCC0G42005A6W
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCC0G42005A6W.cpp Source File

MCC0G42005A6W.cpp

00001 #include "mbed.h"
00002 #include <stdarg.h>
00003 #include "MCC0G42005A6W.h"
00004 
00005 // Note on I2C pull up resistors  These should not be too low otherwise the display will
00006 // not pull the SDA line low enough to register an ACK.  Suggest 10K.
00007 // based on  Text LCD module "SB1602E" class library  @author  Tedd OKANO, Masato YAMANISHI & Toyomasa Watarai
00008 
00009 
00010 LCD_COG::LCD_COG( PinName I2C_sda, PinName I2C_scl, const char *init_message ) : i2c_p( new I2C( I2C_sda, I2C_scl ) ), i2c( *i2c_p ), charsInLine( MaxCharsInALine )
00011 {
00012     init( init_message );
00013     i2c.frequency(50000);
00014 
00015 }
00016 
00017 LCD_COG::LCD_COG( I2C &i2c_, const char *init_message ) : i2c_p( NULL ), i2c( i2c_ ), charsInLine( MaxCharsInALine )
00018 {
00019     init( init_message );
00020     i2c.frequency(50000);    
00021 }
00022 
00023 LCD_COG::~LCD_COG()
00024 {
00025     if ( NULL != i2c_p )
00026         delete  i2c_p;
00027 }
00028 
00029 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00030 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00031 void LCD_COG::init( const char *init_message )
00032 {
00033 
00034     const char init_seq0[]  = {
00035 
00036         0x3A,    // 8-Bit data length extension Bit RE=1; REV=0                                send_command(0x3a); // 8bit, 4line, IS=0, RE=1
00037         // 0x09,    // 4 line display //??????
00038         TOPVIEW, // Bottom view       view bottom view(0x06), top view (0x05)  BOTTOMVIEW
00039         0x1E,    // Bias setting BS1=1                                                         send_command(0x1e); // set BS1 (1/6 bias)
00040         0x39,    // 8-Bit data length extension Bit RE=0; IS=1                                 send_command(0x39); // 8bit, 4line, IS=1 , RE=0
00041         0x1c,    // BS0=1 -> Bias=1/6                                                          send_command(0x1c); // set BS0 (1/6 bias) + osc
00042         0x6d,    // 0110 Set DON and amp ratio                                                 send_command(0x6d); // Set DON and amp ratio
00043         0x5d,    // 0101 Set ION, BON, contrast bits 4&5                                       send_command(0x5d); // Set ION, BON, contrast bits 4&5
00044         0x79,    // Set contrast (DB3-DB0=C3-C0)                                               send_command(0x79); // Set contrast lower 4bits
00045         0x38,    // 8-Bit data length extension Bit RE=0; IS=0
00046         Comm_DisplayOnOff,
00047         Comm_ClearDisplay,
00048         Comm_EntryModeSet,
00049     };
00050 
00051     //  i2c_addr    = SLAVEADRESS;
00052 
00053     wait( 0.04 );    // 40ms    interval after hardware reset
00054 
00055     for ( unsigned int i = 0; i < sizeof( init_seq0 ); i++ ) {
00056         lcd_command( init_seq0[ i ] );
00057         wait(30e-6);// // 30e-6  0.00003 30ns ?  
00058     }
00059 
00060     set_CGRAM( 7, '\x1F' );
00061 
00062     curs[0] = 0;
00063     curs[1] = 0;
00064     curs[2] = 0;
00065     curs[3] = 0;
00066 
00067     if ( init_message ) {
00068         //i2c.frequency(100000);
00069         puts( 0, init_message );
00070         curs[0] = 0;
00071 //        puts( 1, "Dit is de 2e regel 2" ); curs[1] = 0;
00072 //        puts( 2, "Dit is de 3e regel 3" ); curs[2] = 0;
00073 //        puts( 3, "Dit is de 4e regel 4" ); curs[3] = 0;
00074     }
00075 }
00076 
00077 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00078 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00079 void LCD_COG::printf( const char line, const char *format, ... )
00080 {
00081     char    s[ 32 ];
00082     va_list args;
00083 
00084     va_start( args, format );
00085     vsnprintf( s, 32, format, args );
00086     va_end( args );
00087 
00088     puts( line, s );
00089 }
00090 
00091 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00092 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00093 
00094 void LCD_COG::printf( char x, unsigned char y, const char *format, ... )
00095 {
00096     char    s[ 32 ];
00097     va_list args;
00098 
00099     va_start( args, format );
00100     vsnprintf( s, 32, format, args );
00101     va_end( args );
00102 
00103     curs[ y ] = x;
00104     puts( y, s );
00105 }
00106 
00107 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00108 void LCD_COG::putc( unsigned char line, char c )
00109 {
00110 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00111     if ( (c == '\n') || (c == '\r') ) {
00112         clear_rest_of_line( line );
00113         curs[ line ]    = 0;
00114         return;
00115     }
00116 
00117     putcxy( c, curs[line]++, line );
00118 }
00119 
00120 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00121 void LCD_COG::puts( char line, const char *s )
00122 {
00123 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00124     while ( char c    = *s++ )
00125         putc( line, c );
00126 }
00127 
00128 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00129 void LCD_COG::putcxy( char c, unsigned char x, unsigned char y )
00130 {
00131 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00132     const char    Comm_SetDDRAMAddress        =   LCD_HOME_L1;
00133     const char    DDRAMAddress_Ofst[]         = { LINE1, LINE2, LINE3, LINE4 };
00134 
00135     if ( (x >= charsInLine) || (y >= LINES) )
00136         return;
00137 
00138     lcd_command( (Comm_SetDDRAMAddress | DDRAMAddress_Ofst[y]) + x );
00139     lcd_data( c );
00140 }
00141 
00142 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00143 void LCD_COG::clear( void )
00144 {
00145 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00146     lcd_command( Comm_ClearDisplay );
00147     wait( 2e-3 );  // 2e-3  0.002  2ms
00148     curs[ 0 ]    = 0;
00149     curs[ 1 ]    = 0;
00150     curs[ 2 ]    = 0;
00151     curs[ 3 ]    = 0;
00152 }
00153 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00154 void LCD_COG::contrast( char contrast )
00155 {
00156 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00157     lcd_command( Comm_FunctionSet_Extended );
00158     lcd_command( Comm_ContrastSet         |  (contrast     & 0x0f) );
00159     lcd_command( Comm_PwrIconContrast     | ((contrast>>4) & 0x03) );
00160     lcd_command( Comm_FunctionSet_Normal   );
00161 }
00162 
00163 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00164 void LCD_COG::put_custom_char( char c_code, const char *cg, char x, char y )
00165 {
00166 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00167     for ( int i = 0; i < 5; i++ ) {
00168         set_CGRAM( c_code, cg );
00169         putcxy( c_code, x, y );
00170     }
00171 }
00172 
00173 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00174 void LCD_COG::set_CGRAM( char char_code, const char* cg )
00175 {
00176 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00177     for ( int i = 0; i < 8; i++ ) {
00178         lcd_command( (Comm_SetCGRAM | (char_code << 3) | i) );
00179         lcd_data( *cg++ );
00180     }
00181 }
00182 
00183 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00184 void LCD_COG::set_CGRAM( char char_code, char v )
00185 {
00186 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00187     char    c[ 8 ];
00188 
00189     for ( int i = 0; i < 8; i++ )
00190         c[ i ]    = v;
00191 
00192     set_CGRAM( char_code, c );
00193 }
00194 
00195 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00196 void LCD_COG::clear_rest_of_line( unsigned char line )
00197 {
00198 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00199     for ( int i = curs[ line ]; i < charsInLine; i++ )
00200         putcxy( ' ', i, line );
00201 }
00202 
00203 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00204 int LCD_COG::lcd_write( char first, char second )
00205 {
00206 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00207     char cmd[2];
00208 
00209     cmd[ 0 ]    = first;
00210     cmd[ 1 ]    = second;
00211 
00212     return ( i2c.write( SLAVEADRESS, cmd, 2 ) );
00213 }
00214 
00215 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00216 int LCD_COG::lcd_command( char command )
00217 {
00218 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00219     return ( lcd_write( COMMAND, command ) );
00220 }
00221 
00222 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00223 int LCD_COG::lcd_data( char data )
00224 {
00225 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00226     return ( lcd_write( DATA, data ) );
00227 }
00228