1. 程式人生 > >u8g2庫的相關資料

u8g2庫的相關資料

hms default pro 顯示屏 .org case read one images

鏈接arduino中文社區---傳送門

我的資料備份

  1 #include <Arduino.h>
  2 #include <SPI.h>
  3 #include <U8g2lib.h>
  4 #include <dht11.h>
  5 dht11 DHT11;
  6 #define DHT11PIN A2
  7 //U8G2_SSD1306_128X64_NONAME_1_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 4, /* data=*/ 5, /* cs=*/ 3, /* dc=*/ 6, /* reset=*/ 7);
//多腳顯示屏 8 U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ A4, /* data=*/ A5, /* reset=*/ U8X8_PIN_NONE); // 四角顯示屏 9 double Fahrenheit(double celsius) 10 { 11 return 1.8 * celsius + 32; 12 } //攝氏溫度度轉化為華氏溫度 13 14 double Kelvin(double celsius) 15 { 16 return celsius + 273.15; 17 } //
攝氏溫度轉化為開氏溫度 18 19 // 露點(點在此溫度時,空氣飽和並產生露珠) 20 // 參考: http://wahiduddin.net/calc/density_algorithms.htm 21 double dewPoint(double celsius, double humidity) 22 { 23 double A0= 373.15/(273.15 + celsius); 24 double SUM = -7.90298 * (A0-1); 25 SUM += 5.02808 * log10(A0); 26 SUM += -1.3816e-7 * (pow(10, (11.344*(1
-1/A0)))-1) ; 27 SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; 28 SUM += log10(1013.246); 29 double VP = pow(10, SUM-3) * humidity; 30 double T = log(VP/0.61078); // temp var 31 return (241.88 * T) / (17.558-T); 32 } 33 34 // 快速計算露點,速度是5倍dewPoint() 35 // 參考: http://en.wikipedia.org/wiki/Dew_point 36 double dewPointFast(double celsius, double humidity) 37 { 38 double a = 17.271; 39 double b = 237.7; 40 double temp = (a * celsius) / (b + celsius) + log(humidity/100); 41 double Td = (b * temp) / (a - temp); 42 return Td; 43 } 44 void setup(void) { 45 u8g2.begin(); //選擇U8G2模式,或者U8X8模式 46 Serial.begin(9600); 47 Serial.println("DHT11 TEST PROGRAM "); 48 Serial.print("LIBRARY VERSION: "); 49 Serial.println(DHT11LIB_VERSION); 50 Serial.println(); 51 } 52 53 void loop(void) { 54 u8g2.firstPage(); 55 do { 56 u8g2.setFont(u8g2_font_5x7_tr); //設置字體//font_ncenB14_tr 57 u8g2.setCursor(0, 15); //設置光標處 58 u8g2.print("H:"); //輸出內容 59 u8g2.setCursor(0,30); //設置光標處 60 u8g2.print("T:"); //輸出內容 61 } while ( u8g2.nextPage() ); 62 63 64 65 66 67 Serial.println("\n"); 68 69 int chk = DHT11.read(DHT11PIN); 70 71 Serial.print("Read sensor: "); 72 switch (chk) 73 { 74 case DHTLIB_OK: 75 Serial.println("OK"); 76 break; 77 case DHTLIB_ERROR_CHECKSUM: 78 Serial.println("Checksum error"); 79 break; 80 case DHTLIB_ERROR_TIMEOUT: 81 Serial.println("Time out error"); 82 break; 83 default: 84 Serial.println("Unknown error"); 85 break; 86 } 87 88 Serial.print("Humidity (%): "); 89 Serial.println((float)DHT11.humidity, 2); 90 91 Serial.print("Temperature (oC): "); 92 Serial.println((float)DHT11.temperature, 2); 93 94 Serial.print("Temperature (oF): "); 95 Serial.println(Fahrenheit(DHT11.temperature), 2); 96 97 Serial.print("Temperature (K): "); 98 Serial.println(Kelvin(DHT11.temperature), 2); 99 100 Serial.print("Dew Point (oC): "); 101 Serial.println(dewPoint(DHT11.temperature, DHT11.humidity)); 102 103 Serial.print("Dew PointFast (oC): "); 104 Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity)); 105 do { 106 u8g2.setFont(u8g2_font_5x7_tr); //設置字體 107 u8g2.setCursor(40,15); //設置光標處 108 u8g2.print((float)DHT11.humidity); //輸出內容 109 u8g2.setCursor(40,30); //設置光標處 110 u8g2.print((float)DHT11.temperature); //輸出內容 111 } while ( u8g2.nextPage() ); 112 delay(2000); 113 }

顯示屏買了兩次才發現4線的最好用,因為利用的IO口比較少,節省資源易操作。

技術分享圖片

u8g2庫的相關資料