ATY_LIB V2_102_230218
ATY_LIB for general devices or ALGO
 
Loading...
Searching...
No Matches
ssd1306.c File Reference
#include "ssd1306.h"

Go to the source code of this file.

Data Structures

struct  SSD1306_t
 

Macros

#define ABS(x)   ((x) > 0 ? (x) : -(x))
 

Functions

void SSD1306_WriteCommand (uint8_t command)
 Writes single byte command to slave.
 
void SSD1306_WriteData (uint8_t dat)
 Writes single byte data to slave.
 
void SSD1306_Init (void)
 
void SSD1306_UpdateScreen (void)
 Updates buffer from internal RAM to LCD.
 
void SSD1306_ToggleInvert (void)
 Toggles pixels invertion inside internal RAM.
 
void SSD1306_Fill (uint8_t color)
 Fills entire LCD with desired color.
 
void SSD1306_DrawPixel (uint16_t x, uint16_t y, uint8_t color)
 Draws pixel at desired location.
 
void SSD1306_GotoXY (uint16_t x, uint16_t y)
 Sets cursor pointer to desired location for strings.
 
void SSD1306_DrawLine (uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint8_t c)
 Draws line on LCD.
 
void SSD1306_ON (void)
 
void SSD1306_OFF (void)
 

Variables

static __XDATA uint8_t SSD1306_Buffer_all [SSD1306_WIDTH *SSD1306_HEIGHT/8]
 
static SSD1306_t SSD1306
 

Macro Definition Documentation

◆ ABS

#define ABS (   x)    ((x) > 0 ? (x) : -(x))

Definition at line 18 of file ssd1306.c.

Function Documentation

◆ SSD1306_DrawLine()

void SSD1306_DrawLine ( uint16_t  x0,
uint16_t  y0,
uint16_t  x1,
uint16_t  y1,
uint8_t  c 
)

Draws line on LCD.

Note
SSD1306_UpdateScreen() must be called after that in order to see updated LCD screen
Parameters
x0Line X start point. Valid input is 0 to SSD1306_WIDTH - 1
y0Line Y start point. Valid input is 0 to SSD1306_HEIGHT - 1
x1Line X end point. Valid input is 0 to SSD1306_WIDTH - 1
y1Line Y end point. Valid input is 0 to SSD1306_HEIGHT - 1
cColor to be used. This parameter can be a value of SSD1306_COLOR_t enumeration
Return values
None

Definition at line 223 of file ssd1306.c.

224{
225 int16_t dx, dy, sx, sy, err, e2, i, tmp;
226
227 /* Check for overflow */
228 if (x0 >= SSD1306_WIDTH)
229 {
230 x0 = SSD1306_WIDTH - 1;
231 }
232 if (x1 >= SSD1306_WIDTH)
233 {
234 x1 = SSD1306_WIDTH - 1;
235 }
236 if (y0 >= SSD1306_HEIGHT)
237 {
238 y0 = SSD1306_HEIGHT - 1;
239 }
240 if (y1 >= SSD1306_HEIGHT)
241 {
242 y1 = SSD1306_HEIGHT - 1;
243 }
244
245 dx = (x0 < x1) ? (x1 - x0) : (x0 - x1);
246 dy = (y0 < y1) ? (y1 - y0) : (y0 - y1);
247 sx = (x0 < x1) ? 1 : -1;
248 sy = (y0 < y1) ? 1 : -1;
249 err = ((dx > dy) ? dx : -dy) / 2;
250
251 if (dx == 0)
252 {
253 if (y1 < y0)
254 {
255 tmp = y1;
256 y1 = y0;
257 y0 = tmp;
258 }
259
260 if (x1 < x0)
261 {
262 tmp = x1;
263 x1 = x0;
264 x0 = tmp;
265 }
266
267 /* Vertical line */
268 for (i = y0; i <= y1; i++)
269 {
270 SSD1306_DrawPixel(x0, i, c);
271 }
272
273 /* Return from function */
274 return;
275 }
276
277 if (dy == 0)
278 {
279 if (y1 < y0)
280 {
281 tmp = y1;
282 y1 = y0;
283 y0 = tmp;
284 }
285
286 if (x1 < x0)
287 {
288 tmp = x1;
289 x1 = x0;
290 x0 = tmp;
291 }
292
293 /* Horizontal line */
294 for (i = x0; i <= x1; i++)
295 {
296 SSD1306_DrawPixel(i, y0, c);
297 }
298
299 /* Return from function */
300 return;
301 }
302
303 while (1)
304 {
305 SSD1306_DrawPixel(x0, y0, c);
306 if (x0 == x1 && y0 == y1)
307 {
308 break;
309 }
310 e2 = err;
311 if (e2 > -dx)
312 {
313 err -= dy;
314 x0 += sx;
315 }
316 if (e2 < dy)
317 {
318 err += dx;
319 y0 += sy;
320 }
321 }
322}
short int16_t
Definition: fw_types.h:24
uint8_t __XDATA i
void SSD1306_DrawPixel(uint16_t x, uint16_t y, uint8_t color)
Draws pixel at desired location.
Definition: ssd1306.c:191
#define SSD1306_HEIGHT
Definition: ssd1306.h:37
#define SSD1306_WIDTH
Definition: ssd1306.h:33

