ssd1306 stm32l010

Dependents:   oled_test7_16x24_010_1 i2c_HT16K33_test1_767_3 HAL_oled_16x24_010_1

Committer:
caa45040
Date:
Tue Aug 02 06:48:13 2022 +0000
Revision:
0:5d70351255d1
Child:
1:20e48680a5f8
ssd1306 stm32l010

Who changed what in which revision?

UserRevisionLine numberNew contents of line
caa45040 0:5d70351255d1 1
caa45040 0:5d70351255d1 2 #include "mbed.h"
caa45040 0:5d70351255d1 3 #include "SSD1308.h"
caa45040 0:5d70351255d1 4
caa45040 0:5d70351255d1 5 //#include "font_8x8.h"
caa45040 0:5d70351255d1 6 #include "font_16x24.h"
caa45040 0:5d70351255d1 7
caa45040 0:5d70351255d1 8
caa45040 0:5d70351255d1 9 //i2c initialization
caa45040 0:5d70351255d1 10 //I2C i2c(I2C_SDA, I2C_SCL); //767
caa45040 0:5d70351255d1 11 I2C i2c(PA_10, PA_9); //010
caa45040 0:5d70351255d1 12
caa45040 0:5d70351255d1 13
caa45040 0:5d70351255d1 14 //void fff(char *str1)
caa45040 0:5d70351255d1 15 //{
caa45040 0:5d70351255d1 16 // printf(str1);//debug
caa45040 0:5d70351255d1 17 //}
caa45040 0:5d70351255d1 18
caa45040 0:5d70351255d1 19
caa45040 0:5d70351255d1 20
caa45040 0:5d70351255d1 21 /**
caa45040 0:5d70351255d1 22 *@brief Constructor
caa45040 0:5d70351255d1 23 *@param I2C *i2c reference to i2c
caa45040 0:5d70351255d1 24 *@param uint8_t deviceAddress slaveaddress
caa45040 0:5d70351255d1 25 */
caa45040 0:5d70351255d1 26 //SSD1308::SSD1308(I2C *i2c, uint8_t deviceAddress) : _i2c(i2c) {
caa45040 0:5d70351255d1 27 SSD1308::SSD1308( uint8_t deviceAddress) {
caa45040 0:5d70351255d1 28
caa45040 0:5d70351255d1 29
caa45040 0:5d70351255d1 30 _writeOpcode = deviceAddress & 0xFE; // low order bit = 0 for write
caa45040 0:5d70351255d1 31 _readOpcode = deviceAddress | 0x01; // low order bit = 1 for read
caa45040 0:5d70351255d1 32
caa45040 0:5d70351255d1 33 initialize();
caa45040 0:5d70351255d1 34 }//SSD1308
caa45040 0:5d70351255d1 35
caa45040 0:5d70351255d1 36 //イニシャライズ
caa45040 0:5d70351255d1 37 void SSD1308::initialize()
caa45040 0:5d70351255d1 38 {
caa45040 0:5d70351255d1 39
caa45040 0:5d70351255d1 40 //fff("initialize\r\n");//debug
caa45040 0:5d70351255d1 41
caa45040 0:5d70351255d1 42 setHorizontalAddressingMode(); //メモリーアクセスモード
caa45040 0:5d70351255d1 43
caa45040 0:5d70351255d1 44 clearDisplay();
caa45040 0:5d70351255d1 45
caa45040 0:5d70351255d1 46 //setInverted(false); //反転
caa45040 0:5d70351255d1 47
caa45040 0:5d70351255d1 48 setDisplayOn();
caa45040 0:5d70351255d1 49
caa45040 0:5d70351255d1 50 //fff("\r\n");
caa45040 0:5d70351255d1 51
caa45040 0:5d70351255d1 52 //charge pump add=>ca45040
caa45040 0:5d70351255d1 53 i2c.write(SSD1308_SA0, "\200\215\200\024", 4); //0x80,0x8d,0x80,0x14
caa45040 0:5d70351255d1 54 //set access
caa45040 0:5d70351255d1 55 //setDisplayFlip(false, false);
caa45040 0:5d70351255d1 56
caa45040 0:5d70351255d1 57
caa45040 0:5d70351255d1 58
caa45040 0:5d70351255d1 59 }//initialize
caa45040 0:5d70351255d1 60
caa45040 0:5d70351255d1 61 //クリアディスプレー
caa45040 0:5d70351255d1 62 void SSD1308::clearDisplay()
caa45040 0:5d70351255d1 63 {
caa45040 0:5d70351255d1 64
caa45040 0:5d70351255d1 65 //fff("clearDisplay\r\n");//debug
caa45040 0:5d70351255d1 66
caa45040 0:5d70351255d1 67 //setDisplayOff();
caa45040 0:5d70351255d1 68 setPageAddress(0, MAX_PAGE); // all pages
caa45040 0:5d70351255d1 69 setColumnAddress(0, MAX_COL); // all columns
caa45040 0:5d70351255d1 70
caa45040 0:5d70351255d1 71 /*
caa45040 0:5d70351255d1 72 for (uint8_t page = 0; page < PAGES; page++) {
caa45040 0:5d70351255d1 73 for (uint8_t col = 0; col < COLUMNS; col++) {
caa45040 0:5d70351255d1 74 _sendData(0x00);
caa45040 0:5d70351255d1 75 }
caa45040 0:5d70351255d1 76 }
caa45040 0:5d70351255d1 77 */
caa45040 0:5d70351255d1 78
caa45040 0:5d70351255d1 79 //128*64/8=1024
caa45040 0:5d70351255d1 80 char databytes[2] = { DATA_MODE, 0x00 };
caa45040 0:5d70351255d1 81 for (int icount = 0; icount < 1024; icount++) {
caa45040 0:5d70351255d1 82 //_sendData(0x00);
caa45040 0:5d70351255d1 83
caa45040 0:5d70351255d1 84 i2c.write(_writeOpcode, databytes, 2);
caa45040 0:5d70351255d1 85
caa45040 0:5d70351255d1 86 }
caa45040 0:5d70351255d1 87
caa45040 0:5d70351255d1 88 //setDisplayOn();
caa45040 0:5d70351255d1 89 }//clearDisplay
caa45040 0:5d70351255d1 90
caa45040 0:5d70351255d1 91 //ライトビットマップ
caa45040 0:5d70351255d1 92 void SSD1308::writeBitmap(uint8_t* data,
caa45040 0:5d70351255d1 93 uint8_t start_page, uint8_t end_page,
caa45040 0:5d70351255d1 94 uint8_t start_col, uint8_t end_col)
caa45040 0:5d70351255d1 95 {
caa45040 0:5d70351255d1 96
caa45040 0:5d70351255d1 97 //fff(" writeBitmap\r\n");//debug
caa45040 0:5d70351255d1 98
caa45040 0:5d70351255d1 99 int count = (end_page - start_page + 1) * (end_col - start_col + 1);
caa45040 0:5d70351255d1 100
caa45040 0:5d70351255d1 101 //setDisplayOff();
caa45040 0:5d70351255d1 102 setPageAddress(start_page, end_page); // set page window
caa45040 0:5d70351255d1 103 setColumnAddress(start_col, end_col); // set column window
caa45040 0:5d70351255d1 104
caa45040 0:5d70351255d1 105
caa45040 0:5d70351255d1 106
caa45040 0:5d70351255d1 107 char databytes[2] = { DATA_MODE, 0 };
caa45040 0:5d70351255d1 108 for (int i=0; i<count; i++) {
caa45040 0:5d70351255d1 109 //_sendData(data[i]); // Write Data
caa45040 0:5d70351255d1 110
caa45040 0:5d70351255d1 111 databytes[1] = data[i];
caa45040 0:5d70351255d1 112 i2c.write(_writeOpcode, databytes, 2);
caa45040 0:5d70351255d1 113
caa45040 0:5d70351255d1 114 }
caa45040 0:5d70351255d1 115
caa45040 0:5d70351255d1 116
caa45040 0:5d70351255d1 117 //setDisplayOn();
caa45040 0:5d70351255d1 118 }//writeBitmap
caa45040 0:5d70351255d1 119
caa45040 0:5d70351255d1 120
caa45040 0:5d70351255d1 121 //ライトビッグキャラ
caa45040 0:5d70351255d1 122 void SSD1308::writeBigChar(uint8_t row, uint8_t col, char chr)
caa45040 0:5d70351255d1 123 {
caa45040 0:5d70351255d1 124
caa45040 0:5d70351255d1 125 //fff("writeBigChar\r\n");//debug
caa45040 0:5d70351255d1 126
caa45040 0:5d70351255d1 127
caa45040 0:5d70351255d1 128 writeBitmap((uint8_t*) font_16x24[int(chr) - FONT16x24_START],
caa45040 0:5d70351255d1 129 row, (row + FONT16x24_BYTES - 1),
caa45040 0:5d70351255d1 130 col, (col + FONT16x24_WIDTH - 1));
caa45040 0:5d70351255d1 131
caa45040 0:5d70351255d1 132
caa45040 0:5d70351255d1 133 }//writeBigChar
caa45040 0:5d70351255d1 134
caa45040 0:5d70351255d1 135
caa45040 0:5d70351255d1 136 /*
caa45040 0:5d70351255d1 137 //センドコマンド コマンドx1
caa45040 0:5d70351255d1 138 void SSD1308::_sendCommand(uint8_t command)
caa45040 0:5d70351255d1 139 {
caa45040 0:5d70351255d1 140 char databytes[2] = { COMMAND_MODE, command };
caa45040 0:5d70351255d1 141 i2c.write(_writeOpcode,databytes, 2);
caa45040 0:5d70351255d1 142 //_i2c.write(_writeOpcode, databytes, 2); // Write command
caa45040 0:5d70351255d1 143 }//
caa45040 0:5d70351255d1 144 */
caa45040 0:5d70351255d1 145
caa45040 0:5d70351255d1 146
caa45040 0:5d70351255d1 147
caa45040 0:5d70351255d1 148 /*
caa45040 0:5d70351255d1 149 //センドコマンド コマンドx1 パラメーターx1
caa45040 0:5d70351255d1 150 void SSD1308::_sendCommand(uint8_t command, uint8_t param1)
caa45040 0:5d70351255d1 151 {
caa45040 0:5d70351255d1 152 char databytes[4] = { COMMAND_MODE, command, COMMAND_MODE, param1 };
caa45040 0:5d70351255d1 153
caa45040 0:5d70351255d1 154 i2c.write(_writeOpcode, databytes, 4);
caa45040 0:5d70351255d1 155 //_i2c.write(_writeOpcode, databytes, 4); // Write command
caa45040 0:5d70351255d1 156
caa45040 0:5d70351255d1 157 }//
caa45040 0:5d70351255d1 158 */
caa45040 0:5d70351255d1 159
caa45040 0:5d70351255d1 160 /*
caa45040 0:5d70351255d1 161 //センドコマンド コマンドx1 パラメーターx2
caa45040 0:5d70351255d1 162 void SSD1308::_sendCommand(uint8_t command, uint8_t param1, uint8_t param2)
caa45040 0:5d70351255d1 163 {
caa45040 0:5d70351255d1 164 char databytes[6] = { COMMAND_MODE, command, COMMAND_MODE, param1, COMMAND_MODE, param2 };
caa45040 0:5d70351255d1 165 i2c.write(_writeOpcode, databytes, 6);
caa45040 0:5d70351255d1 166 //_i2c.write(_writeOpcode, databytes, 6); // Write command
caa45040 0:5d70351255d1 167
caa45040 0:5d70351255d1 168 }//
caa45040 0:5d70351255d1 169 */
caa45040 0:5d70351255d1 170
caa45040 0:5d70351255d1 171 /*
caa45040 0:5d70351255d1 172 //センドコマンド コマンドx1 パラメーターx5
caa45040 0:5d70351255d1 173 void SSD1308::_sendCommand(uint8_t command, uint8_t param1, uint8_t param2,
caa45040 0:5d70351255d1 174 uint8_t param3, uint8_t param4,
caa45040 0:5d70351255d1 175 uint8_t param5)
caa45040 0:5d70351255d1 176 {
caa45040 0:5d70351255d1 177 char databytes[12] = { COMMAND_MODE, command, COMMAND_MODE, param1, COMMAND_MODE, param2,
caa45040 0:5d70351255d1 178 COMMAND_MODE, param3, COMMAND_MODE, param4, COMMAND_MODE, param5 };
caa45040 0:5d70351255d1 179 i2c.write(_writeOpcode, databytes, 12);
caa45040 0:5d70351255d1 180 //_i2c.write(_writeOpcode, databytes, 12); // Write command
caa45040 0:5d70351255d1 181 }//
caa45040 0:5d70351255d1 182 */
caa45040 0:5d70351255d1 183
caa45040 0:5d70351255d1 184 /*
caa45040 0:5d70351255d1 185 //センドコマンド コマンドx1 パラメーターx6
caa45040 0:5d70351255d1 186 void SSD1308::_sendCommand(uint8_t command, uint8_t param1, uint8_t param2,
caa45040 0:5d70351255d1 187 uint8_t param3, uint8_t param4,
caa45040 0:5d70351255d1 188 uint8_t param5, uint8_t param6)
caa45040 0:5d70351255d1 189 {
caa45040 0:5d70351255d1 190 char databytes[14] = { COMMAND_MODE, command, COMMAND_MODE, param1, COMMAND_MODE, param2,
caa45040 0:5d70351255d1 191 COMMAND_MODE, param3, COMMAND_MODE, param4, COMMAND_MODE, param5, COMMAND_MODE, param6 };
caa45040 0:5d70351255d1 192 i2c.write(_writeOpcode, databytes, 14);
caa45040 0:5d70351255d1 193 //_i2c.write(_writeOpcode, databytes, 14); // Write command
caa45040 0:5d70351255d1 194
caa45040 0:5d70351255d1 195 }//
caa45040 0:5d70351255d1 196 */
caa45040 0:5d70351255d1 197
caa45040 0:5d70351255d1 198 /*
caa45040 0:5d70351255d1 199 //センドデータ コマンドx1
caa45040 0:5d70351255d1 200 void SSD1308::_sendData(uint8_t data)
caa45040 0:5d70351255d1 201 {
caa45040 0:5d70351255d1 202 char databytes[2] = { DATA_MODE, data };
caa45040 0:5d70351255d1 203 i2c.write(_writeOpcode, databytes, 2);
caa45040 0:5d70351255d1 204 //_i2c.write(_writeOpcode, databytes, 2); // Write Data
caa45040 0:5d70351255d1 205 }//
caa45040 0:5d70351255d1 206 */
caa45040 0:5d70351255d1 207
caa45040 0:5d70351255d1 208 /*
caa45040 0:5d70351255d1 209 //センドデータ コマンドx1 データ
caa45040 0:5d70351255d1 210 void SSD1308::_sendData(uint8_t len, uint8_t* data)
caa45040 0:5d70351255d1 211 {
caa45040 0:5d70351255d1 212 for (int i=0; i<len ; i++) {
caa45040 0:5d70351255d1 213 _sendData(data[i]); // Write Data
caa45040 0:5d70351255d1 214 }
caa45040 0:5d70351255d1 215 }//_sendData
caa45040 0:5d70351255d1 216 */
caa45040 0:5d70351255d1 217
caa45040 0:5d70351255d1 218 //セット
caa45040 0:5d70351255d1 219 void SSD1308::setHorizontalAddressingMode()
caa45040 0:5d70351255d1 220 {
caa45040 0:5d70351255d1 221
caa45040 0:5d70351255d1 222 //fff("setHorizontalAddressingMode\r\n");//debug
caa45040 0:5d70351255d1 223
caa45040 0:5d70351255d1 224 setMemoryAddressingMode(HORIZONTAL_ADDRESSING_MODE);
caa45040 0:5d70351255d1 225 }//setHorizontalAddressingMode
caa45040 0:5d70351255d1 226
caa45040 0:5d70351255d1 227 //セットメモリーアドレシングモード
caa45040 0:5d70351255d1 228 void SSD1308::setMemoryAddressingMode(uint8_t mode)
caa45040 0:5d70351255d1 229 {
caa45040 0:5d70351255d1 230
caa45040 0:5d70351255d1 231 //fff("setMemoryAddressingMode\r\n");
caa45040 0:5d70351255d1 232
caa45040 0:5d70351255d1 233 //_sendCommand(SET_MEMORY_ADDRESSING_MODE, mode);
caa45040 0:5d70351255d1 234
caa45040 0:5d70351255d1 235
caa45040 0:5d70351255d1 236 char databytes[4] = { COMMAND_MODE, SET_MEMORY_ADDRESSING_MODE, COMMAND_MODE, mode };
caa45040 0:5d70351255d1 237
caa45040 0:5d70351255d1 238 i2c.write(_writeOpcode, databytes, 4);
caa45040 0:5d70351255d1 239
caa45040 0:5d70351255d1 240
caa45040 0:5d70351255d1 241
caa45040 0:5d70351255d1 242 }//setMemoryAddressingMode
caa45040 0:5d70351255d1 243
caa45040 0:5d70351255d1 244 //セットページアドレス
caa45040 0:5d70351255d1 245 void SSD1308::setPageAddress(uint8_t start, uint8_t end)
caa45040 0:5d70351255d1 246 {
caa45040 0:5d70351255d1 247
caa45040 0:5d70351255d1 248 //fff("setPageAddress\r\n");
caa45040 0:5d70351255d1 249
caa45040 0:5d70351255d1 250 //_sendCommand(SET_PAGE_ADDRESS, start, end);
caa45040 0:5d70351255d1 251
caa45040 0:5d70351255d1 252 char databytes[6] = { COMMAND_MODE, SET_PAGE_ADDRESS, COMMAND_MODE, start, COMMAND_MODE, end };
caa45040 0:5d70351255d1 253 i2c.write(_writeOpcode, databytes, 6);
caa45040 0:5d70351255d1 254
caa45040 0:5d70351255d1 255
caa45040 0:5d70351255d1 256
caa45040 0:5d70351255d1 257 }//setPageAddress
caa45040 0:5d70351255d1 258
caa45040 0:5d70351255d1 259 //セットカラムアクセス
caa45040 0:5d70351255d1 260 void SSD1308::setColumnAddress(uint8_t start, uint8_t end)
caa45040 0:5d70351255d1 261 {
caa45040 0:5d70351255d1 262
caa45040 0:5d70351255d1 263 //fff("setColumnAddress");
caa45040 0:5d70351255d1 264
caa45040 0:5d70351255d1 265 //_sendCommand(SET_COLUMN_ADDRESS, start, end);
caa45040 0:5d70351255d1 266
caa45040 0:5d70351255d1 267 char databytes[6] = { COMMAND_MODE, SET_COLUMN_ADDRESS, COMMAND_MODE, start, COMMAND_MODE, end };
caa45040 0:5d70351255d1 268 i2c.write(_writeOpcode, databytes, 6);
caa45040 0:5d70351255d1 269
caa45040 0:5d70351255d1 270
caa45040 0:5d70351255d1 271 }//setColumnAddress
caa45040 0:5d70351255d1 272
caa45040 0:5d70351255d1 273 //セットディスプレーオン
caa45040 0:5d70351255d1 274 void SSD1308::setDisplayOn()
caa45040 0:5d70351255d1 275 {
caa45040 0:5d70351255d1 276
caa45040 0:5d70351255d1 277 //fff("setDisplayOn\r\n");//debug
caa45040 0:5d70351255d1 278
caa45040 0:5d70351255d1 279 //_sendCommand(SET_DISPLAY_POWER_ON);
caa45040 0:5d70351255d1 280
caa45040 0:5d70351255d1 281
caa45040 0:5d70351255d1 282 char databytes[2] = { COMMAND_MODE, SET_DISPLAY_POWER_ON };
caa45040 0:5d70351255d1 283 i2c.write(_writeOpcode,databytes, 2);
caa45040 0:5d70351255d1 284
caa45040 0:5d70351255d1 285 }//setDisplayOn
caa45040 0:5d70351255d1 286
caa45040 0:5d70351255d1 287
caa45040 0:5d70351255d1 288 /*
caa45040 0:5d70351255d1 289 //セットディスプレーオフ
caa45040 0:5d70351255d1 290 void SSD1308::setDisplayOff()
caa45040 0:5d70351255d1 291 {
caa45040 0:5d70351255d1 292 _sendCommand(SET_DISPLAY_POWER_OFF);
caa45040 0:5d70351255d1 293 }//setDisplayOff
caa45040 0:5d70351255d1 294 */
caa45040 0:5d70351255d1 295
caa45040 0:5d70351255d1 296 /*
caa45040 0:5d70351255d1 297 //セットディスプレー Flip
caa45040 0:5d70351255d1 298 void SSD1308::setDisplayFlip(bool left, bool down)
caa45040 0:5d70351255d1 299 {
caa45040 0:5d70351255d1 300 if (left) {
caa45040 0:5d70351255d1 301 // column address 0 is mapped to SEG0 (Reset)
caa45040 0:5d70351255d1 302 _sendCommand(SET_SEGMENT_REMAP_0);
caa45040 0:5d70351255d1 303 } else {
caa45040 0:5d70351255d1 304 // column address 127 is mapped to SEG0
caa45040 0:5d70351255d1 305 _sendCommand(SET_SEGMENT_REMAP_127);
caa45040 0:5d70351255d1 306 }
caa45040 0:5d70351255d1 307
caa45040 0:5d70351255d1 308 if (down) {
caa45040 0:5d70351255d1 309 // Reset mode
caa45040 0:5d70351255d1 310 _sendCommand(SET_COMMON_REMAP_0);
caa45040 0:5d70351255d1 311 } else {
caa45040 0:5d70351255d1 312 // Flip Up/Down (Need to rewrite display before H effect shows)
caa45040 0:5d70351255d1 313 _sendCommand(SET_COMMON_REMAP_63);
caa45040 0:5d70351255d1 314 }
caa45040 0:5d70351255d1 315
caa45040 0:5d70351255d1 316 }//setDisplayFlip
caa45040 0:5d70351255d1 317 */