4.12.1 National Semiconductor LM75 Temperature Sensor
Functions
These functions are intended for easy interfacing between C programs and the LM75 I2C bus
temperature sensor.
The prototypes for these functions are placed in the file lm75.h, located in the ..\INC subdirectory. This
file must be #include -ed before using the functions.
The I2C bus functions prototypes are automatically #include -ed with the lm75.h.
Prior to #include -ing the lm75.h file, you must declare which microcontroller port and port bits are
used for communication with the LM75 through the I2C bus.
Example:
/* the I2C bus is connected to PORTB */
/* the SDA signal is bit 3 */
/* the SCL signal is bit 4 */
#asm
.equ __i2c_port=0x18
.equ __sda_bit=3
.equ __scl_bit=4
#endasm
/* now you can include the LM75 Functions */
#include <lm75.h>
The LM75 Functions are:
void lm75_init(unsigned char chip,signed char thyst,signed char tos, unsigned char pol)
this function initializes the LM75 sensor chip.
Before calling this function the I2C bus must be initialized by calling the i2c_init function.
This is the first function that must be called prior to using the other LM75 Functions.
If more then one chip is connected to the I2C bus, then the function must be called for each one,
specifying accordingly the function parameter chip.
Maximum 8 LM75 chips can be connected to the I2C bus, their chip address can be from 0 to 7.
The LM75 is configured in comparator mode, where it functions like a thermostat.
The O.S. output becomes active when the temperature exceeds the tos limit, and leaves the active
state when the temperature drops below the thyst limit.
Both thyst and tos are expressed in °C.
pol represents the polarity of the LM75 O.S. output in active state.
If pol is 0, the output is active low and if pol is 1, the output is active high.
Refer to the LM75 data sheet for more information.
int lm75_temperature_10(unsigned char chip)
this function returns the temperature of the LM75 sensor with the address chip.
The temperature is in °C and is multiplied by 10.
Example how to display the temperature of two LM75 sensors with addresses 0 and 1:
/* the LM75 I2C bus is connected to PORTB */
/* the SDA signal is bit 3 */
/* the SCL signal is bit 4 */
#asm
.equ __i2c_port=0x18
.equ __sda_bit=3
.equ __scl_bit=4
#endasm
/* include the LM75 Functions */
#include <lm75.h>
/* the LCD module is connected to PORTC */
#asm
.equ __lcd_port=0x15
#endasm
/* include the LCD Functions */
#include <lcd.h>
/* include the prototype for sprintf */
#include <stdio.h>
/* include the prototype for abs */
#include <math.h>
char display_buffer[33];
void main(void) {
int t0,t1;
/* initialize the LCD, 2 rows by 16 columns */
lcd_init(16);
/* initialize the I2C bus */
i2c_init();
/* initialize the LM75 sensor with address 0 */
/* thyst=20°C tos=25°C */
lm75_init(0,20,25,0);
/* initialize the LM75 sensor with address 1 */
/* thyst=30°C tos=35°C */
lm75_init(1,30,35,0);
/* temperature display loop */
while (1)
{
/* read the temperature of sensor #0 *10°C */
t0=lm75_temperature_10(0);
/* read the temperature of sensor #1 *10°C */
t1=lm75_temperature_10(1);
/* prepare the displayed temperatures */
/* in the display_buffer */
sprintf(display_buffer,
"t0=%-i.%-u%cC\nt1=%-i.%-u%cC",
t0/10,abs(t0%10),0xdf,t1/10,abs(t1%10),0xdf);
/* display the temperatures */
lcd_clear();
lcd_puts(display_buffer);
};
}
4.12.2 Dallas Semiconductor DS1621 Thermometer/Thermostat
Functions
These functions are intended for easy interfacing between C programs and the DS1621 I2C bus
thermometer/thermostat.
The prototypes for these functions are placed in the file ds1621.h, located in the ..\INC subdirectory.
This file must be #include -ed before using the functions.
The I2C bus functions prototypes are automatically #include -ed with the ds1621.h.
Prior to #include -ing the ds1621.h file, you must declare which microcontroller port and port bits are
used for communication with the DS1621 through the I2C bus.
Example:
/* the I2C bus is connected to PORTB */
/* the SDA signal is bit 3 */
/* the SCL signal is bit 4 */
#asm
.equ __i2c_port=0x18
.equ __sda_bit=3
.equ __scl_bit=4
#endasm
/* now you can include the DS1621 Functions */
#include <ds1621.h>
The DS1621 Functions are:
void ds1621_init(unsigned char chip,signed char tlow,signed char thigh, unsigned char pol)
this function initializes the DS1621 chip.
Before calling this function the I2C bus must be initialized by calling the i2c_init function.
This is the first function that must be called prior to using the other DS1621 Functions.
If more then one chip is connected to the I2C bus, then the function must be called for each one,
specifying accordingly the function parameter chip.
Maximum 8 DS1621 chips can be connected to the I2C bus, their chip address can be from 0 to 7.
Besides measuring temperature, the DS1621 functions also like a thermostat.
The Tout output becomes active when the temperature exceeds the thigh limit, and leaves the active
state when the temperature drops below the tlow limit.
Both tlow and thigh are expressed in °C.
pol represents the polarity of the DS1621 Tout output in active state.
If pol is 0, the output is active low and if pol is 1, the output is active high.
Refer to the DS1621 data sheet for more information.
unsigned char ds1621_get_status(unsigned char chip)
this function reads the contents of the configuration/status register of the DS1621 with address
chip.
Refer to the DS1621 data sheet for more information about this register.
void ds1621_set_status(unsigned char chip, unsigned char data)
this function sets the contents of the configuration/status register of the DS1621 with address
chip.
Refer to the DS1621 data sheet for more information about this register.
void ds1621_start(unsigned char chip)
this functions exits the DS1621, with address chip, from the power-down mode and starts the
temperature measurements and the thermostat.
void ds1621_stop(unsigned char chip)
this functions enters the DS1621, with address chip, in power-down mode and stops the
temperature measurements and the thermostat.
int ds1621_temperature_10(unsigned char chip)
this function returns the temperature of the DS1621 sensor with the address chip.
The temperature is in °C and is multiplied by 10.
Example how to display the temperature of two DS1621 sensors with addresses 0 and 1:
/* the DS1621 I2C bus is connected to PORTB */
/* the SDA signal is bit 3 */
/* the SCL signal is bit 4 */
#asm
.equ __i2c_port=0x18
.equ __sda_bit=3
.equ __scl_bit=4
#endasm
/* include the DS1621 Functions */
#include <ds1621.h>
/* the LCD module is connected to PORTC */
#asm
.equ __lcd_port=0x15
#endasm
/* include the LCD Functions */
#include <lcd.h>
/* include the prototype for sprintf */
#include <stdio.h>
/* include the prototype for abs */
#include <math.h>
char display_buffer[33];
void main(void) {
int t0,t1;
/* initialize the LCD, 2 rows by 16 columns */
lcd_init(16);
/* initialize the I2C bus */
i2c_init();
/* initialize the DS1621 sensor with address 0 */
/* tlow=20°C thigh=25°C */
ds1621_init(0,20,25,0);
/* initialize the DS1621 sensor with address 1 */
/* tlow=30°C thigh=35°C */
ds1621_init(1,30,35,0);
/* temperature display loop */
while (1)
{
/* read the temperature of DS1621 #0 *10°C */
t0=ds1621_temperature_10(0);
/* read the temperature of DS1621 #1 *10°C */
t1=ds1621_temperature_10(1);
/* prepare the displayed temperatures */
/* in the display_buffer */
sprintf(display_buffer,
"t0=%-i.%-u%cC\nt1=%-i.%-u%cC",
t0/10,abs(t0%10),0xdf,t1/10,abs(t1%10),0xdf);
/* display the temperatures */
lcd_clear();
lcd_puts(display_buffer);
};
}