diff --git a/ssd1306.c b/ssd1306.c --- a/ssd1306.c +++ b/ssd1306.c @@ -20,7 +20,7 @@ #define SSD1306_COLUMN_MARGIN_START 2 #define SSD1306_COLUMN_MARGIN_END 2 #define SSD1306_X_PIXEL 128 -#define SSD1306_Y_PIXEL 64 +#define SSD1306_Y_PIXEL 32 /* Private macro -------------------------------------------------------------*/ #define ssd1306_Buffer (_SSD1306_Buffer + SSD1306_COLUMN_MARGIN_START) @@ -86,82 +86,116 @@ void ssd1306_Init(void) uint32_t i; for(i=5000; i>1; i--) SSD_Reset_High(); + + + WriteCommand(0xAE); + WriteCommand(0xD5); + WriteCommand(0x80); + WriteCommand(0xA8); + WriteCommand(0x1F); + WriteCommand(0xD3); + WriteCommand(0x00); + WriteCommand(0x40 | 0x00); // line #0 + WriteCommand(0x8D); + WriteCommand(0x14); //10 or 14 if not externalvcc + WriteCommand(0x20); + WriteCommand(0x00); + WriteCommand(0xA0 | 0x1); // segremap + WriteCommand(0xC8); // comscandep + WriteCommand(0xDA); // setcompins + WriteCommand(0x02); + WriteCommand(0x81); // contrast + WriteCommand(0x0F); // contrast value. 8f is a good one. + WriteCommand(0xD9); + WriteCommand(0xF1); //22 or F1 if not externalvcc + WriteCommand(0xDB); + WriteCommand(0x40); + WriteCommand(0xA4); // dispalyallon_resume + WriteCommand(0xA6); // normaldisplay + + WriteCommand(0xAF); // display on + + + // Clear buffer +// for(i=0;i<900;i++) +// ssd1306_Buffer[i] = 0; /************************************************* // ssd1306 Initialization Command *************************************************/ + /* // Lower Column Address - WriteCommand(0x00); /* Set Lower Column Address */ + WriteCommand(0x00); // Set Lower Column Address GPIO_SetBits(GPIOA,GPIO_Pin_15);//turn on status LED // High Column Address - WriteCommand(0x10); /* Set Higher Column Address*/ + WriteCommand(0x10); // Set Higher Column Address // Display Start Line - WriteCommand(0x40); /* Set Display Start Line */ + WriteCommand(0x40); // Set Display Start Line #ifdef DEBUG_BOARD curContrast = lastContrast = 0x30; #else curContrast = lastContrast = 0xCF; #endif // Contrast Control Register - WriteCommand(0x81); /* Set Contrast Control */ - WriteCommand(lastContrast); /* 0 ~ 255 0x1f*/ + WriteCommand(0x81); // Set Contrast Control + WriteCommand(lastContrast); // 0 ~ 255 0x1f // Re-map - WriteCommand(0xA1); /* [A0]:column address 0 is map - to SEG0 , [A1]: columnaddress 131 is map to SEG0*/ + WriteCommand(0xA1); // [A0]:column address 0 is map to SEG0 , [A1]: columnaddress 131 is map to SEG0 // Entire Display ON/OFF - WriteCommand(0xA4); /* A4=ON */ + WriteCommand(0xA4); // A4=ON // Normal or Inverse Display - WriteCommand(0XA6); /* Normal Display*/ + WriteCommand(0XA6); // Normal Display // Multiplex Ratio - WriteCommand(0xA8); /* Set Multiplex Ratio */ - WriteCommand(0x3f); /* Set to 36 Mux*/ + WriteCommand(0xA8); // Set Multiplex Ratio + WriteCommand(0x3f); // Set to 36 Mux // Set DC-DC - WriteCommand(0xAD); /* Set DC-DC */ - WriteCommand(0x8B); /* 8B=ON, 8A=Off */ + WriteCommand(0xAD); // Set DC-DC + WriteCommand(0x8B); // 8B=ON, 8A=Off // Display ON/OFF - WriteCommand(0xAE); /* AF=ON , AE=OFF*/ + WriteCommand(0xAE); // AF=ON , AE=OFF // Display Offset - WriteCommand(0xD3); /* Set Display Offset */ - WriteCommand(0x00); /* No offset */ + WriteCommand(0xD3); // Set Display Offset + WriteCommand(0x00); // No offset // Display Clock Divide - WriteCommand(0xD5); /* Set Clock Divide */ - WriteCommand(0x20); /* Set to 80Hz */ + WriteCommand(0xD5); // Set Clock Divide + WriteCommand(0x20); // Set to 80Hz // Area Color Mode - WriteCommand(0xD8); /* Set Area Color On or Off*/ - WriteCommand(0x00); /* Mono Mode */ + WriteCommand(0xD8); // Set Area Color On or Off + WriteCommand(0x00); // Mono Mode // COM Pins Hardware Configuration - WriteCommand(0xDA); /* Set Pins HardwareConfiguration */ + WriteCommand(0xDA); // Set Pins HardwareConfiguration WriteCommand(0x12); // VCOMH - WriteCommand(0xDB); /* Set VCOMH */ + WriteCommand(0xDB); // Set VCOMH WriteCommand(0x00); // VP - WriteCommand(0xD9); /* Set VP */ - WriteCommand(0x22); /* P1=2 , P2=2 */ + WriteCommand(0xD9); // Set VP + WriteCommand(0x22); // P1=2 , P2=2 // Set Common output scan direction - WriteCommand(0xc8);/* Set COM scan direction */ + WriteCommand(0xc8);// Set COM scan direction // For SSD1306 Set the address mode - WriteCommand(0x20);/* Set address mode */ - WriteCommand(0x00);/* Set address mode horizontal */ + WriteCommand(0x20);// Set address mode + WriteCommand(0x00);// Set address mode horizontal // Set the page start address WriteCommand(0xb0); WriteCommand(0x00); WriteCommand(0x10); - /* Turn on the controller */ + // Turn on the controller pre_on = ssd1306_ON(); // // Set Charge pump -// WriteCommand(0x8D); /* Set Charge pump */ -// WriteCommand(0x14); /* 0x14=ON, 0x10=Off */ +// WriteCommand(0x8D); // Set Charge pump +// WriteCommand(0x14); // 0x14=ON, 0x10=Off // // // Turn on the display // WriteCommand(0xaf); +*/ } void ssd1306_block_write(void) @@ -180,8 +214,14 @@ void ssd1306_block_write(void) uint32_t i = 0; - for(i=0;i<1024;i++) + + + + for(i=0;i<512;i++) WriteData(ssd1306_Buffer[i]); +// for(i=0;i<512;i++) + // * * * * +// WriteData(0b00000011); } @@ -555,3 +595,61 @@ const DeviceProp ssd1306_Prop = .yPixel = 64, }; + + + + + + + + +/* +void ssd1306_DrawChar(uint8_t x, uint8_t y, uint8_t c, struct FONT_DEF font) +{ + uint8_t col, column[font.u8Width]; + + // Check if the requested character is available + if ((c >= font.u8FirstChar) && (c <= font.u8LastChar)) + { + // Retrieve appropriate columns from font data + for (col = 0; col < font.u8Width; col++) + { + column[col] = font.au8FontTable[((c - 32) * font.u8Width) + col]; // Get first column of appropriate character + } + } + else + { + // Requested character is not available in this font ... send a space instead + for (col = 0; col < font.u8Width; col++) + { + column[col] = 0xFF; // Send solid space + } + } + + // Render each column + uint16_t xoffset, yoffset; + for (xoffset = 0; xoffset < font.u8Width; xoffset++) + { + for (yoffset = 0; yoffset < (font.u8Height + 1); yoffset++) + { + uint8_t bit = 0x00; + bit = (column[xoffset] << (8 - (yoffset + 1))); // Shift current row bit left + bit = (bit >> 7); // Shift current row but right (results in 0x01 for black, and 0x00 for white) + if (bit) + { + ssd1306_DrawPixel(x + xoffset, y + yoffset, 1); + } + } + } +} + +ssd1306_DrawString(uint8_t x, uint8_t y, const char *text, struct FONT_DEF font) +{ + uint8_t l; + for (l = 0; l < strlen(text); l++) + { + ssd1306DrawChar(x + (l * (font.u8Width + 1)), y, text[l], font); + } +} + +*/