搜索
bottom↓
回复: 3

请问马老师关于温度传感器DS18B20的问题!!!

[复制链接]

出0入12汤圆

发表于 2008-5-19 16:09:19 | 显示全部楼层 |阅读模式
马老师你好,我用M16和DS18B20做了个数码管显示温度的板子,可是每次下程序温度显示都是128.5°C,不会变化,而且这个传感器的最高温度只为125度啊,这是为什么啊?

出0入0汤圆

发表于 2008-5-19 16:14:50 | 显示全部楼层
网站上有成功的例程。这个问题问多了,马老师会有反感。

出0入0汤圆

发表于 2008-5-19 19:21:42 | 显示全部楼层
CVAVR中有现成的函数,下面是说明和简单使用的例子


Maxim/Dallas Semiconductor DS1820/DS18S20 Temperature Sensors Functions

These functions are intended for easy interfacing between C programs and the DS1820/DS18S20 1 Wire bus temperature sensors.
The prototypes for these functions are placed in the file ds1820.h, located in the ..\INC subdirectory. This file must be #include -ed before using the functions.
The 1 Wire bus functions prototypes are automatically #include -ed with the ds1820.h.

Prior to #include -ing the ds1820.h file, you must declare which microcontroller port and port bit are used for communication with the DS1820/DS18S20 through the 1 Wire bus.

Example:

/* specify the port and bit used for the 1 Wire bus */

#asm

    .equ __w1_port=0x18 ;PORTB

    .equ __w1_bit=2

#endasm

/* include the DS1820/DS18S20 functions prototypes */

#include <ds1820.h>

The DS1820/DS18S20 functions are:

unsigned char ds1820_read_spd(unsigned char *addr)

this function reads the contents of the SPD for the DS1820/DS18S20 sensor with the ROM code stored in an array of 8 bytes located at address addr.
The functions returns the value 1 on succes and 0 in case of error.
If only one DS1820/DS18S20 sensor is used, no ROM code array is necessary and the pointer addr must be NULL (0).
The contents of the SPD will be stored in the structure:

struct __ds1820_scratch_pad_struct
       {
       unsigned char temp_lsb,temp_msb,

                temp_high,temp_low,
                res1,res2,
                cnt_rem,cnt_c,
                crc;
       } __ds1820_scratch_pad;

defined in the ds1820.h header file.

int ds1820_temperature_10(unsigned char *addr)

this function returns the temperature of the DS1820/DS18S20 sensor with the ROM code stored in an array of 8 bytes located at address addr.

The temperature is measured in 癈 and is multiplied by 10. In case of error the function returns the value -9999.
If only one DS1820/DS18S20 sensor is used, no ROM code array is necessary and the pointer addr must be NULL (0).
If more several sensors are used, then the program must first identify the ROM codes for all the sensors. Only after that the ds1820_temperature_10 function may be used, with the addr pointer pointing to the array which holds the ROM code for the needed device.

Example:


#include <90s8515.h>

/* specify the port and bit used for the 1 Wire bus */

#asm

    .equ __w1_port=0x18 ;PORTB

    .equ __w1_bit=2

#endasm

/* include the DS1820/DS18S20 functions prototypes */

#include <ds1820.h>

/* include the printf function prototype */

#include <stdio.h>

/* include the abs function prototype */

#include <math.h>

/* quartz crystal frequency [Hz] */
#define xtal 4000000L

/* Baud rate */
#define baud 9600

/* maximum number of DS1820/DS18S20 connected to the bus */

#define MAX_DEVICES 8

/* DS1820/DS18S20 devices ROM code storage area,

   9 bytes are used for each device

   (see the w1_search function description),

   but only the first 8 bytes contain the ROM code

   and CRC */

unsigned char rom_codes[MAX_DEVICES][9];

main()

