当前位置:网站首页>OLED screen driver based on stm32
OLED screen driver based on stm32
2022-07-02 12:48:00 【sunflower-ly】
This experiment is based on STM32F103ZET6 Single chip microcomputer ,OLED The screen IIC Interface , The MCU pins used are PC8,PC9,OLED The screen shows BMP picture , So you need to prepare one in advance BMP picture ;
unsigned char show1[]=
{
0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0xC0,0x40,0x40,0x40,0x60,0x20,0x20,
0x20,0x20,0x20,0x20,0x20,0x60,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0x60,0x20,
0x30,0x10,0x18,0x08,0x08,0x04,0x04,0x04,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x06,0x04,0x04,0x0C,0x08,0x90,
0xF0,0x30,0x68,0xCC,0xC6,0x43,0x41,0x40,0x40,0x40,0xC0,0x80,0x80,0x80,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x38,0x0E,0x01,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x02,0x02,0x02,0x02,0x02,0x02,0x06,0x04,0x07,
0x0C,0x08,0x08,0x08,0x10,0x10,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,
0x02,0x06,0x1C,0xF0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x07,0x78,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x38,0xC0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x0C,0x04,0x04,0x04,0x04,0x0C,0x38,0xE0,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x07,0x18,0xE0,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xC0,0x30,0x0E,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0x18,0x0C,0x06,0x02,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x03,0xCE,0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x70,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,
0x1F,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,
0x0C,0x18,0x30,0x20,0x60,0xC0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x40,0x20,0x20,0x30,0x10,0x08,
0x0C,0x06,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x38,0x07,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x80,0x80,0x40,0x60,0x30,0x0F,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x07,0x0C,0x18,0x10,0x10,0x10,
0x18,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x03,0x02,0x02,0x06,0x04,0x04,0x04,
0x04,0x04,0x0C,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x04,0x04,
0x04,0x04,0x04,0x02,0x02,0x02,0x03,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x08,0x0C,0x04,0x04,0x06,0x02,0x02,0x01,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
OLED initialization
void OLED_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // Push pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;// Speed 50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_8|GPIO_Pin_9); // Power up ?
delay_ms(500);
// Corresponding OLED Initialization of the basic register of the screen , Corresponding screen model .
OLED_WR_Byte(0xAE,OLED_CMD); // Turn off display
OLED_WR_Byte(0xD5,OLED_CMD); // Set the clock division factor , Oscillation frequency
OLED_WR_Byte(80,OLED_CMD); //[3:0], Division factor ;[7:4], Oscillation frequency
OLED_WR_Byte(0xA8,OLED_CMD); // Set the number of drive paths
OLED_WR_Byte(0X3F,OLED_CMD); // Default 0X3F(1/64)
OLED_WR_Byte(0xD3,OLED_CMD); // Set the display offset
OLED_WR_Byte(0X00,OLED_CMD); // The default is 0
OLED_WR_Byte(0x40,OLED_CMD); // Set display start line [5:0], Row number .
OLED_WR_Byte(0x8D,OLED_CMD); // Charge pump settings
OLED_WR_Byte(0x14,OLED_CMD); //bit2, Turn on / close
OLED_WR_Byte(0x20,OLED_CMD); // Set memory address mode
OLED_WR_Byte(0x02,OLED_CMD); //[1:0],00, Column address mode ;01, Line address mode ;10, Page address mode ; Default 10;
OLED_WR_Byte(0xA1,OLED_CMD); // Segment redefinition settings ,bit0:0,0->0;1,0->127;
OLED_WR_Byte(0xC0,OLED_CMD); // Set up COM Scanning direction ;bit3:0, Common mode ;1, Redefining patterns COM[N-1]->COM0;N: Number of driving paths
OLED_WR_Byte(0xDA,OLED_CMD); // Set up COM Hardware pin configuration
OLED_WR_Byte(0x12,OLED_CMD); //[5:4] To configure
OLED_WR_Byte(0x81,OLED_CMD); // Contrast settings
OLED_WR_Byte(0xEF,OLED_CMD); //1~255; Default 0X7F ( Brightness setting , The bigger, the brighter )
OLED_WR_Byte(0xD9,OLED_CMD); // Set precharge cycle
OLED_WR_Byte(0xf1,OLED_CMD); //[3:0],PHASE 1;[7:4],PHASE 2;
OLED_WR_Byte(0xDB,OLED_CMD); // Set up VCOMH Voltage multiplier
OLED_WR_Byte(0x30,OLED_CMD); //[6:4] 000,0.65*vcc;001,0.77*vcc;011,0.83*vcc;
OLED_WR_Byte(0xA4,OLED_CMD); // Global display on ;bit0:1, Turn on ;0, close ;( hang / Black screen )
OLED_WR_Byte(0xA6,OLED_CMD); // Set display mode ;bit0:1, Inverse display ;0, Normal display
OLED_WR_Byte(0xAF,OLED_CMD); // Turn on the display
OLED_Clear();
OLED_Refresh_Gram();
}
This screen is based on IIC Interface , Therefore, it is necessary to include IIC Contents of the agreement : Start signal OLED_IICStart() And stop signal OLED_IICStop().
void OLED_IICStart()
{
OLED_SCL = 1;
OLED_SDA = 1;
OLED_SDA = 0;
OLED_SCL = 0;
}
void OLED_IICStop()
{
OLED_SCL = 0;
OLED_SDA = 0;
OLED_SCL = 1;
OLED_SDA = 1;
}
Writing data : Write a byte
void OLED_Byte(u8 dat)// Write a byte
{
u8 i;
for(i=0;i<8;i++)
{
if(dat & 0x80)
OLED_SDA=1;
else
OLED_SDA=0;
OLED_SCL=1;
OLED_SCL=0;
dat<<=1;
}
OLED_SDA=1;
OLED_SCL=1;
OLED_SCL=0;
}
void OLED_WR_Byte(u8 dat,u8 cmd)// Write data or write commands cmd Control command or data
{
if(cmd == 1)
{
OLED_IICStart();
OLED_Byte(0X78);
OLED_Byte(0X40);
OLED_Byte(dat);
OLED_IICStop();
}
else
{
OLED_IICStart();
OLED_Byte(0X78);
OLED_Byte(0X00);
OLED_Byte(dat);
OLED_IICStop();
}
}
to update OLED The screen
// Update the memory to LCD
void OLED_Refresh_Gram(void)
{
u8 i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte (0xb0+i,OLED_CMD); // Set page address (0~7)
OLED_WR_Byte (0x00,OLED_CMD); // Set the display position — List the addresses
OLED_WR_Byte (0x10,OLED_CMD); // Set the display position — Column address
for(n=0;n<128;n++)OLED_WR_Byte(OLED_GRAM[n][i],OLED_DATA);
}
}
Clear the screen
// Screen clearing function , Clear the screen , The whole screen is black ! It's the same as not lit !!!
void OLED_Clear(void)
{
u8 i,n;
for(i=0;i<8;i++)
{
for(n=0;n<128;n++)
{
OLED_GRAM[n][i]=0;
}
}
OLED_Refresh_Gram();// Update display
}
Show BMP Picture function
// Show BMP picture 128×64
// Starting point coordinates (x,y),x The scope of the 0~127,y For the scope of the page 0~7
void OLED_DrawBMP(u8 x0, u8 y0,u8 x1, u8 y1,u8 BMP[])
{
u16 j=0;
u8 x,y;
for(y=y0;y<y1;y++)
{
OLED_WR_Byte(0xb0+y,OLED_CMD);
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0x10,OLED_CMD);
for(x=x0;x<x1;x++)
{
OLED_WR_Byte(BMP[j++],OLED_DATA);
}
}
}
The main function
int main()
{
delay_init();
OLED_Init();
OLED_Clear();
OLED_DrawBMP(0,0,128,8,show1);
delay_ms(1000);
while(1);;
}
边栏推荐
- 移动式布局(流式布局)
- 腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
- Interesting interview questions
- Linear DP acwing 902 Shortest editing distance
- [ybtoj advanced training guidance] cross the river [BFS]
- JSON序列化 与 解析
- About asp Net MVC project in local vs running response time is too long to access, the solution!
- JDBC prevent SQL injection problems and solutions [preparedstatement]
- 趣味 面试题
- 染色法判定二分图 AcWing 860. 染色法判定二分图
猜你喜欢

堆 AcWing 838. 堆排序

区间DP AcWing 282. 石子合并

BOM DOM

Direct control PTZ PTZ PTZ PTZ camera debugging (c)

"As a junior college student, I found out how difficult it is to counter attack after graduation."

Bom Dom

The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night

spfa AcWing 851. SPFA finding the shortest path

Js4day (DOM start: get DOM element content, modify element style, modify form element attributes, setinterval timer, carousel Map Case)

8 examples of using date commands
随机推荐
What data types does redis have and their application scenarios
IPhone 6 plus is listed in Apple's "retro products" list
spfa AcWing 852. spfa判断负环
JDBC prevent SQL injection problems and solutions [preparedstatement]
Wechat official account payment prompt MCH_ ID parameter format error
std::vector批量导入快速去重方法
Docsify deploy IIS
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
软件测试面试题-2022年大厂面试题合集
获取文件版权信息
Deep copy event bus
Interval DP acwing 282 Stone merging
浏览器node事件循环
架构师必须了解的 5 种最佳软件架构模式
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
3 A VTT端接 稳压器 NCP51200MNTXG资料
移动式布局(流式布局)
C#修饰符
JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)
线性DP AcWing 895. 最长上升子序列