Create Library
MCC0G42005A6W.cpp@1:3ea320ddd830, 2021-04-13 (annotated)
- Committer:
- qynx
- Date:
- Tue Apr 13 13:06:59 2021 +0000
- Revision:
- 1:3ea320ddd830
- Parent:
- 0:bf6598759869
Small changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
qynx | 0:bf6598759869 | 1 | #include "mbed.h" |
qynx | 0:bf6598759869 | 2 | #include <stdarg.h> |
qynx | 0:bf6598759869 | 3 | #include "MCC0G42005A6W.h" |
qynx | 0:bf6598759869 | 4 | |
qynx | 0:bf6598759869 | 5 | // Note on I2C pull up resistors These should not be too low otherwise the display will |
qynx | 0:bf6598759869 | 6 | // not pull the SDA line low enough to register an ACK. Suggest 10K. |
qynx | 0:bf6598759869 | 7 | // based on Text LCD module "SB1602E" class library @author Tedd OKANO, Masato YAMANISHI & Toyomasa Watarai |
qynx | 0:bf6598759869 | 8 | |
qynx | 0:bf6598759869 | 9 | |
qynx | 0:bf6598759869 | 10 | LCD_COG::LCD_COG( PinName I2C_sda, PinName I2C_scl, const char *init_message ) : i2c_p( new I2C( I2C_sda, I2C_scl ) ), i2c( *i2c_p ), charsInLine( MaxCharsInALine ) |
qynx | 0:bf6598759869 | 11 | { |
qynx | 0:bf6598759869 | 12 | init( init_message ); |
qynx | 1:3ea320ddd830 | 13 | i2c.frequency(50000); |
qynx | 0:bf6598759869 | 14 | |
qynx | 0:bf6598759869 | 15 | } |
qynx | 0:bf6598759869 | 16 | |
qynx | 0:bf6598759869 | 17 | LCD_COG::LCD_COG( I2C &i2c_, const char *init_message ) : i2c_p( NULL ), i2c( i2c_ ), charsInLine( MaxCharsInALine ) |
qynx | 0:bf6598759869 | 18 | { |
qynx | 0:bf6598759869 | 19 | init( init_message ); |
qynx | 1:3ea320ddd830 | 20 | i2c.frequency(50000); |
qynx | 0:bf6598759869 | 21 | } |
qynx | 0:bf6598759869 | 22 | |
qynx | 0:bf6598759869 | 23 | LCD_COG::~LCD_COG() |
qynx | 0:bf6598759869 | 24 | { |
qynx | 0:bf6598759869 | 25 | if ( NULL != i2c_p ) |
qynx | 0:bf6598759869 | 26 | delete i2c_p; |
qynx | 0:bf6598759869 | 27 | } |
qynx | 0:bf6598759869 | 28 | |
qynx | 0:bf6598759869 | 29 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 30 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 31 | void LCD_COG::init( const char *init_message ) |
qynx | 0:bf6598759869 | 32 | { |
qynx | 0:bf6598759869 | 33 | |
qynx | 0:bf6598759869 | 34 | const char init_seq0[] = { |
qynx | 0:bf6598759869 | 35 | |
qynx | 0:bf6598759869 | 36 | 0x3A, // 8-Bit data length extension Bit RE=1; REV=0 send_command(0x3a); // 8bit, 4line, IS=0, RE=1 |
qynx | 0:bf6598759869 | 37 | // 0x09, // 4 line display //?????? |
qynx | 0:bf6598759869 | 38 | TOPVIEW, // Bottom view view bottom view(0x06), top view (0x05) BOTTOMVIEW |
qynx | 0:bf6598759869 | 39 | 0x1E, // Bias setting BS1=1 send_command(0x1e); // set BS1 (1/6 bias) |
qynx | 0:bf6598759869 | 40 | 0x39, // 8-Bit data length extension Bit RE=0; IS=1 send_command(0x39); // 8bit, 4line, IS=1 , RE=0 |
qynx | 0:bf6598759869 | 41 | 0x1c, // BS0=1 -> Bias=1/6 send_command(0x1c); // set BS0 (1/6 bias) + osc |
qynx | 0:bf6598759869 | 42 | 0x6d, // 0110 Set DON and amp ratio send_command(0x6d); // Set DON and amp ratio |
qynx | 0:bf6598759869 | 43 | 0x5d, // 0101 Set ION, BON, contrast bits 4&5 send_command(0x5d); // Set ION, BON, contrast bits 4&5 |
qynx | 0:bf6598759869 | 44 | 0x79, // Set contrast (DB3-DB0=C3-C0) send_command(0x79); // Set contrast lower 4bits |
qynx | 0:bf6598759869 | 45 | 0x38, // 8-Bit data length extension Bit RE=0; IS=0 |
qynx | 0:bf6598759869 | 46 | Comm_DisplayOnOff, |
qynx | 0:bf6598759869 | 47 | Comm_ClearDisplay, |
qynx | 0:bf6598759869 | 48 | Comm_EntryModeSet, |
qynx | 0:bf6598759869 | 49 | }; |
qynx | 0:bf6598759869 | 50 | |
qynx | 0:bf6598759869 | 51 | // i2c_addr = SLAVEADRESS; |
qynx | 0:bf6598759869 | 52 | |
qynx | 0:bf6598759869 | 53 | wait( 0.04 ); // 40ms interval after hardware reset |
qynx | 0:bf6598759869 | 54 | |
qynx | 0:bf6598759869 | 55 | for ( unsigned int i = 0; i < sizeof( init_seq0 ); i++ ) { |
qynx | 0:bf6598759869 | 56 | lcd_command( init_seq0[ i ] ); |
qynx | 1:3ea320ddd830 | 57 | wait(30e-6);// // 30e-6 0.00003 30ns ? |
qynx | 0:bf6598759869 | 58 | } |
qynx | 0:bf6598759869 | 59 | |
qynx | 0:bf6598759869 | 60 | set_CGRAM( 7, '\x1F' ); |
qynx | 0:bf6598759869 | 61 | |
qynx | 0:bf6598759869 | 62 | curs[0] = 0; |
qynx | 0:bf6598759869 | 63 | curs[1] = 0; |
qynx | 0:bf6598759869 | 64 | curs[2] = 0; |
qynx | 0:bf6598759869 | 65 | curs[3] = 0; |
qynx | 0:bf6598759869 | 66 | |
qynx | 0:bf6598759869 | 67 | if ( init_message ) { |
qynx | 0:bf6598759869 | 68 | //i2c.frequency(100000); |
qynx | 0:bf6598759869 | 69 | puts( 0, init_message ); |
qynx | 0:bf6598759869 | 70 | curs[0] = 0; |
qynx | 0:bf6598759869 | 71 | // puts( 1, "Dit is de 2e regel 2" ); curs[1] = 0; |
qynx | 0:bf6598759869 | 72 | // puts( 2, "Dit is de 3e regel 3" ); curs[2] = 0; |
qynx | 0:bf6598759869 | 73 | // puts( 3, "Dit is de 4e regel 4" ); curs[3] = 0; |
qynx | 0:bf6598759869 | 74 | } |
qynx | 0:bf6598759869 | 75 | } |
qynx | 0:bf6598759869 | 76 | |
qynx | 0:bf6598759869 | 77 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 78 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 79 | void LCD_COG::printf( const char line, const char *format, ... ) |
qynx | 0:bf6598759869 | 80 | { |
qynx | 0:bf6598759869 | 81 | char s[ 32 ]; |
qynx | 0:bf6598759869 | 82 | va_list args; |
qynx | 0:bf6598759869 | 83 | |
qynx | 0:bf6598759869 | 84 | va_start( args, format ); |
qynx | 0:bf6598759869 | 85 | vsnprintf( s, 32, format, args ); |
qynx | 0:bf6598759869 | 86 | va_end( args ); |
qynx | 0:bf6598759869 | 87 | |
qynx | 0:bf6598759869 | 88 | puts( line, s ); |
qynx | 0:bf6598759869 | 89 | } |
qynx | 0:bf6598759869 | 90 | |
qynx | 0:bf6598759869 | 91 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 92 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 93 | |
qynx | 0:bf6598759869 | 94 | void LCD_COG::printf( char x, unsigned char y, const char *format, ... ) |
qynx | 0:bf6598759869 | 95 | { |
qynx | 0:bf6598759869 | 96 | char s[ 32 ]; |
qynx | 0:bf6598759869 | 97 | va_list args; |
qynx | 0:bf6598759869 | 98 | |
qynx | 0:bf6598759869 | 99 | va_start( args, format ); |
qynx | 0:bf6598759869 | 100 | vsnprintf( s, 32, format, args ); |
qynx | 0:bf6598759869 | 101 | va_end( args ); |
qynx | 0:bf6598759869 | 102 | |
qynx | 0:bf6598759869 | 103 | curs[ y ] = x; |
qynx | 0:bf6598759869 | 104 | puts( y, s ); |
qynx | 0:bf6598759869 | 105 | } |
qynx | 0:bf6598759869 | 106 | |
qynx | 0:bf6598759869 | 107 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 108 | void LCD_COG::putc( unsigned char line, char c ) |
qynx | 0:bf6598759869 | 109 | { |
qynx | 0:bf6598759869 | 110 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 111 | if ( (c == '\n') || (c == '\r') ) { |
qynx | 0:bf6598759869 | 112 | clear_rest_of_line( line ); |
qynx | 0:bf6598759869 | 113 | curs[ line ] = 0; |
qynx | 0:bf6598759869 | 114 | return; |
qynx | 0:bf6598759869 | 115 | } |
qynx | 0:bf6598759869 | 116 | |
qynx | 0:bf6598759869 | 117 | putcxy( c, curs[line]++, line ); |
qynx | 0:bf6598759869 | 118 | } |
qynx | 0:bf6598759869 | 119 | |
qynx | 0:bf6598759869 | 120 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 121 | void LCD_COG::puts( char line, const char *s ) |
qynx | 0:bf6598759869 | 122 | { |
qynx | 0:bf6598759869 | 123 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 124 | while ( char c = *s++ ) |
qynx | 0:bf6598759869 | 125 | putc( line, c ); |
qynx | 0:bf6598759869 | 126 | } |
qynx | 0:bf6598759869 | 127 | |
qynx | 0:bf6598759869 | 128 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 129 | void LCD_COG::putcxy( char c, unsigned char x, unsigned char y ) |
qynx | 0:bf6598759869 | 130 | { |
qynx | 0:bf6598759869 | 131 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 132 | const char Comm_SetDDRAMAddress = LCD_HOME_L1; |
qynx | 0:bf6598759869 | 133 | const char DDRAMAddress_Ofst[] = { LINE1, LINE2, LINE3, LINE4 }; |
qynx | 0:bf6598759869 | 134 | |
qynx | 0:bf6598759869 | 135 | if ( (x >= charsInLine) || (y >= LINES) ) |
qynx | 0:bf6598759869 | 136 | return; |
qynx | 0:bf6598759869 | 137 | |
qynx | 0:bf6598759869 | 138 | lcd_command( (Comm_SetDDRAMAddress | DDRAMAddress_Ofst[y]) + x ); |
qynx | 0:bf6598759869 | 139 | lcd_data( c ); |
qynx | 0:bf6598759869 | 140 | } |
qynx | 0:bf6598759869 | 141 | |
qynx | 0:bf6598759869 | 142 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 143 | void LCD_COG::clear( void ) |
qynx | 0:bf6598759869 | 144 | { |
qynx | 0:bf6598759869 | 145 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 146 | lcd_command( Comm_ClearDisplay ); |
qynx | 1:3ea320ddd830 | 147 | wait( 2e-3 ); // 2e-3 0.002 2ms |
qynx | 0:bf6598759869 | 148 | curs[ 0 ] = 0; |
qynx | 0:bf6598759869 | 149 | curs[ 1 ] = 0; |
qynx | 0:bf6598759869 | 150 | curs[ 2 ] = 0; |
qynx | 0:bf6598759869 | 151 | curs[ 3 ] = 0; |
qynx | 0:bf6598759869 | 152 | } |
qynx | 0:bf6598759869 | 153 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 154 | void LCD_COG::contrast( char contrast ) |
qynx | 0:bf6598759869 | 155 | { |
qynx | 0:bf6598759869 | 156 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 157 | lcd_command( Comm_FunctionSet_Extended ); |
qynx | 0:bf6598759869 | 158 | lcd_command( Comm_ContrastSet | (contrast & 0x0f) ); |
qynx | 0:bf6598759869 | 159 | lcd_command( Comm_PwrIconContrast | ((contrast>>4) & 0x03) ); |
qynx | 0:bf6598759869 | 160 | lcd_command( Comm_FunctionSet_Normal ); |
qynx | 0:bf6598759869 | 161 | } |
qynx | 0:bf6598759869 | 162 | |
qynx | 0:bf6598759869 | 163 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 164 | void LCD_COG::put_custom_char( char c_code, const char *cg, char x, char y ) |
qynx | 0:bf6598759869 | 165 | { |
qynx | 0:bf6598759869 | 166 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 167 | for ( int i = 0; i < 5; i++ ) { |
qynx | 0:bf6598759869 | 168 | set_CGRAM( c_code, cg ); |
qynx | 0:bf6598759869 | 169 | putcxy( c_code, x, y ); |
qynx | 0:bf6598759869 | 170 | } |
qynx | 0:bf6598759869 | 171 | } |
qynx | 0:bf6598759869 | 172 | |
qynx | 0:bf6598759869 | 173 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 174 | void LCD_COG::set_CGRAM( char char_code, const char* cg ) |
qynx | 0:bf6598759869 | 175 | { |
qynx | 0:bf6598759869 | 176 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 177 | for ( int i = 0; i < 8; i++ ) { |
qynx | 0:bf6598759869 | 178 | lcd_command( (Comm_SetCGRAM | (char_code << 3) | i) ); |
qynx | 0:bf6598759869 | 179 | lcd_data( *cg++ ); |
qynx | 0:bf6598759869 | 180 | } |
qynx | 0:bf6598759869 | 181 | } |
qynx | 0:bf6598759869 | 182 | |
qynx | 0:bf6598759869 | 183 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 184 | void LCD_COG::set_CGRAM( char char_code, char v ) |
qynx | 0:bf6598759869 | 185 | { |
qynx | 0:bf6598759869 | 186 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 187 | char c[ 8 ]; |
qynx | 0:bf6598759869 | 188 | |
qynx | 0:bf6598759869 | 189 | for ( int i = 0; i < 8; i++ ) |
qynx | 0:bf6598759869 | 190 | c[ i ] = v; |
qynx | 0:bf6598759869 | 191 | |
qynx | 0:bf6598759869 | 192 | set_CGRAM( char_code, c ); |
qynx | 0:bf6598759869 | 193 | } |
qynx | 0:bf6598759869 | 194 | |
qynx | 0:bf6598759869 | 195 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 196 | void LCD_COG::clear_rest_of_line( unsigned char line ) |
qynx | 0:bf6598759869 | 197 | { |
qynx | 0:bf6598759869 | 198 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 199 | for ( int i = curs[ line ]; i < charsInLine; i++ ) |
qynx | 0:bf6598759869 | 200 | putcxy( ' ', i, line ); |
qynx | 0:bf6598759869 | 201 | } |
qynx | 0:bf6598759869 | 202 | |
qynx | 0:bf6598759869 | 203 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 204 | int LCD_COG::lcd_write( char first, char second ) |
qynx | 0:bf6598759869 | 205 | { |
qynx | 0:bf6598759869 | 206 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 207 | char cmd[2]; |
qynx | 0:bf6598759869 | 208 | |
qynx | 0:bf6598759869 | 209 | cmd[ 0 ] = first; |
qynx | 0:bf6598759869 | 210 | cmd[ 1 ] = second; |
qynx | 0:bf6598759869 | 211 | |
qynx | 0:bf6598759869 | 212 | return ( i2c.write( SLAVEADRESS, cmd, 2 ) ); |
qynx | 0:bf6598759869 | 213 | } |
qynx | 0:bf6598759869 | 214 | |
qynx | 0:bf6598759869 | 215 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 216 | int LCD_COG::lcd_command( char command ) |
qynx | 0:bf6598759869 | 217 | { |
qynx | 0:bf6598759869 | 218 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 219 | return ( lcd_write( COMMAND, command ) ); |
qynx | 0:bf6598759869 | 220 | } |
qynx | 0:bf6598759869 | 221 | |
qynx | 0:bf6598759869 | 222 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 223 | int LCD_COG::lcd_data( char data ) |
qynx | 0:bf6598759869 | 224 | { |
qynx | 0:bf6598759869 | 225 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
qynx | 0:bf6598759869 | 226 | return ( lcd_write( DATA, data ) ); |
qynx | 0:bf6598759869 | 227 | } |
qynx | 0:bf6598759869 | 228 |