1 //=====[Libraries]============================================================= 2 //#include "mbed.h" 3 //#include "arm_book_lib.h" 4 5 #include "display.h" 6 #include "main.h" 7 #include <stdbool.h> 8 9 /* Demo includes */ 10 #include "logger.h" 11 #include "dwt.h" 12 #include "systick.h" 13 14 /********************** arm_book Defines *******************************/ 15 //#include "arm_book_lib.h" 16 // Functional states 17 #ifndef OFF 18 #define OFF 0 19 #endif 20 #ifndef ON 21 #define ON ( !OFF ) 22 #endif 23 24 // Electrical states 25 #ifndef LOW 26 #define LOW 0 27 #endif 28 #ifndef HIGH 29 #define HIGH ( !LOW ) 30 #endif 31 32 //=====[Declaration of private defines]======================================== 33 #define DISPLAY_IR_CLEAR_DISPLAY 0b00000001 34 #define DISPLAY_IR_SET_CURSOR_HOME 0b00000010 35 #define DISPLAY_IR_ENTRY_MODE_SET 0b00000100 36 #define DISPLAY_IR_DISPLAY_CONTROL 0b00001000 37 #define DISPLAY_IR_FUNCTION_SET 0b00100000 38 #define DISPLAY_IR_SET_DDRAM_ADDR 0b10000000 39 40 #define DISPLAY_IR_ENTRY_MODE_SET_INCREMENT 0b00000010 41 #define DISPLAY_IR_ENTRY_MODE_SET_DECREMENT 0b00000000 42 #define DISPLAY_IR_ENTRY_MODE_SET_SHIFT 0b00000001 43 #define DISPLAY_IR_ENTRY_MODE_SET_NO_SHIFT 0b00000000 44 45 #define DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_ON 0b00000100 46 #define DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_OFF 0b00000000 47 #define DISPLAY_IR_DISPLAY_CONTROL_CURSOR_ON 0b00000010 48 #define DISPLAY_IR_DISPLAY_CONTROL_CURSOR_OFF 0b00000000 49 #define DISPLAY_IR_DISPLAY_CONTROL_BLINK_ON 0b00000001 50 #define DISPLAY_IR_DISPLAY_CONTROL_BLINK_OFF 0b00000000 51 52 #define DISPLAY_IR_FUNCTION_SET_8BITS 0b00010000 53 #define DISPLAY_IR_FUNCTION_SET_4BITS 0b00000000 54 #define DISPLAY_IR_FUNCTION_SET_2LINES 0b00001000 55 #define DISPLAY_IR_FUNCTION_SET_1LINE 0b00000000 56 #define DISPLAY_IR_FUNCTION_SET_5x10DOTS 0b00000100 57 #define DISPLAY_IR_FUNCTION_SET_5x8DOTS 0b00000000 58 59 #define DISPLAY_20x4_LINE1_FIRST_CHARACTER_ADDRESS 0 60 #define DISPLAY_20x4_LINE2_FIRST_CHARACTER_ADDRESS 64 61 #define DISPLAY_20x4_LINE3_FIRST_CHARACTER_ADDRESS 20 62 #define DISPLAY_20x4_LINE4_FIRST_CHARACTER_ADDRESS 84 63 64 #define DISPLAY_RS_INSTRUCTION 0 65 #define DISPLAY_RS_DATA 1 66 67 #define DISPLAY_RW_WRITE 0 68 #define DISPLAY_RW_READ 1 69 70 #define DISPLAY_PIN_RS 4 71 #define DISPLAY_PIN_RW 5 72 #define DISPLAY_PIN_EN 6 73 #define DISPLAY_PIN_D0 7 74 #define DISPLAY_PIN_D1 8 75 #define DISPLAY_PIN_D2 9 76 #define DISPLAY_PIN_D3 10 77 #define DISPLAY_PIN_D4 11 78 #define DISPLAY_PIN_D5 12 79 #define DISPLAY_PIN_D6 13 80 #define DISPLAY_PIN_D7 14 81 82 #define DISPLAY_DEL_37US 37ul 83 #define DISPLAY_DEL_01US 01ul 84 85 //=====[Declaration of private data types]===================================== 86 87 //=====[Declaration and initialization of public global objects]=============== 88 89 /* 90 DigitalOut displayD0( D0 ); 91 DigitalOut displayD1( D1 ); 92 DigitalOut displayD2( D2 ); 93 DigitalOut displayD3( D3 ); 94 DigitalOut displayD4( D4 ); 95 DigitalOut displayD5( D5 ); 96 DigitalOut displayD6( D6 ); 97 DigitalOut displayD7( D7 ); 98 DigitalOut displayRs( D8 ); 99 DigitalOut displayEn( D9 ); 100 */ 101 102 //=====[Declaration of external public global variables]======================= 103 104 //=====[Declaration and initialization of private global variables]============ 105 static display_t display; 106 static bool initial8BitCommunicationIsCompleted; 107 108 //=====[Declarations (prototypes) of private functions]======================== 109 static void displayPinWrite( uint8_t pinName, int value ); 110 static void displayDataBusWrite( uint8_t dataByte ); 111 static void displayCodeWrite( bool type, uint8_t dataBus ); 112 113 //=====[Implementations of public functions]=================================== 114 void displayInit( displayConnection_t connection ) 115 { 116 display.connection = connection; 117 118 initial8BitCommunicationIsCompleted = false; 119 120 HAL_Delay(50); 121 122 displayCodeWrite( DISPLAY_RS_INSTRUCTION, 123 DISPLAY_IR_FUNCTION_SET | 124 DISPLAY_IR_FUNCTION_SET_8BITS ); 125 HAL_Delay(5); 126 127 displayCodeWrite( DISPLAY_RS_INSTRUCTION, 128 DISPLAY_IR_FUNCTION_SET | 129 DISPLAY_IR_FUNCTION_SET_8BITS ); 130 HAL_Delay(1); 131 132 displayCodeWrite( DISPLAY_RS_INSTRUCTION, 133 DISPLAY_IR_FUNCTION_SET | 134 DISPLAY_IR_FUNCTION_SET_8BITS ); 135 HAL_Delay(1); 136 137 switch( display.connection ) { 138 case DISPLAY_CONNECTION_GPIO_8BITS: 139 displayCodeWrite( DISPLAY_RS_INSTRUCTION, 140 DISPLAY_IR_FUNCTION_SET | 141 DISPLAY_IR_FUNCTION_SET_8BITS | 142 DISPLAY_IR_FUNCTION_SET_2LINES | 143 DISPLAY_IR_FUNCTION_SET_5x8DOTS ); 144 HAL_Delay(1); 145 break; 146 147 case DISPLAY_CONNECTION_GPIO_4BITS: 148 displayCodeWrite( DISPLAY_RS_INSTRUCTION, 149 DISPLAY_IR_FUNCTION_SET | 150 DISPLAY_IR_FUNCTION_SET_4BITS ); 151 HAL_Delay(1); 152 153 initial8BitCommunicationIsCompleted = true; 154 155 displayCodeWrite( DISPLAY_RS_INSTRUCTION, 156 DISPLAY_IR_FUNCTION_SET | 157 DISPLAY_IR_FUNCTION_SET_4BITS | 158 DISPLAY_IR_FUNCTION_SET_2LINES | 159 DISPLAY_IR_FUNCTION_SET_5x8DOTS ); 160 HAL_Delay(1); 161 break; 162 } 163 164 displayCodeWrite( DISPLAY_RS_INSTRUCTION, 165 DISPLAY_IR_DISPLAY_CONTROL | 166 DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_OFF | 167 DISPLAY_IR_DISPLAY_CONTROL_CURSOR_OFF | 168 DISPLAY_IR_DISPLAY_CONTROL_BLINK_OFF ); 169 HAL_Delay(1); 170 171 displayCodeWrite( DISPLAY_RS_INSTRUCTION, 172 DISPLAY_IR_CLEAR_DISPLAY ); 173 HAL_Delay(1); 174 175 displayCodeWrite( DISPLAY_RS_INSTRUCTION, 176 DISPLAY_IR_ENTRY_MODE_SET | 177 DISPLAY_IR_ENTRY_MODE_SET_INCREMENT | 178 DISPLAY_IR_ENTRY_MODE_SET_NO_SHIFT ); 179 HAL_Delay(1); 180 181 displayCodeWrite( DISPLAY_RS_INSTRUCTION, 182 DISPLAY_IR_DISPLAY_CONTROL | 183 DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_ON | 184 DISPLAY_IR_DISPLAY_CONTROL_CURSOR_OFF | 185 DISPLAY_IR_DISPLAY_CONTROL_BLINK_OFF ); 186 HAL_Delay(1); 187 } 188 189 void displayClear(void) 190 { 191 displayCodeWrite( DISPLAY_RS_INSTRUCTION, 192 DISPLAY_IR_CLEAR_DISPLAY); 193 194 195 HAL_Delay(1); 196 } 197 198 void displayCharPositionWrite( uint8_t charPositionX, uint8_t charPositionY ) 199 { 200 switch( charPositionY ) { 201 case 0: 202 displayCodeWrite( DISPLAY_RS_INSTRUCTION, 203 DISPLAY_IR_SET_DDRAM_ADDR | 204 ( DISPLAY_20x4_LINE1_FIRST_CHARACTER_ADDRESS + 205 charPositionX ) ); 206 //HAL_Delay(1); 207 break; 208 209 case 1: 210 displayCodeWrite( DISPLAY_RS_INSTRUCTION, 211 DISPLAY_IR_SET_DDRAM_ADDR | 212 ( DISPLAY_20x4_LINE2_FIRST_CHARACTER_ADDRESS + 213 charPositionX ) ); 214 //HAL_Delay(1); 215 break; 216 217 case 2: 218 displayCodeWrite( DISPLAY_RS_INSTRUCTION, 219 DISPLAY_IR_SET_DDRAM_ADDR | 220 ( DISPLAY_20x4_LINE3_FIRST_CHARACTER_ADDRESS + 221 charPositionX ) ); 222 //HAL_Delay(1); 223 break; 224 225 case 3: 226 displayCodeWrite( DISPLAY_RS_INSTRUCTION, 227 DISPLAY_IR_SET_DDRAM_ADDR | 228 ( DISPLAY_20x4_LINE4_FIRST_CHARACTER_ADDRESS + 229 charPositionX ) ); 230 //HAL_Delay(1); 231 break; 232 } 233 } 234 235 void displayStringWrite( const char * str ) 236 { 237 while (*str) { 238 displayCodeWrite(DISPLAY_RS_DATA, *str++); 239 } 240 } 241 242 //=====[Implementations of private functions]================================== 243 static void displayCodeWrite( bool type, uint8_t dataBus ) 244 { 245 if ( type == DISPLAY_RS_INSTRUCTION ) 246 displayPinWrite( DISPLAY_PIN_RS, DISPLAY_RS_INSTRUCTION ); 247 else 248 displayPinWrite( DISPLAY_PIN_RS, DISPLAY_RS_DATA ); 249 250 displayPinWrite( DISPLAY_PIN_RW, DISPLAY_RW_WRITE ); 251 displayDataBusWrite( dataBus ); 252 systick_delay_us(DISPLAY_DEL_37US); 253 254 } 255 256 static void displayPinWrite( uint8_t pinName, int value ) 257 { 258 switch( display.connection ) { 259 case DISPLAY_CONNECTION_GPIO_8BITS: 260 switch( pinName ) { 261 /* 262 case DISPLAY_PIN_D0: displayD0 = value; break; 263 case DISPLAY_PIN_D1: displayD1 = value; break; 264 case DISPLAY_PIN_D2: displayD2 = value; break; 265 case DISPLAY_PIN_D3: displayD3 = value; break; 266 case DISPLAY_PIN_D4: displayD4 = value; break; 267 case DISPLAY_PIN_D5: displayD5 = value; break; 268 case DISPLAY_PIN_D6: displayD6 = value; break; 269 case DISPLAY_PIN_D7: displayD7 = value; break; 270 case DISPLAY_PIN_RS: displayRs = value; break; 271 case DISPLAY_PIN_EN: displayEn = value; break; 272 case DISPLAY_PIN_RW: break; 273 */ 274 default: break; 275 } 276 break; 277 278 case DISPLAY_CONNECTION_GPIO_4BITS: 279 switch( pinName ) { 280 case DISPLAY_PIN_D4: HAL_GPIO_WritePin(D4_GPIO_Port, D4_Pin, value); break; 281 case DISPLAY_PIN_D5: HAL_GPIO_WritePin(D5_GPIO_Port, D5_Pin, value); break; 282 case DISPLAY_PIN_D6: HAL_GPIO_WritePin(D6_GPIO_Port, D6_Pin, value); break; 283 case DISPLAY_PIN_D7: HAL_GPIO_WritePin(D7_GPIO_Port, D7_Pin, value); break; 284 case DISPLAY_PIN_RS: HAL_GPIO_WritePin(D8_GPIO_Port, D8_Pin, value); break; 285 case DISPLAY_PIN_EN: HAL_GPIO_WritePin(D9_GPIO_Port, D9_Pin, value); break; 286 case DISPLAY_PIN_RW: break; 287 default: break; 288 } 289 break; 290 } 291 } 292 293 static void displayDataBusWrite( uint8_t dataBus ) 294 { 295 displayPinWrite( DISPLAY_PIN_EN, OFF ); 296 displayPinWrite( DISPLAY_PIN_D7, dataBus & 0b10000000 ); 297 displayPinWrite( DISPLAY_PIN_D6, dataBus & 0b01000000 ); 298 displayPinWrite( DISPLAY_PIN_D5, dataBus & 0b00100000 ); 299 displayPinWrite( DISPLAY_PIN_D4, dataBus & 0b00010000 ); 300 301 switch( display.connection ) { 302 case DISPLAY_CONNECTION_GPIO_8BITS: 303 displayPinWrite( DISPLAY_PIN_D3, dataBus & 0b00001000 ); 304 displayPinWrite( DISPLAY_PIN_D2, dataBus & 0b00000100 ); 305 displayPinWrite( DISPLAY_PIN_D1, dataBus & 0b00000010 ); 306 displayPinWrite( DISPLAY_PIN_D0, dataBus & 0b00000001 ); 307 break; 308 309 case DISPLAY_CONNECTION_GPIO_4BITS: 310 if ( initial8BitCommunicationIsCompleted == true) { 311 displayPinWrite( DISPLAY_PIN_EN, ON ); 312 //HAL_Delay(1); 313 systick_delay_us(DISPLAY_DEL_01US); 314 315 displayPinWrite( DISPLAY_PIN_EN, OFF ); 316 //HAL_Delay(1); 317 systick_delay_us(DISPLAY_DEL_01US); 318 319 displayPinWrite( DISPLAY_PIN_D7, dataBus & 0b00001000 ); 320 displayPinWrite( DISPLAY_PIN_D6, dataBus & 0b00000100 ); 321 displayPinWrite( DISPLAY_PIN_D5, dataBus & 0b00000010 ); 322 displayPinWrite( DISPLAY_PIN_D4, dataBus & 0b00000001 ); 323 } 324 break; 325 326 } 327 displayPinWrite( DISPLAY_PIN_EN, ON ); 328 //HAL_Delay(1); 329 systick_delay_us(DISPLAY_DEL_01US); 330 331 displayPinWrite( DISPLAY_PIN_EN, OFF ); 332 //HAL_Delay(1); 333 systick_delay_us(DISPLAY_DEL_01US); 334 } 335 336 /********************** end of file ******************************************/ 337
