前言:
之前购买了一款SSD1306驱动的0.96inchOLED显示屏幕,采用ESP8266使用u8g2库成功驱动。如今想自己摸索一下如何利用I2C驱动SSD1306.
I2C协议
I2C开始 SCL为高电平期间,SDA由高到低
void IIC_START()
{
IIC_OUT();
IIC_SCL_H;
IIC_SDA_H;
delay_us(1);
IIC_SDA_L;
delay_us(3);
IIC_SCL_L;
}
I2C停止 SCL为高电平期间,SDA由低到高
void IIC_STOP()
{
IIC_OUT();
IIC_SCL_L;
IIC_SDA_L;
delay_us(1);
IIC_SCL_H;
delay_us(3);
IIC_SDA_H;
delay_us(3);
}
I2C发送ACK
void IIC_ACK()
{
IIC_SCL_L;
IIC_OUT();
IIC_SDA_L;
delay_us(3);
IIC_SCL_H;
delay_us(3);
IIC_SCL_L;
}
I2C发送NACK
void IIC_NACK()
{
IIC_SCL_L;
IIC_OUT();
IIC_SDA_H;
delay_us(3);
IIC_SCL_H;
delay_us(3);
IIC_SCL_L;
}
I2C等待ACK
u8 IIC_WAIT_ACK(void)
{
int time;
IIC_IN();
IIC_SDA_H;
delay_us(3);
IIC_SCL_H;
delay_us(3);
while(GPIO_ReadInputDataBit(IIC_SDA,IIC_SDA_PIN)==Bit_SET)
{
time++;
if(time>2500){IIC_STOP();return 0;}
}
IIC_SCL_L;
return 1;
}
I2C发送一个bit
void IIC_SEND_BIT(u8 bit)
{
IIC_OUT();
IIC_SCL_L;
if(bit){
IIC_SDA_H;
}else{
IIC_SDA_L;
}
IIC_SCL_H;
delay_us(3);
IIC_SCL_L;
}
I2C发送一个字节
void IIC_SEND_BYTE(u8 byte)
{
u8 i=0;
for(i=0;i<8;i++){
IIC_SEND_BIT(byte>>(7-i)&0x01);
}
IIC_SDA_H;
delay_us(1);
IIC_SCL_H;
delay_us(3);
IIC_SCL_L;
}
I2C接受一个字节
u8 IIC_RECEIVE_BYTE(u8 ack)
{
u8 temp,i;
IIC_IN();
for(i=0;i<8;i++)
{
IIC_SCL_L;
delay_us(1);
IIC_SCL_H;
temp<<=1;
if(GPIO_ReadInputDataBit(IIC_SDA,IIC_SDA_PIN)==Bit_SET)temp&=0x01;
delay_us(3);
}
if(!ack)
IIC_NACK();
else
IIC_ACK();
return temp;
}
SSD1306
初始化指令
const unsigned char OLED_init_cmd[25]=
{
0xAE,
0xD5,
0x80,
0xA8,
0X3F,
0xD3,
0X00,
0x40,
0x8D,
0x14,
0x20,
0x02,
0xA1,
0xC8,
0xDA,
0x12,
0x81,
0xEF,
0xD9,
0xf1,
0xDB,
0x30,
0xA4,
0xA6,
0xAF,
};
- SSD1306发送指令
void OLED_send_cmd(unsigned char cmd)
{
IIC_START();
IIC_SEND_BYTE(OLED_ADR);
IIC_SEND_BYTE(0x00);
IIC_SEND_BYTE(cmd);
IIC_STOP();
}
- SSD1306发送数据
void OLED_send_data(unsigned char data)
{
IIC_START();
IIC_SEND_BYTE(OLED_ADR);
IIC_SEND_BYTE(0x40);
IIC_SEND_BYTE(data);
IIC_STOP();
}
- SSD1306设置GDDRAM模式
void OLED_set_gddram_mode(u8 mode)
{
OLED_send_cmd(0x20);
OLED_send_cmd(mode);
}
- SSD1306设置对比度
void OLED_set_contrast(unsigned char constrast)
{
OLED_send_cmd(0x81);
OLED_send_cmd(constrast);
}
- 水平模式 垂直模式的页寻址和列寻址
void Column_set_VR(u8 Column_Begin,u8 Column_End)
{
if(Column_Begin>127||Column_End>127||Column_Begin>Column_End)return;
OLED_send_cmd(0x21);
OLED_send_cmd(Column_Begin);
OLED_send_cmd(Column_End);
}
void Page_set_VR(u8 Page_Begin,u8 Page_End)
{
if(Page_Begin>7||Page_End>127||Page_Begin>Page_End)return;
OLED_send_cmd(0x22);
OLED_send_cmd(Page_Begin);
OLED_send_cmd(Page_End);
}
- 页模式的页寻址以及列寻址
void Column_set(unsigned char column)
{
OLED_send_cmd(0x10|(column>>4));
OLED_send_cmd(0x00|(column&0x0f));
}
void Page_set(unsigned char page)
{
OLED_send_cmd(0xB0+page);
}
SSD1306驱动源代码
#include "ssd1306.h"
#include "iic.h"
#include "stm32f10x.h"
#include "font.h"
#define OLED_ADR 0X78
const unsigned char ascii[][16]={
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xCC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x08,0x00,0x30,0x00,0x40,0x00,0x08,0x00,0x30,0x00,0x40,0x00,0x00,0x00},
{0x00,0x00,0x02,0x20,0x03,0xFC,0x1E,0x20,0x02,0x20,0x03,0xFC,0x1E,0x20,0x00,0x00},
{0x00,0x00,0x0E,0x18,0x11,0x04,0x11,0x04,0x3F,0xFF,0x10,0x84,0x0C,0x78,0x00,0x00},
{0x0F,0x00,0x10,0x8C,0x0F,0x30,0x01,0xC0,0x06,0x78,0x18,0x84,0x00,0x78,0x00,0x00},
{0x00,0x78,0x0F,0x84,0x10,0xC4,0x11,0x34,0x0E,0x98,0x00,0xE4,0x00,0x84,0x00,0x08},
{0x00,0x00,0x48,0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xE0,0x18,0x18,0x20,0x04,0x40,0x02,0x00,0x00},
{0x00,0x00,0x40,0x02,0x20,0x04,0x18,0x18,0x07,0xE0,0x00,0x00,0x00,0x00,0x00,0x00},
{0x02,0x40,0x02,0x40,0x01,0x80,0x0F,0xF0,0x01,0x80,0x02,0x40,0x02,0x40,0x00,0x00},
{0x00,0x00,0x00,0x80,0x00,0x80,0x00,0x80,0x07,0xF0,0x00,0x80,0x00,0x80,0x00,0x80},
{0x00,0x00,0x00,0x09,0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x00},
{0x00,0x00,0x00,0x0C,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x06,0x00,0x18,0x00,0xE0,0x03,0x00,0x1C,0x00,0x20,0x00,0x00,0x00},
{0x00,0x00,0x07,0xF0,0x08,0x08,0x10,0x04,0x10,0x04,0x08,0x08,0x07,0xF0,0x00,0x00},
{0x00,0x00,0x00,0x00,0x08,0x04,0x08,0x04,0x1F,0xFC,0x00,0x04,0x00,0x04,0x00,0x00},
{0x00,0x00,0x0E,0x0C,0x10,0x14,0x10,0x24,0x10,0x44,0x10,0x84,0x0F,0x0C,0x00,0x00},
{0x00,0x00,0x0C,0x18,0x10,0x04,0x10,0x84,0x10,0x84,0x11,0x44,0x0E,0x38,0x00,0x00},
{0x00,0x00,0x00,0x60,0x01,0xA0,0x02,0x24,0x0C,0x24,0x1F,0xFC,0x00,0x24,0x00,0x24},
{0x00,0x00,0x1F,0x98,0x11,0x04,0x11,0x04,0x11,0x04,0x10,0x88,0x10,0x70,0x00,0x00},
{0x00,0x00,0x07,0xF0,0x08,0x88,0x11,0x04,0x11,0x04,0x09,0x04,0x00,0xF8,0x00,0x00},
{0x00,0x00,0x18,0x00,0x10,0x00,0x10,0x7C,0x11,0x80,0x16,0x00,0x18,0x00,0x00,0x00},
{0x00,0x00,0x0E,0x38,0x11,0x44,0x10,0x84,0x10,0x84,0x11,0x44,0x0E,0x38,0x00,0x00},
{0x00,0x00,0x0F,0x80,0x10,0x48,0x10,0x44,0x10,0x44,0x08,0x88,0x07,0xF0,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x0C,0x03,0x0C,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x80,0x01,0x40,0x02,0x20,0x04,0x10,0x08,0x08,0x10,0x04,0x00,0x00},
{0x00,0x00,0x02,0x40,0x02,0x40,0x02,0x40,0x02,0x40,0x02,0x40,0x02,0x40,0x00,0x00},
{0x00,0x00,0x10,0x04,0x08,0x08,0x04,0x10,0x02,0x20,0x01,0x40,0x00,0x80,0x00,0x00},
{0x00,0x00,0x0E,0x00,0x12,0x00,0x10,0x0C,0x10,0xEC,0x11,0x00,0x0E,0x00,0x00,0x00},
{0x03,0xE0,0x0C,0x18,0x13,0xE4,0x14,0x14,0x17,0xF4,0x08,0x14,0x07,0xE8,0x00,0x00},
{0x00,0x04,0x00,0x3C,0x03,0xC4,0x1C,0x40,0x07,0x40,0x00,0xE4,0x00,0x1C,0x00,0x04},
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x04,0x11,0x04,0x0E,0x88,0x00,0x70,0x00,0x00},
{0x03,0xE0,0x0C,0x18,0x10,0x04,0x10,0x04,0x10,0x04,0x10,0x08,0x1C,0x10,0x00,0x00},
{0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x04,0x10,0x04,0x08,0x08,0x07,0xF0,0x00,0x00},
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x04,0x17,0xC4,0x10,0x04,0x08,0x18,0x00,0x00},
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x00,0x17,0xC0,0x10,0x00,0x08,0x00,0x00,0x00},
{0x03,0xE0,0x0C,0x18,0x10,0x04,0x10,0x04,0x10,0x44,0x1C,0x78,0x00,0x40,0x00,0x00},
{0x10,0x04,0x1F,0xFC,0x10,0x84,0x00,0x80,0x00,0x80,0x10,0x84,0x1F,0xFC,0x10,0x04},
{0x00,0x00,0x10,0x04,0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x04,0x00,0x00,0x00,0x00},
{0x00,0x03,0x00,0x01,0x10,0x01,0x10,0x01,0x1F,0xFE,0x10,0x00,0x10,0x00,0x00,0x00},
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x03,0x80,0x14,0x64,0x18,0x1C,0x10,0x04,0x00,0x00},
{0x10,0x04,0x1F,0xFC,0x10,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x0C,0x00,0x00},
{0x10,0x04,0x1F,0xFC,0x1F,0x80,0x00,0x7C,0x1F,0x80,0x1F,0xFC,0x10,0x04,0x00,0x00},
{0x10,0x04,0x1F,0xFC,0x0C,0x04,0x03,0x00,0x00,0xE0,0x10,0x18,0x1F,0xFC,0x10,0x00},
{0x07,0xF0,0x08,0x08,0x10,0x04,0x10,0x04,0x10,0x04,0x08,0x08,0x07,0xF0,0x00,0x00},
{0x10,0x04,0x1F,0xFC,0x10,0x84,0x10,0x80,0x10,0x80,0x10,0x80,0x0F,0x00,0x00,0x00},
{0x07,0xF0,0x08,0x08,0x10,0x14,0x10,0x14,0x10,0x0C,0x08,0x0A,0x07,0xF2,0x00,0x00},
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x00,0x11,0xC0,0x11,0x30,0x0E,0x0C,0x00,0x04},
{0x00,0x00,0x0E,0x1C,0x11,0x04,0x10,0x84,0x10,0x84,0x10,0x44,0x1C,0x38,0x00,0x00},
{0x18,0x00,0x10,0x00,0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x00,0x18,0x00,0x00,0x00},
{0x10,0x00,0x1F,0xF8,0x10,0x04,0x00,0x04,0x00,0x04,0x10,0x04,0x1F,0xF8,0x10,0x00},
{0x10,0x00,0x1E,0x00,0x11,0xE0,0x00,0x1C,0x00,0x70,0x13,0x80,0x1C,0x00,0x10,0x00},
{0x10,0x00,0x1F,0xC0,0x00,0x7C,0x1F,0x80,0x00,0x7C,0x1F,0xC0,0x10,0x00,0x00,0x00},
{0x10,0x04,0x18,0x0C,0x16,0x34,0x01,0xC0,0x01,0xC0,0x16,0x34,0x18,0x0C,0x10,0x04},
{0x10,0x00,0x1C,0x00,0x13,0x04,0x00,0xFC,0x13,0x04,0x1C,0x00,0x10,0x00,0x00,0x00},
{0x08,0x04,0x10,0x1C,0x10,0x64,0x10,0x84,0x13,0x04,0x1C,0x04,0x10,0x18,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xFE,0x40,0x02,0x40,0x02,0x40,0x02,0x00,0x00},
{0x00,0x00,0x20,0x00,0x1C,0x00,0x03,0x80,0x00,0x60,0x00,0x1C,0x00,0x03,0x00,0x00},
{0x00,0x00,0x40,0x02,0x40,0x02,0x40,0x02,0x7F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x20,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x00,0x00,0x00,0x00},
{0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01},
{0x00,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x98,0x01,0x24,0x01,0x24,0x01,0x48,0x00,0xFC,0x00,0x04,0x00,0x00},
{0x08,0x00,0x0F,0xFC,0x00,0x88,0x01,0x04,0x01,0x04,0x00,0x88,0x00,0x70,0x00,0x00},
{0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x01,0x04,0x00,0x88,0x00,0x00},
{0x00,0x00,0x00,0xF8,0x01,0x04,0x01,0x04,0x01,0x04,0x09,0x08,0x0F,0xFC,0x00,0x04},
{0x00,0x00,0x00,0xF8,0x01,0x24,0x01,0x24,0x01,0x24,0x01,0x24,0x00,0xE8,0x00,0x00},
{0x00,0x00,0x01,0x04,0x01,0x04,0x07,0xFC,0x09,0x04,0x09,0x04,0x04,0x00,0x00,0x00},
{0x00,0x00,0x00,0xD6,0x01,0x29,0x01,0x29,0x01,0x29,0x01,0xC9,0x01,0x06,0x00,0x00},
{0x08,0x04,0x0F,0xFC,0x00,0x84,0x01,0x00,0x01,0x00,0x01,0x04,0x00,0xFC,0x00,0x04},
{0x00,0x00,0x01,0x04,0x19,0x04,0x19,0xFC,0x00,0x04,0x00,0x04,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x03,0x00,0x01,0x01,0x01,0x19,0x01,0x19,0xFE,0x00,0x00,0x00,0x00},
{0x08,0x04,0x0F,0xFC,0x00,0x24,0x00,0x60,0x01,0x94,0x01,0x0C,0x01,0x04,0x00,0x00},
{0x00,0x00,0x08,0x04,0x08,0x04,0x1F,0xFC,0x00,0x04,0x00,0x04,0x00,0x00,0x00,0x00},
{0x01,0x04,0x01,0xFC,0x01,0x04,0x01,0x00,0x01,0xFC,0x01,0x04,0x01,0x00,0x00,0xFC},
{0x01,0x04,0x01,0xFC,0x00,0x84,0x01,0x00,0x01,0x00,0x01,0x04,0x00,0xFC,0x00,0x04},
{0x00,0x00,0x00,0xF8,0x01,0x04,0x01,0x04,0x01,0x04,0x01,0x04,0x00,0xF8,0x00,0x00},
{0x01,0x01,0x01,0xFF,0x00,0x89,0x01,0x04,0x01,0x04,0x00,0x88,0x00,0x70,0x00,0x00},
{0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x00,0x89,0x01,0xFF,0x00,0x01},
{0x01,0x04,0x01,0x04,0x01,0xFC,0x00,0x84,0x01,0x04,0x01,0x00,0x01,0x80,0x00,0x00},
{0x00,0x00,0x00,0xCC,0x01,0x24,0x01,0x24,0x01,0x24,0x01,0x24,0x01,0x98,0x00,0x00},
{0x00,0x00,0x01,0x00,0x01,0x00,0x07,0xF8,0x01,0x04,0x01,0x04,0x00,0x08,0x00,0x00},
{0x01,0x00,0x01,0xF8,0x00,0x04,0x00,0x04,0x00,0x04,0x01,0x08,0x01,0xFC,0x00,0x04},
{0x01,0x00,0x01,0xC0,0x01,0x30,0x00,0x0C,0x01,0x30,0x01,0xC0,0x01,0x00,0x00,0x00},
{0x01,0x80,0x01,0x70,0x00,0x0C,0x01,0x30,0x01,0xE0,0x00,0x1C,0x01,0x60,0x01,0x80},
{0x00,0x00,0x01,0x04,0x01,0x8C,0x01,0x70,0x00,0x74,0x01,0x8C,0x01,0x04,0x00,0x00},
{0x01,0x00,0x01,0x81,0x01,0x61,0x00,0x1E,0x00,0x18,0x01,0x60,0x01,0x80,0x01,0x00},
{0x00,0x00,0x01,0x84,0x01,0x0C,0x01,0x34,0x01,0x44,0x01,0x84,0x01,0x0C,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x3F,0x7C,0x40,0x02,0x40,0x02},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},
{0x40,0x02,0x40,0x02,0x3F,0x7C,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x40,0x00,0x80,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x40,0x00,0x00,0x00},
}
unsigned char GDDRAM[8][128]={0};
const unsigned char OLED_init_cmd[25]=
{
0xAE,
0xD5,
0x80,
0xA8,
0X3F,
0xD3,
0X00,
0x40,
0x8D,
0x14,
0x20,
0x02,
0xA1,
0xC8,
0xDA,
0x12,
0x81,
0xEF,
0xD9,
0xf1,
0xDB,
0x30,
0xA4,
0xA6,
0xAF,
};
void OLED_init(void)
{
unsigned char i;
IIC_init();
for(i=0;i<25;i++)
{
OLED_send_cmd(OLED_init_cmd[i]);
}
}
void OLED_set_gddram_mode(u8 mode)
{
OLED_send_cmd(0x20);
OLED_send_cmd(mode);
}
void Column_set(unsigned char column)
{
OLED_send_cmd(0x10|(column>>4));
OLED_send_cmd(0x00|(column&0x0f));
}
void Page_set(unsigned char page)
{
OLED_send_cmd(0xB0+page);
}
void Column_set_VR(u8 Column_Begin,u8 Column_End)
{
OLED_send_cmd(0x21);
OLED_send_cmd(Column_Begin);
OLED_send_cmd(Column_End);
}
void Page_set_VR(u8 Page_Begin,u8 Page_End)
{
OLED_send_cmd(0x22);
OLED_send_cmd(Page_Begin);
OLED_send_cmd(Page_End);
}
void OLED_send_cmd(unsigned char cmd)
{
IIC_START();
IIC_SEND_BYTE(OLED_ADR);
IIC_SEND_BYTE(0x00);
IIC_SEND_BYTE(cmd);
IIC_STOP();
}
void OLED_send_data(unsigned char data)
{
IIC_START();
IIC_SEND_BYTE(OLED_ADR);
IIC_SEND_BYTE(0x40);
IIC_SEND_BYTE(data);
IIC_STOP();
}
void OLED_set_contrast(unsigned char constrast)
{
OLED_send_cmd(0x81);
OLED_send_cmd(constrast);
}
void OLED_full_OLED(void)
{
unsigned char page,column;
OLED_set_gddram_mode(0X02);
for(page=0;page<8;page++)
{
Page_set(page);
Column_set(0);
for(column=0;column<128;column++)
{
OLED_send_data(0xff);
GDDRAM[page][column]=0XFF;
}
}
}
void OLED_clean_OLED(void)
{
unsigned char page,column;
OLED_set_gddram_mode(0X02);
for(page=0;page<8;page++)
{
Page_set(page);
Column_set(0);
for(column=0;column<128;column++)
{
OLED_send_data(0x00);
GDDRAM[page][column]=0X00;
}
}
}
void OLED_OFF(void)
{
OLED_send_cmd(0xAE);
}
void OLED_ON(void)
{
OLED_send_cmd(0xAF);
}
void OLED_reverse_All(unsigned char mode)
{
if(mode){
OLED_send_cmd(0xA6);
}else{
OLED_send_cmd(0xA7);
}
}
void OLED_reverse_Part(unsigned char x,unsigned char y,unsigned char width,unsigned char height)
{
unsigned char i,j;
for(i=x;i<x+width;i++){
for(j=y;j<y+height;j++){
if(OLED_getP(i,j)){OLED_resetP(i,j);}else{OLED_setP(i,j);}
}
}
}
void OLED_clean(void)
{
unsigned char i,j;
for(i=0;i<128;i++){
for(j=0;j<64;j++){
GDDRAM[j][i]=0;
}
}
}
void OLED_full(void)
{
unsigned char i,j;
for(i=0;i<128;i++){
for(j=0;j<64;j++){
GDDRAM[j][i]=0xff;
}
}
}
void OLED_setP(unsigned char x,unsigned char y)
{
if(x>128||y>64)return;
GDDRAM[y/8][x]|=(0x01<<(y%8));
}
void OLED_resetP(unsigned char x,unsigned char y)
{
if(x>128||y>64)return;
GDDRAM[y/8][x]&=~(0x01<<(y%8));
}
unsigned char OLED_getP(unsigned char x,unsigned char y)
{
if(x>128||y>64)return 0;
return (GDDRAM[y/8][x]>>(y%8))&0x01;
}
void OLED_display(void)
{
unsigned char i,j;
OLED_set_gddram_mode(2);
for(i=0;i<8;i++){
Page_set(i);
Column_set(0);
for(j=0;j<128;j++){
OLED_send_data(GDDRAM[i][j]);
}
}
}
void OLED_drawXbm(unsigned char x,unsigned char y,const unsigned char*data,unsigned char width,unsigned char height)
{
unsigned char i,j;
unsigned short offset=0;
for(i=x;i<x+width;i++){
for(j=y;j<y+height;j++){
if(((*data)>>(7-offset%8))&0x01){
OLED_setP(i,j);
}else{
OLED_resetP(i,j);
}
offset++;
if(offset%8==0){
data++;
}
}
}
}
void OLED_drawFrame(unsigned char data[8][128])
{
unsigned char i,j;
OLED_set_gddram_mode(0);
Column_set_VR(0,127);
Page_set_VR(0,7);
for(i=0;i<8;i++){
for(j=0;j<128;j++){
OLED_send_data(data[i][j]);
}
}
}
void OLED_drawChar(unsigned char x,unsigned char y,const char data,unsigned char width,unsigned char height)
{
unsigned char i,j,offset=0,index=0;
const unsigned char *p;
index=(int)data;
if(index>126||index<32)return;
p=&ascii[index-32][0];
for(i=x;i<x+width;i++){
for(j=y;j<y+height;j++){
if((*p>>(7-offset%8))&0x01){
OLED_setP(i,j);
}else{
OLED_resetP(i,j);
}
offset++;
if(offset%8==0){
p++;
}
}
}
}
void OLED_drawString(unsigned char x,unsigned char y,const char *data,unsigned char width,unsigned char height)
{
while(*data!=0){
OLED_drawChar(x,y,*data,width,height);
x+=width;
data++;
}
}
void OLED_drawP(unsigned char x,unsigned char y)
{
if(x>128||y>64)return;
OLED_set_gddram_mode(0X02);
Page_set(y/8);
Column_set(x);
GDDRAM[y/8][x]|=(0x01<<(y%8));
OLED_send_data(GDDRAM[y/8][x]);
}
void OLED_drawL_V(unsigned char X_B,unsigned char Y_B,unsigned char height)
{
u8 Y;
for(Y=Y_B;Y<Y_B+height;Y++)
{
OLED_setP(X_B,Y);
}
}
void OLED_drawL_R(unsigned char X_B,unsigned char Y_B,unsigned char width)
{
u8 X;
for(X=X_B;X<X_B+width;X++)
{
OLED_setP(X,Y_B);
}
}
void OLED_drawRect_H(unsigned char xpos,unsigned char ypos,unsigned char iwidth,unsigned char iheight)
{
OLED_drawL_R(xpos,ypos,iwidth);
OLED_drawL_R(xpos,ypos+iheight-1,iwidth);
OLED_drawL_V(xpos,ypos,iheight);
OLED_drawL_V(xpos+iwidth-1,ypos,iheight);
}
void OLED_drawRect_S(unsigned char xpos,unsigned char ypos,unsigned char iwidth,unsigned char iheight)
{
u8 i,j;
for(i=xpos;i<xpos+iwidth;i++){
for(j=ypos;j<ypos+iheight;j++){
OLED_setP(i,j);
}
}
}
void OLED_drawRectdotO(unsigned char xpos,unsigned char ypos,unsigned char iwidth,unsigned char iheight,unsigned char density){
unsigned char i;
for(i=xpos+2;i<xpos+iwidth-2;i+=density){
OLED_setP(i, ypos+2);
}
for(i=xpos+2;i<xpos+iwidth-2;i+=density){
OLED_setP(i, ypos+iheight-3);
}
for(i=ypos+2;i<ypos+iheight-2;i+=density){
OLED_setP(xpos+2, i);
}
for(i=ypos+2;i<ypos+iheight-2;i+=density){
OLED_setP(xpos+iwidth-3, i);
}
OLED_drawRect_H(xpos,ypos,iwidth,iheight);
}
void OLED_drawRectdotI(unsigned char xpos,unsigned char ypos,unsigned char iwidth,unsigned char iheight,unsigned char density){
unsigned char i;
for(i=xpos;i<xpos+iwidth;i+=density){
OLED_setP(i, ypos);
}
for(i=xpos;i<xpos+iwidth;i+=density){
OLED_setP(i, ypos+iheight-1);
}
for(i=ypos;i<ypos+iheight;i+=density){
OLED_setP(xpos, i);
}
for(i=ypos;i<ypos+iheight;i+=density){
OLED_setP(xpos+iwidth-1, i);
}
OLED_drawRect_H(xpos+2,ypos+2,iwidth-4,iheight-4);
}
I2C驱动源代码
#include"iic.h"
#include"delay.h"
#include "stm32f10x.h"
#define IIC_CLOCK RCC_APB2Periph_GPIOA
#define IIC_SDA GPIOA
#define IIC_SCL GPIOA
#define IIC_SDA_PIN GPIO_Pin_5
#define IIC_SCL_PIN GPIO_Pin_7
#define IIC_SDA_H GPIO_SetBits(IIC_SDA,IIC_SDA_PIN)
#define IIC_SDA_L GPIO_ResetBits(IIC_SDA,IIC_SDA_PIN)
#define IIC_SCL_H GPIO_SetBits(IIC_SCL,IIC_SCL_PIN)
#define IIC_SCL_L GPIO_ResetBits(IIC_SCL,IIC_SCL_PIN)
void IIC_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(IIC_CLOCK,ENABLE);
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=IIC_SDA_PIN;
GPIO_Init(IIC_SDA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=IIC_SCL_PIN;
GPIO_Init(IIC_SCL,&GPIO_InitStructure);
IIC_OUT();
IIC_SCL_H;
IIC_SDA_H;
}
void IIC_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=IIC_SDA_PIN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(IIC_SDA,&GPIO_InitStructure);
}
void IIC_IN(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=IIC_SDA_PIN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Init(IIC_SDA,&GPIO_InitStructure);
}
void IIC_START()
{
IIC_OUT();
IIC_SCL_H;
IIC_SDA_H;
delay_us(1);
IIC_SDA_L;
delay_us(3);
IIC_SCL_L;
}
void IIC_STOP()
{
IIC_OUT();
IIC_SCL_L;
IIC_SDA_L;
delay_us(1);
IIC_SCL_H;
delay_us(3);
IIC_SDA_H;
delay_us(3);
}
void IIC_ACK()
{
IIC_SCL_L;
IIC_OUT();
IIC_SDA_L;
delay_us(3);
IIC_SCL_H;
delay_us(3);
IIC_SCL_L;
}
void IIC_NACK()
{
IIC_SCL_L;
IIC_OUT();
IIC_SDA_H;
delay_us(3);
IIC_SCL_H;
delay_us(3);
IIC_SCL_L;
}
u8 IIC_WAIT_ACK(void)
{
int time;
IIC_IN();
IIC_SDA_H;
delay_us(3);
IIC_SCL_H;
delay_us(3);
while(GPIO_ReadInputDataBit(IIC_SDA,IIC_SDA_PIN)==Bit_SET)
{
time++;
if(time>2500){IIC_STOP();return 0;}
}
IIC_SCL_L;
return 1;
}
void IIC_SEND_BIT(u8 bit)
{
IIC_OUT();
IIC_SCL_L;
if(bit){
IIC_SDA_H;
}else{
IIC_SDA_L;
}
IIC_SCL_H;
delay_us(3);
IIC_SCL_L;
}
void IIC_SEND_BYTE(u8 byte)
{
u8 i=0;
for(i=0;i<8;i++){
IIC_SEND_BIT(byte>>(7-i)&0x01);
}
IIC_SDA_H;
delay_us(1);
IIC_SCL_H;
delay_us(3);
IIC_SCL_L;
}
u8 IIC_RECEIVE_BYTE(u8 ack)
{
u8 temp,i;
IIC_IN();
for(i=0;i<8;i++)
{
IIC_SCL_L;
delay_us(1);
IIC_SCL_H;
temp<<=1;
if(GPIO_ReadInputDataBit(IIC_SDA,IIC_SDA_PIN)==Bit_SET)temp&=0x01;
delay_us(3);
}
if(!ack)
IIC_NACK();
else
IIC_ACK();
return temp;
}
|