Version 2.1 of TextLCD_SB1602E (forked).

Dependents:   BME280_environmental_recorder kids_workshop

Fork of SB1602E by Tedd OKANO

Committer:
MACRUM
Date:
Sat Jul 23 14:27:20 2016 +0000
Revision:
4:f2fb19e5486f
Parent:
3:c4216ef28143
Add reset sequence

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 *
MACRUM 2:baf578069dfc 3 * @author Tedd OKANO, Masato YAMANISHI & Toyomasa Watarai
MACRUM 2:baf578069dfc 4 * @version 2.1
MACRUM 2:baf578069dfc 5 * @date 07-April-2015
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
MACRUM 2:baf578069dfc 24 * revision 2.1 07-Apl-2015 a. add printf() with X and Y position
MACRUM 2:baf578069dfc 25 * b. add setter for number of chars in a line (e.g. 8x2 LCD support)
okano 0:995f80348e02 26 */
okano 0:995f80348e02 27
okano 0:995f80348e02 28 #include <stdarg.h>
okano 0:995f80348e02 29 #include "mbed.h"
okano 0:995f80348e02 30 #include "SB1602E.h"
okano 0:995f80348e02 31
okano 0:995f80348e02 32
MACRUM 4:f2fb19e5486f 33 SB1602E::SB1602E( PinName I2C_sda, PinName I2C_scl, PinName reset, char *init_massage ) : i2c_p( new I2C( I2C_sda, I2C_scl )), i2c( *i2c_p ), rst_p(new DigitalOut(reset)), rst(*rst_p), charsInLine( MaxCharsInALine )
okano 0:995f80348e02 34 {
okano 0:995f80348e02 35 init( init_massage );
okano 0:995f80348e02 36 }
okano 0:995f80348e02 37
MACRUM 4:f2fb19e5486f 38 SB1602E::SB1602E( I2C &i2c_, char *init_massage ) : i2c_p( NULL ), i2c( i2c_ ), rst_p(NULL), rst(*rst_p), charsInLine( MaxCharsInALine )
okano 0:995f80348e02 39 {
okano 0:995f80348e02 40 init( init_massage );
okano 0:995f80348e02 41 }
okano 0:995f80348e02 42
okano 0:995f80348e02 43 SB1602E::~SB1602E()
okano 0:995f80348e02 44 {
okano 0:995f80348e02 45 if ( NULL != i2c_p )
okano 0:995f80348e02 46 delete i2c_p;
okano 0:995f80348e02 47 }
okano 0:995f80348e02 48
okano 1:fce3e353410c 49 #define DEFAULT_CONTRAST 0x35
okano 1:fce3e353410c 50
okano 0:995f80348e02 51 void SB1602E::init( char *init_massage )
okano 0:995f80348e02 52 {
okano 0:995f80348e02 53 const char init_seq0[] = {
okano 0:995f80348e02 54 Comm_FunctionSet_Normal,
okano 0:995f80348e02 55 Comm_ReturnHome, // This may be required to reset the scroll function
okano 0:995f80348e02 56 Comm_FunctionSet_Extended,
okano 0:995f80348e02 57 Comm_InternalOscFrequency,
okano 0:995f80348e02 58 Comm_ContrastSet | ( DEFAULT_CONTRAST & 0xF),
okano 0:995f80348e02 59 Comm_PwrIconContrast | ((DEFAULT_CONTRAST >> 4) & 0x3),
okano 0:995f80348e02 60 Comm_FollowerCtrl | 0x0A,
okano 0:995f80348e02 61 };
okano 0:995f80348e02 62 const char init_seq1[] = {
okano 0:995f80348e02 63 Comm_DisplayOnOff,
okano 0:995f80348e02 64 Comm_ClearDisplay,
okano 0:995f80348e02 65 Comm_EntryModeSet,
okano 0:995f80348e02 66 };
okano 0:995f80348e02 67
okano 0:995f80348e02 68 i2c_addr = 0x7C;
okano 0:995f80348e02 69
okano 0:995f80348e02 70 wait( 0.04 ); // interval after hardware reset
okano 0:995f80348e02 71
MACRUM 4:f2fb19e5486f 72 rst = 0;
MACRUM 4:f2fb19e5486f 73 wait(0.1);
MACRUM 4:f2fb19e5486f 74 rst = 1;
MACRUM 4:f2fb19e5486f 75
okano 0:995f80348e02 76 for ( int i = 0; i < sizeof( init_seq0 ); i++ ) {
okano 0:995f80348e02 77 lcd_command( init_seq0[ i ] );
okano 0:995f80348e02 78 wait( 30e-6 );
okano 0:995f80348e02 79 }
okano 0:995f80348e02 80
okano 0:995f80348e02 81 wait( 0.2 );
okano 0:995f80348e02 82
okano 0:995f80348e02 83 for ( int i = 0; i < sizeof( init_seq1 ); i++ ) {
okano 0:995f80348e02 84 lcd_command( init_seq1[ i ] );
okano 0:995f80348e02 85 wait( 2e-3 );
okano 0:995f80348e02 86 }
okano 0:995f80348e02 87
okano 0:995f80348e02 88 set_CGRAM( 7, '\x1F' );
okano 0:995f80348e02 89
okano 0:995f80348e02 90 curs[ 0 ] = 0;
okano 0:995f80348e02 91 curs[ 1 ] = 0;
okano 0:995f80348e02 92
okano 0:995f80348e02 93 if ( init_massage )
okano 0:995f80348e02 94 {
okano 0:995f80348e02 95 puts( 0, init_massage );
okano 0:995f80348e02 96 curs[ 0 ] = 0;
okano 0:995f80348e02 97 }
okano 0:995f80348e02 98 }
okano 0:995f80348e02 99
okano 0:995f80348e02 100 void SB1602E::printf( char line, char *format, ... )
okano 0:995f80348e02 101 {
okano 0:995f80348e02 102 char s[ 32 ];
okano 0:995f80348e02 103 va_list args;
okano 0:995f80348e02 104
okano 0:995f80348e02 105 va_start( args, format );
okano 0:995f80348e02 106 vsnprintf( s, 32, format, args );
okano 0:995f80348e02 107 va_end( args );
okano 0:995f80348e02 108
okano 0:995f80348e02 109 puts( line, s );
okano 0:995f80348e02 110 }
okano 0:995f80348e02 111
MACRUM 2:baf578069dfc 112 void SB1602E::printf( char x, char y, char *format, ... )
MACRUM 2:baf578069dfc 113 {
MACRUM 2:baf578069dfc 114 char s[ 32 ];
MACRUM 2:baf578069dfc 115 va_list args;
MACRUM 2:baf578069dfc 116
MACRUM 2:baf578069dfc 117 va_start( args, format );
MACRUM 2:baf578069dfc 118 vsnprintf( s, 32, format, args );
MACRUM 2:baf578069dfc 119 va_end( args );
MACRUM 2:baf578069dfc 120
MACRUM 2:baf578069dfc 121 curs[ y ] = x;
MACRUM 2:baf578069dfc 122 puts( y, s );
MACRUM 2:baf578069dfc 123 }
MACRUM 2:baf578069dfc 124
okano 0:995f80348e02 125 void SB1602E::putc( char line, char c )
okano 0:995f80348e02 126 {
okano 0:995f80348e02 127 if ( (c == '\n') || (c == '\r') ) {
okano 0:995f80348e02 128 clear_lest_of_line( line );
okano 0:995f80348e02 129 curs[ line ] = 0;
okano 0:995f80348e02 130 return;
okano 0:995f80348e02 131 }
okano 0:995f80348e02 132
okano 0:995f80348e02 133 putcxy( c, curs[ line ]++, line );
okano 0:995f80348e02 134 }
okano 0:995f80348e02 135
okano 0:995f80348e02 136 void SB1602E::puts( char line, char *s )
okano 0:995f80348e02 137 {
okano 0:995f80348e02 138 while ( char c = *s++ )
okano 0:995f80348e02 139 putc( line, c );
okano 0:995f80348e02 140 }
okano 0:995f80348e02 141
okano 0:995f80348e02 142 void SB1602E::putcxy( char c, char x, char y )
okano 0:995f80348e02 143 {
okano 0:995f80348e02 144 const char Comm_SetDDRAMAddress = 0x80;
okano 0:995f80348e02 145 const char DDRAMAddress_Ofst[] = { 0x00, 0x40 };
okano 0:995f80348e02 146
MACRUM 2:baf578069dfc 147 if ( (x >= charsInLine) || (y >= 2) )
okano 0:995f80348e02 148 return;
okano 0:995f80348e02 149
okano 0:995f80348e02 150 lcd_command( (Comm_SetDDRAMAddress | DDRAMAddress_Ofst[ y ]) + x );
okano 0:995f80348e02 151 lcd_data( c );
okano 0:995f80348e02 152 }
okano 0:995f80348e02 153
okano 0:995f80348e02 154 void SB1602E::clear( void )
okano 0:995f80348e02 155 {
okano 0:995f80348e02 156 lcd_command( Comm_ClearDisplay );
MACRUM 3:c4216ef28143 157 //wait( 2e-3 );
MACRUM 3:c4216ef28143 158 wait_ms(2);
okano 0:995f80348e02 159 curs[ 0 ] = 0;
okano 0:995f80348e02 160 curs[ 1 ] = 0;
okano 0:995f80348e02 161 }
okano 0:995f80348e02 162
okano 0:995f80348e02 163 void SB1602E::contrast( char contrast )
okano 0:995f80348e02 164 {
okano 0:995f80348e02 165 lcd_command( Comm_FunctionSet_Extended );
okano 0:995f80348e02 166 lcd_command( Comm_ContrastSet | (contrast & 0x0f) );
okano 0:995f80348e02 167 lcd_command( Comm_PwrIconContrast | ((contrast>>4) & 0x03) );
okano 0:995f80348e02 168 lcd_command( Comm_FunctionSet_Normal );
okano 0:995f80348e02 169 }
okano 0:995f80348e02 170
okano 0:995f80348e02 171 void SB1602E::put_custom_char( char c_code, const char *cg, char x, char y )
okano 0:995f80348e02 172 {
okano 0:995f80348e02 173 for ( int i = 0; i < 5; i++ ) {
okano 0:995f80348e02 174 set_CGRAM( c_code, cg );
okano 0:995f80348e02 175 putcxy( c_code, x, y );
okano 0:995f80348e02 176 }
okano 0:995f80348e02 177 }
okano 0:995f80348e02 178
okano 0:995f80348e02 179 void SB1602E::set_CGRAM( char char_code, const char* cg )
okano 0:995f80348e02 180 {
okano 0:995f80348e02 181 for ( int i = 0; i < 8; i++ ) {
okano 0:995f80348e02 182 lcd_command( (Comm_SetCGRAM | (char_code << 3) | i) );
okano 0:995f80348e02 183 lcd_data( *cg++ );
okano 0:995f80348e02 184 }
okano 0:995f80348e02 185 }
okano 0:995f80348e02 186
okano 0:995f80348e02 187 void SB1602E::set_CGRAM( char char_code, char v )
okano 0:995f80348e02 188 {
okano 0:995f80348e02 189 char c[ 8 ];
okano 0:995f80348e02 190
okano 0:995f80348e02 191 for ( int i = 0; i < 8; i++ )
okano 0:995f80348e02 192 c[ i ] = v;
okano 0:995f80348e02 193
okano 0:995f80348e02 194 set_CGRAM( char_code, c );
okano 0:995f80348e02 195 }
okano 0:995f80348e02 196
okano 0:995f80348e02 197 void SB1602E::clear_lest_of_line( char line )
okano 0:995f80348e02 198 {
MACRUM 2:baf578069dfc 199 for ( int i = curs[ line ]; i < charsInLine; i++ )
okano 0:995f80348e02 200 putcxy( ' ', i, line );
okano 0:995f80348e02 201 }
okano 0:995f80348e02 202
okano 0:995f80348e02 203 int SB1602E::lcd_write( char first, char second )
okano 0:995f80348e02 204 {
okano 0:995f80348e02 205 char cmd[2];
okano 0:995f80348e02 206
okano 0:995f80348e02 207 cmd[ 0 ] = first;
okano 0:995f80348e02 208 cmd[ 1 ] = second;
okano 0:995f80348e02 209
okano 0:995f80348e02 210 return ( i2c.write( i2c_addr, cmd, 2 ) );
okano 0:995f80348e02 211
okano 0:995f80348e02 212 }
okano 0:995f80348e02 213
okano 0:995f80348e02 214 int SB1602E::lcd_command( char command )
okano 0:995f80348e02 215 {
MACRUM 3:c4216ef28143 216 int ret = lcd_write( COMMAND, command );
MACRUM 3:c4216ef28143 217 wait_us(30);
MACRUM 3:c4216ef28143 218 return ( ret );
okano 0:995f80348e02 219 }
okano 0:995f80348e02 220
okano 0:995f80348e02 221 int SB1602E::lcd_data( char data )
okano 0:995f80348e02 222 {
okano 0:995f80348e02 223 return ( lcd_write( DATA, data ) );
okano 0:995f80348e02 224 }
okano 0:995f80348e02 225
okano 0:995f80348e02 226 // Following function has been imported from Masato YAMANISHI san's code.
okano 0:995f80348e02 227 // Thank you!
okano 0:995f80348e02 228 // http://developer.mbed.org/users/masato/code/TextLCD_SB1602E/file/39110c58e55c/TextLCD_SB1602E.h
okano 0:995f80348e02 229
okano 0:995f80348e02 230 const unsigned char icon_data[]= {
okano 0:995f80348e02 231 // アイコンアドレス, 該当ビット
okano 0:995f80348e02 232 0x00, 0x10, // 0b10000,
okano 0:995f80348e02 233 0x02, 0x10, // 0b10000,
okano 0:995f80348e02 234 0x04, 0x10, // 0b10000,
okano 0:995f80348e02 235 0x06, 0x10, // 0b10000,
okano 0:995f80348e02 236
okano 0:995f80348e02 237 0x07, 0x10, // 0b10000,
okano 0:995f80348e02 238 0x07, 0x08, // 0b01000,
okano 0:995f80348e02 239 0x09, 0x10, // 0b10000,
okano 0:995f80348e02 240 0x0B, 0x10, // 0b10000,
okano 0:995f80348e02 241
okano 0:995f80348e02 242 0x0D, 0x08, // 0b01000,
okano 0:995f80348e02 243 0x0D, 0x04, // 0b00100,
okano 0:995f80348e02 244 0x0D, 0x02, // 0b00010,
okano 0:995f80348e02 245 0x0D, 0x10, // 0b10000,
okano 0:995f80348e02 246
okano 0:995f80348e02 247 0x0F, 0x10, // 0b10000, // アンテナマーク
okano 0:995f80348e02 248 };
okano 0:995f80348e02 249
okano 0:995f80348e02 250 void SB1602E::puticon(unsigned short flg)
okano 0:995f80348e02 251 {
okano 0:995f80348e02 252 static unsigned char icon_buff[16]; // アイコンの編集用
okano 0:995f80348e02 253 unsigned char i;
okano 0:995f80348e02 254
okano 0:995f80348e02 255 for(i=0; i<sizeof(icon_data)/2; i++) {
okano 0:995f80348e02 256 if(flg & (0x1000>>i)) { // 該当ビットが立っていたら
okano 0:995f80348e02 257 icon_buff[icon_data[i*2]] |= icon_data[i*2+1]; // バッファを立てます。
okano 0:995f80348e02 258 } else {
okano 0:995f80348e02 259 icon_buff[icon_data[i*2]] &= ~icon_data[i*2+1]; // バッファをクリアします。
okano 0:995f80348e02 260 }
okano 0:995f80348e02 261 }
okano 0:995f80348e02 262 // 一括でLCDに書き込みます。
okano 0:995f80348e02 263 for(i=0; i<16; i++) {
okano 0:995f80348e02 264 lcd_command(Comm_FunctionSet_Extended); // 0b00111001); // コマンド
okano 0:995f80348e02 265 lcd_command(Comm_SetCGRAM + i); // 0b01000000+i); // アイコン領域のアドレスを設定
okano 0:995f80348e02 266 lcd_data(icon_buff[i]); // アイコンデータ
okano 0:995f80348e02 267 }
okano 0:995f80348e02 268 }
okano 0:995f80348e02 269
okano 0:995f80348e02 270
okano 0:995f80348e02 271
okano 0:995f80348e02 272
okano 0:995f80348e02 273
okano 0:995f80348e02 274