之前arduinoUNOR3可以正常使用换到ESP32C3之后就不能用了orz附上代码
- /**
- * @author dangel
- * @email d_angel5044@outlook.com
- * @create date 2022-04-23 14:32:56
- * @modify date 2022-04-23 14:32:57
- * @desc [description]
- */
- #include <SPI.h>
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #include <Adafruit_Sensor.h>
- #include <DHT.h>
- #include <DHT_U.h>
- #include <DS3231.h>
- #define SCREEN_WIDTH 128
- #define SCREEN_HEIGHT 64
- #define OLED_RESET -1
- #define SCREEN_ADDRESS 0x3c
- #define CHAR_WIDTH 16
- #define CHAR_HEIGHT 16
- #define DHTPIN 2
- #define DHTTYPE DHT11
- #define SDA 28
- #define SCL 27
- // #define LedPin 13
- // #define ButtonPin 7
- int16_t POSX = 0;
- int16_t POSY = 0;
- static const uint8_t PROGMEM CHAR_SHIJIAN_32x16[] =
- {
- 0x00,0x08,0x00,0x08,0x7C,0x08,0x44,0x08,0x45,0xFE,0x44,0x08,0x44,0x08,0x7C,0x08,
- 0x44,0x88,0x44,0x48,0x44,0x48,0x44,0x08,0x7C,0x08,0x44,0x08,0x00,0x28,0x00,0x10,//"时"0
- 0x20,0x00,0x13,0xFC,0x10,0x04,0x40,0x04,0x47,0xC4,0x44,0x44,0x44,0x44,0x44,0x44,
- 0x47,0xC4,0x44,0x44,0x44,0x44,0x44,0x44,0x47,0xC4,0x40,0x04,0x40,0x14,0x40,0x08//"间"1
- };
- static const uint8_t PROGMEM CHAR_WENDU_16x16[] =
- {
- 0x00,0x00,0x23,0xF8,0x12,0x08,0x12,0x08,0x83,0xF8,0x42,0x08,0x42,0x08,0x13,0xF8,
- 0x10,0x00,0x27,0xFC,0xE4,0xA4,0x24,0xA4,0x24,0xA4,0x24,0xA4,0x2F,0xFE,0x00,0x00,//"温"0
- 0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,0x22,0x20,0x3F,0xFC,0x22,0x20,0x22,0x20,
- 0x23,0xE0,0x20,0x00,0x2F,0xF0,0x24,0x10,0x42,0x20,0x41,0xC0,0x86,0x30,0x38,0x0E,//"度"1
- };
- static const uint8_t PROGMEM CHAR_SHIDU_16x16[] =
- {
- 0x00,0x00,0x27,0xF8,0x14,0x08,0x14,0x08,0x87,0xF8,0x44,0x08,0x44,0x08,0x17,0xF8,
- 0x11,0x20,0x21,0x20,0xE9,0x24,0x25,0x28,0x23,0x30,0x21,0x20,0x2F,0xFE,0x00,0x00,//"湿"0
- 0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,0x22,0x20,0x3F,0xFC,0x22,0x20,0x22,0x20,
- 0x23,0xE0,0x20,0x00,0x2F,0xF0,0x24,0x10,0x42,0x20,0x41,0xC0,0x86,0x30,0x38,0x0E,//"度"1
- };
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- DHT_Unified dht(DHTPIN,DHTTYPE);
- RTClib myRTC;
- uint32_t Sensor_Read_Delay;
- float temperature,humidity;
- uint8_t RENDER_COUNT;
- uint8_t READ_COUNT;
- // bool BUTTON_STATE;
- // bool LED_STATE;
- void Draw_CH(uint8_t* tar_chars,int char_num);
- bool Read_Sensor();
- void Render_sence_1();
- // void Read_Button();
- void setup(){
- Serial.begin(9600);
- // pinMode(ButtonPin,INPUT_PULLUP);
- // pinMode(LedPin,OUTPUT);
- // LED_STATE = 0;
- // digitalWrite(LedPin,LED_STATE);
- //Wire.begin(28,27);
- if(!display.begin(SSD1306_SWITCHCAPVCC,SCREEN_ADDRESS)){
- Serial.println(F("SSD1306 allocation failed"));
- }
- display.clearDisplay();
- display.setTextSize(2);
- display.setTextColor(SSD1306_WHITE);
- display.setCursor(0,0);
- display.print(F("START!"));
- display.display();
- delay(500);
- dht.begin();
- sensor_t sensor;
- dht.temperature().getSensor(&sensor);
- Wire.begin();
- Sensor_Read_Delay = sensor.min_delay/1000;
- Serial.println(F("START!"));
- }
- void loop() {
- if(READ_COUNT == 20){
- Read_Sensor();
- READ_COUNT = 0;
- }
- if(RENDER_COUNT == 10){
- Render_sence_1();
- RENDER_COUNT = 0;
- }
- // Read_Button();
- RENDER_COUNT++;
- READ_COUNT++;
- delay(50);
- }
- void Render_sence_1(){
- //RTC part
- DateTime now = myRTC.now();
- //Render part
- POSX = 0;
- POSY = 0;
- display.clearDisplay();
- Draw_CH(CHAR_SHIJIAN_32x16,2);
- display.setCursor(32,POSY);
- display.setTextSize(1);
- //Render date
- display.print(now.year());
- display.print(F("."));
- if(now.month()<10)display.print(F("0"));
- display.print(now.month());
- display.print(F("."));
- if(now.day()<10)display.print(F("0"));
- display.print(now.day());
- //Render time
- display.setCursor(32,8);
- if(now.hour()>12){
- display.print(F("PM"));
- if(now.hour()-12<10)display.print(F("0"));
- display.print(now.hour()-12);
- }else{
- display.print(F("AM"));
- if(now.hour()<10)display.print(F("0"));
- display.print(now.hour());
- }
- display.print(F(":"));
- if(now.minute()<10)display.print(F("0"));
- display.print(now.minute());
- display.print(F(":"));
- if(now.second()<10)display.print(F("0"));
- display.print(now.second());
- POSY += CHAR_HEIGHT;
- display.setTextSize(2);
- Draw_CH(CHAR_WENDU_16x16,2);
- display.setCursor(32,POSY);
- display.print(temperature);display.print(F("C"));
- POSY += CHAR_HEIGHT;
- Draw_CH(CHAR_SHIDU_16x16,2);
- display.setCursor(32,POSY);
- display.print(humidity);display.print(F("%"));
- POSY += CHAR_HEIGHT;
- display.display();
- };
- void Draw_CH(const uint8_t* tar_chars,int char_num){
- uint8_t t[32];
- for (int i = 0; i < char_num; i++)
- {
- for (int j = 0; j<32; j++)
- {
- t[j] = pgm_read_byte(&tar_chars[j+32*i]);
- }
- display.drawBitmap(POSX,POSY,t,CHAR_WIDTH,CHAR_HEIGHT,WHITE);
- POSX += CHAR_WIDTH;
- if((i+1)%8==0){
- POSX = 0;
- POSY += CHAR_HEIGHT;
- }
- }
- // POSY += CHAR_HEIGHT;
- POSX = 0;
- }
- // void Read_Button(){
- // BUTTON_STATE = digitalRead(ButtonPin);
- // if(BUTTON_STATE == LOW){
- // delay(20);
- // if(BUTTON_STATE == LOW){
- // LED_STATE = !LED_STATE;
- // digitalWrite(LedPin,LED_STATE);
- // Serial.println("click");
- // }
- // }
- // delay(200);
- // }
- bool Read_Sensor(){
- sensors_event_t event;
- dht.temperature().getEvent(&event);
- if(isnan(event.temperature)){
- temperature = 99.99;
- return 0;
- }else{
- temperature = event.temperature;
- }
- dht.humidity().getEvent(&event);
- if(isnan(event.relative_humidity)){
- humidity = 99.99;
- return 0;
- }else{
- humidity = event.relative_humidity;
- }
- return 1;
- }
复制代码
|