当前位置:网站首页>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);;
}
边栏推荐
- js1day(輸入輸出語法,數據類型,數據類型轉換,var和let區別)
- 2.6 using recursion and stack - [tower of Hanoi problem]
- Rust language document Lite (Part 1) - cargo, output, basic syntax, data type, ownership, structure, enumeration and pattern matching
- 线性DP AcWing 896. 最长上升子序列 II
- Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
- Redis avalanche, penetration, breakdown
- Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
- Mobile layout (flow layout)
- JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
- 深拷貝 事件總線
猜你喜欢

Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?

Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution

AI mid stage technology research

Js1day (input / output syntax, data type, data type conversion, VaR and let differences)

Deep Copy Event bus

LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY

async/await 异步函数

JS iterator generator asynchronous code processing promise+ generator - > await/async

线性DP AcWing 895. 最长上升子序列

Rust search server, rust quick service finding tutorial
随机推荐
Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution
BOM DOM
Js10day (API phased completion, regular expression introduction, custom attributes, filtering sensitive word cases, registration module verification cases)
Js7day (event object, event flow, event capture and bubble, prevent event flow, event delegation, student information table cases)
移动式布局(流式布局)
js3day(数组操作,js冒泡排序,函数,调试窗口,作用域及作用域链,匿名函数,对象,Math对象)
js4day(DOM开始:获取DOM元素内容,修改元素样式,修改表单元素属性,setInterval定时器,轮播图案例)
spfa AcWing 852. SPFA judgement negative ring
架构师必须了解的 5 种最佳软件架构模式
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
模块化 CommonJS ES Module
Rust language document Lite (Part 1) - cargo, output, basic syntax, data type, ownership, structure, enumeration and pattern matching
Browser storage scheme
js2day(又是i++和++i,if语句,三元运算符,switch、while语句,for循环语句)
深拷貝 事件總線
Linear DP acwing 897 Longest common subsequence
阿里发布的Redis开发文档,涵盖了所有的redis操作
[ybtoj advanced training guide] similar string [string] [simulation]
Leetcode - Sword finger offer 51 Reverse pairs in an array
Fluent fluent library encapsulation