极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 20651|回复: 1

【教程】mega2560+2.8 LCD显示时间和温湿度

[复制链接]
发表于 2014-9-27 15:08:59 | 显示全部楼层 |阅读模式

之前我们介绍过如何使用2.8寸TFT LCD触摸屏扩展板的教程,但是uno的引脚都被占用了,没法加其他模块。今天我们会介绍使用TFT LCD屏来显示时间和温湿度,显示效果非常的美观。

需要用到的配件:


步骤:

1. 硬件连接
  把2.8 touch shield插到Iteaduino MEGA 2560左侧的arduino接口上;DS1307时钟模块的SDA接到D22引脚上,SCL接到D23引脚上,并把CR2032电池装配到背面的电池座上;DHT11模块的s引脚接到D25引脚上。
// Connetion:
// DS1307:  SDA pin   -> Arduino Digital 22
//          SCL pin   -> Arduino Digital 23
//          VCC pin   -> 5V
//          GND pin   -> GND
// DHT11    S pin     -> D25
//          V pin     -> 5V
//          G pin     -> GND



实物连接图:



2. 烧写程序
Demo中需要用到一些库,可以从下面地址中下载。
UTFT: http://www.henningkarlsen.com/electronics/library.php?id=51
DS1307:http://www.henningkarlsen.com/electronics/library.php?id=34
把库文件的压缩包解压到arduino/libraries目录下。
打开Arduino\libraries\UTFT\hardware\avr\HW_AVR_defines.h文件,把下面的定义去掉注释,然后保存。
#define USE_UNO_SHIELD_ON_MEGA 1
烧程序之前我们需要先修正一下DS1307当前的时间。注意:程序上的时间只能修改一次,避免下次MEGA上电后修改为当前时间,如果想再次修改时间,需要先把eeprom_address或eeprom_value修改一下。例如eeprom_address改为0x01或eeprom_value改为0x02。




