Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 #define DISPLAY_RESET D0 00004 00005 #define SSD1306_LCDWIDTH 128 00006 #define SSD1306_LCDHEIGHT 64 00007 #define SSD1306_SETCONTRAST 0x81 00008 #define SSD1306_DISPLAYALLON_RESUME 0xA4 00009 #define SSD1306_DISPLAYALLON 0xA5 00010 #define SSD1306_NORMALDISPLAY 0xA6 00011 #define SSD1306_INVERTDISPLAY 0xA7 00012 #define SSD1306_DISPLAYOFF 0xAE 00013 #define SSD1306_DISPLAYON 0xAF 00014 #define SSD1306_SETDISPLAYOFFSET 0xD3 00015 #define SSD1306_SETCOMPINS 0xDA 00016 #define SSD1306_SETVCOMDETECT 0xDB 00017 #define SSD1306_SETDISPLAYCLOCKDIV 0xD5 00018 #define SSD1306_SETPRECHARGE 0xD9 00019 #define SSD1306_SETMULTIPLEX 0xA8 00020 #define SSD1306_SETLOWCOLUMN 0x00 00021 #define SSD1306_SETHIGHCOLUMN 0x10 00022 #define SSD1306_SETSTARTLINE 0x40 00023 #define SSD1306_MEMORYMODE 0x20 00024 #define SSD1306_COLUMNADDR 0x21 00025 #define SSD1306_PAGEADDR 0x22 00026 #define SSD1306_COMSCANINC 0xC0 00027 #define SSD1306_COMSCANDEC 0xC8 00028 #define SSD1306_SEGREMAP 0xA0 00029 #define SSD1306_CHARGEPUMP 0x8D 00030 #define SSD1306_EXTERNALVCC 0x1 00031 #define SSD1306_SWITCHCAPVCC 0x2 00032 00033 I2C i2c(I2C_SDA, I2C_SCL); 00034 00035 char _i2c_address; 00036 char display_buffer[1024]; 00037 00038 void ssd1306_command(char c){ 00039 char control = 0x00; 00040 i2c.start(); 00041 i2c.write(_i2c_address); 00042 i2c.write(control); 00043 i2c.write(c); 00044 i2c.stop(); 00045 } 00046 00047 void ssd1306_data(char c){ 00048 i2c.start(); 00049 i2c.write(_i2c_address); 00050 i2c.write(0x40); 00051 i2c.write(c); 00052 i2c.stop(); 00053 } 00054 00055 void setColAddress() 00056 { 00057 ssd1306_command(SSD1306_COLUMNADDR); // 0x21 COMMAND 00058 ssd1306_command(0); // Column start address 00059 ssd1306_command(SSD1306_LCDWIDTH-1); // Column end address 00060 } 00061 00062 void setPageAddress() 00063 { 00064 ssd1306_command(SSD1306_PAGEADDR); // 0x22 COMMAND 00065 ssd1306_command(0); // Start Page address 00066 ssd1306_command((SSD1306_LCDHEIGHT/8)-1);// End Page address 00067 } 00068 00069 void TransferBuffer() 00070 { 00071 int j=0; 00072 00073 // set the Column and Page addresses to 0,0 00074 setColAddress(); 00075 setPageAddress(); 00076 00077 i2c.start(); 00078 i2c.write(_i2c_address); 00079 i2c.write(0X40); // data not command 00080 for(j=0;j<1024;j++) 00081 { 00082 i2c.write(display_buffer[j]); 00083 } 00084 00085 i2c.stop(); 00086 } 00087 00088 void InitializeDisplay() 00089 { 00090 DigitalOut(DISPLAY_RESET,1); 00091 // VDD (3.3V) goes high at start, lets just chill for a ms 00092 wait_ms(1); 00093 // bring reset low 00094 DigitalOut(DISPLAY_RESET,0); 00095 // wait 10ms 00096 wait_ms(10); 00097 // bring out of reset 00098 DigitalOut(DISPLAY_RESET,1); 00099 // turn on VCC (9V?) 00100 00101 // Init sequence for 128x64 OLED module 00102 ssd1306_command(SSD1306_DISPLAYOFF); // 0xAE 00103 00104 ssd1306_command(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5 00105 ssd1306_command(0x80); // the suggested ratio 0x80 00106 00107 ssd1306_command(SSD1306_SETMULTIPLEX); // 0xA8 00108 ssd1306_command(0x3F); 00109 00110 ssd1306_command(SSD1306_SETDISPLAYOFFSET); // 0xD3 00111 ssd1306_command(0x0); // no offset 00112 00113 ssd1306_command(SSD1306_SETSTARTLINE);// | 0x0); // line #0 00114 00115 ssd1306_command(SSD1306_CHARGEPUMP); // 0x8D 00116 ssd1306_command(0x14); // using internal VCC 00117 00118 ssd1306_command(SSD1306_MEMORYMODE); // 0x20 00119 ssd1306_command(0x00); // 0x00 horizontal addressing 00120 00121 ssd1306_command(SSD1306_SEGREMAP | 0x1); // rotate screen 180 00122 00123 ssd1306_command(SSD1306_COMSCANDEC); // rotate screen 180 00124 00125 ssd1306_command(SSD1306_SETCOMPINS); // 0xDA 00126 ssd1306_command(0x12); 00127 00128 ssd1306_command(SSD1306_SETCONTRAST); // 0x81 00129 ssd1306_command(0xCF); 00130 00131 ssd1306_command(SSD1306_SETPRECHARGE); // 0xd9 00132 ssd1306_command(0xF1); 00133 00134 ssd1306_command(SSD1306_SETVCOMDETECT); // 0xDB 00135 ssd1306_command(0x40); 00136 00137 ssd1306_command(SSD1306_DISPLAYALLON_RESUME); // 0xA4 00138 00139 ssd1306_command(SSD1306_NORMALDISPLAY); // 0xA6 00140 00141 ssd1306_command(SSD1306_DISPLAYON); //switch on OLED 00142 } 00143 00144 int main() { 00145 00146 // fill buffer with something for test 00147 memset( display_buffer, 0X02, 1024); // tried other values 00148 00149 _i2c_address = 0x78; 00150 00151 InitializeDisplay(); 00152 00153 TransferBuffer(); // try sending buffer 00154 00155 while(1) 00156 { 00157 wait_ms(1000); // keyboard code here 00158 if(DigitalIn(LED1) == 0){DigitalOut(LED1,1);} 00159 else{DigitalOut(LED1,0);} 00160 } 00161 }
Generated on Mon Jul 25 2022 18:15:10 by
1.7.2