亚洲春色中文字幕久久久-三上亚,一吻二脱三床四吻胸,国产真实伦对白视频全集,在线毛片观看,精品成品入口黄网,国产毛aⅴ片久久久,亚洲AV色香蕉一区二区三区老师,萧皇后A级艳片,色情日本视频更新,99久久亚洲精品日本无码

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索

esp8266+lcd1602四線驅動接法(arduino程序)

查看數: 5748 | 評論數: 4 | 收藏 8
關燈 | 提示:支持鍵盤翻頁<-左 右->
    組圖打開中,請稍候......
發布時間: 2022-8-3 20:39

正文摘要:

硬件 esp8266開發板(ch340g)其它也差不多lcd1602 代碼(lcd1602四線驅動接法,省線,速度不如八線驅動) # include <LiquidCrystal.h> // 對應gpio5,4,0,2,14,12口,5 ----> rs,4 ----> en,d4-d7 ...

回復

ID:956038 發表于 2026-6-18 23:00
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define  ComMode    0x52  //4COM,1/3bias  1000    010 1001  0  
#define  RCosc      0x30  //內部RC振蕩器(上電默認)1000 0011 0000
#define  LCD_on     0x06  //打開LCD 偏壓發生器1000     0000 0 11 0
#define  LCD_off    0x04  //關閉LCD顯示
#define  Sys_en     0x02  //系統振蕩器開 1000   0000 0010
#define  CTRl_cmd   0x80  //寫控制命令
#define  Data_cmd   0xa0  //寫數據命令

const char* ssid     = "Xiaomi_56FB";
const char* password = "zhang8114";

// 東八區,一次設置,后面不再額外加時區
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "ntp.aliyun.com", 8*3600, 1000);

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

#define HT_CS    D6
#define HT_WR    D7
#define HT_DATA  D8

byte segCode[] = {0xA0,0xF8,0x90,0xB8,0xC8,0x28,0x20,0xF8,0x00,0x08};
int year, month, day, week, hour, minute, second;

// --- HT1621 底層 ---
void htSendBit(byte dat){
  digitalWrite(HT_WR, LOW);
  digitalWrite(HT_DATA, dat);
  delayMicroseconds(1);
  digitalWrite(HT_WR, HIGH);
  delayMicroseconds(1);
}
void htSendByte(byte dat, byte len){
  for(byte i=0;i<len;i++){
    htSendBit(dat & 0x80);
    dat <<= 1;
  }
}
void htWriteCmd(byte cmd){
  digitalWrite(HT_CS, LOW);
  htSendByte(0x00,4);
  htSendByte(cmd,8);
  digitalWrite(HT_CS, HIGH);
}
void htWriteRam(byte addr, byte dat){
  digitalWrite(HT_CS, LOW);
  htSendByte(0xA0,4);
  htSendByte(addr<<2,6);
  htSendByte(dat,8);
  digitalWrite(HT_CS, HIGH);
}
void LCDoff(void)
{  
  htWriteCmd(LCD_off);  
}


void LCDon(void)
{  
  htWriteCmd(LCD_on);  
}
void ht1621Init(){
  pinMode(HT_DATA, OUTPUT);
  pinMode(HT_WR, OUTPUT);
  pinMode(HT_CS, OUTPUT);
  LCDoff();
  htWriteCmd(Sys_en);
  htWriteCmd(RCosc);   
  htWriteCmd(ComMode);
  htWriteCmd(LCD_on );   
  LCDon();
}
void htShowNum(byte addr, byte num){
  if(num>9) num=0;
  htWriteRam(addr, segCode[num]);
}
void htDispTime(){
  htShowNum(0, hour/10);
  htShowNum(1, hour%10);
  htShowNum(2, minute/10);
  htShowNum(3, minute%10);
  htShowNum(4, second/10);
  htShowNum(5, second%10);
}

// --- 時間解析(去掉了重復的時區偏移) ---
void parseTimeFromUnix(unsigned long epoch) {
  // 直接用timeClient給的epoch,不再+8*3600
  unsigned long secInDay = epoch % 86400;
  hour = secInDay / 3600;
  minute = (secInDay % 3600) / 60;
  second = secInDay % 60;

  unsigned long days = epoch / 86400;
  week = (days + 4) % 7; // 1970-01-01是周四,這里直接算對

  year = 1970;
  while(true) {
    unsigned long daysInYear = 365;
    if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) daysInYear = 366;
    if(days < daysInYear) break;
    days -= daysInYear;
    year++;
  }

  const int DAYS_PER_MONTH[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  month = 0;
  while(true) {
    int daysInMonth = DAYS_PER_MONTH[month];
    if(month == 1 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) daysInMonth = 29;
    if(days < daysInMonth) break;
    days -= daysInMonth;
    month++;
  }
  month += 1;
  day = days + 1;
}

