Page 326 - ARM Based Microcontroller Projects Using MBED
P. 326

312                              11. I C BUS PROJECTS
                                                     2
                   For negative temperatures:
                 • Shift the HIGH byte by 4 bits to the left into a 16-bit word called say, M.
                 • Shift the LOW byte by 4 bits to the right into a byte called say, L.
                 • Add M and L to find the 16-bit result.
                 • Generate 2’s complement of the result (complement and add 1).
                 • Multiply the result with 0.0625 (resolution) to find the temperature.
                   For example, if the returned two bytes are: 0011 0010 1000 0000

                   M¼0011 0010 0000
                   L¼1000
                   M+L¼001100101000whichisequivalentto808indecimal.Therefore,801 0.0625¼50.5°C.
                   Similarly, for a negative temperature, if the returned bytes are: 1110 0111 1000 0000
                   M¼1110 0111 0000
                   L¼1000
                   M+L¼1110 0111 1000
                   Taking the complement and adding 1 (2’s complement) we get: 0001 1000 0111+1¼0001
                 1000 1000 which is equivalent to 392. Therefore 392 0.0625¼24.5°C, or  24.5°C.
                   Fig. 11.9 shows the program listing (program: TMP102). At the beginning of the program
                                                        2
                 GPIO pins PB_7 and PB_6 are assigned to I C1 with the name TMP102, the device address
                 is set to 0x90, and the registers used in the program are defined. Inside the main program
                 function ConfigureTMP102 is called to configure the sensor to normal mode of operation.
                 The remainder of the program runs in an endless loop. Inside this loop the temperature
                 is read as a 12-bit data and is converted into a 16-bit data in variable M. The actual temper-
                 ature in °C is then calculated and displayed on the PC screen in the format: nn.nn. Notice
                 that only positive temperatures are displayed by this program. The loop is repeated every
                 second.
                   A typical display of the temperature is shown in Fig. 11.10.



                 11.3.8 Suggestions for Additional Work
                   Modify the program given in Fig. 11.9 so that the negative temperatures can also be
                 displayed.




                                                11.4 SUMMARY

                   In this chapter we have learned about the following:
                    2
                 •I C bus
                          2
                 • Mbed I C bus functions
                                     2
                 • A project using an I C compatible device
   321   322   323   324   325   326   327   328   329   330   331