Information
This code is old.
Version 2.0 is available.
Latest version has improved usability.
Please find following page.
- Demo program with same functions of TextLCD_SB1602E.
- Component page.
- Library page
Information
このコードは古いバージョンです
バージョン2.0 が公開されています.
新しいバージョンでは使い勝手が改善されています.
以下のページを御覧ください.
- 新ライブラリ対応のデモ・プログラム(機能はこれまでのものと同じです)
- コンポーネント・ページ
TextLCD_SB1602E.h@0:694061176edf, 2010-01-23 (annotated)
- Committer:
- okano
- Date:
- Sat Jan 23 13:45:18 2010 +0000
- Revision:
- 0:694061176edf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okano | 0:694061176edf | 1 | /* |
okano | 0:694061176edf | 2 | * LCD module "SB1602E" library |
okano | 0:694061176edf | 3 | * |
okano | 0:694061176edf | 4 | * SB1602E is an I2C based low voltage text LCD panel (based Sitronix ST7032 chip) |
okano | 0:694061176edf | 5 | * The module by StrawberryLinux |
okano | 0:694061176edf | 6 | * http://strawberry-linux.com/catalog/items?code=27002 (Online shop page (Japanese)) |
okano | 0:694061176edf | 7 | * http://strawberry-linux.com/pub/ST7032i.pdf (datasheet of the chip) |
okano | 0:694061176edf | 8 | * |
okano | 0:694061176edf | 9 | * This is a library to operate this module easy. |
okano | 0:694061176edf | 10 | * |
okano | 0:694061176edf | 11 | * Copyright (c) 2010 Tedd OKANO |
okano | 0:694061176edf | 12 | * Released under the MIT License: http://mbed.org/license/mit |
okano | 0:694061176edf | 13 | * |
okano | 0:694061176edf | 14 | * revision 1.0 22-Jan-2010 a. 1st release |
okano | 0:694061176edf | 15 | * revision 1.1 23-Jan-2010 a. class name has been changed from lcd_SB1602E to TextLCD_SB1602E |
okano | 0:694061176edf | 16 | * b. printf() added |
okano | 0:694061176edf | 17 | * c. copyright notice added |
okano | 0:694061176edf | 18 | */ |
okano | 0:694061176edf | 19 | |
okano | 0:694061176edf | 20 | #ifndef MBED_TextLCD_SB1602E |
okano | 0:694061176edf | 21 | #define MBED_TextLCD_SB1602E |
okano | 0:694061176edf | 22 | |
okano | 0:694061176edf | 23 | #include <stdarg.h> |
okano | 0:694061176edf | 24 | #include "mbed.h" |
okano | 0:694061176edf | 25 | #include "I2cBusDevice.h" |
okano | 0:694061176edf | 26 | |
okano | 0:694061176edf | 27 | // SB1602E IIC address |
okano | 0:694061176edf | 28 | |
okano | 0:694061176edf | 29 | const char SB1602E_addr = 0x7C; |
okano | 0:694061176edf | 30 | |
okano | 0:694061176edf | 31 | // SB1602E initialization command sequence |
okano | 0:694061176edf | 32 | |
okano | 0:694061176edf | 33 | #ifdef INIT_VALUE_DATASHEET_ORIGINAL |
okano | 0:694061176edf | 34 | |
okano | 0:694061176edf | 35 | const char Comm_FunctionSet_Normal = 0x38; |
okano | 0:694061176edf | 36 | const char Comm_FunctionSet_Extended = 0x39; |
okano | 0:694061176edf | 37 | const char Comm_InternalOscFrequency = 0x14; |
okano | 0:694061176edf | 38 | const char Comm_ContrastSet = 0x78; |
okano | 0:694061176edf | 39 | const char Comm_PwrIconContrast = 0x5E; |
okano | 0:694061176edf | 40 | const char Comm_FollowerCtrl = 0x6A; |
okano | 0:694061176edf | 41 | const char Comm_DisplayOnOff = 0x0C; |
okano | 0:694061176edf | 42 | const char Comm_ClearDisplay = 0x01; |
okano | 0:694061176edf | 43 | const char Comm_EntryModeSet = 0x06; |
okano | 0:694061176edf | 44 | |
okano | 0:694061176edf | 45 | #else |
okano | 0:694061176edf | 46 | |
okano | 0:694061176edf | 47 | const char Comm_FunctionSet_Normal = 0x38; |
okano | 0:694061176edf | 48 | const char Comm_FunctionSet_Extended = 0x39; |
okano | 0:694061176edf | 49 | const char Comm_InternalOscFrequency = 0x14; |
okano | 0:694061176edf | 50 | const char Comm_ContrastSet = 0x70; |
okano | 0:694061176edf | 51 | const char Comm_PwrIconContrast = 0x5C; |
okano | 0:694061176edf | 52 | const char Comm_FollowerCtrl = 0x60; |
okano | 0:694061176edf | 53 | const char Comm_DisplayOnOff = 0x0C; |
okano | 0:694061176edf | 54 | const char Comm_ClearDisplay = 0x01; |
okano | 0:694061176edf | 55 | const char Comm_EntryModeSet = 0x04; |
okano | 0:694061176edf | 56 | const char Comm_ReturnHome = 0x02; |
okano | 0:694061176edf | 57 | |
okano | 0:694061176edf | 58 | #endif |
okano | 0:694061176edf | 59 | |
okano | 0:694061176edf | 60 | // SB1602E general commands |
okano | 0:694061176edf | 61 | |
okano | 0:694061176edf | 62 | const char Comm_SetDDRAMAddress = 0x80; |
okano | 0:694061176edf | 63 | const char DDRAMAddress_Ofst[] = { 0x00, 0x40 }; |
okano | 0:694061176edf | 64 | |
okano | 0:694061176edf | 65 | const char Comm_SetCGRAM = 0x40; |
okano | 0:694061176edf | 66 | |
okano | 0:694061176edf | 67 | // SB1602E setting values |
okano | 0:694061176edf | 68 | |
okano | 0:694061176edf | 69 | const char default_Contrast = 0x35; |
okano | 0:694061176edf | 70 | |
okano | 0:694061176edf | 71 | const char COMMAND = 0x00; |
okano | 0:694061176edf | 72 | const char DATA = 0x40; |
okano | 0:694061176edf | 73 | |
okano | 0:694061176edf | 74 | const char MaxCharsInALine = 0x10; // buffer deoth for one line (no scroll function used) |
okano | 0:694061176edf | 75 | |
okano | 0:694061176edf | 76 | const char init_seq0_length = 7; |
okano | 0:694061176edf | 77 | const char init_seq0[ init_seq0_length ] |
okano | 0:694061176edf | 78 | = { |
okano | 0:694061176edf | 79 | Comm_FunctionSet_Normal, |
okano | 0:694061176edf | 80 | Comm_ReturnHome, // This may be required to reset the scroll function |
okano | 0:694061176edf | 81 | Comm_FunctionSet_Extended, |
okano | 0:694061176edf | 82 | Comm_InternalOscFrequency, |
okano | 0:694061176edf | 83 | Comm_ContrastSet | ( default_Contrast & 0xF), |
okano | 0:694061176edf | 84 | Comm_PwrIconContrast | ((default_Contrast >> 4) & 0x3), |
okano | 0:694061176edf | 85 | Comm_FollowerCtrl | 0x0A, |
okano | 0:694061176edf | 86 | |
okano | 0:694061176edf | 87 | }; |
okano | 0:694061176edf | 88 | // required 30us interval |
okano | 0:694061176edf | 89 | |
okano | 0:694061176edf | 90 | const char init_seq1_length = 3; |
okano | 0:694061176edf | 91 | const char init_seq1[ init_seq1_length ] |
okano | 0:694061176edf | 92 | = { |
okano | 0:694061176edf | 93 | Comm_DisplayOnOff, |
okano | 0:694061176edf | 94 | Comm_ClearDisplay, |
okano | 0:694061176edf | 95 | Comm_EntryModeSet, |
okano | 0:694061176edf | 96 | }; |
okano | 0:694061176edf | 97 | // required 30us, 2ms interval |
okano | 0:694061176edf | 98 | |
okano | 0:694061176edf | 99 | |
okano | 0:694061176edf | 100 | class TextLCD_SB1602E : I2cBusDevice { |
okano | 0:694061176edf | 101 | public: |
okano | 0:694061176edf | 102 | |
okano | 0:694061176edf | 103 | explicit TextLCD_SB1602E( I2C *i2c, char dev_address = SB1602E_addr, char *init_massage = NULL ) : I2cBusDevice( i2c, dev_address ) { |
okano | 0:694061176edf | 104 | wait( 0.04 ); // interval after hardware reset |
okano | 0:694061176edf | 105 | |
okano | 0:694061176edf | 106 | for ( int i = 0; i < init_seq0_length; i++ ) { |
okano | 0:694061176edf | 107 | lcd_command( init_seq0[ i ] ); |
okano | 0:694061176edf | 108 | wait( 30e-6 ); |
okano | 0:694061176edf | 109 | } |
okano | 0:694061176edf | 110 | |
okano | 0:694061176edf | 111 | wait( 0.2 ); |
okano | 0:694061176edf | 112 | |
okano | 0:694061176edf | 113 | for ( int i = 0; i < init_seq1_length; i++ ) { |
okano | 0:694061176edf | 114 | lcd_command( init_seq1[ i ] ); |
okano | 0:694061176edf | 115 | wait( 2e-3 ); |
okano | 0:694061176edf | 116 | } |
okano | 0:694061176edf | 117 | |
okano | 0:694061176edf | 118 | if ( init_massage ) |
okano | 0:694061176edf | 119 | puts( 0, init_massage ); |
okano | 0:694061176edf | 120 | |
okano | 0:694061176edf | 121 | set_CGRAM( 7, '\x1F' ); |
okano | 0:694061176edf | 122 | |
okano | 0:694061176edf | 123 | curs[ 0 ] = 0; |
okano | 0:694061176edf | 124 | curs[ 1 ] = 0; |
okano | 0:694061176edf | 125 | } |
okano | 0:694061176edf | 126 | |
okano | 0:694061176edf | 127 | |
okano | 0:694061176edf | 128 | ~TextLCD_SB1602E() { |
okano | 0:694061176edf | 129 | } |
okano | 0:694061176edf | 130 | |
okano | 0:694061176edf | 131 | void clear( void ) { |
okano | 0:694061176edf | 132 | lcd_command( Comm_ClearDisplay ); |
okano | 0:694061176edf | 133 | wait( 2e-3 ); |
okano | 0:694061176edf | 134 | curs[ 0 ] = 0; |
okano | 0:694061176edf | 135 | curs[ 1 ] = 0; |
okano | 0:694061176edf | 136 | } |
okano | 0:694061176edf | 137 | |
okano | 0:694061176edf | 138 | |
okano | 0:694061176edf | 139 | void put_custom_char( char c_code, const char *cg, char x, char y ) { |
okano | 0:694061176edf | 140 | for ( int i = 0; i < 5; i++ ) { |
okano | 0:694061176edf | 141 | set_CGRAM( c_code, cg ); |
okano | 0:694061176edf | 142 | putcxy( c_code, x, y ); |
okano | 0:694061176edf | 143 | } |
okano | 0:694061176edf | 144 | } |
okano | 0:694061176edf | 145 | |
okano | 0:694061176edf | 146 | void contrast( char contrast ) { |
okano | 0:694061176edf | 147 | lcd_command( Comm_FunctionSet_Extended ); |
okano | 0:694061176edf | 148 | lcd_command( Comm_ContrastSet | (contrast & 0x0f) ); |
okano | 0:694061176edf | 149 | lcd_command( Comm_PwrIconContrast | ((contrast>>4) & 0x03) ); |
okano | 0:694061176edf | 150 | lcd_command( Comm_FunctionSet_Normal ); |
okano | 0:694061176edf | 151 | } |
okano | 0:694061176edf | 152 | |
okano | 0:694061176edf | 153 | void set_CGRAM( char char_code, const char* cg ) { |
okano | 0:694061176edf | 154 | for ( int i = 0; i < 8; i++ ) { |
okano | 0:694061176edf | 155 | lcd_command( (Comm_SetCGRAM | (char_code << 3) | i) ); |
okano | 0:694061176edf | 156 | lcd_data( *cg++ ); |
okano | 0:694061176edf | 157 | } |
okano | 0:694061176edf | 158 | } |
okano | 0:694061176edf | 159 | |
okano | 0:694061176edf | 160 | void set_CGRAM( char char_code, char v ) { |
okano | 0:694061176edf | 161 | char c[ 8 ]; |
okano | 0:694061176edf | 162 | |
okano | 0:694061176edf | 163 | for ( int i = 0; i < 8; i++ ) |
okano | 0:694061176edf | 164 | c[ i ] = v; |
okano | 0:694061176edf | 165 | |
okano | 0:694061176edf | 166 | set_CGRAM( char_code, c ); |
okano | 0:694061176edf | 167 | } |
okano | 0:694061176edf | 168 | |
okano | 0:694061176edf | 169 | void putcxy( char c, char x, char y ) { |
okano | 0:694061176edf | 170 | if ( (x >= MaxCharsInALine) || (y >= 2) ) |
okano | 0:694061176edf | 171 | return; |
okano | 0:694061176edf | 172 | |
okano | 0:694061176edf | 173 | lcd_command( (Comm_SetDDRAMAddress | DDRAMAddress_Ofst[ y ]) + x ); |
okano | 0:694061176edf | 174 | lcd_data( c ); |
okano | 0:694061176edf | 175 | } |
okano | 0:694061176edf | 176 | |
okano | 0:694061176edf | 177 | void putc( char line, char c ) { |
okano | 0:694061176edf | 178 | if ( (c == '\n') || (c == '\r') ) { |
okano | 0:694061176edf | 179 | clear_lest_of_line( line ); |
okano | 0:694061176edf | 180 | curs[ line ] = 0; |
okano | 0:694061176edf | 181 | return; |
okano | 0:694061176edf | 182 | } |
okano | 0:694061176edf | 183 | |
okano | 0:694061176edf | 184 | putcxy( c, curs[ line ]++, line ); |
okano | 0:694061176edf | 185 | } |
okano | 0:694061176edf | 186 | |
okano | 0:694061176edf | 187 | void puts( char line, char *s ) { |
okano | 0:694061176edf | 188 | while ( char c = *s++ ) |
okano | 0:694061176edf | 189 | putc( line, c ); |
okano | 0:694061176edf | 190 | } |
okano | 0:694061176edf | 191 | |
okano | 0:694061176edf | 192 | void printf( char line, char *format, ... ) { |
okano | 0:694061176edf | 193 | char s[ 32 ]; |
okano | 0:694061176edf | 194 | va_list args; |
okano | 0:694061176edf | 195 | |
okano | 0:694061176edf | 196 | va_start( args, format ); |
okano | 0:694061176edf | 197 | vsnprintf( s, 32, format, args ); |
okano | 0:694061176edf | 198 | va_end( args ); |
okano | 0:694061176edf | 199 | |
okano | 0:694061176edf | 200 | puts( line, s ); |
okano | 0:694061176edf | 201 | } |
okano | 0:694061176edf | 202 | |
okano | 0:694061176edf | 203 | private: |
okano | 0:694061176edf | 204 | char curs[2]; |
okano | 0:694061176edf | 205 | |
okano | 0:694061176edf | 206 | void clear_lest_of_line( char line ) { |
okano | 0:694061176edf | 207 | for ( int i = curs[ line ]; i < MaxCharsInALine; i++ ) |
okano | 0:694061176edf | 208 | putcxy( ' ', i, line ); |
okano | 0:694061176edf | 209 | } |
okano | 0:694061176edf | 210 | |
okano | 0:694061176edf | 211 | int lcd_write( char first, char second ) { |
okano | 0:694061176edf | 212 | char cmd[2]; |
okano | 0:694061176edf | 213 | |
okano | 0:694061176edf | 214 | cmd[ 0 ] = first; |
okano | 0:694061176edf | 215 | cmd[ 1 ] = second; |
okano | 0:694061176edf | 216 | |
okano | 0:694061176edf | 217 | return ( write( cmd, 2 ) ); |
okano | 0:694061176edf | 218 | |
okano | 0:694061176edf | 219 | } |
okano | 0:694061176edf | 220 | |
okano | 0:694061176edf | 221 | int lcd_command( char command ) { |
okano | 0:694061176edf | 222 | return ( lcd_write( COMMAND, command ) ); |
okano | 0:694061176edf | 223 | } |
okano | 0:694061176edf | 224 | |
okano | 0:694061176edf | 225 | int lcd_data( char data ) { |
okano | 0:694061176edf | 226 | return ( lcd_write( DATA, data ) ); |
okano | 0:694061176edf | 227 | } |
okano | 0:694061176edf | 228 | } |
okano | 0:694061176edf | 229 | ; |
okano | 0:694061176edf | 230 | |
okano | 0:694061176edf | 231 | #endif |
okano | 0:694061176edf | 232 | |
okano | 0:694061176edf | 233 | |
okano | 0:694061176edf | 234 | |
okano | 0:694061176edf | 235 | |
okano | 0:694061176edf | 236 | |
okano | 0:694061176edf | 237 | |
okano | 0:694061176edf | 238 | |
okano | 0:694061176edf | 239 |