oled单片机代码
时间: 2023-07-09 13:10:11 浏览: 86
oled的代码
这是一个基于STM32F103C8T6的OLED显示屏的代码示例,使用的OLED屏幕型号为SSD1306。代码中使用了I2C协议进行通信。
```
#include "stm32f10x.h"
#include "oled.h"
#define OLED_CMD 0x00 //写命令
#define OLED_DATA 0x40 //写数据
void OLED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); //开启时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //PB6和PB7
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; //开漏输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 400000;
I2C_Init(I2C1,&I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
OLED_WR_Cmd(0xAE); //关闭显示
OLED_WR_Cmd(0x00); //设置低列地址
OLED_WR_Cmd(0x10); //设置高列地址
OLED_WR_Cmd(0x40); //设置起始行地址
OLED_WR_Cmd(0xB0); //设置页地址
OLED_WR_Cmd(0x81); //对比度设置
OLED_WR_Cmd(0xCF); //电荷泵设置
OLED_WR_Cmd(0xA1); //设置段重定向
OLED_WR_Cmd(0xA6); //普通显示模式
OLED_WR_Cmd(0xA8); //设置多路复用比
OLED_WR_Cmd(0x3F); //1/64
OLED_WR_Cmd(0xC8); //设置扫描方向
OLED_WR_Cmd(0xD3); //显示偏移
OLED_WR_Cmd(0x00); //不偏移
OLED_WR_Cmd(0xD5); //设置显示时钟分频
OLED_WR_Cmd(0x80);
OLED_WR_Cmd(0xD9); //设置预充电周期
OLED_WR_Cmd(0xF1);
OLED_WR_Cmd(0xDA); //设置COM硬件引脚配置
OLED_WR_Cmd(0x12);
OLED_WR_Cmd(0xDB); //设置VCOMH
OLED_WR_Cmd(0x40);
OLED_WR_Cmd(0x8D); //设置电荷泵使能
OLED_WR_Cmd(0x14);
OLED_WR_Cmd(0xAF); //开启显示
OLED_Clear();
}
void OLED_WR_Cmd(uint8_t cmd)
{
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, 0x78, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, OLED_CMD);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, cmd);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1, ENABLE);
}
void OLED_WR_Data(uint8_t data)
{
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, 0x78, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, OLED_DATA);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, data);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1, ENABLE);
}
void OLED_Clear(void)
{
uint8_t i, n;
for (i=0; i<8; i++)
{
OLED_WR_Cmd(0xb0+i);
OLED_WR_Cmd(0x00);
OLED_WR_Cmd(0x10);
for (n=0; n<128; n++)
{
OLED_WR_Data(0);
}
}
}
void OLED_ShowStr(uint8_t x, uint8_t y, uint8_t *str)
{
uint8_t i = 0;
while (str[i] != '\0')
{
OLED_ShowChar(x, y, str[i]);
x += 8;
i++;
}
}
void OLED_ShowChar(uint8_t x, uint8_t y, uint8_t chr)
{
uint8_t c = 0, i = 0;
c = chr - 32;
if (x > 120)
{
x = 0;
y++;
}
OLED_WR_Cmd(0xb0+y);
OLED_WR_Cmd(((x&0xf0)>>4)|0x10);
OLED_WR_Cmd((x&0x0f)|0x00);
for (i=0; i<8; i++)
{
OLED_WR_Data(F8X16[c*16+i]);
}
OLED_WR_Cmd(0xb0+y+1);
OLED_WR_Cmd(((x&0xf0)>>4)|0x10);
OLED_WR_Cmd((x&0x0f)|0x00);
for (i=0; i<8; i++)
{
OLED_WR_Data(F8X16[c*16+i+8]);
}
}
uint32_t OLED_Pow(uint8_t m, uint8_t n)
{
uint32_t result = 1;
while (n--)
{
result *= m;
}
return result;
}
void OLED_ShowNum(uint8_t x, uint8_t y, uint32_t num, uint8_t len, uint8_t size)
{
uint8_t t, i;
uint8_t enshow = 0;
for (i=0; i<len; i++)
{
t = (num/OLED_Pow(10, len-i-1))%10;
if (enshow == 0 && i < (len-1))
{
if (t == 0)
{
OLED_ShowChar(x+(size/2)*i, y, ' ');
continue;
}
else
{
enshow = 1;
}
}
OLED_ShowChar(x+(size/2)*i, y, t+'0');
}
}
void OLED_ShowChinese(uint8_t x, uint8_t y, uint8_t num)
{
uint8_t i = 0, n = 0;
uint8_t m = 0, c = 0;
uint8_t *p = 0;
OLED_WR_Cmd(0xb0+y);
OLED_WR_Cmd(((x&0xf0)>>4)|0x10);
OLED_WR_Cmd((x&0x0f)|0x00);
while (n < 16)
{
m = 8;
OLED_WR_Cmd(0xb0+y+n);
OLED_WR_Cmd(((x&0xf0)>>4)|0x10);
OLED_WR_Cmd((x&0x0f)|0x00);
p = (uint8_t *)F8X16;
p += num*16;
for (i=0; i<8; i++)
{
c = *(p+n*2);
if ((c & m) == m)
{
OLED_WR_Data(1);
}
else
{
OLED_WR_Data(0);
}
m >>= 1;
}
m = 8;
for (i=0; i<8; i++)
{
c = *(p+n*2+1);
if ((c & m) == m)
{
OLED_WR_Data(1);
}
else
{
OLED_WR_Data(0);
}
m >>= 1;
}
n++;
}
}
void OLED_Set_Pos(uint8_t x, uint8_t y)
{
OLED_WR_Cmd(0xb0+y);
OLED_WR_Cmd(((x&0xf0)>>4)|0x10);
OLED_WR_Cmd((x&0x0f)|0x00);
}
void OLED_Fill(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint8_t dot)
{
uint8_t x, y;
for (y=y1; y<=y2; y++)
{
OLED_WR_Cmd(0xb0+y);
OLED_WR_Cmd(((x1&0xf0)>>4)|0x10);
OLED_WR_Cmd((x1&0x0f)|0x00);
for (x=x1; x<=x2; x++)
{
OLED_WR_Data(dot);
}
}
}
```
代码中包含了初始化OLED屏幕、清屏、显示字符、显示数字等函数。可以根据具体需求进行修改和调整。
阅读全文