|
The self-made class LM75The next step for a comfortable access to the temperature sensor of type LM75 is to implement an own class called The following source code shows the implementation of the class
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 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 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:
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 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. |