当前位置:网站首页>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 预防sql注入问题与解决方法[PreparedStatement]
- Js7day (event object, event flow, event capture and bubble, prevent event flow, event delegation, student information table cases)
- Wechat official account payment prompt MCH_ ID parameter format error
- FBX import under ue4/ue5 runtime
- 8 examples of using date commands
- 堆 AcWing 838. 堆排序
- Hash table acwing 840 Simulated hash table
- Interesting interview questions
- 堆 AcWing 839. 模拟堆
- BOM DOM
猜你喜欢
![JDBC prevent SQL injection problems and solutions [preparedstatement]](/img/32/f71f5a31cdf710704267ff100b85d7.png)
JDBC prevent SQL injection problems and solutions [preparedstatement]

腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?

bellman-ford AcWing 853. 有边数限制的最短路

阿里发布的Redis开发文档,涵盖了所有的redis操作

spfa AcWing 852. SPFA judgement negative ring

Modular commonjs es module

BOM DOM

架构师必须了解的 5 种最佳软件架构模式

Async/await asynchronous function

Interval DP acwing 282 Stone merging
随机推荐
[FFH] little bear driver calling process (take calling LED light driver as an example)
BOM DOM
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
Js5day (event monitoring, function assignment to variables, callback function, environment object this, select all, invert selection cases, tab column cases)
Redis introduction, scenario and data type
Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
Js1day (input / output syntax, data type, data type conversion, VaR and let differences)
Redis bloom filter
Dijkstra AcWing 850. Dijkstra finding the shortest circuit II
Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
Efficiency comparison between ArrayList and LinkedList
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
VLAN experiment
Visual studio efficient and practical extension tools and plug-ins
JDBC 预防sql注入问题与解决方法[PreparedStatement]
AI mid stage technology research
Redis sentinel mechanism and configuration
Deep copy event bus
std::vector批量导入快速去重方法
线性DP AcWing 898. 数字三角形