{

unsigned char i,devices;

int temp;

/* initialize the UART's baud rate */
UBRR=xtal/16/baud-1;

/* initialize the UART control register
   TX enabled, no interrupts, 8 data bits */
UCR=8;

/* detect how many DS1820/DS18S20 devices

   are connected to the bus and

   store their ROM codes in the rom_codes array */

devices=w1_search(0xf0,rom_codes);

/* display the number */

printf("%-u DEVICE(S) DETECTED\n\r",devices);

/* if no devices were detected then halt */

if (devices==0) while (1); /* loop forever */

/* measure and display the temperature(s) */      

while (1)

      {

      for (i=0;i<devices;)

          {

          temp=ds1820_temperature_10(&rom_codes[0]);

          printf("t%-u=%-i.%-u\xf8C\n\r",++i,temp/10,

          abs(temp%10));         

          };

      };
}

unsigned char ds1820_set_alarm(unsigned char *addr,signed char temp_low,signed char temp_high)

        this function sets the low (temp_low) and high (temp_high) temperature alarms of the DS1820/DS18S20.
In case of success the function returns the value 1, else it returns 0.
The alarm temperatures are stored in both the DS1820/DS18S20's scratchpad SRAM and its EEPROM.
The ROM code needed to address the device is stored in an array of 8 bytes located at address addr.

If only one DS1820/DS18S20 sensor is used, no ROM code array is necessary and the pointer addr must be NULL (0).
The alarm status for all the DS1820/DS18S20 devices on the 1 Wire bus can be determined by calling the w1_search function with the Alarm Search (ECh) command.
Example:


#include <90s8515.h>

/* specify the port and bit used for the 1 Wire bus */

#asm

    .equ __w1_port=0x18 ;PORTB

    .equ __w1_bit=2

#endasm

/* include the DS1820/DS18S20 functions prototypes */

#include <ds1820.h>

/* include the printf function prototype */

#include <stdio.h>

/* include the abs function prototype */

#include <math.h>

/* quartz crystal frequency [Hz] */
#define xtal 4000000L

/* Baud rate */
#define baud 9600

/* maximum number of DS1820/DS18S20 connected to the bus */

#define MAX_DEVICES 8

/* DS1820/DS18S20 devices ROM code storage area,

   9 bytes are used for each device

   (see the w1_search function description),

   but only the first 8 bytes contain the ROM code

   and CRC */

unsigned char rom_codes[MAX_DEVICES][9];

/* allocate space for ROM codes of the devices

   which generate an alarm */

unsigned char alarm_rom_codes[MAX_DEVICES][9];

main()

{

unsigned char i,devices;

int temp;

/* initialize the UART's baud rate */
UBRR=xtal/16/baud-1;

/* initialize the UART control register
   TX enabled, no interrupts, 8 data bits */
UCR=8;

/* detect how many DS1820/DS18S20 devices

   are connected to the bus and

   store their ROM codes in the rom_codes array */

devices=w1_search(0xf0,rom_codes);

/* display the number */

printf("%-u DEVICE(S) DETECTED\n\r",devices);

/* if no devices were detected then halt */

if (devices==0) while (1); /* loop forever */

/* set the temperature alarms for all the devices

   temp_low=25癈 temp_high=35癈 */

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

    {

    printf("INITIALIZING DEVICE #%-u ",i+1);

    if (ds1820_set_alarm(&rom_codes[0],25,35))

       putsf("OK"); else putsf("ERROR");

    };

while (1)

      {

      /* measure and display the temperature(s) */

      for (i=0;i<devices;)

          {

          temp=ds1820_temperature_10(&rom_codes[0]);

          printf("t%-u=%-i.%-u\xf8C\n\r",++i,temp/10,

          abs(temp%10));         

          };

      /* display the number of devices which

         generated an alarm */      
        printf("ALARM GENERATED BY %-u DEVICE(S)\n\r",
      w1_search(0xec,alarm_rom_codes));
      };
}


Refer to the DS1820/DS18S20 data sheet for more information.

出0入0汤圆

发表于 2011-8-11 21:59:11 | 显示全部楼层
收藏,不知此程序能传输的最远距离是多少?
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-19 17:14

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

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