This commit is contained in:
matiasus 2022-12-07 20:27:09 +01:00
parent cc293bb66e
commit bbd7b1d15c
3 changed files with 66 additions and 145 deletions

View File

@ -7,7 +7,7 @@
* *
* @author Marian Hrinko * @author Marian Hrinko
* @datum 06.10.2020 * @datum 06.10.2020
* @update 24.11.2022 * @update 06.12.2022
* @file ssd1306.c * @file ssd1306.c
* @version 3.0 * @version 3.0
* @tested AVR Atmega328p * @tested AVR Atmega328p
@ -20,9 +20,8 @@
* --------------------------------------------------------------------------------------+ * --------------------------------------------------------------------------------------+
* @usage Basic Setup for OLED Display * @usage Basic Setup for OLED Display
*/ */
// @includes
#include "ssd1306.h" #include "ssd1306.h"
#include <util/delay.h>
// +---------------------------+ // +---------------------------+
// | Set MUX Ratio | // | Set MUX Ratio |
@ -358,38 +357,30 @@ uint8_t SSD1306_SetPosition (uint8_t x, uint8_t y)
if (SSD1306_SUCCESS != status) { // check status if (SSD1306_SUCCESS != status) { // check status
return status; // error return status; // error
} }
// start COLUMN status = SSD1306_Send_Command (x); // start COLUMN
status = SSD1306_Send_Command (x);
if (SSD1306_SUCCESS != status) { // check status if (SSD1306_SUCCESS != status) { // check status
return status; // error return status; // error
} }
// end COLUMN status = SSD1306_Send_Command (END_COLUMN_ADDR); // end COLUMN
status = SSD1306_Send_Command (END_COLUMN_ADDR); // 127
if (SSD1306_SUCCESS != status) { // check status if (SSD1306_SUCCESS != status) { // check status
return status; // error return status; // error
} }
// update column index _indexCol = x; // update column index
_indexCol = x;
// PAGE // PAGE
// ------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------
status = SSD1306_Send_Command (SSD1306_SET_PAGE_ADDR); // 0x22 status = SSD1306_Send_Command (SSD1306_SET_PAGE_ADDR); // 0x22
if (SSD1306_SUCCESS != status) { // check status if (SSD1306_SUCCESS != status) { // check status
return status; // error return status; // error
} }
// start PAGE status = SSD1306_Send_Command (y); // start PAGE
status = SSD1306_Send_Command (y);
if (SSD1306_SUCCESS != status) { // check status if (SSD1306_SUCCESS != status) { // check status
return status; // error return status; // error
} }
// end PAGE status = SSD1306_Send_Command (END_PAGE_ADDR); // end PAGE
status = SSD1306_Send_Command (END_PAGE_ADDR); // 7 for 128x64
if (SSD1306_SUCCESS != status) { // check status if (SSD1306_SUCCESS != status) { // check status
return status; // error return status; // error
} }
// update column index _indexPage = y; // update column index
_indexPage = y;
// TWI stop // TWI stop
// ------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------
TWI_Stop (); TWI_Stop ();
@ -478,7 +469,6 @@ uint8_t SSD1306_DrawChar (char ch)
if (SSD1306_SUCCESS != status) { // check status if (SSD1306_SUCCESS != status) { // check status
return status; // error return status; // error
} }
_indexCol = _indexCol + CHARS_COLS_LENGTH + 1; // update global col _indexCol = _indexCol + CHARS_COLS_LENGTH + 1; // update global col
// TWI stop // TWI stop
@ -548,7 +538,6 @@ uint8_t SSD1306_DrawPixel (uint8_t x, uint8_t y)
if (SSD1306_SUCCESS != status) { // check status if (SSD1306_SUCCESS != status) { // check status
return status; // error return status; // error
} }
// PAGE // PAGE
// ------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------
status = SSD1306_Send_Command (SSD1306_SET_PAGE_ADDR); // 0x22 status = SSD1306_Send_Command (SSD1306_SET_PAGE_ADDR); // 0x22
@ -565,62 +554,18 @@ uint8_t SSD1306_DrawPixel (uint8_t x, uint8_t y)
if (SSD1306_SUCCESS != status) { // check status if (SSD1306_SUCCESS != status) { // check status
return status; // error return status; // error
} }
// TWI control byte data stream // TWI control byte data stream
// ------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------
status = TWI_MT_Send_Data (SSD1306_DATA); // send data 0xC0 status = TWI_MT_Send_Data (SSD1306_DATA); // send data 0xC0
if (SSD1306_SUCCESS != status) { // check status if (SSD1306_SUCCESS != status) { // check status
return status; // error return status; // error
} }
// send clear byte to memory lcd // PIXEL
// ------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------
status = TWI_MT_Send_Data (pixel); // send pixel status = TWI_MT_Send_Data (pixel); // send pixel
if (SSD1306_SUCCESS != status) { // check status if (SSD1306_SUCCESS != status) { // check status
return status; // error return status; // error
} }
// TWI stop
// -------------------------------------------------------------------------------------
TWI_Stop ();
return SSD1306_SUCCESS; // success
}
/**
* @desc Send Same Bytes
*
* @param uint8_t
* @param uint8_t
*
* @return uint8_t
*/
uint8_t SSD1306_SendBytes (uint8_t byte, uint8_t length)
{
uint8_t status = INIT_STATUS; // TWI init status 0xFF
uint16_t i = 0; // counter
// TWI start & SLAW
// -------------------------------------------------------------------------------------
status = SSD1306_Send_StartAndSLAW (SSD1306_ADDR); // start & SLAW
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// TWI control byte data stream
// -------------------------------------------------------------------------------------
status = TWI_MT_Send_Data (SSD1306_DATA_STREAM); // send data 0x40
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// send character byte to memory lcd
// -------------------------------------------------------------------------------------
while (i < length) {
status = TWI_MT_Send_Data (byte); // send data col
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
i++; // update counter
}
// TWI stop // TWI stop
// ------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------
TWI_Stop (); TWI_Stop ();
@ -640,22 +585,32 @@ uint8_t SSD1306_SendBytes (uint8_t byte, uint8_t length)
uint8_t SSD1306_DrawLineHorz (uint8_t y, uint8_t x1, uint8_t x2) uint8_t SSD1306_DrawLineHorz (uint8_t y, uint8_t x1, uint8_t x2)
{ {
uint8_t status = INIT_STATUS; // TWI init status 0xFF uint8_t status = INIT_STATUS; // TWI init status 0xFF
uint16_t i = 0; // counter uint8_t i = 0; // counter for y dim
uint8_t j = 0; // counter uint8_t j = 0; // counter for x dim
uint8_t page = y >> 3; // page uint8_t page = y >> 3; // page
uint8_t pixel = 1 << (y - (page << 3)); // pixel uint8_t pixel = 1 << (y - (page << 3)); // which pixel
uint8_t ram[RAM_Y_END][RAM_X_END]; // buffer [3/7 x 127]
uint8_t ram[RAM_Y_END][RAM_X_END]; // fill RAM buffer
// -------------------------------------------------------------------------------------
while (i < RAM_Y_END) { while (i < RAM_Y_END)
if (i == page) { {
memset (ram[i], pixel, RAM_X_END); if (i == page) { // for correspond page
} else { //memset (ram[i], pixel, RAM_X_END); // draw line
memset (ram[i], 0x00, RAM_X_END); while (j < x1) { //
ram[i][j++] = CLEAR_COLOR; // clear to x1
}
while (j < x2) { //
ram[i][j++] = pixel; // set pixels to x2
}
while (j < RAM_X_END) { //
ram[i][j++] = CLEAR_COLOR; // clear to x end
}
} else { //
memset (ram[i], CLEAR_COLOR, RAM_X_END); // clear whole page
} }
i++; i++; // next page
} }
// TWI start & SLAW // TWI start & SLAW
// ------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------
status = SSD1306_Send_StartAndSLAW (SSD1306_ADDR); // start & SLAW status = SSD1306_Send_StartAndSLAW (SSD1306_ADDR); // start & SLAW
@ -669,16 +624,15 @@ uint8_t SSD1306_DrawLineHorz (uint8_t y, uint8_t x1, uint8_t x2)
return status; // error return status; // error
} }
// start COLUMN // start COLUMN
status = SSD1306_Send_Command (0); status = SSD1306_Send_Command (START_COLUMN_ADDR);
if (SSD1306_SUCCESS != status) { // check status if (SSD1306_SUCCESS != status) { // check status
return status; // error return status; // error
} }
// end COLUMN // end COLUMN
status = SSD1306_Send_Command (127); status = SSD1306_Send_Command (END_COLUMN_ADDR);
if (SSD1306_SUCCESS != status) { // check status if (SSD1306_SUCCESS != status) { // check status
return status; // error return status; // error
} }
// PAGE // PAGE
// ------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------
status = SSD1306_Send_Command (SSD1306_SET_PAGE_ADDR); // 0x22 status = SSD1306_Send_Command (SSD1306_SET_PAGE_ADDR); // 0x22
@ -686,62 +640,43 @@ uint8_t SSD1306_DrawLineHorz (uint8_t y, uint8_t x1, uint8_t x2)
return status; // error return status; // error
} }
// start PAGE // start PAGE
status = SSD1306_Send_Command (0); status = SSD1306_Send_Command (START_PAGE_ADDR);
if (SSD1306_SUCCESS != status) { // check status if (SSD1306_SUCCESS != status) { // check status
return status; // error return status; // error
} }
// end PAGE // end PAGE
status = SSD1306_Send_Command (3); status = SSD1306_Send_Command (END_PAGE_ADDR);
if (SSD1306_SUCCESS != status) { // check status if (SSD1306_SUCCESS != status) { // check status
return status; // error return status; // error
} }
// TWI control byte data stream // TWI control byte data stream
// ------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------
status = TWI_MT_Send_Data (SSD1306_DATA_STREAM); // send data 0x40 status = TWI_MT_Send_Data (SSD1306_DATA_STREAM); // send data 0x40
if (SSD1306_SUCCESS != status) { // check status if (SSD1306_SUCCESS != status) { // check status
return status; // error return status; // error
} }
// PIXELS
// -------------------------------------------------------------------------------------
i = 0; i = 0;
j = 0; j = 0;
// send pixels
// -------------------------------------------------------------------------------------
while (i < RAM_Y_END) { while (i < RAM_Y_END) {
while (j < RAM_X_END) { while (j < RAM_X_END) {
status = TWI_MT_Send_Data (ram[i][j]); // send data col status = TWI_MT_Send_Data (ram[i][j]); // send RAM buffer
if (SSD1306_SUCCESS != status) { // check status if (SSD1306_SUCCESS != status) { // check status
return status; // error return status; // error
} }
j++; j++; // next column
} }
j = 0; j = 0; // null x counter
i++; i++; // next page
} }
// TWI stop // TWI stop
// ------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------
TWI_Stop (); TWI_Stop ();
return SSD1306_SUCCESS; // success return return SSD1306_SUCCESS; // success return
} }
/**
* @desc Draw line vertical
*
* @param uint8_t
* @param uint8_t
* @param uint8_t
*
* @return uint8_t
*/
uint8_t SSD1306_DrawLineVert (uint8_t x, uint8_t y1, uint8_t y2)
{
return SSD1306_SUCCESS; // success
}
/** /**
* @desc Draw line by Bresenham algoritm * @desc Draw line by Bresenham algoritm
* *

View File

@ -1,22 +1,23 @@
/** /**
* ---------------------------------------------------------------+ * -------------------------------------------------------------------------------------+
* @desc SSD1306 OLED Driver * @desc SSD1306 OLED Driver
* ---------------------------------------------------------------+ * -------------------------------------------------------------------------------------+
* Copyright (C) 2020 Marian Hrinko. * Copyright (C) 2020 Marian Hrinko.
* Written by Marian Hrinko (mato.hrinko@gmail.com) * Written by Marian Hrinko (mato.hrinko@gmail.com)
* *
* @author Marian Hrinko * @author Marian Hrinko
* @datum 06.10.2020 * @datum 06.10.2020
* @update 19.07.2021 * @update 21.11.2022
* @file ssd1306.h * @file ssd1306.h
* @version 2.0 * @version 3.0
* @tested AVR Atmega328 * @tested AVR Atmega328p
* *
* @depend font.h, twi.h * @depend font.h, twi.h
* ---------------------------------------------------------------+ * -------------------------------------------------------------------------------------+
* @descr Version 1.0 -> applicable for 1 display * @descr Version 1.0 -> applicable for 1 display
* Version 2.0 -> rebuild to 'cacheMemLcd' array * Version 2.0 -> rebuild to 'cacheMemLcd' array
* ---------------------------------------------------------------+ * Version 3.0 -> remove 'cacheMemLcd' approach
* -------------------------------------------------------------------------------------+
* @usage Basic Setup for OLED Display * @usage Basic Setup for OLED Display
*/ */
@ -28,20 +29,17 @@
#include "font.h" #include "font.h"
#include "twi.h" #include "twi.h"
// Success // Success / Error
// ------------------------------------------- // ------------------------------------------------------------------------------------
#define SSD1306_SUCCESS 0 #define SSD1306_SUCCESS 0
// Error
// -------------------------------------------
#define SSD1306_ERROR 1 #define SSD1306_ERROR 1
// Address definition // Address definition
// ----------------------------------- // ------------------------------------------------------------------------------------
#define SSD1306_ADDR 0x3C #define SSD1306_ADDR 0x3C
// Command definition // Command definition
// ----------------------------------- // ------------------------------------------------------------------------------------
#define SSD1306_COMMAND 0x80 // Continuation bit=1, D/C=0; 1000 0000 #define SSD1306_COMMAND 0x80 // Continuation bit=1, D/C=0; 1000 0000
#define SSD1306_COMMAND_STREAM 0x00 // Continuation bit=0, D/C=0; 0000 0000 #define SSD1306_COMMAND_STREAM 0x00 // Continuation bit=0, D/C=0; 0000 0000
#define SSD1306_DATA 0xC0 // Continuation bit=1, D/C=1; 1100 0000 #define SSD1306_DATA 0xC0 // Continuation bit=1, D/C=1; 1100 0000
@ -73,18 +71,17 @@
#define SSD1306_VCOM_DESELECT 0xDB #define SSD1306_VCOM_DESELECT 0xDB
// Clear Color // Clear Color
// ----------------------------------- // ------------------------------------------------------------------------------------
#define CLEAR_COLOR 0x00 #define CLEAR_COLOR 0x00
// Init Status // Init Status
// ----------------------------------- // ------------------------------------------------------------------------------------
#define INIT_STATUS 0xFF #define INIT_STATUS 0xFF
// AREA definition // AREA definition
// ----------------------------------- // ------------------------------------------------------------------------------------
#define START_PAGE_ADDR 0 #define START_PAGE_ADDR 0
#define END_PAGE_ADDR 3 // 3 for 128 x 32 version #define END_PAGE_ADDR 3 // 7 for 128x64, 3 for 128x32 version
//#define END_PAGE_ADDR 7 // 7 for 128 x 64 version
#define START_COLUMN_ADDR 0 #define START_COLUMN_ADDR 0
#define END_COLUMN_ADDR 127 #define END_COLUMN_ADDR 127
#define RAM_X_END END_COLUMN_ADDR + 1 #define RAM_X_END END_COLUMN_ADDR + 1
@ -95,9 +92,6 @@
#define MAX_X END_COLUMN_ADDR #define MAX_X END_COLUMN_ADDR
#define MAX_Y (END_PAGE_ADDR + 1) * 8 #define MAX_Y (END_PAGE_ADDR + 1) * 8
// @var set area
unsigned int _counter;
/** /**
* @desc SSD1306 Init * @desc SSD1306 Init
* *
@ -199,16 +193,6 @@
*/ */
uint8_t SSD1306_DrawPixel (uint8_t, uint8_t); uint8_t SSD1306_DrawPixel (uint8_t, uint8_t);
/**
* @desc Send Same Bytes
*
* @param uint8_t
* @param uint8_t
*
* @return uint8_t
*/
uint8_t SSD1306_SendBytes (uint8_t, uint8_t);
/** /**
* @desc Draw line * @desc Draw line
* *

10
main.c
View File

@ -38,8 +38,6 @@ int main(void)
SSD1306_Init (); SSD1306_Init ();
// clear screen // clear screen
SSD1306_ClearScreen (); SSD1306_ClearScreen ();
// draw string
// SSD1306_DrawString ("SSD1306 MATIASUS Copyright (C) 2020 Marian Hrinko. Copyright (C) 2020 Marian Hrinko. Copyright (C) 2020 Marian Hrinko.");
/* /*
while (x < MAX_Y) { while (x < MAX_Y) {
SSD1306_DrawLine (x,x,0,y++); SSD1306_DrawLine (x,x,0,y++);
@ -57,11 +55,15 @@ int main(void)
//SSD1306_DrawLineHorz (10,0,MAX_X); //SSD1306_DrawLineHorz (10,0,MAX_X);
while (i < MAX_Y) { while (i < MAX_Y) {
SSD1306_DrawLine (0,MAX_X,i,i); SSD1306_DrawLine (0,MAX_X-10,i,i);
_delay_ms (100); //_delay_ms (100);
i++; i++;
} }
SSD1306_SetPosition (0, 0) ;
// draw string
SSD1306_DrawString ("SSD1306 MATIASUS Copyright (C) 2020 Marian Hrinko. Copyright (C) 2020 Marian Hrinko. Copyright (C) 2020 Marian Hrinko.");
// return value // return value
return 0; return 0;
} }