Difference between revisions of "Matrix - Temperature Sensor"

From FriendlyELEC WiKi
Jump to: navigation, search
Line 1: Line 1:
 
==介绍==
 
==介绍==
[[File:TemperatureSensor.png|thumb|Temperature Sensor]]
+
[[File:TemperatureSensor01.png|thumb|Temperature Sensor]]
 
非常流行的TO-92封装的DS18B20。DS18B20的测量范围是-55摄氏度到+125摄氏度,可选9-bit或12-bit,在-10摄氏度到+85摄氏度时可以精确到0.5度,采用一线协议通讯。我们把DS18B20的3个针脚全部引出,V对应电源引脚,G对应地引脚,S对应数据引脚。
 
非常流行的TO-92封装的DS18B20。DS18B20的测量范围是-55摄氏度到+125摄氏度,可选9-bit或12-bit,在-10摄氏度到+85摄氏度时可以精确到0.5度,采用一线协议通讯。我们把DS18B20的3个针脚全部引出,V对应电源引脚,G对应地引脚,S对应数据引脚。
 
==特性==
 
==特性==

Revision as of 09:41, 31 July 2015

1 介绍

Temperature Sensor

非常流行的TO-92封装的DS18B20。DS18B20的测量范围是-55摄氏度到+125摄氏度,可选9-bit或12-bit,在-10摄氏度到+85摄氏度时可以精确到0.5度,采用一线协议通讯。我们把DS18B20的3个针脚全部引出,V对应电源引脚,G对应地引脚,S对应数据引脚。

2 特性

  • -55摄氏度到+125摄氏度
  • 一线协议
  • 体积小巧, 带固定孔,方便嵌入到外壳
  • 2.54mm排针接口,接线方便,通用性强

3 使用方法

3.1 连接

  • 连接到Tiny4412 SDK (1506)
GND针脚: 接地
VCC: 接5V
S:接GPIO_PIN1

3.2 Linux下的C示例

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "libfahw.h"
 
void parseCmd(int argc, char **argv, int *pin)
{
    int num = atoi(argv[1]);
    switch(num) {
    case 1:
        *pin = TINY4412_GPIO_PIN1;
        break;
    case 2:
        *pin = TINY4412_GPIO_PIN2;
        break;
    case 3:
        *pin = TINY4412_GPIO_PIN3;
        break;
    case 4:
        *pin = TINY4412_GPIO_PIN4;
        break;
    case 5:
        *pin = TINY4412_GPIO_PIN5;
        break;
    case 6:
        *pin = TINY4412_GPIO_PIN6;
        break;
    case 7:
        *pin = TINY4412_GPIO_PIN7;
        break;
    case 8:
        *pin = TINY4412_GPIO_PIN8;
        break;
    case 9:
        *pin = TINY4412_GPIO_PIN9;
        break;
    case 10:
        *pin = TINY4412_GPIO_PIN10;
        break;
    default:
        printf("Unsupported pin TINY4412_GPIO_PIN%d\n", num);
        num = 1;
        *pin = TINY4412_GPIO_PIN1;
        break;
    }
    printf("Using pin TINY4412_GPIO_PIN%d\n", num);
}
 
int main(int argc, char ** argv)
{
    int devFD = -1;
    int pin = TINY4412_GPIO_PIN1;
    char *temperature = (char *) malloc(32);
    memset(temperature, 0, 32);
 
    if (argc == 2) {
        parseCmd(argc, argv, &pin);
    } else {
        printf("Using default pin TINY4412_GPIO_PIN1\n");
    }
 
    if ((devFD = ds18b20Init(pin)) == -1) {
        printf("Fail to init ds18b20\n");
        return -1;
    }
    if (ds18b20Read(temperature) > 0) {
        printf("Temperature = %s\n", temperature);
    } else {
        printf("Fail to get temperature\n");
    }
    free(temperature);
    ds18b20DeInit(devFD);
    return 0;
}

3.3 编译并运行示例

git clone http://github.com/friendlyarm/fa-hardware.git
cd fa-hardware
cd demo
cd matrix-temperature_sensor
make

将编译生成的ds18b20通过ftp上传到开发板上运行即可测试。

4 相关资料