// --- OLED顯示 ---
void oledDispTime(){
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print(year);display.print("-");
  display.print(month);display.print("-");
  display.println(day);
  display.setCursor(0,16);
  display.print("Week:");display.println(week);
  display.setTextSize(2);
  display.setCursor(0,32);
  if(hour<10) display.print("0");
  display.print(hour);display.print(":");
  if(minute<10) display.print("0");
  display.print(minute);display.print(":");
  if(second<10) display.print("0");
  display.println(second);
  display.display();
}

void setup() {
  Serial.begin(9600);
  ht1621Init();
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)){ while(1); }
  display.clearDisplay();
  display.display();
  WiFi.begin(ssid,password);
  while(WiFi.status() != WL_CONNECTED) delay(300);
  timeClient.begin();
}

void loop() {
  timeClient.update();
  parseTimeFromUnix(timeClient.getEpochTime());
  oledDispTime();
  htDispTime();
  delay(500);
}
ID:956038 發表于 2026-6-18 22:57
如何用ESP8266+HT1621B顯示時間,我一直都點不亮
ID:830316 發表于 2023-12-31 13:35
時間哪里沒有實時更新呢,博主
ID:598987 發表于 2022-8-4 20:48
  • 附上代碼二,在上面的基礎上。添加 心知天氣 (api申請)的顯示,增加一點實用性
  • 主要代碼來自于 太極創客,
    1. /**********************************************************************
    2.   項目名稱/Project          : 零基礎入門學用物聯網
    3.   程序名稱/Program name     : weather_now
    4.   團隊/Team                : 太極創客團隊 / Taichi-Maker
    5.   作者/Author              : CYNO朔
    6.   日期/Date(YYYYMMDD)     : 20200602
    7.   程序目的/Purpose          :
    8.   通過心知天氣免費服務獲取實時天氣信息。
    9.   -----------------------------------------------------------------------
    10.   其它說明 / Other Description
    11.   心知天氣API文檔說明:

    12.   本程序為太極創客團隊制作的免費視頻教程《零基礎入門學用物聯網 》中一部分。該教程系統的
    13.   向您講述ESP8266的物聯網應用相關的軟件和硬件知識。
    14. ***********************************************************************/
    15. #include <ArduinoJson.h>
    16. #include <ESP8266WiFi.h>
    17. #include <LiquidCrystal.h>

    18. const char* ssid     = "bonfire";       // 連接WiFi名(此處使用taichi-maker為示例)
    19. // 請將您需要連接的WiFi名填入引號中
    20. const char* password = "1234567800";          // 連接WiFi密碼(此處使用12345678為示例)
    21. // 請將您需要連接的WiFi密碼填入引號中

    22. const char* host = "api.知心天氣的服務器";     // 將要連接的服務器地址
    23. const int httpPort = 80;                    // 將要連接的服務器端口

    24. // 心知天氣HTTP請求所需信息
    25. String reqUserKey = "xxxxxxxxxxxxxx";   // 私鑰
    26. String reqLocation = "22.98486:114.7199";  // 城市
    27. String reqUnit = "c";                      // 攝氏/華氏

    28. LiquidCrystal lcd(5, 4, 0, 2, 14, 12);  // 實例化lcd驅動

    29. void setup() {
    30.   lcd.begin(16, 2); //設置行列
    31.   lcd.print("Hello Joie ^v^");//打印信息
    32.   Serial.begin(9600);
    33.   Serial.println("");

    34.   // 連接WiFi
    35.   connectWiFi();
    36. }

    37. void loop() {
    38.   // 建立心知天氣API當前天氣請求資源地址
    39.   String reqRes = "/v3/weather/now.json?key=" + reqUserKey +
    40.                   + "&location=" + reqLocation +
    41.                   "&language=en&unit=" + reqUnit;

    42.   // 向心知天氣服務器服務器請求信息并對信息進行解析
    43.   httpRequest(reqRes);
    44.   delay(3000);

    45. //  lcd.clear();
    46. //  lcd.setCursor(0, 1); //設置光標位置
    47. //  lcd.print("time:");
    48. //  lcd.print(millis() / 1000); //計算運行時間
    49. }

    50. // 向心知天氣服務器服務器請求信息并對信息進行解析
    51. void httpRequest(String reqRes) {
    52.   WiFiClient client;

    53.   // 建立http請求信息
    54.   String httpRequest = String("GET ") + reqRes + " HTTP/1.1\r\n" +
    55.                        "Host: " + host + "\r\n" +
    56.                        "Connection: close\r\n\r\n";
    57.   Serial.println("");
    58.   Serial.print("Connecting to "); Serial.print(host);

    59.   // 嘗試連接服務器
    60.   if (client.connect(host, 80)) {
    61.     Serial.println(" Success!");

    62.     // 向服務器發送http請求信息
    63.     client.print(httpRequest);
    64.     Serial.println("Sending request: ");
    65.     Serial.println(httpRequest);

    66.     // 獲取并顯示服務器響應狀態行
    67.     String status_response = client.readStringUntil('\n');
    68.     Serial.print("status_response: ");
    69.     Serial.println(status_response);

    70.     // 使用find跳過HTTP響應頭
    71.     if (client.find("\r\n\r\n")) {
    72.       Serial.println("Found Header End. Start Parsing.");
    73.     }

    74.     // 利用ArduinoJson庫解析心知天氣響應信息
    75.     parseInfo(client);
    76.   } else {
    77.     Serial.println(" connection failed!");
    78.   }
    79.   //斷開客戶端與服務器連接工作
    80.   client.stop();
    81. }

    82. // 連接WiFi
    83. void connectWiFi() {
    84.   WiFi.begin(ssid, password);                  // 啟動網絡連接
    85.   Serial.print("Connecting to ");              // 串口監視器輸出網絡連接信息
    86.   Serial.print(ssid); Serial.println(" ...");  // 告知用戶NodeMCU正在嘗試WiFi連接

    87.   int i = 0;                                   // 這一段程序語句用于檢查WiFi是否連接成功
    88.   while (WiFi.status() != WL_CONNECTED) {      // WiFi.status()函數的返回值是由NodeMCU的WiFi連接狀態所決定的。
    89.     delay(1000);                               // 如果WiFi連接成功則返回值為WL_CONNECTED
    90.     Serial.print(i++); Serial.print(' ');      // 此處通過While循環讓NodeMCU每隔一秒鐘檢查一次WiFi.status()函數返回值
    91.     lcd.setCursor(0,1);
    92.     lcd.print(i++);
    93.   }                                            // 同時NodeMCU將通過串口監視器輸出連接時長讀秒。
    94.   // 這個讀秒是通過變量i每隔一秒自加1來實現的。
    95.   Serial.println("");                          // WiFi連接成功后
    96.   Serial.println("Connection established!");   // NodeMCU將通過串口監視器輸出"連接成功"信息。
    97.   Serial.print("IP address:    ");             // 同時還將輸出NodeMCU的IP地址。這一功能是通過調用
    98.   Serial.println(WiFi.localIP());              // WiFi.localIP()函數來實現的。該函數的返回值即NodeMCU的IP地址。
    99. }

    100. // 利用ArduinoJson庫解析心知天氣響應信息
    101. void parseInfo(WiFiClient client) {
    102.   const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + 2 * JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6) + 230;
    103.   DynamicJsonDocument doc(capacity);

    104.   deserializeJson(doc, client);

    105.   JsonObject results_0 = doc["results"][0];

    106.   JsonObject results_0_now = results_0["now"];
    107.   const char* results_0_now_text = results_0_now["text"]; // "Sunny"
    108.   const char* results_0_now_code = results_0_now["code"]; // "0"
    109.   const char* results_0_now_temperature = results_0_now["temperature"]; // "32"

    110.   const char* results_0_last_update = results_0["last_update"]; // "2020-06-02T14:40:00+08:00"

    111.   // 通過串口監視器顯示以上信息
    112.   String results_0_now_text_str = results_0_now["text"].as<String>();
    113.   int results_0_now_code_int = results_0_now["code"].as<int>();
    114.   String results_0_now_temperature_str = results_0_now["temperature"].as<String>();

    115.   String results_0_last_update_str = results_0["last_update"].as<String>();

    116.   Serial.println(F("======Weahter Now======="));
    117.   Serial.print(F("Weather Now: "));
    118.   Serial.print(results_0_now_text_str);
    119.   Serial.print(F(" "));
    120.   Serial.println(results_0_now_code_int);
    121.   Serial.print(F("Temperature: "));
    122.   Serial.print(results_0_now_temperature_str);
    123.   Serial.println(F(" C"));
    124.   Serial.print(F("Last Update: "));
    125.   Serial.println(results_0_last_update_str);
    126.   Serial.println(F("========================"));

    127.   // 設置lcd1602顯示信息
    128.   lcd.clear();
    129. //  delay(1000);
    130.   lcd.setCursor(0, 0);
    131.   lcd.print(results_0_now_text_str + " " + results_0_now_temperature_str + "c");
    132.   lcd.setCursor(0, 1);
    133.   lcd.print(results_0_last_update_str);
    134. }
    復制代碼


評分

參與人數 1黑幣 +50 收起 理由
admin + 50 回帖助人的獎勵!

查看全部評分

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表