在stm32f103c8t6上使用的的0.96寸i2coled显示屏代码
时间: 2023-09-02 12:14:16 浏览: 141
以下是使用STM32F103C8T6控制0.96寸I2C OLED显示屏的代码,需要注意的是,你需要将i2c.h和i2c.c文件添加到你的工程中,并根据你的硬件连接修改i2c.h文件中的宏定义。
```c
#include "stm32f10x.h"
#include "i2c.h"
#define OLED_ADDRESS 0x78
#define OLED_CMD 0x00
#define OLED_DATA 0x40
void OLED_Init(void);
void OLED_WriteCmd(uint8_t cmd);
void OLED_WriteData(uint8_t data);
void OLED_SetPos(uint8_t x, uint8_t y);
void OLED_Fill(uint8_t fill_Data);
void OLED_ShowChar(uint8_t x, uint8_t y, uint8_t chr);
void OLED_ShowString(uint8_t x, uint8_t y, uint8_t *str);
int main(void)
{
SystemInit();
I2C_Init();
OLED_Init();
while (1)
{
OLED_Fill(0xFF);
OLED_ShowString(0, 0, "Hello World!");
}
}
void OLED_Init(void)
{
OLED_WriteCmd(0xAE);//display off
OLED_WriteCmd(0x20);//Set Memory Addressing Mode
OLED_WriteCmd(0x10);//00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
OLED_WriteCmd(0xb0);//Set Page Start Address for Page Addressing Mode,0-7
OLED_WriteCmd(0xc8);//Set COM Output Scan Direction
OLED_WriteCmd(0x00);//---set low column address
OLED_WriteCmd(0x10);//---set high column address
OLED_WriteCmd(0x40);//--set start line address
OLED_WriteCmd(0x81);//--set contrast control register
OLED_WriteCmd(0xff);
OLED_WriteCmd(0xa1);//--set segment re-map 0 to 127
OLED_WriteCmd(0xa6);//--set normal display
OLED_WriteCmd(0xa8);//--set multiplex ratio(1 to 64)
OLED_WriteCmd(0x3F);//--1/64 duty
OLED_WriteCmd(0xa4);//0xa4,Output follows RAM content;0xa5,Output ignores RAM content
OLED_WriteCmd(0xd3);//-set display offset
OLED_WriteCmd(0x00);//-not offset
OLED_WriteCmd(0xd5);//--set display clock divide ratio/oscillator frequency
OLED_WriteCmd(0xf0);//--set divide ratio
OLED_WriteCmd(0xd9);//--set pre-charge period
OLED_WriteCmd(0x22); //
OLED_WriteCmd(0xda);//--set com pins hardware configuration
OLED_WriteCmd(0x12);
OLED_WriteCmd(0xdb);//--set vcomh
OLED_WriteCmd(0x20);//0x20,0.77xVcc
OLED_WriteCmd(0x8d);//--set DC-DC enable
OLED_WriteCmd(0x14);//
OLED_WriteCmd(0xaf);//--turn on oled panel
}
void OLED_WriteCmd(uint8_t cmd)
{
I2C_Start();
I2C_SendByte(OLED_ADDRESS);
I2C_SendByte(OLED_CMD);
I2C_SendByte(cmd);
I2C_Stop();
}
void OLED_WriteData(uint8_t data)
{
I2C_Start();
I2C_SendByte(OLED_ADDRESS);
I2C_SendByte(OLED_DATA);
I2C_SendByte(data);
I2C_Stop();
}
void OLED_SetPos(uint8_t x, uint8_t y)
{
OLED_WriteCmd(0xb0 + y);
OLED_WriteCmd(((x & 0xf0) >> 4) | 0x10);
OLED_WriteCmd(x & 0x0f);
}
void OLED_Fill(uint8_t fill_Data)
{
uint8_t m, n;
for (m = 0; m < 8; m++)
{
OLED_WriteCmd(0xb0 + m); //page0-page1
OLED_WriteCmd(0x00); //low column start address
OLED_WriteCmd(0x10); //high column start address
for (n = 0; n < 128; n++)
{
OLED_WriteData(fill_Data);
}
}
}
void OLED_ShowChar(uint8_t x, uint8_t y, uint8_t chr)
{
uint8_t c = 0, i = 0;
c = chr - ' '; //得到偏移后的值
if (x > 120)
{
x = 0;
y++;
}
OLED_SetPos(x, y);
for (i = 0; i < 6; i++)
{
OLED_WriteData(F6x8[c][i]);
}
}
void OLED_ShowString(uint8_t x, uint8_t y, uint8_t *str)
{
uint8_t j = 0;
while (str[j] != '\0')
{
OLED_ShowChar(x, y, str[j]);
x += 6;
if (x > 126)
{
x = 0;
y++;
}
j++;
}
}
```
以上代码中的i2c.h和i2c.c文件可以在网上找到,也可以自己编写。另外,需要注意的是,本代码使用的是6x8点阵字体,如果需要使用其他字体,需要修改OLED_ShowChar函数中的字体数组。
阅读全文