ESP32C3使用i2c控制ssd1306驱动的128*64屏幕失败,求大佬指教-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 387|回复: 2

[未解决] ESP32C3使用i2c控制ssd1306驱动的128*64屏幕失败,求大佬指教

[复制链接]
发表于 2022-4-23 16:11 | 显示全部楼层 |阅读模式
之前arduinoUNOR3可以正常使用换到ESP32C3之后就不能用了orz附上代码
  1. /**
  2. * @author dangel
  3. * @email d_angel5044@outlook.com
  4. * @create date 2022-04-23 14:32:56
  5. * @modify date 2022-04-23 14:32:57
  6. * @desc [description]
  7. */

  8. #include <SPI.h>
  9. #include <Wire.h>
  10. #include <Adafruit_GFX.h>
  11. #include <Adafruit_SSD1306.h>
  12. #include <Adafruit_Sensor.h>
  13. #include <DHT.h>
  14. #include <DHT_U.h>
  15. #include <DS3231.h>

  16. #define SCREEN_WIDTH  128
  17. #define SCREEN_HEIGHT 64
  18. #define OLED_RESET -1
  19. #define SCREEN_ADDRESS 0x3c
  20. #define CHAR_WIDTH 16
  21. #define CHAR_HEIGHT 16


  22. #define DHTPIN 2
  23. #define DHTTYPE DHT11

  24. #define SDA 28
  25. #define SCL 27

  26. // #define LedPin 13
  27. // #define ButtonPin 7


  28. int16_t POSX = 0;
  29. int16_t POSY = 0;

  30. static const uint8_t PROGMEM CHAR_SHIJIAN_32x16[] =
  31. {
  32. 0x00,0x08,0x00,0x08,0x7C,0x08,0x44,0x08,0x45,0xFE,0x44,0x08,0x44,0x08,0x7C,0x08,
  33. 0x44,0x88,0x44,0x48,0x44,0x48,0x44,0x08,0x7C,0x08,0x44,0x08,0x00,0x28,0x00,0x10,//"时"0
  34. 0x20,0x00,0x13,0xFC,0x10,0x04,0x40,0x04,0x47,0xC4,0x44,0x44,0x44,0x44,0x44,0x44,
  35. 0x47,0xC4,0x44,0x44,0x44,0x44,0x44,0x44,0x47,0xC4,0x40,0x04,0x40,0x14,0x40,0x08//"间"1
  36. };


  37. static const uint8_t PROGMEM CHAR_WENDU_16x16[] =
  38. {
  39. 0x00,0x00,0x23,0xF8,0x12,0x08,0x12,0x08,0x83,0xF8,0x42,0x08,0x42,0x08,0x13,0xF8,
  40. 0x10,0x00,0x27,0xFC,0xE4,0xA4,0x24,0xA4,0x24,0xA4,0x24,0xA4,0x2F,0xFE,0x00,0x00,//"温"0
  41. 0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,0x22,0x20,0x3F,0xFC,0x22,0x20,0x22,0x20,
  42. 0x23,0xE0,0x20,0x00,0x2F,0xF0,0x24,0x10,0x42,0x20,0x41,0xC0,0x86,0x30,0x38,0x0E,//"度"1
  43. };

  44. static const uint8_t PROGMEM CHAR_SHIDU_16x16[] =
  45. {
  46. 0x00,0x00,0x27,0xF8,0x14,0x08,0x14,0x08,0x87,0xF8,0x44,0x08,0x44,0x08,0x17,0xF8,
  47. 0x11,0x20,0x21,0x20,0xE9,0x24,0x25,0x28,0x23,0x30,0x21,0x20,0x2F,0xFE,0x00,0x00,//"湿"0
  48. 0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,0x22,0x20,0x3F,0xFC,0x22,0x20,0x22,0x20,
  49. 0x23,0xE0,0x20,0x00,0x2F,0xF0,0x24,0x10,0x42,0x20,0x41,0xC0,0x86,0x30,0x38,0x0E,//"度"1
  50. };

  51. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  52. DHT_Unified dht(DHTPIN,DHTTYPE);

  53. RTClib myRTC;

  54. uint32_t Sensor_Read_Delay;
  55. float temperature,humidity;
  56. uint8_t RENDER_COUNT;
  57. uint8_t READ_COUNT;

  58. // bool BUTTON_STATE;
  59. // bool LED_STATE;

  60. void Draw_CH(uint8_t* tar_chars,int char_num);
  61. bool Read_Sensor();
  62. void Render_sence_1();
  63. // void Read_Button();

  64. void setup(){
  65.     Serial.begin(9600);

  66.     // pinMode(ButtonPin,INPUT_PULLUP);
  67.     // pinMode(LedPin,OUTPUT);
  68.     // LED_STATE = 0;
  69.     // digitalWrite(LedPin,LED_STATE);

  70.     //Wire.begin(28,27);

  71.     if(!display.begin(SSD1306_SWITCHCAPVCC,SCREEN_ADDRESS)){
  72.         Serial.println(F("SSD1306 allocation failed"));
  73.     }
  74.     display.clearDisplay();
  75.     display.setTextSize(2);
  76.     display.setTextColor(SSD1306_WHITE);
  77.     display.setCursor(0,0);
  78.     display.print(F("START!"));
  79.     display.display();
  80.     delay(500);
  81.     dht.begin();
  82.     sensor_t sensor;
  83.     dht.temperature().getSensor(&sensor);
  84.     Wire.begin();
  85.     Sensor_Read_Delay = sensor.min_delay/1000;
  86.     Serial.println(F("START!"));
  87. }

  88. void loop() {
  89.     if(READ_COUNT == 20){
  90.         Read_Sensor();
  91.         READ_COUNT = 0;
  92.     }

  93.     if(RENDER_COUNT == 10){
  94.         Render_sence_1();
  95.         RENDER_COUNT = 0;
  96.     }

  97.     // Read_Button();

  98.     RENDER_COUNT++;
  99.     READ_COUNT++;
  100.     delay(50);
  101. }

  102. void Render_sence_1(){

  103.     //RTC part
  104.     DateTime now = myRTC.now();

  105.     //Render part
  106.     POSX = 0;
  107.     POSY = 0;
  108.     display.clearDisplay();

  109.     Draw_CH(CHAR_SHIJIAN_32x16,2);
  110.     display.setCursor(32,POSY);
  111.     display.setTextSize(1);

  112.     //Render date
  113.     display.print(now.year());
  114.     display.print(F("."));
  115.     if(now.month()<10)display.print(F("0"));
  116.     display.print(now.month());
  117.     display.print(F("."));
  118.     if(now.day()<10)display.print(F("0"));
  119.     display.print(now.day());

  120.     //Render time
  121.     display.setCursor(32,8);
  122.     if(now.hour()>12){
  123.         display.print(F("PM"));
  124.         if(now.hour()-12<10)display.print(F("0"));
  125.         display.print(now.hour()-12);
  126.     }else{
  127.         display.print(F("AM"));
  128.         if(now.hour()<10)display.print(F("0"));
  129.         display.print(now.hour());
  130.     }
  131.     display.print(F(":"));
  132.     if(now.minute()<10)display.print(F("0"));
  133.     display.print(now.minute());
  134.     display.print(F(":"));
  135.     if(now.second()<10)display.print(F("0"));
  136.     display.print(now.second());

  137.     POSY += CHAR_HEIGHT;
  138.     display.setTextSize(2);
  139.     Draw_CH(CHAR_WENDU_16x16,2);
  140.     display.setCursor(32,POSY);
  141.     display.print(temperature);display.print(F("C"));
  142.     POSY += CHAR_HEIGHT;

  143.     Draw_CH(CHAR_SHIDU_16x16,2);
  144.     display.setCursor(32,POSY);
  145.     display.print(humidity);display.print(F("%"));
  146.     POSY += CHAR_HEIGHT;

  147.     display.display();


  148. };

  149. void Draw_CH(const uint8_t* tar_chars,int char_num){
  150.     uint8_t t[32];
  151.     for (int i = 0; i < char_num; i++)
  152.     {
  153.         for (int j = 0; j<32; j++)
  154.         {
  155.             t[j] = pgm_read_byte(&tar_chars[j+32*i]);
  156.         }
  157.         display.drawBitmap(POSX,POSY,t,CHAR_WIDTH,CHAR_HEIGHT,WHITE);
  158.         POSX += CHAR_WIDTH;
  159.         if((i+1)%8==0){
  160.             POSX = 0;
  161.             POSY += CHAR_HEIGHT;
  162.         }
  163.     }
  164.     // POSY += CHAR_HEIGHT;
  165.     POSX = 0;
  166. }

  167. // void Read_Button(){
  168. //     BUTTON_STATE = digitalRead(ButtonPin);
  169. //     if(BUTTON_STATE == LOW){
  170. //         delay(20);
  171. //         if(BUTTON_STATE == LOW){
  172. //             LED_STATE = !LED_STATE;
  173. //             digitalWrite(LedPin,LED_STATE);
  174. //             Serial.println("click");
  175. //         }
  176. //     }
  177. //     delay(200);
  178. // }

  179. bool Read_Sensor(){
  180.     sensors_event_t event;
  181.     dht.temperature().getEvent(&event);
  182.     if(isnan(event.temperature)){
  183.         temperature = 99.99;
  184.         return 0;
  185.     }else{
  186.         temperature = event.temperature;
  187.     }
  188.     dht.humidity().getEvent(&event);
  189.     if(isnan(event.relative_humidity)){
  190.         humidity = 99.99;
  191.         return 0;
  192.     }else{
  193.         humidity = event.relative_humidity;
  194.     }
  195.     return 1;
  196. }
复制代码


发表于 2022-5-4 12:13 | 显示全部楼层
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define display_SCREEN_WIDTH 128
#define display_SCREEN_HEIGHT 64
#define display_OLED_RESET     4
Adafruit_SSD1306 display(display_SCREEN_WIDTH, display_SCREEN_HEIGHT, &Wire, display_OLED_RESET);

void setup(){
  Serial.begin(9600);
  while (!(display.begin(SSD1306_SWITCHCAPVCC,0x3C))) {
    Serial.println("没有搜索到SSD1306");
    delay(500);
  }
}

void loop(){

}

adafruit的库中的显示屏不要“#define SDA 28#define SCL 27”
发表于 2022-5-4 12:53 | 显示全部楼层
Wire.setPins()
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 07:47 , Processed in 0.086946 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表