ITEAD创易工作室 发表于 2014-9-27 15:08:59

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

http://player.youku.com/player.php/sid/XNzkxNjM1NjQ0/v.swf
之前我们介绍过如何使用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代码:

// 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


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

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

UTFT myGLCD(ITDB28,A5,A4,A3,A2);
extern unsigned int itead;
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];

// Init the DS1307
DS1307 rtc(22, 23);

#define eeprom_address         0x00
#define eeprom_value         0x01

int DHT11PIN=25;

int humidity;
int temperature;

char str_hum;
char str_temp;

unsigned long start_millis;

void setup()
{
// Set the clock to run-mode
rtc.halt(false);

byte value;
value = EEPROM.read(eeprom_address);
if(value != eeprom_value)
{
    EEPROM.write(eeprom_address, eeprom_value);
// The following lines can be commented out to use the values already stored in the DS1307
rtc.setDOW(THURSDAY);      // Set Day-of-Week to THURSDAY
rtc.setTime(15, 17, 0);   // Set the time to 15:17:00 (24hr format)
rtc.setDate(25, 9, 2014);   // Set the date to September 9th, 2014
}
myGLCD.InitLCD();
myGLCD.clrScr();
//myGLCD.fillScr(255, 255, 255);
myGLCD.setColor(0, 0, 0);
myGLCD.setFont(BigFont);
myGLCD.drawBitmap (319, 68, 100, 104, itead,90,0,0);
myGLCD.setColor(0, 0, 255);
myGLCD.print("Temperature:", 200, 0,90);
myGLCD.print("^C", 175, 150,90);


myGLCD.print("Humidity:", 150, 0,90);
myGLCD.print("%", 125, 150,90);

myGLCD.setColor(255, 0, 0);
start_millis = millis();
}

void loop()
{
if(millis()-start_millis>1000)
{
    start_millis = millis();
// Send Day-of-Week

myGLCD.print(rtc.getDOWStr(FORMAT_SHORT), 20, 0,90);

// Send date

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


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

//read dht11
int chk = dht11_read(DHT11PIN);

if(chk==0)
{
    itoa(humidity,str_hum,10);
    itoa(temperature,str_temp,10);
    //myGLCD.setColor(255, 255, 255);
    myGLCD.print("", 175, 80,90);
    //myGLCD.setColor(255, 255, 255);
    myGLCD.print("", 125, 80,90);
    //myGLCD.setColor(255, 0, 0);
    myGLCD.print(str_temp, 175, 80,90);
    myGLCD.print(str_hum, 125, 80,90);
   
}


}


}

int dht11_read(int pin)
{
        // BUFFER TO RECEIVE
        uint8_t bits;
        uint8_t cnt = 7;
        uint8_t idx = 0;

        // EMPTY BUFFER
        for (int i=0; i< 5; i++) bits = 0;

        // REQUEST SAMPLE
        pinMode(pin, OUTPUT);
        digitalWrite(pin, LOW);
        delay(18);
        digitalWrite(pin, HIGH);
        delayMicroseconds(40);
        pinMode(pin, INPUT);

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

        loopCnt = 10000;
        while(digitalRead(pin) == HIGH)
                if (loopCnt-- == 0) return -2;

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

                unsigned long t = micros();

                loopCnt = 10000;
                while(digitalRead(pin) == HIGH)
                        if (loopCnt-- == 0) return -2;

                if ((micros() - t) > 40) bits |= (1 << cnt);
                if (cnt == 0)   // next byte?
                {
                        cnt = 7;    // restart at MSB
                        idx++;      // next byte!
                }
                else cnt--;
        }

        // WRITE TO RIGHT VARS
      // as bits and bits are allways zero they are omitted in formulas.
        humidity    = bits;
        temperature = bits;

        uint8_t sum = bits + bits;

        if (bits != sum) return -1;
        return 0;
}


把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。是不是很棒呢!

ITEAD创易工作室 发表于 2014-10-6 10:46:36


这个效果更不错哦。
页: [1]
查看完整版本: 【教程】mega2560+2.8 LCD显示时间和温湿度