ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 13.06.2025
Просмотров: 2222
Скачиваний: 0
CodeVisionAVR
4.14.2 Maxim/Dallas Semiconductor DS18B20 Temperature Sensor
Functions
These functions are intended for easy interfacing between C programs and the DS18B20 1 Wire bus temperature sensor.
The prototypes for these functions are placed in the file ds18b20.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 ds18b20.h.
Prior to #include -ing the ds18b20.h file, you must declare which microcontroller port and port bit are used for communication with the DS18B20 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 DS18B20 functions prototypes */ #include <ds18b20.h>
The DS18B20 functions are:
unsigned char ds18b20_read_spd(unsigned char *addr)
this function reads the contents of the SPD for the DS18B20 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 DS18B20 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 __ds18b20_scratch_pad_struct
{
unsigned char temp_lsb,temp_msb, temp_high,temp_low, conf_register,
res1,
res2,
res3,
crc;
} __ds18b20_scratch_pad;
defined in the ds18b20.h header file.
unsigned char ds18b20_init(unsigned char *addr,signed char temp_low,signed char temp_high,usigned char resolution)
this function sets the low (temp_low) and high (temp_high) temperature alarms and specifies the temperature measurement resolution of the DS18B20.
The resolution argument may take the value of one of the following macros defined in the ds18b20.h header file:
DS18B20_9BIT_RES for 9 bit tempearture measurement resolution (0.5°C) DS18B20_10BIT_RES for 10 bit tempearture measurement resolution (0.25°C) DS18B20_11BIT_RES for 11 bit tempearture measurement resolution (0.125°C) DS18B20_12BIT_RES for 12 bit tempearture measurement resolution (0.0625°C)
© 1998-2007 HP InfoTech S.R.L. |
Page 163 |
CodeVisionAVR
In case of success the function returns the value 1, else it returns 0.
The alarm temperatures and resolution are stored in both the DS18B20'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 DS18B20 sensor is used, no ROM code array is necessary and the pointer addr must be NULL (0).
The alarm status for all the DS18B20 devices on the 1 Wire bus can be determined by calling the w1_search function with the Alarm Search (ECh) command.
float ds18b20_temperature(unsigned char *addr)
this function returns the temperature of the DS18B20 sensor with the ROM code stored in an array of 8 bytes located at address addr.
The temperature is measured in °C. In case of error the function returns the value -9999.
If only one DS18B20 sensor is used, no ROM code array is necessary and the pointer addr must be NULL (0).
Prior on calling the the ds18b20_temperature function for the first time, the ds18b20_init function must be used to specify the desired temperature measurement resolution.
If more several sensors are used, then the program must first identify the ROM codes for all the sensors.
Only after that the ds18b20_temperature 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 DS18B20 functions prototypes */ #include <ds18b20.h>
/* include the printf function prototype */ #include <stdio.h>
/* quartz crystal frequency [Hz] */ #define xtal 4000000L
/* Baud rate */ #define baud 9600
/* maximum number of DS18B20 connected to the bus */ #define MAX_DEVICES 8
/* DS18B20 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];
© 1998-2007 HP InfoTech S.R.L. |
Page 164 |
CodeVisionAVR
main()
{
unsigned char i,devices;
/* 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 DS18B20 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 & temperature measurement resolutions for all the devices temp_low=25°C temp_high=35°C resolution 12bits */
for (i=0;i<devices;i++)
{
printf("INITIALIZING DEVICE #%-u ",i+1);
if (ds18b20_init(&rom_codes[i][0],25,35,DS18B20_12BIT_RES)) putsf("OK"); else putsf("ERROR");
};
while (1)
{
/* measure and display the temperature(s) */ for (i=0;i<devices;)
printf("t%u=%+.3f\xf8C\n\r",i+1, ds18b20_temperature(&rom_codes[i++][0]));
/* 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 DS18B20 data sheet for more information.
© 1998-2007 HP InfoTech S.R.L. |
Page 165 |
CodeVisionAVR
4.14.3 Maxim/Dallas Semiconductor DS2430 EEPROM Functions
These functions are intended for easy interfacing between C programs and the DS2430 1 Wire bus EEPROM.
The prototypes for these functions are placed in the file ds2430.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 ds2430.h.
Prior to #include -ing the ds2430.h file, you must declare which microcontroller port and port bits are used for communication with the DS2430 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 DS2430 functions prototypes */ #include <ds2430.h>
The DS2430 functions are:
unsigned char ds2430_read_block(unsigned char *romcode,unsigned char *dest, unsigned char addr,unsigned char size);
this function reads a block of size bytes starting from the DS2430 EEPROM memory address addr and stores it in the string dest located in SRAM.
It returns 1 if successful, 0 if not.
The DS2430 device is selected using it's ROM code stored in an array of 8 bytes located at address romcode.
unsigned char ds2430_read(unsigned char *romcode,unsigned char addr, unsigned char *data);
this function reads a byte from the DS2430 EEPROM memory address addr and stores it in the SRAM memory location pointed by data.
It returns 1 if successful, 0 if not.
The DS2430 device is selected using it's ROM code stored in an array of 8 bytes located at address romcode.
unsigned char ds2430_write_block(unsigned char *romcode, unsigned char *source,unsigned char addr,unsigned char size);
this function writes a block of size bytes, from the string source, located in SRAM, in the DS2430 EEPROM starting from memory address addr.
It returns 1 if successful, 0 if not.
The DS2430 device is selected using it's ROM code stored in an array of 8 bytes located at address romcode.
unsigned char ds2430_write(unsigned char *romcode, unsigned char addr,unsigned char data);
this function writes the byte data at DS2430 EEPROM memory address addr. It returns 1 if successful, 0 if not.
The DS2430 device is selected using it's ROM code stored in an array of 8 bytes located at address romcode.
© 1998-2007 HP InfoTech S.R.L. |
Page 166 |
CodeVisionAVR
unsigned char ds2430_read_appreg_block(unsigned char *romcode, unsigned char *dest,unsigned char addr,unsigned char size);
this function reads a block of size bytes starting from the DS2430 application register address addr and stores it in the string dest located in SRAM.
It returns 1 if successful, 0 if not.
The DS2430 device is selected using it's ROM code stored in an array of 8 bytes located at address romcode.
unsigned char ds2430_write_appreg_block(unsigned char *romcode, unsigned char *source,unsigned char addr,unsigned char size);
this function reads a block of size bytes starting from the DS2430 application register address addr and stores it in the string dest located in SRAM.
It returns 1 if successful, 0 if not.
The DS2430 device is selected using it's ROM code stored in an array of 8 bytes located at address romcode.
If only one DS2430 EEPROM is used, no ROM code array is necessary and the pointer romcode must be NULL (0).
If several 1 Wire device are used, then the program must first identify the ROM codes for all the devices. Only after that the DS2430 functions may be used, with the romcode pointer pointing to the array which holds the ROM code for the needed device.
Example:
/* specify the port and bit used for the 1 Wire bus
The |
DS2430 devices |
are connected to |
||
bit |
6 of PORTA of the AT90S8515 as follows: |
|||
[DS2430] |
[STK200 PORTA HEADER] |
|||
1 |
GND |
- |
9 |
GND |
2 |
DATA |
- |
7 |
PA6 |
All the devices must be connected in parallel
AN 4.7k PULLUP RESISTOR MUST BE CONNECTED BETWEEN DATA (PA6) AND +5V !
*/
#asm
.equ __w1_port=0x1b
.equ __w1_bit=6 #endasm
// test the DS2430 functions #include <ds2430.h> #include <90s8515.h> #include <stdio.h>
/* DS2430 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 */
#define MAX_DEVICES 8
unsigned char rom_code[MAX_DEVICES][9];
© 1998-2007 HP InfoTech S.R.L. |
Page 167 |
CodeVisionAVR
char text[]="Hello world!"; char buffer[32];
#define START_ADDR 2
main() {
unsigned char i,devices;
//init UART UCR=8;
UBRR=25; // Baud=9600 @ 4MHz
//detect how many 1 Wire devices are present on the bus devices=w1_search(0xF0,&rom_code[0][0]);
printf("%-u 1 Wire devices found\n\r",devices); for (i=0;i<devices;i++)
//make sure to select only the DS2430 types
//0x14 is the DS2430 family code
if (rom_code[i][0]==DS2430_FAMILY_CODE)
{
printf("\n\r");
// write text in each DS2430 at START_ADDR if (ds2430_write_block(&rom_code[i][0],
text,START_ADDR,sizeof(text)))
{
printf("Data written OK in DS2430 #%-u!\n\r",i+1); // display the text written in each DS2430
if (ds2430_read_block(&rom_code[i][0],buffer,START_ADDR, sizeof(text)))
printf("Data read OK!\n\rDS2430 #%-u text: %s\n\r", i+1,buffer);
else printf("Error reading data from DS2430 #%-u!\n\r", i+1);
}
else printf("Error writing data to DS2430 #%-u!\n\r",i+1); };// stop
while (1);
}
Refer to the DS2430 data sheet for more information.
© 1998-2007 HP InfoTech S.R.L. |
Page 168 |
CodeVisionAVR
4.14.4 Maxim/Dallas Semiconductor DS2433 EEPROM Functions
These functions are intended for easy interfacing between C programs and the DS2433 1 Wire bus EEPROM.
The prototypes for these functions are placed in the file ds2433.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 ds2433.h.
Prior to #include -ing the ds2433.h file, you must declare which microcontroller port and port bits are used for communication with the DS2433 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 DS2433 functions prototypes */ #include <ds2433.h>
The DS2433 functions are:
unsigned char ds2433_read_block(unsigned char *romcode,unsigned char *dest, unsigned int addr,unsigned int size);
this function reads a block of size bytes starting from the DS2433 EEPROM memory address addr and stores it in the string dest located in SRAM.
It returns 1 if successful, 0 if not.
The DS2433 device is selected using it's ROM code stored in an array of 8 bytes located at address romcode.
unsigned char ds2433_read(unsigned char *romcode,unsigned int addr, unsigned char *data);
this function reads a byte from the DS2433 EEPROM memory address addr and stores it in the SRAM memory location pointed by data.
It returns 1 if successful, 0 if not.
The DS2433 device is selected using it's ROM code stored in an array of 8 bytes located at address romcode.
unsigned char ds2433_write_block(unsigned char *romcode, unsigned char *source,unsigned int addr,unsigned int size);
this function writes a block of size bytes, from the string source, located in SRAM, in the DS2433 EEPROM starting from memory address addr.
It returns 1 if successful, 0 if not.
The DS2433 device is selected using it's ROM code stored in an array of 8 bytes located at address romcode.
unsigned char ds2433_write(unsigned char *romcode,unsigned int addr, unsigned char data);
this function writes the byte data at DS2433 EEPROM memory address addr. It returns 1 if successful, 0 if not.
The DS2433 device is selected using it's ROM code stored in an array of 8 bytes located at address romcode.
© 1998-2007 HP InfoTech S.R.L. |
Page 169 |
CodeVisionAVR
If only one DS2433 EEPROM is used, no ROM code array is necessary and the pointer romcode must be NULL (0).
If several 1 Wire device are used, then the program must first identify the ROM codes for all the devices. Only after that the DS2433 functions may be used, with the romcode pointer pointing to the array which holds the ROM code for the needed device.
Example:
/* specify the port and bit used for the 1 Wire bus
The |
DS2433 devices |
are connected to |
||
bit |
6 of PORTA of the AT90S8515 as follows: |
|||
[DS2433] |
[STK200 PORTA HEADER] |
|||
1 |
GND |
- |
9 |
GND |
2 |
DATA |
- |
7 |
PA6 |
All the devices must be connected in parallel
AN 4.7k PULLUP RESISTOR MUST BE CONNECTED BETWEEN DATA (PA6) AND +5V !
*/
#asm
.equ __w1_port=0x1b
.equ __w1_bit=6 #endasm
// test the DS2433 functions #include <ds2433.h> #include <90s8515.h> #include <stdio.h>
/* DS2433 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 */
#define MAX_DEVICES 8
unsigned char rom_code[MAX_DEVICES][9];
char text[]="This is a long text to \ be able to test writing across the \ scratchpad boundary";
char buffer[100]; #define START_ADDR 2
main() {
unsigned char i,devices;
//init UART UCR=8;
UBRR=25; // Baud=9600 @ 4MHz
//detect how many 1 Wire devices are present on the bus devices=w1_search(0xF0,&rom_code[0][0]);
printf("%-u 1 Wire devices found\n\r",devices);
© 1998-2007 HP InfoTech S.R.L. |
Page 170 |
CodeVisionAVR
for (i=0;i<devices;i++)
//make sure to select only the DS2433 types
//0x23 is the DS2433 family code
if (rom_code[i][0]==DS2433_FAMILY_CODE)
{
printf("\n\r");
// write text in each DS2433 at START_ADDR if (ds2433_write_block(&rom_code[i][0],
text,START_ADDR,sizeof(text)))
{
printf("Data written OK in DS2433 #%-u!\n\r",i+1); // display the text written in each DS2433
if (ds2433_read_block(&rom_code[i][0],buffer,START_ADDR, sizeof(text)))
printf("Data read OK!\n\rDS2433 #%-u text: %s\n\r", i+1,buffer);
else printf("Error reading data from DS2433 #%-u!\n\r",i+1);
}
else printf("Error writing data to DS2433 #%-u!\n\r",i+1); };
// stop while (1);
}
Refer to the DS2433 data sheet for more information.
© 1998-2007 HP InfoTech S.R.L. |
Page 171 |