温度センサのプログラム(PCに表示)

Dependencies:   mbed

Committer:
MIKUNAGATA
Date:
Sat Nov 16 06:14:54 2019 +0000
Revision:
0:4b059e3c0849
TEMP

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MIKUNAGATA 0:4b059e3c0849 1
MIKUNAGATA 0:4b059e3c0849 2 /*
MIKUNAGATA 0:4b059e3c0849 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
MIKUNAGATA 0:4b059e3c0849 4
MIKUNAGATA 0:4b059e3c0849 5 Permission is hereby granted, free of charge, to any person obtaining a copy
MIKUNAGATA 0:4b059e3c0849 6 of this software and associated documentation files (the "Software"), to deal
MIKUNAGATA 0:4b059e3c0849 7 in the Software without restriction, including without limitation the rights
MIKUNAGATA 0:4b059e3c0849 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
MIKUNAGATA 0:4b059e3c0849 9 copies of the Software, and to permit persons to whom the Software is
MIKUNAGATA 0:4b059e3c0849 10 furnished to do so, subject to the following conditions:
MIKUNAGATA 0:4b059e3c0849 11
MIKUNAGATA 0:4b059e3c0849 12 The above copyright notice and this permission notice shall be included in
MIKUNAGATA 0:4b059e3c0849 13 all copies or substantial portions of the Software.
MIKUNAGATA 0:4b059e3c0849 14
MIKUNAGATA 0:4b059e3c0849 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
MIKUNAGATA 0:4b059e3c0849 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
MIKUNAGATA 0:4b059e3c0849 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
MIKUNAGATA 0:4b059e3c0849 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
MIKUNAGATA 0:4b059e3c0849 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
MIKUNAGATA 0:4b059e3c0849 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
MIKUNAGATA 0:4b059e3c0849 21 THE SOFTWARE.
MIKUNAGATA 0:4b059e3c0849 22 */
MIKUNAGATA 0:4b059e3c0849 23
MIKUNAGATA 0:4b059e3c0849 24 #include "TMP102.h"
MIKUNAGATA 0:4b059e3c0849 25
MIKUNAGATA 0:4b059e3c0849 26 #define TEMP_REG_ADDR 0x00
MIKUNAGATA 0:4b059e3c0849 27
MIKUNAGATA 0:4b059e3c0849 28 TMP102::TMP102(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr)
MIKUNAGATA 0:4b059e3c0849 29 {
MIKUNAGATA 0:4b059e3c0849 30
MIKUNAGATA 0:4b059e3c0849 31 }
MIKUNAGATA 0:4b059e3c0849 32
MIKUNAGATA 0:4b059e3c0849 33 TMP102::~TMP102()
MIKUNAGATA 0:4b059e3c0849 34 {
MIKUNAGATA 0:4b059e3c0849 35
MIKUNAGATA 0:4b059e3c0849 36 }
MIKUNAGATA 0:4b059e3c0849 37
MIKUNAGATA 0:4b059e3c0849 38 float TMP102::read()
MIKUNAGATA 0:4b059e3c0849 39 {
MIKUNAGATA 0:4b059e3c0849 40
MIKUNAGATA 0:4b059e3c0849 41 const char tempRegAddr = TEMP_REG_ADDR;
MIKUNAGATA 0:4b059e3c0849 42
MIKUNAGATA 0:4b059e3c0849 43 m_i2c.write(m_addr, &tempRegAddr, 1); //Pointer to the temperature register
MIKUNAGATA 0:4b059e3c0849 44
MIKUNAGATA 0:4b059e3c0849 45 char reg[2] = {0,0};
MIKUNAGATA 0:4b059e3c0849 46 m_i2c.read(m_addr, reg, 2); //Rea
MIKUNAGATA 0:4b059e3c0849 47
MIKUNAGATA 0:4b059e3c0849 48 int16_t res = ((int8_t)reg[0] << 4) | ((uint8_t)reg[1] >> 4);
MIKUNAGATA 0:4b059e3c0849 49
MIKUNAGATA 0:4b059e3c0849 50 float temp = (float) ((float)res * 0.0625);
MIKUNAGATA 0:4b059e3c0849 51
MIKUNAGATA 0:4b059e3c0849 52 return temp;
MIKUNAGATA 0:4b059e3c0849 53 }