Oled Display Driver, tested to work with EA W082-XLG SPI (HD44780) and WEH001602CRPP5N00001-WSR (WS0010). It can work with other SPI Oled Displays. It should work with 20x4 aswell but has not been tested. Should work for: EA W082-XLG EA W162-X3LW EA W162-X3LG EA W162-XLG EA W162-X9LG EA W162-XBLW EA W162-XBLG EA W202-XLG EA W202-XDLG EA W204-XLG
.
Display.h@0:a52630c11009, 2017-08-24 (annotated)
- Committer:
- swe_skytem
- Date:
- Thu Aug 24 07:12:39 2017 +0000
- Revision:
- 0:a52630c11009
Oled Display Driver, tested to work with EA W082-XLG SPI. It can work with other SPI Oled Displays.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
swe_skytem | 0:a52630c11009 | 1 | |
swe_skytem | 0:a52630c11009 | 2 | //V1.1 Changes: |
swe_skytem | 0:a52630c11009 | 3 | //Added 2ms delay for clear display function |
swe_skytem | 0:a52630c11009 | 4 | //Added Return home in clear display function |
swe_skytem | 0:a52630c11009 | 5 | |
swe_skytem | 0:a52630c11009 | 6 | |
swe_skytem | 0:a52630c11009 | 7 | //INFO |
swe_skytem | 0:a52630c11009 | 8 | |
swe_skytem | 0:a52630c11009 | 9 | //Made for MBED LPC1768 |
swe_skytem | 0:a52630c11009 | 10 | //Made for EA W082-XLG In SPI mode (8x2 Oled display) |
swe_skytem | 0:a52630c11009 | 11 | //Also works with other Oled displays |
swe_skytem | 0:a52630c11009 | 12 | |
swe_skytem | 0:a52630c11009 | 13 | //Default Ports: (For LPC1768) |
swe_skytem | 0:a52630c11009 | 14 | //p5 = mosi |
swe_skytem | 0:a52630c11009 | 15 | //p6 = miso |
swe_skytem | 0:a52630c11009 | 16 | //p7 = sck |
swe_skytem | 0:a52630c11009 | 17 | //p8 = cs |
swe_skytem | 0:a52630c11009 | 18 | |
swe_skytem | 0:a52630c11009 | 19 | // 8x2 display cursor location |
swe_skytem | 0:a52630c11009 | 20 | // |---------------------------------------| |
swe_skytem | 0:a52630c11009 | 21 | // | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
swe_skytem | 0:a52630c11009 | 22 | // |---------------------------------------| |
swe_skytem | 0:a52630c11009 | 23 | // | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | |
swe_skytem | 0:a52630c11009 | 24 | // |---------------------------------------| |
swe_skytem | 0:a52630c11009 | 25 | |
swe_skytem | 0:a52630c11009 | 26 | //IF Degree Symbol is desired, write DEG instead of the symbol, it will display the ° symbol. Example: DispCharacter(DEG); //Writes ° to display |
swe_skytem | 0:a52630c11009 | 27 | |
swe_skytem | 0:a52630c11009 | 28 | |
swe_skytem | 0:a52630c11009 | 29 | //FUNCTIONS: |
swe_skytem | 0:a52630c11009 | 30 | |
swe_skytem | 0:a52630c11009 | 31 | //OpenDisplay(); //Configures the display, should be run at startup in main routine, Clears display and returns to (1,1) |
swe_skytem | 0:a52630c11009 | 32 | //DispLocate(X,Y); //For choosing where the cursor is located. X is the line position (1-8), Y is the line select (1 for first line, 2 is for second line) |
swe_skytem | 0:a52630c11009 | 33 | //DispCharacter('X'); //For writing a single Character to the display |
swe_skytem | 0:a52630c11009 | 34 | //DispLine("HELLO"); //For writing a string to the display, (max 8 bit) |
swe_skytem | 0:a52630c11009 | 35 | |
swe_skytem | 0:a52630c11009 | 36 | //ClearDisp(); //For Clearing Display, also returns the cursor to home (1,1) |
swe_skytem | 0:a52630c11009 | 37 | //DispReturn(); //For Returning the cursor to home (1,1) |
swe_skytem | 0:a52630c11009 | 38 | //ShiftRight(); //Shifts the cursor one to the right |
swe_skytem | 0:a52630c11009 | 39 | //ShiftLeft(); //Shifts the cursor one to the left |
swe_skytem | 0:a52630c11009 | 40 | |
swe_skytem | 0:a52630c11009 | 41 | |
swe_skytem | 0:a52630c11009 | 42 | |
swe_skytem | 0:a52630c11009 | 43 | |
swe_skytem | 0:a52630c11009 | 44 | //Define Pins: |
swe_skytem | 0:a52630c11009 | 45 | #define mosi p5 |
swe_skytem | 0:a52630c11009 | 46 | #define miso p6 |
swe_skytem | 0:a52630c11009 | 47 | #define sck p7 |
swe_skytem | 0:a52630c11009 | 48 | #define DEG 0xD2 //IF Degree Symbol is desired, write DEG instead of the symbol, it will display the ° symbol. Example: DispCharacter(DEG); //Writes ° to display |
swe_skytem | 0:a52630c11009 | 49 | |
swe_skytem | 0:a52630c11009 | 50 | DigitalOut cs(p8); //Chip select |
swe_skytem | 0:a52630c11009 | 51 | |
swe_skytem | 0:a52630c11009 | 52 | //Define SPI connections: |
swe_skytem | 0:a52630c11009 | 53 | SPI spi(mosi, miso, sck); // mosi, miso, sck |
swe_skytem | 0:a52630c11009 | 54 | |
swe_skytem | 0:a52630c11009 | 55 | |
swe_skytem | 0:a52630c11009 | 56 | //For Use in Main Routine: |
swe_skytem | 0:a52630c11009 | 57 | void DispLocate(char Char1, char Char2); //For deciding location of cursor, for use in main routine |
swe_skytem | 0:a52630c11009 | 58 | void ClearDisp(void); //Clears the display, also returns the cursor to home (1,1) |
swe_skytem | 0:a52630c11009 | 59 | void DispReturn(void); //returns cursor to home (1,1) |
swe_skytem | 0:a52630c11009 | 60 | void ShiftRight(void); //Shifts cursor right |
swe_skytem | 0:a52630c11009 | 61 | void ShiftLeft(void); //Shifts cursor left |
swe_skytem | 0:a52630c11009 | 62 | void DispCharacter(void); //For displaying a single character. Example: DispCharacter('A'); //Wrtites A on display |
swe_skytem | 0:a52630c11009 | 63 | void DispLine(void); //For Writing Line to display. Example: DispLine("Hello"); //Writes Hello on the display |
swe_skytem | 0:a52630c11009 | 64 | |
swe_skytem | 0:a52630c11009 | 65 | |
swe_skytem | 0:a52630c11009 | 66 | //Prototypes: |
swe_skytem | 0:a52630c11009 | 67 | void DispConfig(char Char); //For sending command to configure the display, used in the OpenDisplay routine |
swe_skytem | 0:a52630c11009 | 68 | void Delay2us(void); //2us delay, used as a small delay for setup purposes |
swe_skytem | 0:a52630c11009 | 69 | |
swe_skytem | 0:a52630c11009 | 70 | //Init Oled Display: |
swe_skytem | 0:a52630c11009 | 71 | void OpenDisplay(){ |
swe_skytem | 0:a52630c11009 | 72 | |
swe_skytem | 0:a52630c11009 | 73 | //DigitalOut cs(p8); //Chip select |
swe_skytem | 0:a52630c11009 | 74 | cs = 1; //high at idle |
swe_skytem | 0:a52630c11009 | 75 | |
swe_skytem | 0:a52630c11009 | 76 | spi.format(10,3); //10bit, high steady state clock |
swe_skytem | 0:a52630c11009 | 77 | spi.frequency(1000000); //1MHz spi clock |
swe_skytem | 0:a52630c11009 | 78 | |
swe_skytem | 0:a52630c11009 | 79 | //Display init routine |
swe_skytem | 0:a52630c11009 | 80 | DispConfig(0x39); //function set european chararacter set |
swe_skytem | 0:a52630c11009 | 81 | DispConfig(0x08); //display off |
swe_skytem | 0:a52630c11009 | 82 | DispConfig(0x06); //entry mode set increment cursor by 1 not shifting display |
swe_skytem | 0:a52630c11009 | 83 | DispConfig(0x17); //Character mode and internel power on |
swe_skytem | 0:a52630c11009 | 84 | DispConfig(0x01); //clear display |
swe_skytem | 0:a52630c11009 | 85 | DispConfig(0x02); //return home |
swe_skytem | 0:a52630c11009 | 86 | DispConfig(0x0C); //display on |
swe_skytem | 0:a52630c11009 | 87 | |
swe_skytem | 0:a52630c11009 | 88 | wait_ms(10); //Time to stabilize (wont work without) |
swe_skytem | 0:a52630c11009 | 89 | } |
swe_skytem | 0:a52630c11009 | 90 | |
swe_skytem | 0:a52630c11009 | 91 | |
swe_skytem | 0:a52630c11009 | 92 | //Send single line to display |
swe_skytem | 0:a52630c11009 | 93 | void DispLine(char textstr[]){ |
swe_skytem | 0:a52630c11009 | 94 | |
swe_skytem | 0:a52630c11009 | 95 | char len = strlen(textstr); |
swe_skytem | 0:a52630c11009 | 96 | for(char i = 0; i < len; i++){ |
swe_skytem | 0:a52630c11009 | 97 | |
swe_skytem | 0:a52630c11009 | 98 | cs = 0; //chip select to start data transmission |
swe_skytem | 0:a52630c11009 | 99 | |
swe_skytem | 0:a52630c11009 | 100 | spi.write(0x200 | textstr[i]); //add 0x02 for character transmission |
swe_skytem | 0:a52630c11009 | 101 | |
swe_skytem | 0:a52630c11009 | 102 | cs = 1; //chip select to end data transmission |
swe_skytem | 0:a52630c11009 | 103 | |
swe_skytem | 0:a52630c11009 | 104 | Delay2us(); //small delay for cs to stabilize |
swe_skytem | 0:a52630c11009 | 105 | } |
swe_skytem | 0:a52630c11009 | 106 | |
swe_skytem | 0:a52630c11009 | 107 | } |
swe_skytem | 0:a52630c11009 | 108 | |
swe_skytem | 0:a52630c11009 | 109 | |
swe_skytem | 0:a52630c11009 | 110 | void DispCharacter(char instruction){ |
swe_skytem | 0:a52630c11009 | 111 | |
swe_skytem | 0:a52630c11009 | 112 | cs = 0; //chip select to start data transmission |
swe_skytem | 0:a52630c11009 | 113 | |
swe_skytem | 0:a52630c11009 | 114 | spi.write(0x200 | instruction); //add 0x02 for character transmission |
swe_skytem | 0:a52630c11009 | 115 | |
swe_skytem | 0:a52630c11009 | 116 | cs = 1; //chip select to end data transmission |
swe_skytem | 0:a52630c11009 | 117 | |
swe_skytem | 0:a52630c11009 | 118 | Delay2us(); //small delay for cs to stabilize |
swe_skytem | 0:a52630c11009 | 119 | |
swe_skytem | 0:a52630c11009 | 120 | } |
swe_skytem | 0:a52630c11009 | 121 | |
swe_skytem | 0:a52630c11009 | 122 | |
swe_skytem | 0:a52630c11009 | 123 | //Send single character to display |
swe_skytem | 0:a52630c11009 | 124 | void DispConfig(char instruction){ |
swe_skytem | 0:a52630c11009 | 125 | |
swe_skytem | 0:a52630c11009 | 126 | cs = 0; //chip select to start data transmission |
swe_skytem | 0:a52630c11009 | 127 | |
swe_skytem | 0:a52630c11009 | 128 | spi.write(instruction); |
swe_skytem | 0:a52630c11009 | 129 | |
swe_skytem | 0:a52630c11009 | 130 | cs = 1; //chip select to end data transmission |
swe_skytem | 0:a52630c11009 | 131 | |
swe_skytem | 0:a52630c11009 | 132 | Delay2us(); //small delay for cs to stabilize |
swe_skytem | 0:a52630c11009 | 133 | } |
swe_skytem | 0:a52630c11009 | 134 | |
swe_skytem | 0:a52630c11009 | 135 | |
swe_skytem | 0:a52630c11009 | 136 | //Locate Cursor |
swe_skytem | 0:a52630c11009 | 137 | void DispLocate(char X, char Y){ |
swe_skytem | 0:a52630c11009 | 138 | |
swe_skytem | 0:a52630c11009 | 139 | DispReturn(); //Return the cursor to home |
swe_skytem | 0:a52630c11009 | 140 | |
swe_skytem | 0:a52630c11009 | 141 | char tempX; |
swe_skytem | 0:a52630c11009 | 142 | tempX = X; |
swe_skytem | 0:a52630c11009 | 143 | char tempY; |
swe_skytem | 0:a52630c11009 | 144 | tempY = Y; |
swe_skytem | 0:a52630c11009 | 145 | |
swe_skytem | 0:a52630c11009 | 146 | //Shift to next line (64 times to the right) |
swe_skytem | 0:a52630c11009 | 147 | if(tempY > 1){ |
swe_skytem | 0:a52630c11009 | 148 | for(int i = 0; i < 64; i++){ |
swe_skytem | 0:a52630c11009 | 149 | ShiftRight(); |
swe_skytem | 0:a52630c11009 | 150 | } |
swe_skytem | 0:a52630c11009 | 151 | } |
swe_skytem | 0:a52630c11009 | 152 | |
swe_skytem | 0:a52630c11009 | 153 | //Decides how many times the cursor is shifted |
swe_skytem | 0:a52630c11009 | 154 | while(tempX > 1){ |
swe_skytem | 0:a52630c11009 | 155 | tempX--; |
swe_skytem | 0:a52630c11009 | 156 | ShiftRight(); |
swe_skytem | 0:a52630c11009 | 157 | } |
swe_skytem | 0:a52630c11009 | 158 | } |
swe_skytem | 0:a52630c11009 | 159 | |
swe_skytem | 0:a52630c11009 | 160 | |
swe_skytem | 0:a52630c11009 | 161 | //Toggle Chipselect |
swe_skytem | 0:a52630c11009 | 162 | void Chipselect(){ |
swe_skytem | 0:a52630c11009 | 163 | |
swe_skytem | 0:a52630c11009 | 164 | cs = !cs; |
swe_skytem | 0:a52630c11009 | 165 | } |
swe_skytem | 0:a52630c11009 | 166 | |
swe_skytem | 0:a52630c11009 | 167 | |
swe_skytem | 0:a52630c11009 | 168 | void Delay2us(){ |
swe_skytem | 0:a52630c11009 | 169 | |
swe_skytem | 0:a52630c11009 | 170 | wait_us(2); //small delay for cs to stabilize |
swe_skytem | 0:a52630c11009 | 171 | } |
swe_skytem | 0:a52630c11009 | 172 | |
swe_skytem | 0:a52630c11009 | 173 | |
swe_skytem | 0:a52630c11009 | 174 | void ClearDisp(){ |
swe_skytem | 0:a52630c11009 | 175 | |
swe_skytem | 0:a52630c11009 | 176 | DispConfig(0x01); //Clear Display, Sets DDRAM-address 0 into adresscounter |
swe_skytem | 0:a52630c11009 | 177 | wait_ms(2); //wait a little, wont work without |
swe_skytem | 0:a52630c11009 | 178 | DispReturn(); //Returns cursor home |
swe_skytem | 0:a52630c11009 | 179 | |
swe_skytem | 0:a52630c11009 | 180 | } |
swe_skytem | 0:a52630c11009 | 181 | |
swe_skytem | 0:a52630c11009 | 182 | |
swe_skytem | 0:a52630c11009 | 183 | void DispReturn(){ |
swe_skytem | 0:a52630c11009 | 184 | |
swe_skytem | 0:a52630c11009 | 185 | DispConfig(0x02); //Returns the cursor to home |
swe_skytem | 0:a52630c11009 | 186 | } |
swe_skytem | 0:a52630c11009 | 187 | |
swe_skytem | 0:a52630c11009 | 188 | |
swe_skytem | 0:a52630c11009 | 189 | void ShiftRight(){ |
swe_skytem | 0:a52630c11009 | 190 | |
swe_skytem | 0:a52630c11009 | 191 | DispConfig(0x14); //Shift cursor right |
swe_skytem | 0:a52630c11009 | 192 | Delay2us(); |
swe_skytem | 0:a52630c11009 | 193 | } |
swe_skytem | 0:a52630c11009 | 194 | |
swe_skytem | 0:a52630c11009 | 195 | |
swe_skytem | 0:a52630c11009 | 196 | void ShiftLeft(){ |
swe_skytem | 0:a52630c11009 | 197 | |
swe_skytem | 0:a52630c11009 | 198 | DispConfig(0x10); //Shift cursor left |
swe_skytem | 0:a52630c11009 | 199 | Delay2us(); |
swe_skytem | 0:a52630c11009 | 200 | } |