xuyw123 发表于 2008-5-19 16:09:19

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

马老师你好,我用M16和DS18B20做了个数码管显示温度的板子,可是每次下程序温度显示都是128.5°C,不会变化,而且这个传感器的最高温度只为125度啊,这是为什么啊?

yulutong 发表于 2008-5-19 16:14:50

网站上有成功的例程。这个问题问多了,马老师会有反感。

machao 发表于 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 */
#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;

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);

          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 */
#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;

/* allocate space for ROM codes of the devices

   which generate an alarm */

unsigned char alarm_rom_codes;

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,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);

          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.

xinjie1023 发表于 2011-8-11 21:59:11

收藏,不知此程序能传输的最远距离是多少?
页: [1]
查看完整版本: 请问马老师关于温度传感器DS18B20的问题!!!