1 #include "display.h" 2 #include "main.h" 3 #include <stdbool.h> 4 5 // Demo includes 6 #include "logger.h" 7 #include "dwt.h" 8 #include "systick.h" 9 10 // Functional states 11 #ifndef OFF 12 #define OFF 0 13 #endif 14 #ifndef ON 15 #define ON ( !OFF ) 16 #endif 17 18 // Electrical states 19 #ifndef LOW 20 #define LOW 0 21 #endif 22 #ifndef HIGH 23 #define HIGH ( !LOW ) 24 #endif 25 26 #define DISPLAY_IR_CLEAR_DISPLAY 0b00000001 27 #define DISPLAY_IR_ENTRY_MODE_SET 0b00000100 28 #define DISPLAY_IR_DISPLAY_CONTROL 0b00001000 29 #define DISPLAY_IR_FUNCTION_SET 0b00100000 30 #define DISPLAY_IR_SET_DDRAM_ADDR 0b10000000 31 32 #define DISPLAY_IR_ENTRY_MODE_SET_INCREMENT 0b00000010 33 #define DISPLAY_IR_ENTRY_MODE_SET_DECREMENT 0b00000000 34 #define DISPLAY_IR_ENTRY_MODE_SET_SHIFT 0b00000001 35 #define DISPLAY_IR_ENTRY_MODE_SET_NO_SHIFT 0b00000000 36 37 #define DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_ON 0b00000100 38 #define DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_OFF 0b00000000 39 #define DISPLAY_IR_DISPLAY_CONTROL_CURSOR_ON 0b00000010 40 #define DISPLAY_IR_DISPLAY_CONTROL_CURSOR_OFF 0b00000000 41 #define DISPLAY_IR_DISPLAY_CONTROL_BLINK_ON 0b00000001 42 #define DISPLAY_IR_DISPLAY_CONTROL_BLINK_OFF 0b00000000 43 44 #define DISPLAY_IR_FUNCTION_SET_8BITS 0b00010000 45 #define DISPLAY_IR_FUNCTION_SET_4BITS 0b00000000 46 #define DISPLAY_IR_FUNCTION_SET_2LINES 0b00001000 47 #define DISPLAY_IR_FUNCTION_SET_1LINE 0b00000000 48 #define DISPLAY_IR_FUNCTION_SET_5x10DOTS 0b00000100 49 #define DISPLAY_IR_FUNCTION_SET_5x8DOTS 0b00000000 50 51 #define DISPLAY_20x4_LINE1_FIRST_CHARACTER_ADDRESS 0 52 #define DISPLAY_20x4_LINE2_FIRST_CHARACTER_ADDRESS 64 53 #define DISPLAY_20x4_LINE3_FIRST_CHARACTER_ADDRESS 20 54 #define DISPLAY_20x4_LINE4_FIRST_CHARACTER_ADDRESS 84 55 56 #define DISPLAY_RS_INSTRUCTION 0 57 #define DISPLAY_RS_DATA 1 58 59 #define DISPLAY_RW_WRITE 0 60 #define DISPLAY_RW_READ 1 61 62 #define DISPLAY_PIN_RS 8 63 #define DISPLAY_PIN_EN 9 64 #define DISPLAY_PIN_D4 4 65 #define DISPLAY_PIN_D5 5 66 #define DISPLAY_PIN_D6 6 67 #define DISPLAY_PIN_D7 7 68 69 #define DISPLAY_DEL_37US 37ul 70 #define DISPLAY_DEL_01US 01ul 71 72 // declaration of private functions 73 static void display_pin_write(uint8_t pin_name, int value); 74 static void display_data_bus_write(uint8_t data_byte); 75 static void display_code_write(bool type, uint8_t data_bus); 76 77 // public functions 78 79 void display_init(void) 80 { 81 display_code_write(DISPLAY_RS_INSTRUCTION, 82 DISPLAY_IR_FUNCTION_SET | 83 DISPLAY_IR_FUNCTION_SET_4BITS | 84 DISPLAY_IR_FUNCTION_SET_2LINES | 85 DISPLAY_IR_FUNCTION_SET_5x8DOTS); 86 87 display_code_write(DISPLAY_RS_INSTRUCTION, 88 DISPLAY_IR_CLEAR_DISPLAY); 89 90 display_code_write(DISPLAY_RS_INSTRUCTION, 91 DISPLAY_IR_ENTRY_MODE_SET | 92 DISPLAY_IR_ENTRY_MODE_SET_INCREMENT | 93 DISPLAY_IR_ENTRY_MODE_SET_NO_SHIFT); 94 95 display_code_write(DISPLAY_RS_INSTRUCTION, 96 DISPLAY_IR_DISPLAY_CONTROL | 97 DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_ON | 98 DISPLAY_IR_DISPLAY_CONTROL_CURSOR_ON | 99 DISPLAY_IR_DISPLAY_CONTROL_BLINK_ON); 100 101 } 102 103 void display_char_position_write(uint8_t x, uint8_t y) 104 { 105 switch(x) 106 { 107 case 0: 108 display_code_write(DISPLAY_RS_INSTRUCTION, 109 DISPLAY_IR_SET_DDRAM_ADDR | 110 (DISPLAY_20x4_LINE1_FIRST_CHARACTER_ADDRESS + x)); 111 break; 112 113 case 1: 114 display_code_write(DISPLAY_RS_INSTRUCTION, 115 DISPLAY_IR_SET_DDRAM_ADDR | 116 (DISPLAY_20x4_LINE2_FIRST_CHARACTER_ADDRESS + x)); 117 break; 118 119 case 2: 120 display_code_write(DISPLAY_RS_INSTRUCTION, 121 DISPLAY_IR_SET_DDRAM_ADDR | 122 (DISPLAY_20x4_LINE3_FIRST_CHARACTER_ADDRESS + x)); 123 break; 124 125 case 3: 126 display_code_write(DISPLAY_RS_INSTRUCTION, 127 DISPLAY_IR_SET_DDRAM_ADDR | 128 (DISPLAY_20x4_LINE4_FIRST_CHARACTER_ADDRESS + x)); 129 break; 130 default: break; 131 } 132 } 133 134 void display_string_write(const char * str) 135 { 136 while (*str) { 137 display_code_write(DISPLAY_RS_DATA, *str++); 138 } 139 } 140 141 void display_cursor_off(void) 142 { 143 display_code_write(DISPLAY_RS_INSTRUCTION, 144 DISPLAY_IR_DISPLAY_CONTROL | 145 DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_ON | 146 DISPLAY_IR_DISPLAY_CONTROL_CURSOR_OFF | 147 DISPLAY_IR_DISPLAY_CONTROL_BLINK_OFF); 148 } 149 150 void display_cursor_on(void) 151 { 152 display_code_write(DISPLAY_RS_INSTRUCTION, 153 DISPLAY_IR_DISPLAY_CONTROL | 154 DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_ON | 155 DISPLAY_IR_DISPLAY_CONTROL_CURSOR_ON | 156 DISPLAY_IR_DISPLAY_CONTROL_BLINK_ON); 157 } 158 159 // private functions 160 161 static void display_code_write( bool type, uint8_t data_bus ) 162 { 163 if (type == DISPLAY_RS_INSTRUCTION) { 164 display_pin_write( DISPLAY_PIN_RS, DISPLAY_RS_INSTRUCTION ); 165 } 166 else { 167 display_pin_write(DISPLAY_PIN_RS, DISPLAY_RS_DATA); 168 } 169 170 display_data_bus_write(data_bus); 171 } 172 173 static void display_pin_write(uint8_t pin_name, int value) 174 { 175 switch(pin_name) 176 { 177 case DISPLAY_PIN_D4: 178 HAL_GPIO_WritePin(D4_GPIO_Port, D4_Pin, value); 179 break; 180 case DISPLAY_PIN_D5: 181 HAL_GPIO_WritePin(D5_GPIO_Port, D5_Pin, value); 182 break; 183 case DISPLAY_PIN_D6: 184 HAL_GPIO_WritePin(D6_GPIO_Port, D6_Pin, value); 185 break; 186 case DISPLAY_PIN_D7: 187 HAL_GPIO_WritePin(D7_GPIO_Port, D7_Pin, value); 188 break; 189 case DISPLAY_PIN_RS: 190 HAL_GPIO_WritePin(D8_GPIO_Port, D8_Pin, value); 191 break; 192 case DISPLAY_PIN_EN: 193 HAL_GPIO_WritePin(D9_GPIO_Port, D9_Pin, value); 194 break; 195 default: break; 196 } 197 } 198 199 static void display_data_bus_write( uint8_t data_bus ) 200 { 201 display_pin_write( DISPLAY_PIN_EN, OFF ); 202 display_pin_write( DISPLAY_PIN_D7, data_bus & 0b10000000 ); 203 display_pin_write( DISPLAY_PIN_D6, data_bus & 0b01000000 ); 204 display_pin_write( DISPLAY_PIN_D5, data_bus & 0b00100000 ); 205 display_pin_write( DISPLAY_PIN_D4, data_bus & 0b00010000 ); 206 207 display_pin_write( DISPLAY_PIN_EN, ON ); 208 display_pin_write( DISPLAY_PIN_EN, OFF ); 209 display_pin_write( DISPLAY_PIN_D7, data_bus & 0b00001000 ); 210 display_pin_write( DISPLAY_PIN_D6, data_bus & 0b00000100 ); 211 display_pin_write( DISPLAY_PIN_D5, data_bus & 0b00000010 ); 212 display_pin_write( DISPLAY_PIN_D4, data_bus & 0b00000001 ); 213 214 display_pin_write( DISPLAY_PIN_EN, ON ); 215 display_pin_write( DISPLAY_PIN_EN, OFF ); 216 HAL_Delay(1); 217 }