◆ SSD1306_DrawPixel()

void SSD1306_DrawPixel ( uint16_t  x,
uint16_t  y,
uint8_t  color 
)

Draws pixel at desired location.

Note
SSD1306_UpdateScreen() must called after that in order to see updated LCD screen
Parameters
xX location. This parameter can be a value between 0 and SSD1306_WIDTH - 1
yY location. This parameter can be a value between 0 and SSD1306_HEIGHT - 1
colorColor to be used for screen fill. This parameter can be a value of SSD1306_COLOR_t enumeration
Return values
None

Definition at line 191 of file ssd1306.c.

192{
193 if (x >= SSD1306_WIDTH || y >= SSD1306_HEIGHT)
194 {
195 /* Error */
196 return;
197 }
198
199 /* Check if pixels are inverted */
200 if (SSD1306.Inverted)
201 {
202 color = (uint8_t)!color;
203 }
204
205 /* Set color */
206 if (color == SSD1306_COLOR_WHITE)
207 {
208 SSD1306_Buffer_all[x + (y / 8) * SSD1306_WIDTH] |= 1 << (y % 8);
209 }
210 else
211 {
212 SSD1306_Buffer_all[x + (y / 8) * SSD1306_WIDTH] &= ~(1 << (y % 8));
213 }
214}
unsigned char uint8_t
Definition: fw_types.h:18
volatile int16_t y
Definition: main.c:34
volatile int16_t x
Definition: main.c:34
static SSD1306_t SSD1306
Definition: ssd1306.c:32
static __XDATA uint8_t SSD1306_Buffer_all[SSD1306_WIDTH *SSD1306_HEIGHT/8]
Definition: ssd1306.c:21
#define SSD1306_COLOR_WHITE
Definition: ssd1306.h:47
uint8_t Inverted
Definition: ssd1306.c:27

◆ SSD1306_Fill()

void SSD1306_Fill ( uint8_t  Color)

Fills entire LCD with desired color.

Note
SSD1306_UpdateScreen() must be called after that in order to see updated LCD screen
Parameters
ColorColor to be used for screen fill. This parameter can be a value of SSD1306_COLOR_t enumeration

Definition at line 181 of file ssd1306.c.

182{
183 if (SSD1306.Inverted)
184 {
185 color = (uint8_t)!color;
186 }
187 /* Set memory */
188 memset(SSD1306_Buffer_all, (color == SSD1306_COLOR_BLACK) ? 0x00 : 0xFF, SSD1306_WIDTH * SSD1306_HEIGHT / 8);
189}
#define SSD1306_COLOR_BLACK
Definition: ssd1306.h:45

◆ SSD1306_GotoXY()

void SSD1306_GotoXY ( uint16_t  x,
uint16_t  y 
)

Sets cursor pointer to desired location for strings.

Parameters
xX location. This parameter can be a value between 0 and SSD1306_WIDTH - 1
yY location. This parameter can be a value between 0 and SSD1306_HEIGHT - 1
Return values
None

Definition at line 216 of file ssd1306.c.

217{
218 /* Set write pointers */
221}
uint16_t CurrentY
Definition: ssd1306.c:26
uint16_t CurrentX
Definition: ssd1306.c:25

◆ SSD1306_Init()

void SSD1306_Init ( void  )

Initializes SSD1306 LCD

Set the lower start column address of pointer by command 00h~0Fh. Set the upper start column address of pointer by command 10h~1Fh.

set contrast control register, 2 bytes, 0x00 - 0xFF

0xA4,Output follows RAM content 0xA5,Output ignores RAM content

0xA6, Normal display (RESET) 0xA7, Inverse display

Set the page start address of the target display location by command B0h to B7h For Page Addressing Mode only

Set Page Address, 3 bytes For Horizontal and Vertical Addressing Mode only

COM Output Scan Direction 0xC0: normal mode (RESET) Scan from COM0 to COM[N –1] 0xC8: remapped mode. Scan from COM[N-1] to COM0

Set display RAM display start line register from 0-63

Segment Re-map 0xA0: column address 0 is mapped to SEG0 (RESET), 0xA1: column address 127 is mapped to SEG0

Set MUX ratio to N+1 MUX N=A[5:0]: from 16MUX to 64MUX, RESET=111111b (i.e. 63d, 64MUX) A[5:0] from 0 to 14 are invalid entry.

Set Display Offset, Set vertical shift by COM from 0d~63d The value is reset to 00h after RESET

