Contrast not optimal
Fork of SB1602E by
SB1602E.cpp@1:fce3e353410c, 2015-01-05 (annotated)
- Committer:
- okano
- Date:
- Mon Jan 05 05:19:43 2015 +0000
- Revision:
- 1:fce3e353410c
- Parent:
- SB1002E.cpp@0:995f80348e02
- Child:
- 2:baf578069dfc
correction for file name and online-document
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:fce3e353410c | 4 | * @version 2.01 |
okano | 1:fce3e353410c | 5 | * @date 05-Jan-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 |
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 | 1:fce3e353410c | 47 | #define DEFAULT_CONTRAST 0x35 |
okano | 1:fce3e353410c | 48 | |
okano | 0:995f80348e02 | 49 | void SB1602E::init( char *init_massage ) |
okano | 0:995f80348e02 | 50 | { |
okano | 0:995f80348e02 | 51 | const char init_seq0[] = { |
okano | 0:995f80348e02 | 52 | Comm_FunctionSet_Normal, |
okano | 0:995f80348e02 | 53 | Comm_ReturnHome, // This may be required to reset the scroll function |
okano | 0:995f80348e02 | 54 | Comm_FunctionSet_Extended, |
okano | 0:995f80348e02 | 55 | Comm_InternalOscFrequency, |
okano | 0:995f80348e02 | 56 | Comm_ContrastSet | ( DEFAULT_CONTRAST & 0xF), |
okano | 0:995f80348e02 | 57 | Comm_PwrIconContrast | ((DEFAULT_CONTRAST >> 4) & 0x3), |
okano | 0:995f80348e02 | 58 | Comm_FollowerCtrl | 0x0A, |
okano | 0:995f80348e02 | 59 | }; |
okano | 0:995f80348e02 | 60 | const char init_seq1[] = { |
okano | 0:995f80348e02 | 61 | Comm_DisplayOnOff, |
okano | 0:995f80348e02 | 62 | Comm_ClearDisplay, |
okano | 0:995f80348e02 | 63 | Comm_EntryModeSet, |
okano | 0:995f80348e02 | 64 | }; |
okano | 0:995f80348e02 | 65 | |
okano | 0:995f80348e02 | 66 | i2c_addr = 0x7C; |
okano | 0:995f80348e02 | 67 | |
okano | 0:995f80348e02 | 68 | wait( 0.04 ); // interval after hardware reset |
okano | 0:995f80348e02 | 69 | |
okano | 0:995f80348e02 | 70 | for ( int i = 0; i < sizeof( init_seq0 ); i++ ) { |
okano | 0:995f80348e02 | 71 | lcd_command( init_seq0[ i ] ); |
okano | 0:995f80348e02 | 72 | wait( 30e-6 ); |
okano | 0:995f80348e02 | 73 | } |
okano | 0:995f80348e02 | 74 | |
okano | 0:995f80348e02 | 75 | wait( 0.2 ); |
okano | 0:995f80348e02 | 76 | |
okano | 0:995f80348e02 | 77 | for ( int i = 0; i < sizeof( init_seq1 ); i++ ) { |
okano | 0:995f80348e02 | 78 | lcd_command( init_seq1[ i ] ); |
okano | 0:995f80348e02 | 79 | wait( 2e-3 ); |
okano | 0:995f80348e02 | 80 | } |
okano | 0:995f80348e02 | 81 | |
okano | 0:995f80348e02 | 82 | set_CGRAM( 7, '\x1F' ); |
okano | 0:995f80348e02 | 83 | |
okano | 0:995f80348e02 | 84 | curs[ 0 ] = 0; |
okano | 0:995f80348e02 | 85 | curs[ 1 ] = 0; |
okano | 0:995f80348e02 | 86 | |
okano | 0:995f80348e02 | 87 | if ( init_massage ) |
okano | 0:995f80348e02 | 88 | { |
okano | 0:995f80348e02 | 89 | puts( 0, init_massage ); |
okano | 0:995f80348e02 | 90 | curs[ 0 ] = 0; |
okano | 0:995f80348e02 | 91 | } |
okano | 0:995f80348e02 | 92 | } |
okano | 0:995f80348e02 | 93 | |
okano | 0:995f80348e02 | 94 | void SB1602E::printf( char line, char *format, ... ) |
okano | 0:995f80348e02 | 95 | { |
okano | 0:995f80348e02 | 96 | char s[ 32 ]; |
okano | 0:995f80348e02 | 97 | va_list args; |
okano | 0:995f80348e02 | 98 | |
okano | 0:995f80348e02 | 99 | va_start( args, format ); |
okano | 0:995f80348e02 | 100 | vsnprintf( s, 32, format, args ); |
okano | 0:995f80348e02 | 101 | va_end( args ); |
okano | 0:995f80348e02 | 102 | |
okano | 0:995f80348e02 | 103 | puts( line, s ); |
okano | 0:995f80348e02 | 104 | } |
okano | 0:995f80348e02 | 105 | |
okano | 0:995f80348e02 | 106 | void SB1602E::putc( char line, char c ) |
okano | 0:995f80348e02 | 107 | { |
okano | 0:995f80348e02 | 108 | if ( (c == '\n') || (c == '\r') ) { |
okano | 0:995f80348e02 | 109 | clear_lest_of_line( line ); |
okano | 0:995f80348e02 | 110 | curs[ line ] = 0; |
okano | 0:995f80348e02 | 111 | return; |
okano | 0:995f80348e02 | 112 | } |
okano | 0:995f80348e02 | 113 | |
okano | 0:995f80348e02 | 114 | putcxy( c, curs[ line ]++, line ); |
okano | 0:995f80348e02 | 115 | } |
okano | 0:995f80348e02 | 116 | |
okano | 0:995f80348e02 | 117 | void SB1602E::puts( char line, char *s ) |
okano | 0:995f80348e02 | 118 | { |
okano | 0:995f80348e02 | 119 | while ( char c = *s++ ) |
okano | 0:995f80348e02 | 120 | putc( line, c ); |
okano | 0:995f80348e02 | 121 | } |
okano | 0:995f80348e02 | 122 | |
okano | 0:995f80348e02 | 123 | void SB1602E::putcxy( char c, char x, char y ) |
okano | 0:995f80348e02 | 124 | { |
okano | 0:995f80348e02 | 125 | const char Comm_SetDDRAMAddress = 0x80; |
okano | 0:995f80348e02 | 126 | const char DDRAMAddress_Ofst[] = { 0x00, 0x40 }; |
okano | 0:995f80348e02 | 127 | |
okano | 0:995f80348e02 | 128 | if ( (x >= MaxCharsInALine) || (y >= 2) ) |
okano | 0:995f80348e02 | 129 | return; |
okano | 0:995f80348e02 | 130 | |
okano | 0:995f80348e02 | 131 | lcd_command( (Comm_SetDDRAMAddress | DDRAMAddress_Ofst[ y ]) + x ); |
okano | 0:995f80348e02 | 132 | lcd_data( c ); |
okano | 0:995f80348e02 | 133 | } |
okano | 0:995f80348e02 | 134 | |
okano | 0:995f80348e02 | 135 | void SB1602E::clear( void ) |
okano | 0:995f80348e02 | 136 | { |
okano | 0:995f80348e02 | 137 | lcd_command( Comm_ClearDisplay ); |
okano | 0:995f80348e02 | 138 | wait( 2e-3 ); |
okano | 0:995f80348e02 | 139 | curs[ 0 ] = 0; |
okano | 0:995f80348e02 | 140 | curs[ 1 ] = 0; |
okano | 0:995f80348e02 | 141 | } |
okano | 0:995f80348e02 | 142 | |
okano | 0:995f80348e02 | 143 | void SB1602E::contrast( char contrast ) |
okano | 0:995f80348e02 | 144 | { |
okano | 0:995f80348e02 | 145 | lcd_command( Comm_FunctionSet_Extended ); |
okano | 0:995f80348e02 | 146 | lcd_command( Comm_ContrastSet | (contrast & 0x0f) ); |
okano | 0:995f80348e02 | 147 | lcd_command( Comm_PwrIconContrast | ((contrast>>4) & 0x03) ); |
okano | 0:995f80348e02 | 148 | lcd_command( Comm_FunctionSet_Normal ); |
okano | 0:995f80348e02 | 149 | } |
okano | 0:995f80348e02 | 150 | |
okano | 0:995f80348e02 | 151 | void SB1602E::put_custom_char( char c_code, const char *cg, char x, char y ) |
okano | 0:995f80348e02 | 152 | { |
okano | 0:995f80348e02 | 153 | for ( int i = 0; i < 5; i++ ) { |
okano | 0:995f80348e02 | 154 | set_CGRAM( c_code, cg ); |
okano | 0:995f80348e02 | 155 | putcxy( c_code, x, y ); |
okano | 0:995f80348e02 | 156 | } |
okano | 0:995f80348e02 | 157 | } |
okano | 0:995f80348e02 | 158 | |
okano | 0:995f80348e02 | 159 | void SB1602E::set_CGRAM( char char_code, const char* cg ) |
okano | 0:995f80348e02 | 160 | { |
okano | 0:995f80348e02 | 161 | for ( int i = 0; i < 8; i++ ) { |
okano | 0:995f80348e02 | 162 | lcd_command( (Comm_SetCGRAM | (char_code << 3) | i) ); |
okano | 0:995f80348e02 | 163 | lcd_data( *cg++ ); |
okano | 0:995f80348e02 | 164 | } |
okano | 0:995f80348e02 | 165 | } |
okano | 0:995f80348e02 | 166 | |
okano | 0:995f80348e02 | 167 | void SB1602E::set_CGRAM( char char_code, char v ) |
okano | 0:995f80348e02 | 168 | { |
okano | 0:995f80348e02 | 169 | char c[ 8 ]; |
okano | 0:995f80348e02 | 170 | |
okano | 0:995f80348e02 | 171 | for ( int i = 0; i < 8; i++ ) |
okano | 0:995f80348e02 | 172 | c[ i ] = v; |
okano | 0:995f80348e02 | 173 | |
okano | 0:995f80348e02 | 174 | set_CGRAM( char_code, c ); |
okano | 0:995f80348e02 | 175 | } |
okano | 0:995f80348e02 | 176 | |
okano | 0:995f80348e02 | 177 | void SB1602E::clear_lest_of_line( char line ) |
okano | 0:995f80348e02 | 178 | { |
okano | 0:995f80348e02 | 179 | for ( int i = curs[ line ]; i < MaxCharsInALine; i++ ) |
okano | 0:995f80348e02 | 180 | putcxy( ' ', i, line ); |
okano | 0:995f80348e02 | 181 | } |
okano | 0:995f80348e02 | 182 | |
okano | 0:995f80348e02 | 183 | int SB1602E::lcd_write( char first, char second ) |
okano | 0:995f80348e02 | 184 | { |
okano | 0:995f80348e02 | 185 | char cmd[2]; |
okano | 0:995f80348e02 | 186 | |
okano | 0:995f80348e02 | 187 | cmd[ 0 ] = first; |
okano | 0:995f80348e02 | 188 | cmd[ 1 ] = second; |
okano | 0:995f80348e02 | 189 | |
okano | 0:995f80348e02 | 190 | return ( i2c.write( i2c_addr, cmd, 2 ) ); |
okano | 0:995f80348e02 | 191 | |
okano | 0:995f80348e02 | 192 | } |
okano | 0:995f80348e02 | 193 | |
okano | 0:995f80348e02 | 194 | int SB1602E::lcd_command( char command ) |
okano | 0:995f80348e02 | 195 | { |
okano | 0:995f80348e02 | 196 | return ( lcd_write( COMMAND, command ) ); |
okano | 0:995f80348e02 | 197 | } |
okano | 0:995f80348e02 | 198 | |
okano | 0:995f80348e02 | 199 | int SB1602E::lcd_data( char data ) |
okano | 0:995f80348e02 | 200 | { |
okano | 0:995f80348e02 | 201 | return ( lcd_write( DATA, data ) ); |
okano | 0:995f80348e02 | 202 | } |
okano | 0:995f80348e02 | 203 | |
okano | 0:995f80348e02 | 204 | // Following function has been imported from Masato YAMANISHI san's code. |
okano | 0:995f80348e02 | 205 | // Thank you! |
okano | 0:995f80348e02 | 206 | // http://developer.mbed.org/users/masato/code/TextLCD_SB1602E/file/39110c58e55c/TextLCD_SB1602E.h |
okano | 0:995f80348e02 | 207 | |
okano | 0:995f80348e02 | 208 | const unsigned char icon_data[]= { |
okano | 0:995f80348e02 | 209 | // アイコンアドレス, 該当ビット |
okano | 0:995f80348e02 | 210 | 0x00, 0x10, // 0b10000, |
okano | 0:995f80348e02 | 211 | 0x02, 0x10, // 0b10000, |
okano | 0:995f80348e02 | 212 | 0x04, 0x10, // 0b10000, |
okano | 0:995f80348e02 | 213 | 0x06, 0x10, // 0b10000, |
okano | 0:995f80348e02 | 214 | |
okano | 0:995f80348e02 | 215 | 0x07, 0x10, // 0b10000, |
okano | 0:995f80348e02 | 216 | 0x07, 0x08, // 0b01000, |
okano | 0:995f80348e02 | 217 | 0x09, 0x10, // 0b10000, |
okano | 0:995f80348e02 | 218 | 0x0B, 0x10, // 0b10000, |
okano | 0:995f80348e02 | 219 | |
okano | 0:995f80348e02 | 220 | 0x0D, 0x08, // 0b01000, |
okano | 0:995f80348e02 | 221 | 0x0D, 0x04, // 0b00100, |
okano | 0:995f80348e02 | 222 | 0x0D, 0x02, // 0b00010, |
okano | 0:995f80348e02 | 223 | 0x0D, 0x10, // 0b10000, |
okano | 0:995f80348e02 | 224 | |
okano | 0:995f80348e02 | 225 | 0x0F, 0x10, // 0b10000, // アンテナマーク |
okano | 0:995f80348e02 | 226 | }; |
okano | 0:995f80348e02 | 227 | |
okano | 0:995f80348e02 | 228 | void SB1602E::puticon(unsigned short flg) |
okano | 0:995f80348e02 | 229 | { |
okano | 0:995f80348e02 | 230 | static unsigned char icon_buff[16]; // アイコンの編集用 |
okano | 0:995f80348e02 | 231 | unsigned char i; |
okano | 0:995f80348e02 | 232 | |
okano | 0:995f80348e02 | 233 | for(i=0; i<sizeof(icon_data)/2; i++) { |
okano | 0:995f80348e02 | 234 | if(flg & (0x1000>>i)) { // 該当ビットが立っていたら |
okano | 0:995f80348e02 | 235 | icon_buff[icon_data[i*2]] |= icon_data[i*2+1]; // バッファを立てます。 |
okano | 0:995f80348e02 | 236 | } else { |
okano | 0:995f80348e02 | 237 | icon_buff[icon_data[i*2]] &= ~icon_data[i*2+1]; // バッファをクリアします。 |
okano | 0:995f80348e02 | 238 | } |
okano | 0:995f80348e02 | 239 | } |
okano | 0:995f80348e02 | 240 | // 一括でLCDに書き込みます。 |
okano | 0:995f80348e02 | 241 | for(i=0; i<16; i++) { |
okano | 0:995f80348e02 | 242 | lcd_command(Comm_FunctionSet_Extended); // 0b00111001); // コマンド |
okano | 0:995f80348e02 | 243 | lcd_command(Comm_SetCGRAM + i); // 0b01000000+i); // アイコン領域のアドレスを設定 |
okano | 0:995f80348e02 | 244 | lcd_data(icon_buff[i]); // アイコンデータ |
okano | 0:995f80348e02 | 245 | } |
okano | 0:995f80348e02 | 246 | } |
okano | 0:995f80348e02 | 247 | |
okano | 0:995f80348e02 | 248 | |
okano | 0:995f80348e02 | 249 | |
okano | 0:995f80348e02 | 250 | |
okano | 0:995f80348e02 | 251 | |
okano | 0:995f80348e02 | 252 |