demo代码:

  1. // Connetion:
  2. // DS1307:  SDA pin   -> Arduino Digital 22
  3. //          SCL pin   -> Arduino Digital 23
  4. //          VCC pin   -> 5V
  5. //          GND pin   -> GND
  6. // DHT11    S pin     -> D25
  7. //          V pin     -> 5V
  8. //          G pin     -> GND


  9. #include <DS1307.h>
  10. #include <EEPROM.h>

  11. #include <UTFT.h>
  12. #include <avr/pgmspace.h>

  13. UTFT myGLCD(ITDB28,A5,A4,A3,A2);
  14. extern unsigned int itead[0x28A0];
  15. extern uint8_t BigFont[];
  16. extern uint8_t SevenSegNumFont[];

  17. // Init the DS1307
  18. DS1307 rtc(22, 23);

  19. #define eeprom_address         0x00
  20. #define eeprom_value         0x01

  21. int DHT11PIN=25;

  22. int humidity;
  23. int temperature;

  24. char str_hum[10];
  25. char str_temp[10];

  26. unsigned long start_millis;

  27. void setup()
  28. {
  29.   // Set the clock to run-mode
  30.   rtc.halt(false);
  31.   
  32.   byte value;
  33.   value = EEPROM.read(eeprom_address);
  34.   if(value != eeprom_value)
  35.   {
  36.     EEPROM.write(eeprom_address, eeprom_value);
  37.   // The following lines can be commented out to use the values already stored in the DS1307
  38.   rtc.setDOW(THURSDAY);        // Set Day-of-Week to THURSDAY
  39.   rtc.setTime(15, 17, 0);     // Set the time to 15:17:00 (24hr format)
  40.   rtc.setDate(25, 9, 2014);   // Set the date to September 9th, 2014
  41.   }
  42.   myGLCD.InitLCD();
  43.   myGLCD.clrScr();
  44.   //myGLCD.fillScr(255, 255, 255);  
  45.   myGLCD.setColor(0, 0, 0);
  46.   myGLCD.setFont(BigFont);
  47.   myGLCD.drawBitmap (319, 68, 100, 104, itead,90,0,0);
  48.   myGLCD.setColor(0, 0, 255);
  49.   myGLCD.print("Temperature:", 200, 0,90);
  50.   myGLCD.print("^C", 175, 150,90);
  51.   
  52.   
  53.   myGLCD.print("Humidity:", 150, 0,90);
  54.   myGLCD.print("%", 125, 150,90);
  55.   
  56.   myGLCD.setColor(255, 0, 0);
  57.   start_millis = millis();
  58. }

  59. void loop()
  60. {
  61.   if(millis()-start_millis>1000)
  62.   {
  63.     start_millis = millis();
  64.   // Send Day-of-Week

  65.   myGLCD.print(rtc.getDOWStr(FORMAT_SHORT), 20, 0,90);
  66.   
  67.   // Send date

  68.   myGLCD.print(rtc.getDateStr(), 20, 80,90);
  69.   // Send time


  70.   myGLCD.print(rtc.getTimeStr(), 50, 50,90);

  71.   //read dht11
  72.   int chk = dht11_read(DHT11PIN);

  73.   if(chk==0)
  74.   {
  75.     itoa(humidity,str_hum,10);
  76.     itoa(temperature,str_temp,10);
  77.     //myGLCD.setColor(255, 255, 255);
  78.     myGLCD.print("  ", 175, 80,90);
  79.     //myGLCD.setColor(255, 255, 255);
  80.     myGLCD.print("  ", 125, 80,90);
  81.     //myGLCD.setColor(255, 0, 0);
  82.     myGLCD.print(str_temp, 175, 80,90);
  83.     myGLCD.print(str_hum, 125, 80,90);
  84.    
  85.   }
  86.   

  87.   }
  88.   
  89.   
  90. }

  91. int dht11_read(int pin)
  92. {
  93.         // BUFFER TO RECEIVE
  94.         uint8_t bits[5];
  95.         uint8_t cnt = 7;
  96.         uint8_t idx = 0;

  97.         // EMPTY BUFFER
  98.         for (int i=0; i< 5; i++) bits[i] = 0;

  99.         // REQUEST SAMPLE
  100.         pinMode(pin, OUTPUT);
  101.         digitalWrite(pin, LOW);
  102.         delay(18);
  103.         digitalWrite(pin, HIGH);
  104.         delayMicroseconds(40);
  105.         pinMode(pin, INPUT);

  106.         // ACKNOWLEDGE or TIMEOUT
  107.         unsigned int loopCnt = 10000;
  108.         while(digitalRead(pin) == LOW)
  109.                 if (loopCnt-- == 0) return -2;

  110.         loopCnt = 10000;
  111.         while(digitalRead(pin) == HIGH)
  112.                 if (loopCnt-- == 0) return -2;

  113.         // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
  114.         for (int i=0; i<40; i++)
  115.         {
  116.                 loopCnt = 10000;
  117.                 while(digitalRead(pin) == LOW)
  118.                         if (loopCnt-- == 0) return -2;

  119.                 unsigned long t = micros();

  120.                 loopCnt = 10000;
  121.                 while(digitalRead(pin) == HIGH)
  122.                         if (loopCnt-- == 0) return -2;

  123.                 if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
  124.                 if (cnt == 0)   // next byte?
  125.                 {
  126.                         cnt = 7;    // restart at MSB
  127.                         idx++;      // next byte!
  128.                 }
  129.                 else cnt--;
  130.         }

  131.         // WRITE TO RIGHT VARS
  132.         // as bits[1] and bits[3] are allways zero they are omitted in formulas.
  133.         humidity    = bits[0];
  134.         temperature = bits[2];

  135.         uint8_t sum = bits[0] + bits[2];  

  136.         if (bits[4] != sum) return -1;
  137.         return 0;
  138. }
复制代码



把logo图片转成c文件,就不需要读取SD卡了。
转成后的文件是itead.c文件,需要的朋友可以下载附件。

用arduino ide打开demo代码和c文件,烧写到mega2560就可以看到下面的效果了。

3. 显示效果


是不是效果很漂亮啊?!如果你不想用我们itead的logo,你也可以按照下面的步骤修改自己的图片或者logo。

打开Arduino\libraries\UTFT\Tools\ImageConverter565.exe软件,点击open image,选择你喜欢的图片,但是你的图片必须是100*104像素的。然后Array Name选项中把名字修改为itead,点击保存。

把原来的itead.c文件替换掉,重新烧写程序后,你就可以看到新的logo。是不是很棒呢!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2014-10-6 10:46:36 | 显示全部楼层

这个效果更不错哦。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-25 12:54 , Processed in 0.050125 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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