当前位置:网站首页>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);;
}
边栏推荐
- 堆 AcWing 838. 堆排序
- Docsify deploy IIS
- [ybtoj advanced training guidance] cross the river [BFS]
- ArrayList与LinkedList效率的对比
- Traverse entrylist method correctly
- Direct control PTZ PTZ PTZ PTZ camera debugging (c)
- 哈希表 AcWing 841. 字符串哈希
- Package management tools
- Browser node event loop
- Js2day (also i++ and ++i, if statements, ternary operators, switch, while statements, for loop statements)
猜你喜欢
哈希表 AcWing 841. 字符串哈希
spfa AcWing 851. spfa求最短路
Mobile layout (flow layout)
ArrayList与LinkedList效率的对比
JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)
js1day(输入输出语法,数据类型,数据类型转换,var和let区别)
Js8day (rolling event (scroll family), offset family, client family, carousel map case (to be done))
通过反射执行任意类的任意方法
Linear DP acwing 898 Number triangle
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
随机推荐
Redis introduction, scenario and data type
Js10day (API phased completion, regular expression introduction, custom attributes, filtering sensitive word cases, registration module verification cases)
VLAN experiment
Input box assembly of the shutter package
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
Docker compose configuration mysql, redis, mongodb
Browser storage scheme
spfa AcWing 852. spfa判断负环
spfa AcWing 851. spfa求最短路
哈希表 AcWing 840. 模拟散列表
js4day(DOM开始:获取DOM元素内容,修改元素样式,修改表单元素属性,setInterval定时器,轮播图案例)
Linear DP acwing 899 Edit distance
Package management tools
8A 同步降压稳压器 TPS568230RJER_规格信息
BOM DOM
Redis avalanche, penetration, breakdown
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
C modifier
JS iterator generator asynchronous code processing promise+ generator - > await/async
Redis bloom filter