Set COM Pins Hardware Configuration A[4]=0b, Sequential COM pin configuration A[4]=1b(RESET), Alternative COM pin configuration A[5]=0b(RESET), Disable COM Left/Right remap A[5]=1b, Enable COM Left/Right remap

Set Display Divide Ratio/Oscillator Frequency

Set Pre-charge Period

Set V COMH Deselect Level 0x00: 0.65 * Vcc 0x10: 0.77 * Vcc (RESET) 0x11: 0.83 * Vcc

charge pump setting 0x10: Disable charge pump(RESET) 0x14: Enable charge pump during display on

0xAE, Display OFF (sleep mode), 0xAF, Display ON in normal mode

Definition at line 44 of file ssd1306.c.

45{
46 SYS_Delay(100);
47
48 /* Init LCD */
49 SSD1306_WriteCommand(0xAE); //display off
54 SSD1306_WriteCommand(0x00); //---set low column address
55 SSD1306_WriteCommand(0x10); //---set high column address
56
66 /* 0x20,Set Memory Addressing Mode, 2 bytes,
67 * 0x00,Horizontal Addressing Mode (slide horizontally and goto next page)
68 * 0x01,Vertical Addressing Mode (slide vertically and goto next column)
69 * 0x02,Page Addressing Mode (RESET) (slide horizontally and remain in the same page)
70 * 0x03,Invalid
71 */
84 SSD1306_WriteCommand(0x00); // From Page 0
85 SSD1306_WriteCommand(0x07); // To Page 7
86
91 SSD1306_WriteCommand(0xC8); //Set COM Output Scan Direction
110 SSD1306_WriteCommand(0x00); // offset in vertical
118 SSD1306_WriteCommand(0x12); // A[4]=0, A[5]=1
123 SSD1306_WriteCommand(0xF0); // divide ratio
136
143
147
148 /* Clear screen */
150
151 /* Update screen */
153
154 /* Set default values */
155 SSD1306.CurrentX = 0;
156 SSD1306.CurrentY = 0;
157
158 /* Initialized OK */
160}
void SYS_Delay(uint16_t t)
Definition: fw_sys.c:65
void SSD1306_Fill(uint8_t color)
Fills entire LCD with desired color.
Definition: ssd1306.c:181
void SSD1306_WriteCommand(uint8_t command)
Writes single byte command to slave.
Definition: ssd1306.c:34
void SSD1306_UpdateScreen(void)
Updates buffer from internal RAM to LCD.
Definition: ssd1306.c:162
uint8_t Initialized
Definition: ssd1306.c:28

◆ SSD1306_OFF()

void SSD1306_OFF ( void  )

Definition at line 330 of file ssd1306.c.

331{
335}

◆ SSD1306_ON()

void SSD1306_ON ( void  )

Definition at line 324 of file ssd1306.c.

325{
329}

◆ SSD1306_ToggleInvert()

void SSD1306_ToggleInvert ( void  )

Toggles pixels invertion inside internal RAM.

Note
SSD1306_UpdateScreen() must be called after that in order to see updated LCD screen

Definition at line 167 of file ssd1306.c.

168{
169 uint16_t i;
170
171 /* Toggle invert */
173
174 /* Do memory toggle */
175 for (i = 0; i < sizeof(SSD1306_Buffer_all); i++)
176 {
177 SSD1306_Buffer_all[i] = ~SSD1306_Buffer_all[i];
178 }
179}
unsigned short uint16_t
Definition: fw_types.h:19

◆ SSD1306_UpdateScreen()

void SSD1306_UpdateScreen ( void  )

Updates buffer from internal RAM to LCD.

Note
This function must be called each time you do some changes to LCD, to update buffer from RAM to LCD

Definition at line 162 of file ssd1306.c.

163{
165}
uint8_t I2C_Write(uint8_t devAddr, uint8_t memAddr, uint8_t *dat, uint16_t size)
Definition: fw_i2c.c:18
#define SSD1306_I2C_ADDR
Definition: ssd1306.h:26

◆ SSD1306_WriteCommand()

void SSD1306_WriteCommand ( uint8_t  command)

Writes single byte command to slave.

Parameters
commandcommand to be written

Definition at line 34 of file ssd1306.c.

35{
36 I2C_Write(SSD1306_I2C_ADDR, 0x00, &command, 1);
37}

◆ SSD1306_WriteData()

void SSD1306_WriteData ( uint8_t  dat)

Writes single byte data to slave.

Parameters
datdata to be written

Definition at line 39 of file ssd1306.c.

40{
41 I2C_Write(SSD1306_I2C_ADDR, 0x40, &dat, 1);
42}
__CODE int8_t dat[20]

Variable Documentation

◆ SSD1306

SSD1306_t SSD1306
static

Definition at line 32 of file ssd1306.c.

◆ SSD1306_Buffer_all

__XDATA uint8_t SSD1306_Buffer_all[SSD1306_WIDTH *SSD1306_HEIGHT/8]
static

Definition at line 21 of file ssd1306.c.