oled.c
#include "oled.h"
#include "oledfont.h"
void IIC_Start()
{
OLED_SDIN_Set();
OLED_SCLK_Set();
delay_us(5);
OLED_SDIN_Clr();
delay_us(6);
OLED_SCLK_Clr();
}
void IIC_Stop()
{
OLED_SCLK_Clr() ;
OLED_SDIN_Clr();
OLED_SCLK_Set();
delay_us(6);
OLED_SDIN_Set();
delay_us(6);
}
void IIC_Ack()
{
OLED_SCLK_Clr();
OLED_SDIN_Clr();
delay_us(2);
OLED_SCLK_Set();
delay_us(5);
OLED_SCLK_Clr();
}
void Write_IIC_Byte(unsigned char IIC_Byte)
{
unsigned char i;
unsigned char m, da;
da = IIC_Byte;
OLED_SCLK_Clr();
for(i=0; i<8; i++)
{
m = da;
OLED_SCLK_Clr();
m = m & 0x80;
if(m == 0x80)
{
OLED_SDIN_Set();
}
else
OLED_SDIN_Clr();
da = da << 1;
delay_us(2);
OLED_SCLK_Set();
delay_us(2);
OLED_SCLK_Clr();
delay_us(2);
}
}
void Write_IIC_Data(unsigned char IIC_Data)
{
IIC_Start();
Write_IIC_Byte(0x78); // D/C#=0; R/W#=0
IIC_Ack();
Write_IIC_Byte(0x40); // write data
IIC_Ack();
Write_IIC_Byte(IIC_Data);
IIC_Ack();
IIC_Stop();
}
void Write_IIC_Command(unsigned char IIC_Command)
{
IIC_Start();
Write_IIC_Byte(0x78); //Slave address,SA0=0
IIC_Ack();
Write_IIC_Byte(0x00); //write command
IIC_Ack();
Write_IIC_Byte(IIC_Command);
IIC_Ack();
IIC_Stop();
}
void OLED_WR_Byte(unsigned dat, unsigned cmd) //1为写命令,0为写数据。
{
if(cmd)
Write_IIC_Data(dat);
else
Write_IIC_Command(dat);
}
void OLED_Clear(void)
{
u8 i,n;
for(i=0; i<8; i++)
{
OLED_WR_Byte (0xb0 + i, OLED_CMD); //设置页地址(0~7)
OLED_WR_Byte (0x00, OLED_CMD); //设置显示位置—列低地址
OLED_WR_Byte (0x10, OLED_CMD); //设置显示位置—列高地址
for(n=0; n<128; n++)
OLED_WR_Byte(0,OLED_DATA); //0为灭屏状态,无效果,若想看到亮屏效果,可改为0xff.
}
}
void OLED_Init(void) //初始化SSD1306,主函数调用它即可。
{
GPIO_InitTypeDef GPIO_InitStructure; //初始化GPIO,结构体的声明,需放在前面。
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽方式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉方式
GPIO_Init(GPIOC, &GPIO_InitStructure);
delay_ms(200);
OLED_WR_Byte(0xae,OLED_CMD);
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0x10,OLED_CMD);
OLED_WR_Byte(0x40,OLED_CMD);
OLED_WR_Byte(0xb0,OLED_CMD);
OLED_WR_Byte(0x81,OLED_CMD);
OLED_WR_Byte(0xff,OLED_CMD);
OLED_WR_Byte(0xa1,OLED_CMD);
OLED_WR_Byte(0xa6,OLED_CMD);
OLED_WR_Byte(0xc9,OLED_CMD);
OLED_WR_Byte(0xa8,OLED_CMD);
OLED_WR_Byte(0x1f,OLED_CMD);
OLED_WR_Byte(0xd3,OLED_CMD);
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0xd5,OLED_CMD);
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0xd9,OLED_CMD);
OLED_WR_Byte(0xf1,OLED_CMD);
OLED_WR_Byte(0xda,OLED_CMD);
OLED_WR_Byte(0x02,OLED_CMD);
OLED_WR_Byte(0xdb,OLED_CMD);
OLED_WR_Byte(0x40,OLED_CMD);
OLED_WR_Byte(0x8d,OLED_CMD);
OLED_WR_Byte(0x14,OLED_CMD);
OLED_WR_Byte(0xaf,OLED_CMD);
OLED_Clear();
}
oled.h
#ifndef __OLED_H //防止头文件的重复包含和编译
#define __OLED_H
#include "SysTick.h"
//-----------------OLED IIC端口定义-------------------
//----------------- 接线 ------------------------
//----------------- C2 -> SCL-------------------------
//----------------- C3 -> SDA-------------------------
#define OLED_SCLK_Clr() GPIO_ResetBits(GPIOC,GPIO_Pin_2) //C2引脚置低
#define OLED_SCLK_Set() GPIO_SetBits(GPIOC,GPIO_Pin_2) //C2引脚置高
#define OLED_SDIN_Clr() GPIO_ResetBits(GPIOC,GPIO_Pin_3)
#define OLED_SDIN_Set() GPIO_SetBits(GPIOC,GPIO_Pin_3)
#define OLED_CMD 0 //写命令
#define OLED_DATA 1 //写数据
void OLED_Init(void); //函数声明
void OLED_Clear(void);
void OLED_WR_Byte(unsigned dat,unsigned cmd);
void Write_IIC_Data(unsigned char IIC_Data);
void Write_IIC_Command(unsigned char IIC_Command);
#endif