#include "i2c.h"
void SSD1306_SendCmd(uint8_t cmd) {
uint8_t buffer[] = {0, cmd};
IIC_Write(0x78, buffer, 2);
}
void SSD1306_SendData(uint8_t data) {
uint8_t buffer[] = {0x40, data};
IIC_Write(0x78, buffer, 2);
}
// y代表页,每页有x列,每列8个垂直像素
// 分辨率128x32代表, 128列,4页
void SSD1306_SetPos(uint8_t x, uint8_t y) {
SSD1306_SendCmd(0xB0+y);
SSD1306_SendCmd(0x10 | (x>>4));
SSD1306_SendCmd(0x00 | (x&0xF));
}
void SSD1306_Clear(void) {
for (uint8_t y = 0; y < 4; y++) {
SSD1306_SetPos(0, y);
for (uint8_t x = 0; x < 128; x++) {
SSD1306_SendData(0);
}
}
}
void SSD1306_Clear2(uint8_t ch) {
for (uint8_t y = 0; y < 4; y++) {
SSD1306_SetPos(0, y);
for (uint8_t x = 0; x < 128; x++) {
SSD1306_SendData((x^y) | ch);
}
}
}
void SSD1306_ColorTurn(uint8_t flag) {
SSD1306_SendCmd(flag ? 0xA7 : 0xA6);
}
void SSD1306_DisplayOn(void) {
SSD1306_SendCmd(0x8D); // SET DCDC命令
SSD1306_SendCmd(0x14); // DCDC ON
SSD1306_SendCmd(0xAF); // DISPLAY ON
}
void SSD1306_DisplayOff(void) {
SSD1306_SendCmd(0x8D); // SET DCDC命令
SSD1306_SendCmd(0x10); // DCDC OFF
SSD1306_SendCmd(0xAE); // DISPLAY OFF
}
static const uint8_t _InitData[] = {
0XAE, /*display off*/
0X00, /*set lower column address*/
0X10, /*set higher column address*/
0X00, /*set display start line*/
0XB0, /*set page address*/
0X81, /*contract control*/
0XFF, /*128*/
0XA1, /*set segment remap*/
0XA6, /*normal / reverse*/
0XA8, /*multiplex ratio*/
0X1F, /*duty = 1/32*/
0XC8, /*Com scan direction*/
0XD3, /*set display offset*/
0X00,
0XD5, /*set osc division*/
0X80,
0XD9, /*set pre-charge period*/
0X1F,
0XDA, /*set COM pins*/
0X00,
0XDB, /*set vcomh*/
0X40,
};
void WaitMS(uint16_t cnt) {
while (cnt--) for (uint16_t i = 0; i < 0x8000; i++) __NOP();
}
void SSD1306_Init(void) {
IIC_Init();
for (uint8_t i = 0; i < sizeof(_InitData); i++) {
SSD1306_SendCmd(_InitData[i]);
}
SSD1306_Clear();
SSD1306_DisplayOn();
while (1) {
for (uint8_t i = 0; i < 8; i++) {
SSD1306_Clear2(1<<i);
WaitMS(25);
}
}
}
标签:set,++,void,uint8,SendCmd,驱动,SSD1306 From: https://www.cnblogs.com/kehuadong/p/17157335.html