The self-made class LM75

The next step for a comfortable access to the temperature sensor of type LM75 is to implement an own class called LM75. This ought to provide all functions of the real component in form of Java methods. Specificly this means that the measured temperature may be acquired via a method called getTemp(). This way the class LM75 smartly adopts the functionality of a specialised "device driver" for the LM75.

The following source code shows the implementation of the class LM75. It consists of two parts: The constructor, which creates an instance of this class and the method getTemp(), which returns the read temperature value.

1    import java.io.IOException;
2    import jcontrol.comm.I2C;
3    
4    /**
5     * <p>LM75 is a class to access the I2C-compatible
6     * temperature sensor LM75 from National Semiconductor.</p>
7     *
8     * <p>(C) DOMOLOGIC Home Automation GmbH 2003-2005</p>
9     */
10    public class LM75 extends I2C{
11    
12      /**
13       *  Constructor of this class.
14       *
15       *  @param address slave address of the device
16       */
17        public LM75(int address){
18          super(address);
19        }
20    
21        /**
22         *  Returns an Integer value representing the
23         *  current die temperatur of the LM75.
24         *
25         *  @returns An integer representing the current
26         *           temperature in "Centigrades" (10*degree Celsius).
27         */
28        public int getTemp() throws IOException {
29            // allocate buffer for temperature
30            byte[] buf = new byte[2];
31            // read content of 16-bit register #0x00
32            read(new byte[] {0x00}, buf, 0, 2);
33            // return integer value
34            return (buf[0]*10) + (((buf[1] & 0x80) >> 7)*5);
35        }
36    }
Listing 1: LM75.java

In listing 1 can be seen that the method getTemp() is encapsulating the I²C communication with the temperature sensor. Thus the programmer, who hencforth ought to use the class LM75, only has to know the sensors' slave address in order to read out the current temperature value via method getTemp().

Line 32 shows the actual function call, that reads the temperature value form the sensor. It can also be seen, that the command byte 0x00 (for details: see below) is sent to tell the LM75 to sent its temperature value. This is thereupon written into the byte array buf (shortform of "buffer").

The temperature value consists of a total of 9 bits and therefore has to be written into two bytes. The first byte contains the integer part of the temperature (in °C). The most significant bit (MSB = bit 7) acts as a signum bit: A logical "1" represents a negative temperature value. This is just for information and doesn't matter for the conversion formula which returns the temperature value, because JControl uses the same data format. The remaining decimals are contained within the second byte. In case of the LM75, only the MSB of this byte is used and resolves to 0,5°C. So if the second byte equals 0x00, the remainder results in 0,0°C and if its value equals 0x80, the remainder results in 0,5°C. The formula, which converts the two bytes into the temperature value is:

    return (buf[0]*10) + (((buf[1] & 0x80) >> 7)*5);

As can be seen, the integer part is multiplied by 10 and the decimal by 5 (actually by 10/2). This is done, because the small JControl versions do not support floating-point numbers. The method getTemp() thus returns the measured temperature value in 10*°C: In case of an actual temperature of 20,5°C, the value 205 will be returned.

If the pins of the LM75 are connected as shown in section "The experimental set-up", its slave address is set to 144 (= 0x90 in hexadecimal). This address will therefore be used in the next section A small testprogram to control the temperature sensor.

Details of the comand byte: The comand byte tells the sensor, which internal register has to be accessed. The temperature value of the LM75 is located in register 0, therefore the comand byte has to be 0 (= 0x00 in hexadecimal) in order to access this value. All stated information regarding the LM75, as well as further details may be seen in its data sheet.