Fix confilicts

This commit is contained in:
matiasus 2022-11-22 20:51:50 +01:00
commit 68ce61bfcc
9 changed files with 347 additions and 547 deletions

View File

@ -4,7 +4,14 @@
Detailed information are described in [Datasheet SSD1306](https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf).
## Library
C library is aimed for driving [SSD1306 0.96" OLED display](#demonstration) 128x64 through TWI's Atmega16.
C library is aimed for driving [0.96" OLED display with SSD1306 driver](#demonstration) 128x64 through TWI's Atmega328p.
### Versions
- 1.0 - basic functions. The first publication.
- 2.0 - more changes:
- rebuild to cacheMemLcd array. It means that every request is stored in cache array and then is depicted on the display by function [SSD1306_UpdateScreen (uint8_t)](#ssd1306_updatescreen).
- Added new function -> [SSD1306_DrawLine (uint8_t, uint8_t, uint8_t, uint8_t)](#ssd1306_drawline). Possible depicted any line (horizontal, vertical, with slope).
- Possible to use for more than 1 display (not tested, cause displays that I have avialable had the same addresses).
## Dependencies
- [font.c](https://github.com/Matiasus/SSD1306/blob/readme-edits/lib/font.c)
@ -15,15 +22,15 @@ C library is aimed for driving [SSD1306 0.96" OLED display](#demonstration) 128x
Font.c can be modified according to application requirements with form defined in font.c. Maximal permissible horizontal dimension is 8 bits.
### Usage
Prior defined for MCU Atmega16 / Atmega8. Need to be carefull with TWI ports definition.
Prior defined for MCU Atmega328p / Atmega8 / Atmega16. Need to be carefull with TWI ports definition.
| PORT | [Atmega16](http://ww1.microchip.com/downloads/en/devicedoc/doc2466.pdf) | [Atmega8](https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2486-8-bit-AVR-microcontroller-ATmega8_L_datasheet.pdf) |
| PORT | [Atmega16](http://ww1.microchip.com/downloads/en/devicedoc/doc2466.pdf) | [Atmega8](https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2486-8-bit-AVR-microcontroller-ATmega8_L_datasheet.pdf) / [Atmega328](https://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061B.pdf) |
| :---: | :---: | :---: |
| SCL | PC0 | PC5 |
| SDA | PC1 | PC4 |
### Tested
Library was tested and proved on a **_SSD1306 0.96″ OLED Dispay_** with **_Atmega16_**.
Library was tested and proved on **_0.96″ OLED Display with SSD1306 driver_** and **_Atmega328p_**.
## Init OLED Sequence
Init sequence OLED display was defined according to page 64 (next to last page) of [Datasheet SSD1306](https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf).
@ -105,17 +112,19 @@ Init sequence OLED display was defined according to page 64 (next to last page)
// +---------------------------+
```
## Functions
- [SSD1306_Init (void)](#ssd1306_init) - Init display
- [SSD1306_ClearScreen (char)](#ssd1306_clearscreen) - Clear screen
- [SSD1306_InverseScreen (void)](#ssd1306_inversescreen) - Inverse screen
- [SSD1306_SetPosition (char, char)](#ssd1306_setposition) - Set position
- [SSD1306_Init (uint8_t)](#ssd1306_init) - Init display
- [SSD1306_ClearScreen (void)](#ssd1306_clearscreen) - Clear screen
- [SSD1306_NormalScreen (uint8_t)](#ssd1306_normalscreen) - Normal screen
- [SSD1306_InverseScreen (uint8_t)](#ssd1306_inversescreen) - Inverse screen
- [SSD1306_SetPosition (uint8_t, uint8_t)](#ssd1306_setposition) - Set position
- [SSD1306_DrawChar (char)](#ssd1306_drawchar) - Draw specific character
- [SSD1306_DrawString (char*)](#ssd1306_drawstring) - Draw specific string
- [SSD1306_UpdateScreen (void)](#ssd1306_updatescreen) - Display on
- [SSD1306_DrawLineHorizontal (char, char, char)](#ssd1306_drawlinehorizontal) - Draw horizontal line
- [SSD1306_UpdateScreen (uint8_t)](#ssd1306_updatescreen) - Update content on display
- [SSD1306_DrawLine (uint8_t, uint8_t, uint8_t, uint8_t)](#ssd1306_drawline) - Draw line
## Demonstration
<img src="img/demonstration_ssd1306.jpg" />
<img src="img/ssd1306_v20.png" />
## Links
- [Datasheet SSD1306](https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf)
- [Atmega328](https://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061B.pdf)

BIN
img/ssd1306_v20.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

View File

@ -8,7 +8,7 @@
* @author Marian Hrinko
* @datum 07.10.2020
* @file font.c
* @tested AVR Atmega16
* @tested AVR Atmega16, Atmega8, Atmega328
*
* @depend
* ---------------------------------------------------------------+
@ -16,7 +16,7 @@
#include "font.h"
/** @array Charset */
const uint8_t FONTS[][5] PROGMEM = {
const uint8_t FONTS[][CHARS_COLS_LENGTH] PROGMEM = {
{ 0x00, 0x00, 0x00, 0x00, 0x00 }, // 20 space
{ 0x81, 0x81, 0x18, 0x81, 0x81 }, // 21 !
{ 0x00, 0x07, 0x00, 0x07, 0x00 }, // 22 "

View File

@ -8,7 +8,7 @@
* @author Marian Hrinko
* @datum 07.10.2020
* @file font.h
* @tested AVR Atmega16
* @tested AVR Atmega16, Atmega8, Atmega328
*
* @depend
* ---------------------------------------------------------------+

View File

@ -1,25 +1,26 @@
/**
* ---------------------------------------------------------------+
/**
* ---------------------------------------------------------------+
* @desc SSD1306 OLED Driver
* ---------------------------------------------------------------+
* ---------------------------------------------------------------+
* Copyright (C) 2020 Marian Hrinko.
* Written by Marian Hrinko (mato.hrinko@gmail.com)
*
* @author Marian Hrinko
* @datum 06.10.2020
* @update 19.07.2021
* @update 21.11.2022
* @file ssd1306.c
* @version 2.0
* @version 3.0
* @tested AVR Atmega328
*
* @depend font.h, twi.h
* ---------------------------------------------------------------+
* @descr Version 1.0 -> applicable for 1 display
* Version 2.0 -> rebuild to 'cacheMemLcd' array
* Version 3.0 -> remove 'cacheMemLcd' approach
* ---------------------------------------------------------------+
* @usage Basic Setup for OLED Display
*/
#include "ssd1306.h"
// +---------------------------+
@ -97,56 +98,30 @@
// | 0xAF |
// +---------------------------+
/** @array Init command */
// @array Init command
const uint8_t INIT_SSD1306[] PROGMEM = {
// number of initializers
18,
// 0xAE = Set Display OFF
0, SSD1306_DISPLAY_OFF,
// 0xA8
1, SSD1306_SET_MUX_RATIO, 0x3F,
// 0x20 = Set Memory Addressing Mode
// -----------------------------------
// 0x00 - Horizontal Addressing Mode
// 0x01 - Vertical Addressing Mode
// 0x02 - Page Addressing Mode (RESET)
1, SSD1306_MEMORY_ADDR_MODE, 0x00,
// 0x21 = Set Column Address
// -----------------------------------
// 0x00 - Start Column Address
// 0xFF - End Column Address
2, SSD1306_SET_COLUMN_ADDR, START_COLUMN_ADDR, END_COLUMN_ADDR,
// 0x22 = Set Page Address
// -----------------------------------
// 0x00 - Start Column Address
// 0x07 - End Column Address
2, SSD1306_SET_PAGE_ADDR, START_PAGE_ADDR, END_PAGE_ADDR,
// 0x40
0, SSD1306_SET_START_LINE,
// 0xD3
1, SSD1306_DISPLAY_OFFSET, 0x00,
// 0xA0 / remap 0xA1
0, SSD1306_SEG_REMAP_OP,
// 0xC0 / remap 0xC8
0, SSD1306_COM_SCAN_DIR_OP,
// 0xDA
1, SSD1306_COM_PIN_CONF, 0x12,
// 0x81
1, SSD1306_SET_CONTRAST, 0x7F,
// 0xA4
0, SSD1306_DIS_ENT_DISP_ON,
// 0xA6
0, SSD1306_DIS_NORMAL,
// 0xD5
1, SSD1306_SET_OSC_FREQ, 0x80,
// 0xD9, 1st Period = higher value less blinking
1, SSD1306_SET_PRECHARGE, 0xc2,
// Set V COMH Deselect, reset value 0x22 = 0,77xUcc
1, SSD1306_VCOM_DESELECT, 0x20,
// 0x8D
1, SSD1306_SET_CHAR_REG, 0x14,
// 0xAF = Set Display ON
0, SSD1306_DISPLAY_ON
18, // number of initializers
0, SSD1306_DISPLAY_OFF, // 0xAE = Set Display OFF
1, SSD1306_SET_MUX_RATIO, 0x3F, // 0xA8
1, SSD1306_MEMORY_ADDR_MODE, 0x00, // 0x20 = Set Memory Addressing Mode
// 0x00 - Horizontal Addressing Mode
// 0x01 - Vertical Addressing Mode
// 0x02 - Page Addressing Mode (RESET)
2, SSD1306_SET_COLUMN_ADDR, START_COLUMN_ADDR, END_COLUMN_ADDR, // 0x21 = Set Column Address
2, SSD1306_SET_PAGE_ADDR, START_PAGE_ADDR, END_PAGE_ADDR, // 0x22 = Set Page Address
0, SSD1306_SET_START_LINE, // 0x40
1, SSD1306_DISPLAY_OFFSET, 0x00, // 0xD3
0, SSD1306_SEG_REMAP_OP, // 0xA0 / remap 0xA1
0, SSD1306_COM_SCAN_DIR_OP, // 0xC0 / remap 0xC8
1, SSD1306_COM_PIN_CONF, 0x12, // 0xDA
1, SSD1306_SET_CONTRAST, 0x7F, // 0x81
0, SSD1306_DIS_ENT_DISP_ON, // 0xA4
0, SSD1306_DIS_NORMAL, // 0xA6
1, SSD1306_SET_OSC_FREQ, 0x80, // 0xD5
1, SSD1306_SET_PRECHARGE, 0xc2, // 0xD9, 1st Period = higher value less blinking
1, SSD1306_VCOM_DESELECT, 0x20, // Set V COMH Deselect, reset value 0x22 = 0,77xUcc
1, SSD1306_SET_CHAR_REG, 0x14, // 0x8D
0, SSD1306_DISPLAY_ON // 0xAF = Set Display ON
};
// @var array Chache memory Lcd 8 * 128 = 1024
@ -160,69 +135,50 @@ static char cacheMemLcd[CACHE_SIZE_MEM];
* @return uint8_t
*/
uint8_t SSD1306_Init (uint8_t address)
{
// variables
const uint8_t *commands = INIT_SSD1306;
// number of commands
unsigned short int no_of_commands = pgm_read_byte(commands++);
// argument
uint8_t no_of_arguments;
// command
uint8_t command;
// init status
uint8_t status = INIT_STATUS;
{
const uint8_t *commands = INIT_SSD1306; // variables
unsigned short int no_of_commands = pgm_read_byte(commands++); // number of commands
uint8_t no_of_arguments; // number od arguments
uint8_t command; // command
uint8_t status = INIT_STATUS; // TWI init status 0xFF
// TWI: Init
// -------------------------
// TWI INIT
// -------------------------------------------------------------------------------------
TWI_Init ();
// TWI: start & SLAW
// -------------------------
status = SSD1306_Send_StartAndSLAW (address);
// request - start TWI
if (SSD1306_SUCCESS != status) {
// error
return status;
// TWI Start & SLAW
// -------------------------------------------------------------------------------------
status = SSD1306_Send_StartAndSLAW (address); // start & SLAW
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// loop through commands
while (no_of_commands) {
// number of arguments
no_of_arguments = pgm_read_byte (commands++);
// command
command = pgm_read_byte (commands++);
// send command
// -------------------------
status = SSD1306_Send_Command (command);
// request - start TWI
if (SSD1306_SUCCESS != status) {
// error
return status;
// MAIN LOOP
// -------------------------------------------------------------------------------------
while (no_of_commands) { // commands loop
no_of_arguments = pgm_read_byte (commands++); // number of arguments
command = pgm_read_byte (commands++); // command
// SEND COMMAND
// -----------------------------------------------------------------------------------
status = SSD1306_Send_Command (command); // send command
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// send arguments
// -------------------------
// SEND ARGUMENTS
// -----------------------------------------------------------------------------------
while (no_of_arguments--) {
// send command
status = SSD1306_Send_Command (pgm_read_byte(commands++));
// request - start TWI
if (SSD1306_SUCCESS != status) {
// error
return status;
status = SSD1306_Send_Command (pgm_read_byte(commands++)); // send argument
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
}
// decrement
no_of_commands--;
}
// TWI: Stop
// -------------------------
no_of_commands--; // next command
}
// TWI STOP
// -------------------------------------------------------------------------------------
TWI_Stop ();
// success
return SSD1306_SUCCESS;
return SSD1306_SUCCESS; // success
}
/**
@ -234,29 +190,22 @@ uint8_t SSD1306_Init (uint8_t address)
*/
uint8_t SSD1306_Send_StartAndSLAW (uint8_t address)
{
// init status
uint8_t status = INIT_STATUS;
uint8_t status = INIT_STATUS; // TWI init status 0xFF
// TWI: start
// -------------------------
status = TWI_MT_Start ();
// request - start TWI
if (SSD1306_SUCCESS != status) {
// error
return status;
// TWI START
// -------------------------------------------------------------------------------------
status = TWI_MT_Start (); // start
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// TWI: send SLAW
// -------------------------
status = TWI_MT_Send_SLAW (address);
// request - send SLAW
if (SSD1306_SUCCESS != status) {
// error
return status;
// TWI start & SLAW
// -------------------------------------------------------------------------------------
status = TWI_MT_Send_SLAW (address); // start & SLAW
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// success
return SSD1306_SUCCESS;
return SSD1306_SUCCESS; // success
}
/**
@ -268,29 +217,22 @@ uint8_t SSD1306_Send_StartAndSLAW (uint8_t address)
*/
uint8_t SSD1306_Send_Command (uint8_t command)
{
// init status
uint8_t status = INIT_STATUS;
uint8_t status = INIT_STATUS; // TWI init status 0xFF
// send control byte
// -------------------------
status = TWI_MT_Send_Data (SSD1306_COMMAND);
// request - start TWI
if (SSD1306_SUCCESS != status) {
// error
return status;
// TWI send control byte
// -------------------------------------------------------------------------------------
status = TWI_MT_Send_Data (SSD1306_COMMAND); // send control byte
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// TWI send command
// -------------------------------------------------------------------------------------
status = TWI_MT_Send_Data (command); // send command
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// send command
// -------------------------
status = TWI_MT_Send_Data (command);
// request - start TWI
if (SSD1306_SUCCESS != status) {
// error
return status;
}
// success
return SSD1306_SUCCESS;
return SSD1306_SUCCESS; // success
}
/**
@ -302,29 +244,22 @@ uint8_t SSD1306_Send_Command (uint8_t command)
*/
uint8_t SSD1306_NormalScreen (uint8_t address)
{
// init status
uint8_t status = INIT_STATUS;
uint8_t status = INIT_STATUS; // TWI init status 0xFF
// TWI: start & SLAW
// -------------------------
status = SSD1306_Send_StartAndSLAW (address);
// request succesfull
if (SSD1306_SUCCESS != status) {
// error
return status;
// TWI start & SLAW
// -------------------------------------------------------------------------------------
status = SSD1306_Send_StartAndSLAW (address); // start & SLAW
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// TWI send command
// -------------------------------------------------------------------------------------
status = SSD1306_Send_Command (SSD1306_DIS_NORMAL); // command 0xA6
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// send command
// -------------------------
status = SSD1306_Send_Command (SSD1306_DIS_NORMAL);
// request succesfull
if (SSD1306_SUCCESS != status) {
// error
return status;
}
// success
return SSD1306_SUCCESS;
return SSD1306_SUCCESS; // success
}
/**
@ -336,29 +271,22 @@ uint8_t SSD1306_NormalScreen (uint8_t address)
*/
uint8_t SSD1306_InverseScreen (uint8_t address)
{
// init status
uint8_t status = INIT_STATUS;
// TWI: start & SLAW
// -------------------------
status = SSD1306_Send_StartAndSLAW (address);
// request succesfull
if (SSD1306_SUCCESS != status) {
// error
return status;
// TWI start & SLAW
// -------------------------------------------------------------------------------------
status = SSD1306_Send_StartAndSLAW (address); // start & SLAW
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// TWI send command
// -------------------------------------------------------------------------------------
status = SSD1306_Send_Command (SSD1306_DIS_INVERSE); // command 0xA7
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// send command
// -------------------------
status = SSD1306_Send_Command (SSD1306_DIS_INVERSE);
// request succesfull
if (SSD1306_SUCCESS != status) {
// error
return status;
}
// success
return SSD1306_SUCCESS;
return SSD1306_SUCCESS; // success
}
/**
@ -370,48 +298,35 @@ uint8_t SSD1306_InverseScreen (uint8_t address)
*/
uint8_t SSD1306_UpdateScreen (uint8_t address)
{
// init status
uint8_t status = INIT_STATUS;
// init i
uint16_t i = 0;
// TWI: start & SLAW
// -------------------------
status = SSD1306_Send_StartAndSLAW (address);
// request succesfull
if (SSD1306_SUCCESS != status) {
// error
return status;
uint8_t status = INIT_STATUS; // TWI init status 0xFF
uint16_t i = 0; // counter
// TWI start & SLAW
// -------------------------------------------------------------------------------------
status = SSD1306_Send_StartAndSLAW (address); // start & SLAW
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// control byte data stream
// -------------------------
status = TWI_MT_Send_Data (SSD1306_DATA_STREAM);
// request succesfull
if (SSD1306_SUCCESS != status) {
// error
return status;
// 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 cache memory lcd
// -------------------------
// TWI send cache memory lcd
// -------------------------------------------------------------------------------------
while (i < CACHE_SIZE_MEM) {
// send data
status = TWI_MT_Send_Data (cacheMemLcd[i]);
// request succesfull
if (SSD1306_SUCCESS != status) {
// error
return status;
status = TWI_MT_Send_Data (cacheMemLcd[i]); // send data
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// increment
i++;
i++; // next value
}
// stop TWI
// TWI stop
// -------------------------------------------------------------------------------------
TWI_Stop ();
// success
return SSD1306_SUCCESS;
return SSD1306_SUCCESS; // success
}
/**
@ -423,55 +338,45 @@ uint8_t SSD1306_UpdateScreen (uint8_t address)
*/
void SSD1306_ClearScreen (void)
{
// null cache memory lcd
memset (cacheMemLcd, 0x00, CACHE_SIZE_MEM);
memset (cacheMemLcd, 0x00, CACHE_SIZE_MEM); // null cache memory lcd
}
/**
* @desc SSD1306 Set position
*
* @param uint8_t column -> 0 ... 127
* @param uint8_t column -> 0 ... 127
* @param uint8_t page -> 0 ... 7
*
* @return void
*/
void SSD1306_SetPosition (uint8_t x, uint8_t y)
void SSD1306_SetPosition (uint8_t x, uint8_t y)
{
// calculate counter
_counter = x + (y << 7);
_counter = x + (y << 7); // calculate counter
}
/**
* @desc SSD1306 Update text poisition - this ensure that character will not be divided at the end of row, the whole character will be depicted on the new row
* @desc SSD1306 Update text poisition - this ensure that character will not be
* divided at the end of row, the whole character will be depicted on the new row
*
* @param void
*
* @return uint8_t
*/
uint8_t SSD1306_UpdTxtPosition (void)
uint8_t SSD1306_UpdTxtPosition (void)
{
// y / 8
uint8_t y = _counter >> 7;
// y % 8
uint8_t x = _counter - (y << 7);
// x + character length + 1
uint8_t x_new = x + CHARS_COLS_LENGTH + 1;
// check position
if (x_new > END_COLUMN_ADDR) {
// if more than allowable number of pages
if (y > END_PAGE_ADDR) {
// return out of range
return SSD1306_ERROR;
// if x reach the end but page in range
} else if (y < (END_PAGE_ADDR-1)) {
// update
_counter = ((++y) << 7);
uint8_t y = _counter >> 7; // y / 8
uint8_t x = _counter - (y << 7); // y % 8
uint8_t x_new = x + CHARS_COLS_LENGTH + 1; // x + character length + 1
if (x_new > END_COLUMN_ADDR) { // check position
if (y > END_PAGE_ADDR) { // if more than allowable number of pages
return SSD1306_ERROR; // return out of range
} else if (y < (END_PAGE_ADDR-1)) { // if x reach the end but page in range
_counter = ((++y) << 7); // update
}
}
// success
return SSD1306_SUCCESS;
return SSD1306_SUCCESS; // success
}
/**
@ -481,29 +386,21 @@ uint8_t SSD1306_UpdTxtPosition (void)
*
* @return uint8_t
*/
uint8_t SSD1306_DrawChar (char character)
uint8_t SSD1306_DrawChar (char ch)
{
// variables
uint8_t i = 0;
// update text position
// this ensure that character will not be divided at the end of row, the whole character will be depicted on the new row
if (SSD1306_UpdTxtPosition () == SSD1306_ERROR) {
// error
return SSD1306_ERROR;
return SSD1306_ERROR; // error
}
// loop through 5 bits
while (i < CHARS_COLS_LENGTH) {
// read byte
cacheMemLcd[_counter++] = pgm_read_byte(&FONTS[character-32][i++]);
while (i < CHARS_COLS_LENGTH) { // loop through 5 bits
cacheMemLcd[_counter++] = pgm_read_byte(&FONTS[ch-32][i++]); // read byte
}
// update position
_counter++;
// success
return SSD1306_SUCCESS;
_counter++; // update position
return SSD1306_SUCCESS; // success
}
/**
@ -511,16 +408,14 @@ uint8_t SSD1306_DrawChar (char character)
*
* @param char * string
*
* @return void
* @return uint8_t
*/
void SSD1306_DrawString (char *str)
{
// init
int i = 0;
// loop through character of string
while (str[i] != '\0') {
// draw string
SSD1306_DrawChar (str[i++]);
int i = 0; // init
while (str[i] != '\0') { // loop through character of string
SSD1306_DrawChar (str[i++]); // draw string
}
}
@ -537,109 +432,74 @@ uint8_t SSD1306_DrawPixel (uint8_t x, uint8_t y)
uint8_t page = 0;
uint8_t pixel = 0;
// if out of range
if ((x > MAX_X) && (y > MAX_Y)) {
// out of range
return SSD1306_ERROR;
if ((x > MAX_X) && (y > MAX_Y)) { // if out of range
return SSD1306_ERROR; // out of range
}
// find page (y / 8)
page = y >> 3;
// which pixel (y % 8)
pixel = 1 << (y - (page << 3));
// update counter
_counter = x + (page << 7);
// save pixel
cacheMemLcd[_counter++] |= pixel;
// success
return SSD1306_SUCCESS;
page = y >> 3; // find page (y / 8)
pixel = 1 << (y - (page << 3)); // which pixel (y % 8)
_counter = x + (page << 7); // update counter
cacheMemLcd[_counter++] |= pixel; // save pixel
return SSD1306_SUCCESS; // success
}
/**
* @desc Draw line by Bresenham algoritm
*
*
* @param uint8_t x start position / 0 <= cols <= MAX_X-1
* @param uint8_t x end position / 0 <= cols <= MAX_X-1
* @param uint8_t y start position / 0 <= rows <= MAX_Y-1
* @param uint8_t y start position / 0 <= rows <= MAX_Y-1
* @param uint8_t y end position / 0 <= rows <= MAX_Y-1
*
* @return uint8_t
*/
uint8_t SSD1306_DrawLine (uint8_t x1, uint8_t x2, uint8_t y1, uint8_t y2)
{
// determinant
int16_t D;
// deltas
int16_t delta_x, delta_y;
// steps
int16_t trace_x = 1, trace_y = 1;
int16_t D; // determinant
int16_t delta_x, delta_y; // deltas
int16_t trace_x = 1, trace_y = 1; // steps
// delta x
delta_x = x2 - x1;
// delta y
delta_y = y2 - y1;
delta_x = x2 - x1; // delta x
delta_y = y2 - y1; // delta y
// check if x2 > x1
if (delta_x < 0) {
// negate delta x
delta_x = -delta_x;
// negate step x
trace_x = -trace_x;
if (delta_x < 0) { // check if x2 > x1
delta_x = -delta_x; // negate delta x
trace_x = -trace_x; // negate step x
}
// check if y2 > y1
if (delta_y < 0) {
// negate detla y
delta_y = -delta_y;
// negate step y
trace_y = -trace_y;
if (delta_y < 0) { // check if y2 > y1
delta_y = -delta_y; // negate delta y
trace_y = -trace_y; // negate step y
}
// Bresenham condition for m < 1 (dy < dx)
if (delta_y < delta_x) {
// calculate determinant
D = (delta_y << 1) - delta_x;
// draw first pixel
SSD1306_DrawPixel (x1, y1);
// check if x1 equal x2
while (x1 != x2) {
// update x1
x1 += trace_x;
// check if determinant is positive
if (D >= 0) {
// update y1
y1 += trace_y;
// update determinant
D -= 2*delta_x;
// condition for m < 1 (dy < dx)
// -------------------------------------------------------------------------------------
if (delta_y < delta_x) { //
D = (delta_y << 1) - delta_x; // calculate determinant
SSD1306_DrawPixel (x1, y1); // draw first pixel
while (x1 != x2) { // check if x1 equal x2
x1 += trace_x; // update x1
if (D >= 0) { // positive?
y1 += trace_y; // update y1
D -= 2*delta_x; // update determinant
}
// update deteminant
D += 2*delta_y;
// draw next pixel
SSD1306_DrawPixel (x1, y1);
D += 2*delta_y; // update deteminant
SSD1306_DrawPixel (x1, y1); // draw next pixel
}
// for m > 1 (dy > dx)
// condition for m >= 1 (dy >= dx)
// -------------------------------------------------------------------------------------
} else {
// calculate determinant
D = delta_y - (delta_x << 1);
// draw first pixel
SSD1306_DrawPixel (x1, y1);
// check if y2 equal y1
while (y1 != y2) {
// update y1
y1 += trace_y;
// check if determinant is positive
if (D <= 0) {
// update y1
x1 += trace_x;
// update determinant
D += 2*delta_y;
D = delta_y - (delta_x << 1); // calculate determinant
SSD1306_DrawPixel (x1, y1); // draw first pixel
while (y1 != y2) { // check if y2 equal y1
y1 += trace_y; // update y1
if (D <= 0) { // positive?
x1 += trace_x; // update y1
D += 2*delta_y; // update determinant
}
// update deteminant
D -= 2*delta_x;
// draw next pixel
SSD1306_DrawPixel (x1, y1);
D -= 2*delta_x; // update deteminant
SSD1306_DrawPixel (x1, y1); // draw next pixel
}
}
// success return
return SSD1306_SUCCESS;
return SSD1306_SUCCESS; // success return
}

View File

@ -23,11 +23,14 @@
#ifndef __SSD1306_H__
#define __SSD1306_H__
<<<<<<< HEAD
// include libraries
#include <string.h>
#include "font.h"
#include "twi.h"
=======
>>>>>>> origin/develop
// Success
// -------------------------------------------
#define SSD1306_SUCCESS 0
@ -108,6 +111,7 @@
* @desc SSD1306 Send Start and SLAW request
*
* @param uint8_t
<<<<<<< HEAD
*
* @return uint8_t
*/
@ -127,6 +131,27 @@
*
* @param void
*
=======
*
* @return uint8_t
*/
uint8_t SSD1306_Send_StartAndSLAW (uint8_t);
/**
* @desc SSD1306 Send command
*
* @param uint8_t
*
* @return uint8_t
*/
uint8_t SSD1306_Send_Command (uint8_t);
/**
* @desc SSD1306 Clear screen
*
* @param void
*
>>>>>>> origin/develop
* @return void
*/
void SSD1306_ClearScreen (void);

View File

@ -1,18 +1,22 @@
/**
* ---------------------------------------------------+
* ---------------------------------------------------------------+
* @desc Two Wire Interface / I2C Communication
* ---------------------------------------------------+
* ---------------------------------------------------------------+
* Copyright (C) 2020 Marian Hrinko.
* Written by Marian Hrinko (mato.hrinko@gmail.com)
*
* @author Marian Hrinko
* @datum 06.09.2020
* @file twi.c
* @tested AVR Atmega16
* ---------------------------------------------------
* @tested AVR Atmega16, ATmega8, Atmega328
*
* @depend
* ---------------------------------------------------------------+
* @usage Master Transmit Operation
*/
// include libraries
#include <avr/io.h>
#include "twi.h"
/**
@ -22,7 +26,7 @@
*
* @return void
*/
void TWI_Init(void)
void TWI_Init (void)
{
// +++++++++++++++++++++++++++++++++++++++++++++
// Calculation fclk:
@ -34,10 +38,10 @@ void TWI_Init(void)
// TWBR = {(fcpu/fclk) - 16 } / (2*4^Prescaler)
// +++++++++++++++++++++++++++++++++++++++++++++
// @param1 value of TWBR,
// fclk = 400 kHz; TWBR = 3
// fclk = 100 kHz; TWBR = 20
// fclk = 400kHz (m16); TWBR = 3
// fclk = 100kHz (m16); TWBR = 20
// @param2 value of Prescaler = 1
TWI_FREQ(20, 1);
TWI_FREQ (3, 1);
}
/**
@ -47,7 +51,7 @@ void TWI_Init(void)
*
* @return char
*/
char TWI_MT_Start(void)
char TWI_MT_Start (void)
{
// null status flag
TWI_TWSR &= ~0xA8;
@ -73,7 +77,7 @@ char TWI_MT_Start(void)
*
* @return char
*/
char TWI_MT_Send_SLAW(char address)
char TWI_MT_Send_SLAW (char address)
{
// SLA+W
// ----------------------------------------------
@ -83,12 +87,12 @@ char TWI_MT_Send_SLAW(char address)
// wait till flag set
TWI_WAIT_TILL_TWINT_IS_SET();
// find
// test if SLA with WRITE acknowledged
if (TWI_STATUS != TWI_MT_SLAW_ACK) {
// return status
return TWI_STATUS;
}
// return found device address
// success
return SUCCESS;
}
@ -99,7 +103,7 @@ char TWI_MT_Send_SLAW(char address)
*
* @return char
*/
char TWI_MT_Send_Data(char data)
char TWI_MT_Send_Data (char data)
{
// DATA
// ----------------------------------------------
@ -109,12 +113,12 @@ char TWI_MT_Send_Data(char data)
// wait till flag set
TWI_WAIT_TILL_TWINT_IS_SET();
// find
// test if data acknowledged
if (TWI_STATUS != TWI_MT_DATA_ACK) {
// return status
return TWI_STATUS;
}
// return found device address
// success
return SUCCESS;
}
@ -125,7 +129,7 @@ char TWI_MT_Send_Data(char data)
*
* @return char
*/
char TWI_MR_Send_SLAR(char address)
char TWI_MR_Send_SLAR (char address)
{
// SLA+R
// ----------------------------------------------
@ -135,12 +139,12 @@ char TWI_MR_Send_SLAR(char address)
// wait till flag set
TWI_WAIT_TILL_TWINT_IS_SET();
// find
// test if SLA with READ acknowledged
if (TWI_STATUS != TWI_MR_SLAR_ACK) {
// return status
return TWI_STATUS;
}
// return found device address
// success
return SUCCESS;
}
@ -151,12 +155,12 @@ char TWI_MR_Send_SLAR(char address)
*
* @return void
*/
void TWI_Stop(void)
void TWI_Stop (void)
{
// End TWI
// -------------------------------------------------
// send stop sequence
TWI_STOP();
TWI_STOP ();
// wait for TWINT flag is set
// TWI_WAIT_TILL_TWINT_IS_SET();
}

112
lib/twi.h
View File

@ -1,69 +1,46 @@
/**
* ---------------------------------------------------+
* ---------------------------------------------------------------+
* @desc Two Wire Interface / I2C Communication
* ---------------------------------------------------+
* ---------------------------------------------------------------+
* Copyright (C) 2020 Marian Hrinko.
* Written by Marian Hrinko (mato.hrinko@gmail.com)
*
* @author Marian Hrinko
* @datum 06.09.2020
* @file twi.h
* @tested AVR Atmega16
* ---------------------------------------------------
* @tested AVR Atmega16, ATmega8, Atmega328
*
* @depend
* ---------------------------------------------------------------+
* @usage Basic Master Transmit Operation
*/
#include <stdio.h>
#include <avr/io.h>
#ifndef __TWI_H__
#define __TWI_H__
// define register for TWI communication
// -------------------------------------------
#if defined(__AVR_ATmega16__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
#define TWI_TWAR TWAR // TWI (Slave) Address Register
#define TWI_TWBR TWBR // TWI Bit Rate Register
#define TWI_TWDR TWDR // TWI Data Register
#define TWI_TWCR TWCR // TWI Control Register
#define TWI_TWSR TWSR // TWI Status Register
#define TWI_TWAR TWAR // TWI (Slave) Address Register
#define TWI_TWBR TWBR // TWI Bit Rate Register
#define TWI_TWDR TWDR // TWI Data Register
#define TWI_TWCR TWCR // TWI Control Register
#define TWI_TWSR TWSR // TWI Status Register
#endif
// TWI CLK frequency
// @param TWBR
// @param Prescaler
// TWPS1 TWPS0 - PRESCALER
// 0 0 - 1
// 0 1 - 4
// 1 0 - 16
// 1 1 - 64
#define TWI_FREQ(BIT_RATE, PRESCALER) { TWI_TWBR = BIT_RATE; TWI_TWSR |= (TWI_TWSR & 0x03) | PRESCALER; }
// Success
// -------------------------------------------
#ifndef SUCCESS
#define SUCCESS 0
#endif
// TWI start condition
// (1 << TWEN) - TWI Enable
// (1 << TWINT) - TWI Interrupt Flag - must be cleared by set
// (1 << TWSTA) - TWI Start
#define TWI_START() { TWI_TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWSTA); }
// TWI enable
// (1 << TWEN) - TWI Enable
// (1 << TWINT) - TWI Interrupt Flag - must be cleared by set
#define TWI_ENABLE() { TWI_TWCR = (1 << TWEN) | (1 << TWINT); }
// TWI stop condition
// (1 << TWEN) - TWI Enable
// (1 << TWINT) - TWI Interrupt Flag - must be cleared by set
// (1 << TWSTO) - TWI Stop
#define TWI_STOP() { TWI_TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWSTO); }
// TWI test if TWINT Flag is set
#define TWI_WAIT_TILL_TWINT_IS_SET() { while (!(TWI_TWCR & (1 << TWINT))); }
// TWI status mask
#define TWI_STATUS (TWI_TWSR & 0xF8)
// success return value
#define SUCCESS 0
// success return value
#define ERROR 1
// Error
// -------------------------------------------
#ifndef ERROR
#define ERROR 1
#endif
// ++++++++++++++++++++++++++++++++++++++++++
//
@ -106,9 +83,46 @@
#define TWI_ST_DATA_ACK 0xB8 // Data byte in TWDR has been transmitted; ACK has been received
#define TWI_ST_DATA_NACK 0xC0 // Data byte in TWDR has been transmitted; NOT ACK has been received
#define TWI_ST_DATA_LOST_ACK 0xC8 // Last data byte in TWDR has been transmitted (TWEA = '0'); ACK has been received
// TWI CLK frequency
// -------------------------------------------
// @param TWBR
// @param Prescaler
// TWPS1 TWPS0 - PRESCALER
// 0 0 - 1
// 0 1 - 4
// 1 0 - 16
// 1 1 - 64
#define TWI_FREQ(BIT_RATE, PRESCALER) { TWI_TWBR = BIT_RATE; TWI_TWSR |= (TWI_TWSR & 0x03) | PRESCALER; }
// TWI start condition
// -------------------------------------------
// (1 << TWEN) - TWI Enable
// (1 << TWINT) - TWI Interrupt Flag - must be cleared by set
// (1 << TWSTA) - TWI Start
#define TWI_START() { TWI_TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWSTA); }
// TWI stop condition
// -------------------------------------------
// (1 << TWEN) - TWI Enable
// (1 << TWINT) - TWI Interrupt Flag - must be cleared by set
// (1 << TWSTO) - TWI Stop
#define TWI_STOP() { TWI_TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWSTO); }
// TWI enable
// -------------------------------------------
// (1 << TWEN) - TWI Enable
// (1 << TWINT) - TWI Interrupt Flag - must be cleared by set
#define TWI_ENABLE() { TWI_TWCR = (1 << TWEN) | (1 << TWINT); }
// TWI test if TWINT Flag is set
#define TWI_WAIT_TILL_TWINT_IS_SET() { while (!(TWI_TWCR & (1 << TWINT))); }
// TWI status mask
#define TWI_STATUS ( TWI_TWSR & 0xF8 )
/**
* @desc TWI init - initialise communication
* @desc TWI init
*
* @param void
*

112
main.hex
View File

@ -1,112 +0,0 @@
:100000000C943D010C945A010C945A010C945A0121
:100010000C945A010C945A010C945A010C945A01F4
:100020000C945A010C945A010C945A010C945A01E4
:100030000C945A010C945A010C945A010C945A01D4
:100040000C945A010C945A010C945A010C945A01C4
:100050000C945A010C945A010C945A010C945A01B4
:100060000C945A010C945A01000000000081811880
:1000700081810007000700147F147F14242A7F2A3F
:10008000122313086462364955225000050300000C
:10009000001C2241000041221C0014083E081408E4
:1000A000083E0808005030000008080808080060F2
:1000B00060000020100804023E5149453E00427F86
:1000C000400042615149462141454B311814127F8D
:1000D0001027454545393C4A494930017109050316
:1000E0003649494936064949291E0036360000007E
:1000F0005636000008142241001414141414004150
:100100002214080201510906324979413E7E11113B
:10011000117E7F494949363E414141227F4141227A
:100120001C7F494949417F090909013E4149497AF2
:100130007F0808087F00417F41002040413F017F48
:10014000081422417F404040407F020C027F7F0420
:1001500008107F3E4141413E7F090909063E415159
:10016000215E7F09192946464949493101017F012C
:10017000013F4040403F1F2040201F3F4038403F4C
:100180006314081463070870080761514945430068
:100190007F41410002040810200041417F00040219
:1001A0000102044040404040000102040020545439
:1001B00054787F48444438384444442038444448C0
:1001C0007F3854545418087E0901020C5252523E92
:1001D0007F0804047800447D40002040443D007FB7
:1001E0001028440000417F40007C041804787C08FB
:1001F00004047838444444387C1414140808141453
:10020000147C7C080404084854545420043F44409F
:10021000203C4040207C1C2040201C3C4030403C86
:1002200044281028440C5050503C4464544C440022
:100230000836410000007F00000041360800100829
:1002400008100800000000001200AE01A83F0120C5
:10025000000221007F02220007004001D30000A11C
:1002600000C801DA1201817F00A400A601D5800137
:10027000D9C201DB20018D1400AF11241FBECFEFC6
:10028000D8E0DEBFCDBF11E0A0E0B1E0EEECF6E0DB
:1002900002C005900D92A431B107D9F725E0A4E181
:1002A000B1E001C01D92A631B207E1F70E944203FE
:1002B0000C9465030C94000084E18093B800E9EB92
:1002C000F0E08081908182708160892B8083089525
:1002D0008091B90087758093B90084EA8093BC004F
:1002E0008091BC0087FFFCCF8091B900887F883067
:1002F00049F08091B900887F803121F08091B90068
:10030000887F089580E00895880F8093BB0084E87B
:100310008093BC008091BC0087FFFCCF8091B90026
:10032000887F883121F08091B900887F089580E02E
:1003300008958093BB0084E88093BC008091BC004A
:1003400087FFFCCF8091B900887F883221F08091AF
:10035000B900887F089580E00895880F990B816027
:100360008093BB0084E88093BC008091BC0087FF31
:10037000FCCF8091B900887F803421F08091B90052
:10038000887F089580E0089584E98093BC000895F3
:10039000CF93C82F0E946801811104C08C2FCF9188
:1003A0000C948401CF910895CF93C82F80E80E94C8
:1003B0009901811104C08C2FCF910C949901CF9198
:1003C0000895FF920F931F93CF93DF93D82FE8E404
:1003D000F2E0C4910E945C018D2F0E94C80181113E
:1003E0001FC0D0E0E9E4F2E02097B9F0F4908F016B
:1003F0000E5F1F4F319684910E94D401811110C06D
:10040000F801FF2041F0F80184910E94D401FA9490
:100410000F5F1F4FF3CF2197E7CF0E94C40180E009
:10042000DF91CF911F910F91FF9008950E94C80115
:10043000811103C086EA0C94D40108950E94C8017A
:10044000811103C087EA0C94D4010895CF93DF9300
:100450000E94C801811113C080E40E94990181119A
:100460000EC0C4E1D1E089910E949901811107C0B9
:1004700085E0C431D807B9F70E94C40180E0DF915C
:10048000CF91089580E094E0E4E1F1E0DF019C0188
:100490001D9221503040E1F7089590E020E8629FDE
:1004A000800D911D112490931505809314050895D6
:1004B0002091140530911505C901880F892F881FD7
:1004C000990B91952A5F482F479544274795241B00
:1004D00027FF10C0883060F4863060F48F5F20E81A
:1004E000829FC0011124909315058093140502C0CA
:1004F00081E0089580E00895CF93C82F0E945802AC
:10050000813011F12091140530911505D901AC5EAF
:10051000BE4F45E0C402C001112440E050E0805AC3
:100520009109FC01E40FF51FE859FF4FE491ED93A9
:100530004F5F5F4F45305105A1F72A5F3F4F309322
:1005400015052093140580E0CF910895CF93DF9394
:10055000EC018991882319F00E947C02FACFDF9187
:10056000CF91089587FF02C0613420F5262F26958C
:100570002695269530E0F901F695FE2FEE27F795A2
:10058000E795E80FF11DCF01019690931505809333
:100590001405EC5EFE4F88E0289F6019112481E06D
:1005A00090E001C0880F6A95EAF79081892B8083DB
:1005B00080E0089581E008958F929F92AF92BF925C
:1005C000CF92DF92EF92FF920F931F93CF93DF931F
:1005D000082FA62E142FB22E262F30E0281B31090B
:1005E0008B2D90E0841B910937FF06C03195219532
:1005F000310999249A9402C09924939497FF06C0D4
:1006000091958195910988248A9402C088248394C5
:100610007C01EE0CFF1C6901CC0CDD1C82179307DA
:1006200094F4E701C21BD30B612F802F0E94B2020A
:100630000A15D1F0090DD7FD03C0180DCC19DD093D
:10064000CE0DDF1DF1CFEC01CC19DD09612F802F1C
:100650000E94B2021B1541F0180D1C161D06A4F3D2
:10066000090DCE0DDF1DF0CF80E0DF91CF911F91FE
:100670000F91FF90EF90DF90CF90BF90AF909F9041
:100680008F9008958CE30E94E1010E94420224E0D1
:1006900044E06FE780E00E94DC0261E087E00E94B6
:1006A0004D0280E091E00E94A60222E142E16FE764
:1006B00080E00E94DC0263E088E20E944D028CE34D
:0E06C0000E94260280E090E00895F894FFCF9B
:1006CE0053534431333036204F4C45442044524925
:0406DE00564552002B
:00000001FF