Contrast not optimal

Fork of SB1602E by Tedd OKANO

Committer:
okano
Date:
Mon Oct 20 12:54:53 2014 +0000
Revision:
0:995f80348e02
version 2.0 of TextLCD_SB1602E

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okano 0:995f80348e02 1 /** Text LCD module "SB1602E" class library
okano 0:995f80348e02 2 *
okano 0:995f80348e02 3 * @author Tedd OKANO & Masato YAMANISHI
okano 0:995f80348e02 4 * @version 2.0
okano 0:995f80348e02 5 * @date 20-Oct-2014
okano 0:995f80348e02 6 *
okano 0:995f80348e02 7 * SB1602E is an I2C based low voltage text LCD panel (based Sitronix ST7032 chip)
okano 0:995f80348e02 8 * The module by StrawberryLinux
okano 0:995f80348e02 9 * http://strawberry-linux.com/catalog/items?code=27002 (Online shop page (Japanese))
okano 0:995f80348e02 10 * http://strawberry-linux.com/pub/ST7032i.pdf (datasheet of the chip)
okano 0:995f80348e02 11 *
okano 0:995f80348e02 12 * This is a library to operate this module easy.
okano 0:995f80348e02 13 *
okano 0:995f80348e02 14 * Released under the Apache 2 license License
okano 0:995f80348e02 15 *
okano 0:995f80348e02 16 * revision history (class lib name was "TextLCD_SB1602E")
okano 0:995f80348e02 17 * revision 1.0 22-Jan-2010 a. 1st release
okano 0:995f80348e02 18 * revision 1.1 23-Jan-2010 a. class name has been changed from lcd_SB1602E to TextLCD_SB1602E
okano 0:995f80348e02 19 * b. printf() added
okano 0:995f80348e02 20 * c. copyright notice added
okano 0:995f80348e02 21 * revision 1.3 02-May-2014 a. puticon() added (for SB1602B) by Masato YAMANISHI san
okano 0:995f80348e02 22 * revision 2.0 20-Oct-2014 a. class name is changed and published as "SB1602E"
okano 0:995f80348e02 23 * b. re-written for better usability
okano 0:995f80348e02 24 */
okano 0:995f80348e02 25
okano 0:995f80348e02 26 #include <stdarg.h>
okano 0:995f80348e02 27 #include "mbed.h"
okano 0:995f80348e02 28 #include "SB1602E.h"
okano 0:995f80348e02 29
okano 0:995f80348e02 30
okano 0:995f80348e02 31 SB1602E::SB1602E( PinName I2C_sda, PinName I2C_scl, char *init_massage ) : i2c_p( new I2C( I2C_sda, I2C_scl ) ), i2c( *i2c_p )
okano 0:995f80348e02 32 {
okano 0:995f80348e02 33 init( init_massage );
okano 0:995f80348e02 34 }
okano 0:995f80348e02 35
okano 0:995f80348e02 36 SB1602E::SB1602E( I2C &i2c_, char *init_massage ) : i2c_p( NULL ), i2c( i2c_ )
okano 0:995f80348e02 37 {
okano 0:995f80348e02 38 init( init_massage );
okano 0:995f80348e02 39 }
okano 0:995f80348e02 40
okano 0:995f80348e02 41 SB1602E::~SB1602E()
okano 0:995f80348e02 42 {
okano 0:995f80348e02 43 if ( NULL != i2c_p )
okano 0:995f80348e02 44 delete i2c_p;
okano 0:995f80348e02 45 }
okano 0:995f80348e02 46
okano 0:995f80348e02 47 void SB1602E::init( char *init_massage )
okano 0:995f80348e02 48 {
okano 0:995f80348e02 49 const char init_seq0[] = {
okano 0:995f80348e02 50 Comm_FunctionSet_Normal,
okano 0:995f80348e02 51 Comm_ReturnHome, // This may be required to reset the scroll function
okano 0:995f80348e02 52 Comm_FunctionSet_Extended,
okano 0:995f80348e02 53 Comm_InternalOscFrequency,
okano 0:995f80348e02 54 Comm_ContrastSet | ( DEFAULT_CONTRAST & 0xF),
okano 0:995f80348e02 55 Comm_PwrIconContrast | ((DEFAULT_CONTRAST >> 4) & 0x3),
okano 0:995f80348e02 56 Comm_FollowerCtrl | 0x0A,
okano 0:995f80348e02 57 };
okano 0:995f80348e02 58 const char init_seq1[] = {
okano 0:995f80348e02 59 Comm_DisplayOnOff,
okano 0:995f80348e02 60 Comm_ClearDisplay,
okano 0:995f80348e02 61 Comm_EntryModeSet,
okano 0:995f80348e02 62 };
okano 0:995f80348e02 63
okano 0:995f80348e02 64 i2c_addr = 0x7C;
okano 0:995f80348e02 65
okano 0:995f80348e02 66 wait( 0.04 ); // interval after hardware reset
okano 0:995f80348e02 67
okano 0:995f80348e02 68 for ( int i = 0; i < sizeof( init_seq0 ); i++ ) {
okano 0:995f80348e02 69 lcd_command( init_seq0[ i ] );
okano 0:995f80348e02 70 wait( 30e-6 );
okano 0:995f80348e02 71 }
okano 0:995f80348e02 72
okano 0:995f80348e02 73 wait( 0.2 );
okano 0:995f80348e02 74
okano 0:995f80348e02 75 for ( int i = 0; i < sizeof( init_seq1 ); i++ ) {
okano 0:995f80348e02 76 lcd_command( init_seq1[ i ] );
okano 0:995f80348e02 77 wait( 2e-3 );
okano 0:995f80348e02 78 }
okano 0:995f80348e02 79
okano 0:995f80348e02 80 set_CGRAM( 7, '\x1F' );
okano 0:995f80348e02 81
okano 0:995f80348e02 82 curs[ 0 ] = 0;
okano 0:995f80348e02 83 curs[ 1 ] = 0;
okano 0:995f80348e02 84
okano 0:995f80348e02 85 if ( init_massage )
okano 0:995f80348e02 86 {
okano 0:995f80348e02 87 puts( 0, init_massage );
okano 0:995f80348e02 88 curs[ 0 ] = 0;
okano 0:995f80348e02 89 }
okano 0:995f80348e02 90 }
okano 0:995f80348e02 91
okano 0:995f80348e02 92 void SB1602E::printf( char line, char *format, ... )
okano 0:995f80348e02 93 {
okano 0:995f80348e02 94 char s[ 32 ];
okano 0:995f80348e02 95 va_list args;
okano 0:995f80348e02 96
okano 0:995f80348e02 97 va_start( args, format );
okano 0:995f80348e02 98 vsnprintf( s, 32, format, args );
okano 0:995f80348e02 99 va_end( args );
okano 0:995f80348e02 100
okano 0:995f80348e02 101 puts( line, s );
okano 0:995f80348e02 102 }
okano 0:995f80348e02 103
okano 0:995f80348e02 104 void SB1602E::putc( char line, char c )
okano 0:995f80348e02 105 {
okano 0:995f80348e02 106 if ( (c == '\n') || (c == '\r') ) {
okano 0:995f80348e02 107 clear_lest_of_line( line );
okano 0:995f80348e02 108 curs[ line ] = 0;
okano 0:995f80348e02 109 return;
okano 0:995f80348e02 110 }
okano 0:995f80348e02 111
okano 0:995f80348e02 112 putcxy( c, curs[ line ]++, line );
okano 0:995f80348e02 113 }
okano 0:995f80348e02 114
okano 0:995f80348e02 115 void SB1602E::puts( char line, char *s )
okano 0:995f80348e02 116 {
okano 0:995f80348e02 117 while ( char c = *s++ )
okano 0:995f80348e02 118 putc( line, c );
okano 0:995f80348e02 119 }
okano 0:995f80348e02 120
okano 0:995f80348e02 121 void SB1602E::putcxy( char c, char x, char y )
okano 0:995f80348e02 122 {
okano 0:995f80348e02 123 const char Comm_SetDDRAMAddress = 0x80;
okano 0:995f80348e02 124 const char DDRAMAddress_Ofst[] = { 0x00, 0x40 };
okano 0:995f80348e02 125
okano 0:995f80348e02 126 if ( (x >= MaxCharsInALine) || (y >= 2) )
okano 0:995f80348e02 127 return;
okano 0:995f80348e02 128
okano 0:995f80348e02 129 lcd_command( (Comm_SetDDRAMAddress | DDRAMAddress_Ofst[ y ]) + x );
okano 0:995f80348e02 130 lcd_data( c );
okano 0:995f80348e02 131 }
okano 0:995f80348e02 132
okano 0:995f80348e02 133 void SB1602E::clear( void )
okano 0:995f80348e02 134 {
okano 0:995f80348e02 135 lcd_command( Comm_ClearDisplay );
okano 0:995f80348e02 136 wait( 2e-3 );
okano 0:995f80348e02 137 curs[ 0 ] = 0;
okano 0:995f80348e02 138 curs[ 1 ] = 0;
okano 0:995f80348e02 139 }
okano 0:995f80348e02 140
okano 0:995f80348e02 141 void SB1602E::contrast( char contrast )
okano 0:995f80348e02 142 {
okano 0:995f80348e02 143 lcd_command( Comm_FunctionSet_Extended );
okano 0:995f80348e02 144 lcd_command( Comm_ContrastSet | (contrast & 0x0f) );
okano 0:995f80348e02 145 lcd_command( Comm_PwrIconContrast | ((contrast>>4) & 0x03) );
okano 0:995f80348e02 146 lcd_command( Comm_FunctionSet_Normal );
okano 0:995f80348e02 147 }
okano 0:995f80348e02 148
okano 0:995f80348e02 149 void SB1602E::put_custom_char( char c_code, const char *cg, char x, char y )
okano 0:995f80348e02 150 {
okano 0:995f80348e02 151 for ( int i = 0; i < 5; i++ ) {
okano 0:995f80348e02 152 set_CGRAM( c_code, cg );
okano 0:995f80348e02 153 putcxy( c_code, x, y );
okano 0:995f80348e02 154 }
okano 0:995f80348e02 155 }
okano 0:995f80348e02 156
okano 0:995f80348e02 157 void SB1602E::set_CGRAM( char char_code, const char* cg )
okano 0:995f80348e02 158 {
okano 0:995f80348e02 159 for ( int i = 0; i < 8; i++ ) {
okano 0:995f80348e02 160 lcd_command( (Comm_SetCGRAM | (char_code << 3) | i) );
okano 0:995f80348e02 161 lcd_data( *cg++ );
okano 0:995f80348e02 162 }
okano 0:995f80348e02 163 }
okano 0:995f80348e02 164
okano 0:995f80348e02 165 void SB1602E::set_CGRAM( char char_code, char v )
okano 0:995f80348e02 166 {
okano 0:995f80348e02 167 char c[ 8 ];
okano 0:995f80348e02 168
okano 0:995f80348e02 169 for ( int i = 0; i < 8; i++ )
okano 0:995f80348e02 170 c[ i ] = v;
okano 0:995f80348e02 171
okano 0:995f80348e02 172 set_CGRAM( char_code, c );
okano 0:995f80348e02 173 }
okano 0:995f80348e02 174
okano 0:995f80348e02 175 void SB1602E::clear_lest_of_line( char line )
okano 0:995f80348e02 176 {
okano 0:995f80348e02 177 for ( int i = curs[ line ]; i < MaxCharsInALine; i++ )
okano 0:995f80348e02 178 putcxy( ' ', i, line );
okano 0:995f80348e02 179 }
okano 0:995f80348e02 180
okano 0:995f80348e02 181 int SB1602E::lcd_write( char first, char second )
okano 0:995f80348e02 182 {
okano 0:995f80348e02 183 char cmd[2];
okano 0:995f80348e02 184
okano 0:995f80348e02 185 cmd[ 0 ] = first;
okano 0:995f80348e02 186 cmd[ 1 ] = second;
okano 0:995f80348e02 187
okano 0:995f80348e02 188 return ( i2c.write( i2c_addr, cmd, 2 ) );
okano 0:995f80348e02 189
okano 0:995f80348e02 190 }
okano 0:995f80348e02 191
okano 0:995f80348e02 192 int SB1602E::lcd_command( char command )
okano 0:995f80348e02 193 {
okano 0:995f80348e02 194 return ( lcd_write( COMMAND, command ) );
okano 0:995f80348e02 195 }
okano 0:995f80348e02 196
okano 0:995f80348e02 197 int SB1602E::lcd_data( char data )
okano 0:995f80348e02 198 {
okano 0:995f80348e02 199 return ( lcd_write( DATA, data ) );
okano 0:995f80348e02 200 }
okano 0:995f80348e02 201
okano 0:995f80348e02 202 // Following function has been imported from Masato YAMANISHI san's code.
okano 0:995f80348e02 203 // Thank you!
okano 0:995f80348e02 204 // http://developer.mbed.org/users/masato/code/TextLCD_SB1602E/file/39110c58e55c/TextLCD_SB1602E.h
okano 0:995f80348e02 205
okano 0:995f80348e02 206 const unsigned char icon_data[]= {
okano 0:995f80348e02 207 // アイコンアドレス, 該当ビット
okano 0:995f80348e02 208 0x00, 0x10, // 0b10000,
okano 0:995f80348e02 209 0x02, 0x10, // 0b10000,
okano 0:995f80348e02 210 0x04, 0x10, // 0b10000,
okano 0:995f80348e02 211 0x06, 0x10, // 0b10000,
okano 0:995f80348e02 212
okano 0:995f80348e02 213 0x07, 0x10, // 0b10000,
okano 0:995f80348e02 214 0x07, 0x08, // 0b01000,
okano 0:995f80348e02 215 0x09, 0x10, // 0b10000,
okano 0:995f80348e02 216 0x0B, 0x10, // 0b10000,
okano 0:995f80348e02 217
okano 0:995f80348e02 218 0x0D, 0x08, // 0b01000,
okano 0:995f80348e02 219 0x0D, 0x04, // 0b00100,
okano 0:995f80348e02 220 0x0D, 0x02, // 0b00010,
okano 0:995f80348e02 221 0x0D, 0x10, // 0b10000,
okano 0:995f80348e02 222
okano 0:995f80348e02 223 0x0F, 0x10, // 0b10000, // アンテナマーク
okano 0:995f80348e02 224 };
okano 0:995f80348e02 225
okano 0:995f80348e02 226 void SB1602E::puticon(unsigned short flg)
okano 0:995f80348e02 227 {
okano 0:995f80348e02 228 static unsigned char icon_buff[16]; // アイコンの編集用
okano 0:995f80348e02 229 unsigned char i;
okano 0:995f80348e02 230
okano 0:995f80348e02 231 for(i=0; i<sizeof(icon_data)/2; i++) {
okano 0:995f80348e02 232 if(flg & (0x1000>>i)) { // 該当ビットが立っていたら
okano 0:995f80348e02 233 icon_buff[icon_data[i*2]] |= icon_data[i*2+1]; // バッファを立てます。
okano 0:995f80348e02 234 } else {
okano 0:995f80348e02 235 icon_buff[icon_data[i*2]] &= ~icon_data[i*2+1]; // バッファをクリアします。
okano 0:995f80348e02 236 }
okano 0:995f80348e02 237 }
okano 0:995f80348e02 238 // 一括でLCDに書き込みます。
okano 0:995f80348e02 239 for(i=0; i<16; i++) {
okano 0:995f80348e02 240 lcd_command(Comm_FunctionSet_Extended); // 0b00111001); // コマンド
okano 0:995f80348e02 241 lcd_command(Comm_SetCGRAM + i); // 0b01000000+i); // アイコン領域のアドレスを設定
okano 0:995f80348e02 242 lcd_data(icon_buff[i]); // アイコンデータ
okano 0:995f80348e02 243 }
okano 0:995f80348e02 244 }
okano 0:995f80348e02 245
okano 0:995f80348e02 246
okano 0:995f80348e02 247
okano 0:995f80348e02 248
okano 0:995f80348e02 249
okano 0:995f80348e02 250