滋生_ 2023-03-05 17:59 采纳率: 14.3%
浏览 24

esp8266 iic 无法点亮OLED

使用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<len;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);
}
  • 写回答

2条回答 默认 最新

  • 路漫漫其修远. 博客专家认证 2023-03-05 18:59
    关注

    以下是可能导致问题的几个原因:

    • 引脚配置:请确保您的IIC引脚配置正确,特别是SDA和SCL引脚。ESP8266的引脚分配可以在您的开发板的数据手册中找到。请确保将SDA引脚连接到OLED屏幕的SDA引脚,将SCL引脚连接到OLED屏幕的SCL引脚。
    • 电源和电压:确保您的OLED屏幕已正确连接到供电电源,并且所需的电压和电流已得到满足。如果电压和电流不足,可能会导致OLED显示不正确。
    • 代码实现:请确保您的代码正确实现了IIC总线,并且没有其他问题。您可以尝试使用一个简单的IIC示例代码,例如将ESP8266连接到I2C总线上的示例代码。如果示例代码可以正常工作,则可以将其修改为驱动OLED显示器。
      以下是一个使用ESP8266的Wire库控制OLED显示器的示例代码:
    #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引脚,根据您的硬件配置,您需要将它们更改为正确的引脚号码。

    评论

报告相同问题?

问题事件

  • 创建了问题 3月5日

悬赏问题

  • ¥15 出现报错Debug Assertion Failed,如何解决?
  • ¥50 mcf中怎么实现导入的图片变成透明
  • ¥15 ruoyi-flowable流程设计配置的表单时,级联选择如何配置
  • ¥20 金属玻璃的剪切局部化程度怎么用ovito表示出来
  • ¥15 自定义控件在中文模式下不能输入数字
  • ¥15 关于#运维#的问题:用mail.abc.com 端口9696的方式同样能访问hr.abc.com 端口:6080 的页面两个网址都指向同一个外网ip(相关搜索:服务器)
  • ¥15 xgboost建模输出结果由三分类变成四分类
  • ¥15 Windows X86 远线程注入问题解惑
  • ¥15 Vs2022安装时黑框闪退无反应
  • ¥15 嵌入式设备网口down后再up时不能link?