Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TextLCD_SB1602E_test.cpp Source File

TextLCD_SB1602E_test.cpp

00001 /*
00002  *  LCD module "SB1602E" test application
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  *  Copyright (c) 2010 Tedd OKANO
00010  *  Released under the MIT License: http://mbed.org/license/mit
00011  *
00012  *  revision 1.0  22-Jan-2010   a. 1st release
00013  *  revision 1.1  23-Jan-2010   a. class and app name has been changed from lcd_SB1602E to TextLCD_SB1602E
00014  *                              b. printf() test added
00015  *                              c. copyright notice added
00016  */
00017 
00018 #include    "mbed.h"
00019 #include    "TextLCD_SB1602E.h"
00020 
00021 #define        TEST_LOOP                    1
00022 #define        DEFAULT_TIME_INTERVAL        1.0
00023 #define        DEFAULT_TIME_INTERVAL_PUTx   0.2
00024 
00025 #define        CONTRAST_ADJUST_TEST
00026 #define        PRINTF_TEST
00027 #define        CGRAM_FUNCTION_TEST
00028 #define        PUTS_TEST_WITH_ESCAPE
00029 #define        PUTS_TEST_WITHOUT_ESCAPE
00030 #define        PUTC_TEST
00031 
00032 
00033 I2C                i2c( p9, p10 );        // sda, scl
00034 Serial            pc( USBTX, USBRX ); // tx, rx
00035 TextLCD_SB1602E    lcd( &i2c );
00036 
00037 static char    cg[8][8]        =    {// I hope I can improve this bitmap sometime in future (^^;
00038     { 0x1E, 0x1F, 0x1F, 0x1D, 0x1C, 0x1C, 0x1C, 0x1C },
00039     { 0x01, 0x02, 0x13, 0x1B, 0x1F, 0x1F, 0x0F, 0x15 },
00040     { 0x19, 0x1D, 0x1F, 0x0F, 0x1F, 0x0F, 0x1D, 0x18 },
00041     { 0x1F, 0x1D, 0x16, 0x1E, 0x17, 0x1F, 0x1A, 0x1C },
00042     { 0x1E, 0x1F, 0x07, 0x07, 0x1F, 0x1E, 0x00, 0x00 },
00043     { 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55 },
00044     { 0x44, 0x11, 0x44, 0x11, 0x44, 0x11, 0x44, 0x11 },
00045     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
00046 };
00047 
00048 void lcd_test( void );
00049 void set_lines_fixed_value( char line, char value );
00050 void block_pattaern( char direction, char polarity );
00051 
00052 
00053 int main() {
00054     lcd.puts( 0, "TextLCD_SB1602E \r" );
00055 
00056     for ( int i = 10; i > 0; i-- ) {
00057         lcd.printf( 1, "  count down: %2d\r", i );
00058         wait( 1.0 );
00059     }
00060 
00061     do {
00062         lcd_test();
00063     } while ( TEST_LOOP );
00064 }
00065 
00066 void lcd_test( void ) {
00067     char    s[ 18 ];
00068     int        i;
00069 
00070     pc.printf( "LCD test runing\n" );
00071     lcd.contrast( default_Contrast );
00072 
00073 #ifdef CONTRAST_ADJUST_TEST
00074     pc.printf( "  CONTRAST_ADJUST_TEST\n" );
00075     lcd.clear();
00076     lcd.puts( 1, "\x07\x07\x07\x07\x07 contrast()\r" );
00077     wait( DEFAULT_TIME_INTERVAL );
00078 
00079     lcd.puts( 0, "ABCDEFGHIJKLMNOP\r" );
00080 
00081     for ( int t = 0; t < 2; t++ ) {
00082         for ( i = 0; i <= 0x3F; i++ ) {
00083 
00084             lcd.printf( 1, "  cntrst = 0x%02X\r", i );
00085             lcd.contrast( i );
00086             wait ( 0.02 );
00087         }
00088 
00089         for ( i = 0x3E; i > 0 ; i-- ) {
00090             sprintf( s, "  cntrst = 0x%02X\r", i );
00091             lcd.puts( 1, s );
00092             lcd.contrast( i );
00093             wait ( 0.02 );
00094         }
00095 
00096     }
00097 
00098     lcd.contrast( default_Contrast );
00099 
00100 #endif
00101 #ifdef PRINTF_TEST
00102 
00103     pc.printf( "  PRINTF_TEST\n" );
00104     lcd.clear();
00105     lcd.puts( 1, "\x07\x07\x07\x07\x07\x07\x07 printf()\r" );
00106     wait( DEFAULT_TIME_INTERVAL );
00107 
00108     lcd.printf( 0, "%%6.4f, %%c, %%X\"\r" );
00109     for ( i = -37; i < 37; i++ ) {
00110         lcd.printf( 1, "%6.4f, %c, %x\r", (float)i / 37.0, 'a' + i, i + 37 );
00111         wait ( 0.1 );
00112     }
00113 
00114 #endif
00115 #ifdef CGRAM_FUNCTION_TEST
00116     pc.printf( "  CGRAM_FUNCTION_TEST\n" );
00117     lcd.clear();
00118     lcd.puts( 1, "\x07\x07\x07\x07 set_CGRAM()\r" );
00119     wait( DEFAULT_TIME_INTERVAL );
00120 
00121     for ( i = 0; i < 8; i++ )
00122         lcd.put_custom_char( i, cg[ i ], i, 0 );
00123 
00124     for ( i = 0; i < 8; i++ )
00125         lcd.put_custom_char( i, cg[ i ], i + 8, 0 );
00126 
00127     for ( i = 0; i < 8; i++ )
00128         lcd.put_custom_char( i, cg[ i ], i, 1 );
00129 
00130     for ( i = 0; i < 8; i++ )
00131         lcd.put_custom_char( i, cg[ i ], i + 8, 1 );
00132 
00133     wait( DEFAULT_TIME_INTERVAL );
00134     lcd.clear();
00135     lcd.set_CGRAM( 7, '\x1F' );
00136 
00137     lcd.set_CGRAM( 0, '\0' );
00138     lcd.set_CGRAM( 1, '\0' );
00139     lcd.set_CGRAM( 0, '\0' );
00140     lcd.set_CGRAM( 1, '\0' );
00141     set_lines_fixed_value( 0, 1 );
00142     set_lines_fixed_value( 1, 0 );
00143 
00144     block_pattaern( 0, 0 );
00145     block_pattaern( 0, 1 );
00146     block_pattaern( 1, 0 );
00147     block_pattaern( 1, 1 );
00148 
00149 
00150 #endif
00151 #ifdef PUTS_TEST_WITH_ESCAPE
00152     pc.printf( "  PUTS_TEST_WITH_ESCAPE\n" );
00153     lcd.clear();
00154     lcd.puts( 1, "\x07\x07\x07 puts(\"..\\r\")\r" );
00155     wait( DEFAULT_TIME_INTERVAL );
00156 
00157     lcd.puts( 0, "\rWelcome to mbed!\r" );
00158     lcd.puts( 1, "\rwww.mbed.com\r" );
00159     wait( DEFAULT_TIME_INTERVAL );
00160     lcd.puts( 0, "       This is a\r" );
00161     lcd.puts( 1, "        LCD test\r" );
00162     wait( DEFAULT_TIME_INTERVAL );
00163     lcd.puts( 0, "  22-Jan-2010\r" );
00164     lcd.puts( 1, "  Hello world!\r" );
00165     wait( DEFAULT_TIME_INTERVAL );
00166 
00167 #endif
00168 #ifdef PUTS_TEST_WITHOUT_ESCAPE
00169     pc.printf( "  PUTS_TEST_WITHOUT_ESCAPE\n" );
00170     lcd.clear();
00171     lcd.puts( 0, "\r" );
00172     lcd.puts( 1, "\x07\x07\x07\x07\x07 puts(\"..\")\r" );
00173     wait( DEFAULT_TIME_INTERVAL );
00174 
00175     lcd.puts( 0, "0" );
00176     lcd.puts( 1, "ABC" );
00177     wait( DEFAULT_TIME_INTERVAL_PUTx );
00178     lcd.puts( 0, "1" );
00179     lcd.puts( 1, "DEF" );
00180     wait( DEFAULT_TIME_INTERVAL_PUTx );
00181     lcd.puts( 0, "2" );
00182     lcd.puts( 1, "GHI" );
00183     wait( DEFAULT_TIME_INTERVAL_PUTx );
00184     lcd.puts( 0, "3" );
00185     lcd.puts( 1, "JKL" );
00186     wait( DEFAULT_TIME_INTERVAL_PUTx );
00187     lcd.puts( 0, "4" );
00188     lcd.puts( 1, "MNO" );
00189     wait( DEFAULT_TIME_INTERVAL_PUTx );
00190     lcd.puts( 0, "5" );
00191     lcd.puts( 1, "PQR" );
00192     wait( DEFAULT_TIME_INTERVAL_PUTx );
00193     lcd.puts( 0, "6" );
00194     wait( DEFAULT_TIME_INTERVAL_PUTx );
00195     lcd.puts( 0, "7" );
00196     wait( DEFAULT_TIME_INTERVAL_PUTx );
00197     lcd.puts( 0, "8" );
00198     wait( DEFAULT_TIME_INTERVAL_PUTx );
00199     lcd.puts( 0, "9" );
00200     wait( DEFAULT_TIME_INTERVAL_PUTx );
00201     lcd.puts( 0, "A" );
00202     wait( DEFAULT_TIME_INTERVAL_PUTx );
00203     lcd.puts( 0, "B" );
00204     wait( DEFAULT_TIME_INTERVAL_PUTx );
00205     lcd.puts( 0, "C" );
00206     wait( DEFAULT_TIME_INTERVAL_PUTx );
00207     lcd.puts( 0, "D" );
00208     wait( DEFAULT_TIME_INTERVAL_PUTx );
00209     lcd.puts( 0, "E" );
00210     wait( DEFAULT_TIME_INTERVAL_PUTx );
00211     lcd.puts( 0, "F" );
00212     wait( DEFAULT_TIME_INTERVAL_PUTx );
00213 
00214 #endif
00215 #ifdef PUTC_TEST
00216     pc.printf( "  PUTC_TEST\n" );
00217     lcd.clear();
00218     lcd.puts( 1, "\x07\x07\x07\x07\x07\x07\x07\x07\x07 putc()\r" );
00219     wait( DEFAULT_TIME_INTERVAL );
00220 
00221     for ( i = 0; i < 16; i++ ) {
00222         lcd.putc( 0, '0' + i );
00223         lcd.putc( 1, 'A' + i );
00224         wait( DEFAULT_TIME_INTERVAL_PUTx );
00225     }
00226 #endif
00227 }
00228 
00229 void set_lines_fixed_value( char line, char value ) {
00230     for ( int i = 0; i < 16; i++ )
00231         lcd.putc( line, value );
00232 
00233     lcd.puts( line, "\r" );
00234 }
00235 
00236 
00237 void block_pattaern( char direction, char polarity ) {
00238     char    c[ 8 ];
00239 
00240     for ( int x = 0; x < 2; x++ ) {
00241         for ( int i = 0; i < 8; i++ ) {
00242             for ( int j = 0; j < 8; j++ ) {
00243                 if ( j <= i )
00244                     c[ (direction ? j : 7 - j) ]    = (polarity ?  0x00: ~0x00 );
00245                 else
00246                     c[ (direction ? j : 7 - j) ]    = (polarity ? ~0x00 : 0x00 );
00247             }
00248 
00249             lcd.set_CGRAM( (x ? (direction ? 0 : 1) : (direction ? 1 : 0)), c );
00250             wait( 0.05 );
00251         }
00252     }
00253 }