tdse-tp3_01-lcd_display

Index Commits Files Refs
modules/display/display.cpp (7546B)
   1 #include "mbed.h"
   2 #include "arm_book_lib.h"
   3 #include "display.h"
   4 
   5 #define DISPLAY_IR_CLEAR_DISPLAY   0b00000001
   6 #define DISPLAY_IR_ENTRY_MODE_SET  0b00000100
   7 #define DISPLAY_IR_DISPLAY_CONTROL 0b00001000
   8 #define DISPLAY_IR_FUNCTION_SET    0b00100000
   9 #define DISPLAY_IR_SET_DDRAM_ADDR  0b10000000
  10 
  11 #define DISPLAY_IR_ENTRY_MODE_SET_INCREMENT 0b00000010
  12 #define DISPLAY_IR_ENTRY_MODE_SET_DECREMENT 0b00000000
  13 #define DISPLAY_IR_ENTRY_MODE_SET_SHIFT     0b00000001
  14 #define DISPLAY_IR_ENTRY_MODE_SET_NO_SHIFT  0b00000000
  15 
  16 #define DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_ON  0b00000100
  17 #define DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_OFF 0b00000000
  18 #define DISPLAY_IR_DISPLAY_CONTROL_CURSOR_ON   0b00000010
  19 #define DISPLAY_IR_DISPLAY_CONTROL_CURSOR_OFF  0b00000000
  20 #define DISPLAY_IR_DISPLAY_CONTROL_BLINK_ON    0b00000001
  21 #define DISPLAY_IR_DISPLAY_CONTROL_BLINK_OFF   0b00000000
  22 
  23 #define DISPLAY_IR_FUNCTION_SET_8BITS    0b00010000
  24 #define DISPLAY_IR_FUNCTION_SET_4BITS    0b00000000
  25 #define DISPLAY_IR_FUNCTION_SET_2LINES   0b00001000
  26 #define DISPLAY_IR_FUNCTION_SET_1LINE    0b00000000
  27 #define DISPLAY_IR_FUNCTION_SET_5x10DOTS 0b00000100
  28 #define DISPLAY_IR_FUNCTION_SET_5x8DOTS  0b00000000
  29 
  30 #define DISPLAY_20x4_LINE1_FIRST_CHARACTER_ADDRESS 0
  31 #define DISPLAY_20x4_LINE2_FIRST_CHARACTER_ADDRESS 64
  32 #define DISPLAY_20x4_LINE3_FIRST_CHARACTER_ADDRESS 20
  33 #define DISPLAY_20x4_LINE4_FIRST_CHARACTER_ADDRESS 84
  34 
  35 #define DISPLAY_RS_INSTRUCTION 0
  36 #define DISPLAY_RS_DATA        1
  37 
  38 #define DISPLAY_RW_WRITE 0
  39 #define DISPLAY_RW_READ  1
  40 
  41 #define DISPLAY_PIN_RS  8
  42 #define DISPLAY_PIN_EN  9
  43 
  44 #define DISPLAY_PIN_D4 4
  45 #define DISPLAY_PIN_D5 5 
  46 #define DISPLAY_PIN_D6 6 
  47 #define DISPLAY_PIN_D7 7 
  48 
  49 DigitalOut displayD4( D4 );
  50 DigitalOut displayD5( D5 );
  51 DigitalOut displayD6( D6 );
  52 DigitalOut displayD7( D7 );
  53 DigitalOut displayRs( D8 );
  54 DigitalOut displayEn( D9 );
  55 
  56 static bool initial8BitCommunicationIsCompleted;
  57 
  58 static void displayPinWrite( uint8_t pinName, int value );
  59 static void displayDataBusWrite( uint8_t dataByte );
  60 static void displayCodeWrite( bool type, uint8_t dataBus );
  61 
  62 void displayInit()
  63 {
  64     initial8BitCommunicationIsCompleted = false;    
  65 
  66     delay( 50 );
  67     
  68     displayCodeWrite( DISPLAY_RS_INSTRUCTION, 
  69                       DISPLAY_IR_FUNCTION_SET | 
  70                       DISPLAY_IR_FUNCTION_SET_8BITS );
  71     delay( 5 );
  72             
  73     displayCodeWrite( DISPLAY_RS_INSTRUCTION, 
  74                       DISPLAY_IR_FUNCTION_SET | 
  75                       DISPLAY_IR_FUNCTION_SET_8BITS );
  76     delay( 1 ); 
  77 
  78     displayCodeWrite( DISPLAY_RS_INSTRUCTION, 
  79                       DISPLAY_IR_FUNCTION_SET | 
  80                       DISPLAY_IR_FUNCTION_SET_8BITS );
  81     delay( 1 );  
  82 
  83     displayCodeWrite( DISPLAY_RS_INSTRUCTION, 
  84                         DISPLAY_IR_FUNCTION_SET | 
  85                         DISPLAY_IR_FUNCTION_SET_4BITS );
  86     delay( 1 );  
  87 
  88     initial8BitCommunicationIsCompleted = true;  
  89 
  90     displayCodeWrite( DISPLAY_RS_INSTRUCTION, 
  91                         DISPLAY_IR_FUNCTION_SET | 
  92                         DISPLAY_IR_FUNCTION_SET_4BITS | 
  93                         DISPLAY_IR_FUNCTION_SET_2LINES |
  94                         DISPLAY_IR_FUNCTION_SET_5x8DOTS );
  95     delay( 1 );                                      
  96 
  97     displayCodeWrite( DISPLAY_RS_INSTRUCTION, 
  98                       DISPLAY_IR_DISPLAY_CONTROL |
  99                       DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_OFF |      
 100                       DISPLAY_IR_DISPLAY_CONTROL_CURSOR_OFF |       
 101                       DISPLAY_IR_DISPLAY_CONTROL_BLINK_OFF );       
 102     delay( 1 );          
 103 
 104     displayCodeWrite( DISPLAY_RS_INSTRUCTION, 
 105                       DISPLAY_IR_CLEAR_DISPLAY );       
 106     delay( 1 ); 
 107 
 108     displayCodeWrite( DISPLAY_RS_INSTRUCTION, 
 109                       DISPLAY_IR_ENTRY_MODE_SET |
 110                       DISPLAY_IR_ENTRY_MODE_SET_INCREMENT |       
 111                       DISPLAY_IR_ENTRY_MODE_SET_NO_SHIFT );                  
 112     delay( 1 );           
 113 
 114     displayCodeWrite( DISPLAY_RS_INSTRUCTION, 
 115                       DISPLAY_IR_DISPLAY_CONTROL |
 116                       DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_ON |      
 117                       DISPLAY_IR_DISPLAY_CONTROL_CURSOR_ON |    
 118                       DISPLAY_IR_DISPLAY_CONTROL_BLINK_ON );    
 119     delay( 1 );  
 120 }
 121 
 122 void displayCharPositionWrite( uint8_t charPositionX, uint8_t charPositionY )
 123 {    
 124     switch( charPositionY ) {
 125         case 0:
 126             displayCodeWrite( DISPLAY_RS_INSTRUCTION, 
 127                               DISPLAY_IR_SET_DDRAM_ADDR |
 128                               ( DISPLAY_20x4_LINE1_FIRST_CHARACTER_ADDRESS +
 129                                 charPositionX ) );
 130             delay( 1 );         
 131         break;
 132        
 133         case 1:
 134             displayCodeWrite( DISPLAY_RS_INSTRUCTION, 
 135                               DISPLAY_IR_SET_DDRAM_ADDR |
 136                               ( DISPLAY_20x4_LINE2_FIRST_CHARACTER_ADDRESS +
 137                                 charPositionX ) );
 138             delay( 1 );         
 139         break;
 140        
 141         case 2:
 142             displayCodeWrite( DISPLAY_RS_INSTRUCTION, 
 143                               DISPLAY_IR_SET_DDRAM_ADDR |
 144                               ( DISPLAY_20x4_LINE3_FIRST_CHARACTER_ADDRESS +
 145                                 charPositionX ) );
 146             delay( 1 );         
 147         break;
 148 
 149         case 3:
 150             displayCodeWrite( DISPLAY_RS_INSTRUCTION, 
 151                               DISPLAY_IR_SET_DDRAM_ADDR |
 152                               ( DISPLAY_20x4_LINE4_FIRST_CHARACTER_ADDRESS +
 153                                 charPositionX ) );
 154             delay( 1 );         
 155         break;
 156     }
 157 }
 158 
 159 void displayStringWrite( const char * str )
 160 {
 161     while (*str) {
 162         displayCodeWrite(DISPLAY_RS_DATA, *str++);
 163     }
 164 }
 165 
 166 // private functions
 167 
 168 static void displayCodeWrite( bool type, uint8_t dataBus )
 169 {
 170     if ( type == DISPLAY_RS_INSTRUCTION ) {
 171         displayPinWrite( DISPLAY_PIN_RS, DISPLAY_RS_INSTRUCTION);
 172     }
 173     else {
 174         displayPinWrite( DISPLAY_PIN_RS, DISPLAY_RS_DATA);
 175     }
 176     displayDataBusWrite( dataBus );
 177 }
 178 
 179 static void displayPinWrite( uint8_t pinName, int value )
 180 {
 181     switch( pinName ) {
 182         case DISPLAY_PIN_D4: displayD4 = value;   break;
 183         case DISPLAY_PIN_D5: displayD5 = value;   break;
 184         case DISPLAY_PIN_D6: displayD6 = value;   break;
 185         case DISPLAY_PIN_D7: displayD7 = value;   break;
 186         case DISPLAY_PIN_RS: displayRs = value;   break;
 187         case DISPLAY_PIN_EN: displayEn = value;   break;
 188         default: break;
 189     }
 190 }
 191 
 192 static void displayDataBusWrite( uint8_t dataBus )
 193 {
 194     displayPinWrite( DISPLAY_PIN_EN, OFF );
 195     displayPinWrite( DISPLAY_PIN_D7, dataBus & 0b10000000 );
 196     displayPinWrite( DISPLAY_PIN_D6, dataBus & 0b01000000 );
 197     displayPinWrite( DISPLAY_PIN_D5, dataBus & 0b00100000 );
 198     displayPinWrite( DISPLAY_PIN_D4, dataBus & 0b00010000 );
 199 
 200     if ( initial8BitCommunicationIsCompleted == true) {
 201         displayPinWrite( DISPLAY_PIN_EN, ON );         
 202         delay( 1 );
 203         displayPinWrite( DISPLAY_PIN_EN, OFF );              
 204         delay( 1 );        
 205         displayPinWrite( DISPLAY_PIN_D7, dataBus & 0b00001000 );
 206         displayPinWrite( DISPLAY_PIN_D6, dataBus & 0b00000100 );  
 207         displayPinWrite( DISPLAY_PIN_D5, dataBus & 0b00000010 );      
 208         displayPinWrite( DISPLAY_PIN_D4, dataBus & 0b00000001 );                
 209     }
 210    
 211     displayPinWrite( DISPLAY_PIN_EN, ON );              
 212     delay( 1 );
 213     displayPinWrite( DISPLAY_PIN_EN, OFF );  
 214     delay( 1 );                   
 215 }