Ledong手把手入门系列教程之十六

数字温度计

©版权归imtcn.com和Ledong所有,转载请保持本文完整性,并注明出处

这是《Ledong手把手入门系列教程》的第十六课,这一课我们将利用Ledong板测量和显示温度。

1、简介

本实验通过温度传感器读取数据,并将其显示在LCD屏幕上。需要用的元件如下表,其中10KΩ电位计用于调节液晶显示器的亮度和对比度。

实验所需器材:

名    称 数量
Ledong板 1套
LCD 1个
DS18B20温度传感器 1个
74HC595芯片 1片
4.7KΩ电阻 3个
10KΩ电位计 1个
面包板 2块
导线 若干

根据图16-1的方法连接电路:

 

图16-1 连接示意图

2、示例代码

将Ledong用线缆连接到PC上,打开软件程序,并设置好后,将下列代码输入到软件中,通过编译下载命令,将程序下载到Ledong中。此外,还需根据代码的要求,拷贝DallasTemperature文件夹与OneWire文件夹至Ledong安装路径下的libraries文件夹中。

#include <LCD3Wire.h>

#include <OneWire.h>

#include <DallasTemperature.h>

#define LCD_LINES 2 

#define DOUT_PIN  7

#define STR_PIN   6 

#define CLK_PIN   5 

#define ONE_WIRE_BUS 12

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

LCD3Wire lcd = LCD3Wire(LCD_LINES, DOUT_PIN, STR_PIN, CLK_PIN);

char * floatToString(char * outstr, float value, int places, int minwidth, bool rightjustify);

void setup(void) {

    lcd.init();

    sensors.begin();

}

void loop(void) {

  float temp;

  char buff[10];

  sensors.requestTemperatures();

   temp = sensors.getTempCByIndex(0);

   lcd.cursorTo(2,0);

   lcd.printIn(floatToString(buff,temp,3,0,false));

   lcd.print(0xDF);

   lcd.print(‘C’);

}

char * floatToString(char * outstr, float value, int places, int minwidth, bool rightjustify) {

    int digit;

    float tens = 0.1;

    int tenscount = 0;

    int i;

    float tempfloat = value;

    int c = 0;

    int charcount = 1;

    int extra = 0;

    float d = 0.5;

    if (value < 0)

        d *= -1.0;

    for (i = 0; i < places; i++)

        d/= 10.0;   

    tempfloat +=  d;

    if (value < 0)

        tempfloat *= -1.0;

    while ((tens * 10.0) <= tempfloat) {

        tens *= 10.0;

        tenscount += 1;

    }

    if (tenscount > 0)

        charcount += tenscount;

    else

        charcount += 1;

    if (value < 0)

        charcount += 1;

    charcount += 1 + places;

    minwidth += 1;

    if (minwidth > charcount){       

        extra = minwidth – charcount;

        charcount = minwidth;

    }

    if (extra > 0 and rightjustify) {

        for (int i = 0; i< extra; i++) {

            outstr[c++] = ‘ ‘;

        }

    }

    if (value < 0)

        outstr[c++] = ‘-’;

    if (tenscount == 0)

        outstr[c++] = ’0′;

    for (i=0; i< tenscount; i++) {

        digit = (int) (tempfloat/tens);

        itoa(digit, &outstr[c++], 10);

        tempfloat = tempfloat – ((float)digit * tens);

        tens /= 10.0;

    }

    if (places > 0)

    outstr[c++] = ‘.’;

    for (i = 0; i < places; i++) {

        tempfloat *= 10.0;

        digit = (int) tempfloat;

        itoa(digit, &outstr[c++], 10);

        // once written, subtract off that digit

        tempfloat = tempfloat – (float) digit;

    }

    if (extra > 0 and not rightjustify) {

        for (int i = 0; i< extra; i++) {

            outstr[c++] = ‘ ‘;

        }

    }

    outstr[c++] = ‘\0′;

    return outstr;

}

程序下载后,按运行键。可以观察到,液晶显示器将会显示外界的温度。

3、运行效果

Ledong手把手入门系列教程之十六.数字温度计的运行效果图16-2如下:

 

图16-2 运行效果图

4、购买及培训

本例套件可以通过下面的方式购买:

www.imtcn.com

5、电路图

Ledong手把手入门系列教程之十六.数字温度计的原理图16-3如下:

 

图16-3 原理图

Ledong手把手入门系列教程之十六.数字温度计的PCB布线图16-4如下:

 

图16-4 pcb布线图

6、错误处理

本实验导线连接比较复杂,注意认真检查电路的连线是否与简介中描述的一致,尤其是74HC595芯片的连线。

7、补充说明

8、参考文献

http://www.imtcn.com

http://fritzing.org/projects/digital-thermometer/