使用wire库的iic,oled驱动不是库,是51移植过来的,已验证可行。但使用eso8266的iic却无法点亮,不知道哪里出了问题。
以下是各文件代码,已通过编译。
oled.ino 代码
#include "Wire.h"
#include "oled.h"
#include "oledfont.h"
void Write_IIC_Command(unsigned char IIC_Command)
{
Wire.beginTransmission(OLED_ADDRESS);
Wire.write(0x00);
Wire.write(IIC_Command);
Wire.endTransmission();
}
void Write_IIC_Data(unsigned char IIC_Data)
{
Wire.beginTransmission(OLED_ADDRESS);
Wire.write(0x40);
Wire.write(IIC_Data);
Wire.endTransmission();
}
void OLED_WR_Byte(const unsigned char dat,unsigned char cmd)
{
if(cmd) {
Write_IIC_Data(dat);
}
else {
Write_IIC_Command(dat);
}
}
// ************************************************
void OLED_Set_Pos(unsigned char x, unsigned char y)
{ OLED_WR_Byte(0xb0+y,OLED_CMD);
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
OLED_WR_Byte((x&0x0f),OLED_CMD);
}
void OLED_Clear(void)
{
unsigned char 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);
} //更新显示
}
void OLED_ShowChar(unsigned char x,unsigned char y,unsigned char chr)
{
unsigned char c=0,i=0;
c=chr-' ';//得到偏移后的值
OLED_Set_Pos(x,y);
for(i=0;i<6;i++)
OLED_WR_Byte(F6x8[c][i],OLED_DATA);
}
//m^n函数
unsigned int oled_pow(unsigned char m,unsigned char n)
{
unsigned int result=1;
while(n--)result*=m;
return result;
}
//显示2个数字
//x,y :起点坐标
//len :数字的位数
//size:字体大小
//mode:模式 0,填充模式;1,叠加模式
//num:数值(0~4294967295);
void OLED_ShowNum(unsigned char x,unsigned char y,unsigned int num, unsigned char len)
{
unsigned char t,temp;
unsigned char enshow=0;
for(t=0;t++ )
{
temp=(num/oled_pow(10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
OLED_ShowChar(x+6*t,y,' ');
continue;
}else enshow=1;
}
OLED_ShowChar(x+6*t,y,temp+'0');
}
}
void OLEDInit()
{
Wire.begin(SDA_PIN, SCL_PIN);
OLED_WR_Byte(0xAE,OLED_CMD);//--display off
OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
OLED_WR_Byte(0x40,OLED_CMD);//--set start line address
OLED_WR_Byte(0xB0,OLED_CMD);//--set page address
OLED_WR_Byte(0x81,OLED_CMD); // contract control
OLED_WR_Byte(0xFF,OLED_CMD);//--128
OLED_WR_Byte(0xA1,OLED_CMD);//set segment remap
OLED_WR_Byte(0xA6,OLED_CMD);//--normal / reverse
OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
OLED_WR_Byte(0x3F,OLED_CMD);//--1/32 duty
OLED_WR_Byte(0xC8,OLED_CMD);//Com scan direction
OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset
OLED_WR_Byte(0x00,OLED_CMD);//
OLED_WR_Byte(0xD5,OLED_CMD);//set osc division
OLED_WR_Byte(0x80,OLED_CMD);//
OLED_WR_Byte(0xD8,OLED_CMD);//set area color mode off
OLED_WR_Byte(0x05,OLED_CMD);//
OLED_WR_Byte(0xD9,OLED_CMD);//Set Pre-Charge Period
OLED_WR_Byte(0xF1,OLED_CMD);//
OLED_WR_Byte(0xDA,OLED_CMD);//set com pin configuartion
OLED_WR_Byte(0x12,OLED_CMD);//
OLED_WR_Byte(0xDB,OLED_CMD);//set Vcomh
OLED_WR_Byte(0x30,OLED_CMD);//
OLED_WR_Byte(0x8D,OLED_CMD);//set charge pump enable
OLED_WR_Byte(0x14,OLED_CMD);//
OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
OLED_Clear();
}
oled.h 代码
```c++
#ifndef _oled_h_
#define _oled_h_
#ifdef __cplusplus
extern "C" {
#endif
#define OLED_CMD 0 //写命令
#define OLED_DATA 1 //写数据
#define OLED_ADDRESS (0x3c)
#define SDA_PIN 4
#define SCL_PIN 5
void OLEDInit();
void OLED_ShowChar(unsigned char x,unsigned char y,unsigned char chr);
void OLED_ShowNum(unsigned char x,unsigned char y,unsigned int num, unsigned char len);
#ifdef __cplusplus
}
#endif
#endif
main.ino
```c++
#include "oled.h"
void setup()
{
delay(10);
OLEDInit();
delay(10);
}
void loop()
{
// OLED_ShowNum(0, 0, 20, 2);
OLED_ShowChar(0, 1, 'c');
delay(100);
}
以下是可能导致问题的几个原因:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
Wire.begin(D2, D1); //SDA, SCL
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //I2C address
display.clearDisplay();
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Hello, world!");
display.display();
}
void loop() {
// put your main code here, to run repeatedly:
}
上面的代码使用了Adafruit_SSD1306库来控制OLED显示器,但是基于Wire库的IIC实现应该是相同的。请注意,代码中的Wire.begin(D2, D1)语句配置了SDA和SCL引脚,根据您的硬件配置,您需要将它们更改为正确的引脚号码。