当前位置:网站首页>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);;
}
边栏推荐
猜你喜欢
JDBC prevent SQL injection problems and solutions [preparedstatement]
Js2day (also i++ and ++i, if statements, ternary operators, switch, while statements, for loop statements)
Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand
2.6 using recursion and stack - [tower of Hanoi problem]
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
js4day(DOM开始:获取DOM元素内容,修改元素样式,修改表单元素属性,setInterval定时器,轮播图案例)
模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计
Js1day (syntaxe d'entrée / sortie, type de données, conversion de type de données, Var et let différenciés)
bellman-ford AcWing 853. 有边数限制的最短路
传感器 ADXL335BCPZ-RL7 3轴 加速度计 符合 RoHS/WEEE
随机推荐
Intel internal instructions - AVX and avx2 learning notes
Linear DP acwing 895 Longest ascending subsequence
Bom Dom
JS8day(滚动事件(scroll家族),offset家族,client家族,轮播图案例(待做))
2.7 binary tree, post order traversal - [FBI tree]
Js3day (array operation, JS bubble sort, function, debug window, scope and scope chain, anonymous function, object, Math object)
[ybtoj advanced training guidance] cross the river [BFS]
Use MySQL events to regularly perform post seven world line tasks
Mobile layout (flow layout)
Floyd AcWing 854. Floyd finds the shortest path
深拷贝 事件总线
BOM DOM
Linear DP acwing 897 Longest common subsequence
线性DP AcWing 897. 最长公共子序列
NTMFS4C05NT1G N-CH 30V 11.9A MOS管,PDF
Js5day (event monitoring, function assignment to variables, callback function, environment object this, select all, invert selection cases, tab column cases)
Js1day (syntaxe d'entrée / sortie, type de données, conversion de type de données, Var et let différenciés)
1380. Lucky numbers in the matrix [two-dimensional array, matrix]
js2day(又是i++和++i,if语句,三元运算符,switch、while语句,for循环语句)
Linear DP acwing 898 Number triangle