开发软件:
Arduino IDE 1.6.7
材料:
Arduino 328p
0.96寸OLED模块
未使用库文件
【注:OLED驱动电压3.3V】
文件1:OLED_Main.ino
#define byte
unsigned char
#define word
unsigned int
#define dword unsigned long
void LCD_Init(void);
void LCD_CLS(void);
void LCD_P6x8Str(byte x,byte y,byte ch[]);
void LCD_P8x16Str(byte x,byte y,byte ch[]);
void LCD_P14x16Ch(byte x,byte y,byte N);
void LCD_Fill(byte dat);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
LCD_Init();
}
void loop() {
// put your main code here, to run repeatedly:
unsigned char i=0;
LCD_Fill(0xff);
delay(1000);
LCD_Fill(0x00);
delay(1000);
LCD_P14x16Ch(36,0,0); //龙
//
LCD_P8x16Str(48,4,(unsigned char *)"OLED");
LCD_P6x8Str(16,6,(unsigned char *)"chiusir@yahoo.cn");
LCD_P6x8Str(34,7,(unsigned char *)"2011-09-03");
delay(2000);
LCD_CLS();
delay(2000);
}
文件2:OLED_function.ino
const int LCD_SCL=5;
const int LCD_SDA=4;
const int LCD_RST=3;
const int LCD_DC =2;
#define X_WIDTH 128
#define Y_WIDTH 64
#define XLevelL
0x00
#define XLevelH
0x10
#define XLevel
((XLevelH&0x0F)*16+XLevelL)
#define Max_Column
128
#define Max_Row
64
const unsigned char F6x8[][6] =
{
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
// sp
{ 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00 },
// !
{ 0x00, 0x00, 0x07, 0x00, 0x07, 0x00 },
// "
{ 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14 },
// #
{ 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12 },
// $
{ 0x00, 0x62, 0x64, 0x08, 0x13, 0x23 },
// %
{ 0x00, 0x36, 0x49, 0x55, 0x22, 0x50 },
// &
{ 0x00, 0x00, 0x05, 0x03, 0x00, 0x00 },
// '
{ 0x00, 0x00, 0x1c, 0x22, 0x41, 0x00 },
// (
{ 0x00, 0x00, 0x41, 0x22, 0x1c, 0x00 },
// )
{ 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14 },
// *
{ 0x00, 0x08, 0x08, 0x3E, 0x08, 0x08 },
// +
{ 0x00, 0x00, 0x00, 0xA0, 0x60, 0x00 },
// ,
{ 0x00, 0x08, 0x08, 0x08, 0x08, 0x08 },
// -
{ 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 },
// .
{ 0x00, 0x20, 0x10, 0x08, 0x04, 0x02 },
// /
{ 0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E },
// 0
{ 0x00, 0x00, 0x42, 0x7F, 0x40, 0x00 },
// 1
{ 0x00, 0x42, 0x61, 0x51, 0x49, 0x46 },
// 2
{ 0x00, 0x21, 0x41, 0x45, 0x4B, 0x31 },
// 3
{ 0x00, 0x18, 0x14, 0x12, 0x7F, 0x10 },
// 4
{ 0x00, 0x27, 0x45, 0x45, 0x45, 0x39 },
// 5
{ 0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30 },
// 6
{ 0x00, 0x01, 0x71, 0x09, 0x05, 0x03 },
// 7
{ 0x00, 0x36, 0x49, 0x49, 0x49, 0x36 },
// 8
{ 0x00, 0x06, 0x49, 0x49, 0x29, 0x1E },
// 9
{ 0x00, 0x00, 0x36, 0x36, 0x00, 0x00 },
// :
{ 0x00, 0x00, 0x56, 0x36, 0x00, 0x00 },
// ;
{ 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 },
// <</div>
{ 0x00, 0x14, 0x14, 0x14, 0x14, 0x14 },
// =
{ 0x00, 0x00, 0x41, 0x22, 0x14, 0x08 },
// >
{ 0x00, 0x02, 0x01, 0x51, 0x09, 0x06 },
// ?
{ 0x00, 0x32, 0x49, 0x59, 0x51, 0x3E },
// @
{ 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C },
// A
{ 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36 },
// B
{ 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22 },
// C
{ 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C },
// D
{ 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41 },
// E
{ 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01 },
// F
{ 0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A },
// G
{ 0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F },
// H
{ 0x00, 0x00, 0x41, 0x7F, 0x41, 0x00 },
// I
{ 0x00, 0x20, 0x40, 0x41, 0x3F, 0x01 },
// J
{ 0x00, 0x7F, 0x08, 0x14, 0x22, 0x41 },
// K
{ 0x00, 0x7F, 0x40, 0x40, 0x40, 0x40 },
// L
{ 0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F },
// M
{ 0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F },
// N
{ 0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E },
// O
{ 0x00, 0x7F, 0x09, 0x09, 0x09, 0x06 },
// P
{ 0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E },
// Q
{ 0x00, 0x7F, 0x09, 0x19, 0x29, 0x46 },
// R
{ 0x00, 0x46, 0x49, 0x49, 0x49, 0x31 },
// S
{ 0x00, 0x01, 0x01, 0x7F, 0x01, 0x01 },
// T
{ 0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F },
// U
{ 0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F },
// V
{ 0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F },
// W
{ 0x00, 0x63, 0x14, 0x08, 0x14, 0x63 },
// X
{ 0x00, 0x07, 0x08, 0x70, 0x08, 0x07 },
// Y
{ 0x00, 0x61, 0x51, 0x49, 0x45, 0x43 },
// Z
{ 0x00, 0x00, 0x7F, 0x41, 0x41, 0x00 },
// [
{ 0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55 },
// 55
{ 0x00, 0x00, 0x41, 0x41, 0x7F, 0x00 },
// ]
{ 0x00, 0x04, 0x02, 0x01, 0x02, 0x04 },
// ^
{ 0x00, 0x40, 0x40, 0x40, 0x40, 0x40 },
// _
{ 0x00, 0x00, 0x01, 0x02, 0x04, 0x00 },
// '
{ 0x00, 0x20, 0x54, 0x54, 0x54, 0x78 },
// a
{ 0x00, 0x7F, 0x48, 0x44, 0x44, 0x38 },
// b
{ 0x00, 0x38, 0x44, 0x44, 0x44, 0x20 },
// c
{ 0x00, 0x38, 0x44, 0x44, 0x48, 0x7F },
// d
{ 0x00, 0x38, 0x54, 0x54, 0x54, 0x18 },
// e
{ 0x00, 0x08, 0x7E, 0x09, 0x01, 0x02 },
// f
{ 0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C },
// g
{ 0x00, 0x7F, 0x08, 0x04, 0x04, 0x78 },
// h
{ 0x00, 0x00, 0x44, 0x7D, 0x40, 0x00 },
// i
{ 0x00, 0x40, 0x80, 0x84, 0x7D, 0x00 },
// j
{ 0x00, 0x7F, 0x10, 0x28, 0x44, 0x00 },
// k
{ 0x00, 0x00, 0x41, 0x7F, 0x40, 0x00 },
// l
{ 0x00, 0x7C, 0x04, 0x18, 0x04, 0x78 },
// m
{ 0x00, 0x7C, 0x08, 0x04, 0x04, 0x78 },
// n
{ 0x00, 0x38, 0x44, 0x44, 0x44, 0x38 },
// o
{ 0x00, 0xFC, 0x24, 0x24, 0x24, 0x18 },
// p
{ 0x00, 0x18, 0x24, 0x24, 0x18, 0xFC },
// q
{ 0x00, 0x7C, 0x08, 0x04, 0x04, 0x08 },
// r
{ 0x00, 0x48, 0x54, 0x54, 0x54, 0x20 },
// s
{ 0x00, 0x04, 0x3F, 0x44, 0x40, 0x20 },
// t
{ 0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C },
// u
{ 0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C },
// v
{ 0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C },
// w
{ 0x00, 0x44, 0x28, 0x10, 0x28, 0x44 },
// x
{ 0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C },
// y
{ 0x00, 0x44, 0x64, 0x54, 0x4C, 0x44 },
// z
{ 0x14, 0x14, 0x14, 0x14, 0x14, 0x14 }
// horiz lines
};
//显示14x16大小的字
const unsigned char F14x16[] = {
0x10,0x10,0x10,0x10,0x10,0xFF,0x10,0xF0,0x12,0x1C,0xD0,0x10,0x10,0x10,
0x40,0x20,0x10,0x0C,0x03,0x10,0x08,0x3F,0x42,0x41,0x40,0x40,0x40,0x70//龙
};
void LCD_WrDat(unsigned char dat)// 写数据
{
unsigned char i=8;
//LCD_DC=1;;
digitalWrite(LCD_DC,HIGH);
digitalWrite(LCD_SCL,LOW);
// LCD_SCL=0;;
//;;
while(i--)
{
if(dat&0x80)
{
digitalWrite(LCD_SDA,HIGH);
}
else{digitalWrite(LCD_SDA,LOW);}
digitalWrite(LCD_SCL,HIGH);
digitalWrite(LCD_SCL,LOW);
dat<<=1;
}
}
void LCD_WrCmd(unsigned char cmd)//写指令
{
unsigned char i=8;
digitalWrite(LCD_DC,LOW);
digitalWrite(LCD_SCL,LOW);
while(i--)
{
if(cmd&0x80){digitalWrite(LCD_SDA,HIGH);}
else{digitalWrite(LCD_SDA,LOW);}
digitalWrite(LCD_SCL,HIGH);
digitalWrite(LCD_SCL,LOW);
cmd<<=1;
}
}
void LCD_Set_Pos(unsigned char x, unsigned char y)//设置绘图坐标
{
LCD_WrCmd(0xb0+y);
LCD_WrCmd(((x&0xf0)>>4)|0x10);
LCD_WrCmd((x&0x0f)|0x00);
}
void LCD_Fill(unsigned char bmp_dat)
{
unsigned char y,x;
for(y=0;y<8;y++)
{
LCD_WrCmd(0xb0+y);
LCD_WrCmd(0x01);
LCD_WrCmd(0x10);
for(x=0;x
LCD_WrDat(bmp_dat);
}
}
void LCD_CLS(void)//
{
unsigned char y,x;
for(y=0;y<8;y++)
{
LCD_WrCmd(0xb0+y);
LCD_WrCmd(0x01);
LCD_WrCmd(0x10);
for(x=0;x
LCD_WrDat(0);
}
}
void LCD_DLY_ms(unsigned int ms)//时间延迟
{
delay(ms);//直接调用arduino的延迟
}
void LCD_Init(void)
//对OLED进行初始化操作
{
digitalWrite(LCD_SCL,HIGH);
//LCD_CS=1; //预制SLK和SS为高电平
digitalWrite(LCD_RST,LOW);
LCD_DLY_ms(50);
digitalWrite(LCD_RST,HIGH);
//从上电到下面开始初始化要有足够的时间,即等待RC复位完毕
LCD_WrCmd(0xae);//--turn off oled panel
LCD_WrCmd(0x00);//---set low column address
LCD_WrCmd(0x10);//---set high column address
LCD_WrCmd(0x40);//--set start line address
Set Mapping RAM Display Start Line (0x00~0x3F)
LCD_WrCmd(0x81);//--set contrast control register
LCD_WrCmd(0xcf); // Set SEG Output Current Brightness
LCD_WrCmd(0xa1);//--Set SEG/Column Mapping
0xa0左右反置 0xa1正常
LCD_WrCmd(0xc8);//Set COM/Row Scan Direction
0xc0上下反置 0xc8正常
LCD_WrCmd(0xa6);//--set normal display
LCD_WrCmd(0xa8);//--set multiplex ratio(1 to 64)
LCD_WrCmd(0x3f);//--1/64 duty
LCD_WrCmd(0xd3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
LCD_WrCmd(0x00);//-not offset
LCD_WrCmd(0xd5);//--set display clock divide ratio/oscillator frequency
LCD_WrCmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
LCD_WrCmd(0xd9);//--set pre-charge period
LCD_WrCmd(0xf1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
LCD_WrCmd(0xda);//--set com pins hardware configuration
LCD_WrCmd(0x12);
LCD_WrCmd(0xdb);//--set vcomh
LCD_WrCmd(0x40);//Set VCOM Deselect Level
LCD_WrCmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
LCD_WrCmd(0x02);//
LCD_WrCmd(0x8d);//--set Charge Pump enable/disable
LCD_WrCmd(0x14);//--set(0x10) disable
LCD_WrCmd(0xa4);// Disable Entire Display On (0xa4/0xa5)
LCD_WrCmd(0xa6);// Disable Inverse Display On (0xa6/a7)
LCD_WrCmd(0xaf);//--turn on oled panel
LCD_Fill(0x00);
//初始清屏
LCD_Set_Pos(0,0);
}
//==============================================================
//函数名:LCD_P6x8Str(unsigned char x,unsigned char y,unsigned char *p)
//功能描述:写入一组标准ASCII字符串
//参数:显示的位置(x,y),y为页范围0?7,要显示的字符串
//返回:无
//==============================================================
void LCD_P6x8Str(unsigned char x,unsigned char y,unsigned char ch[])
{
unsigned char c=0,i=0,j=0;
while (ch[j]!='\0')
{
c =ch[j]-32;
if(x>126){x=0;y++;}
LCD_Set_Pos(x,y);
for(i=0;i<6;i++)
LCD_WrDat(F6x8[c][i]);
x+=6;
j++;
}
}
//==============================================================
//函数名:LCD_P8x16Str(unsigned char x,unsigned char y,unsigned char *p)
//功能描述:写入一组标准ASCII字符串
//参数:显示的位置(x,y),y为页范围0?7,要显示的字符串
//返回:无
//==============================================================
// 因为328p内存不足而注释
void LCD_P14x16Ch(unsigned char x,unsigned char y,unsigned char N)//输出汉字字符串
{
unsigned char wm=0,ii = 0;
unsigned int adder=28*N;
LCD_Set_Pos(x , y);
for(wm = 0;wm < 14;wm++)
{
LCD_WrDat(F14x16[adder]);
adder += 1;
}
LCD_Set_Pos(x,y + 1);
for(wm = 0;wm < 14;wm++)
{
LCD_WrDat(F14x16[adder]);
adder += 1;
}
}
后记:
一开始看到PCB上面标有IIC的接口还以为是IIC的标准件,结果看文档发现没有明确提及到IIC地址的时候,就感觉不太对劲了。
连接5V需要在所有讯号线路上面串联1K的电阻。