Library for the LM75A Temperature Sensor
Dependents: LM75A_Test_Code MQTT_and_Temperature_Sensore_LM75 2021_Temp_y_Acelerom_V4enlace_serial
LM75A.cpp@1:19dc98c810a5, 2014-08-15 (annotated)
- Committer:
- edodm85
- Date:
- Fri Aug 15 11:56:39 2014 +0000
- Revision:
- 1:19dc98c810a5
- Parent:
- 0:94c821cb7f80
Fixed temp register
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
edodm85 | 0:94c821cb7f80 | 1 | /* Author: Edoardo De Marchi */ |
edodm85 | 0:94c821cb7f80 | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
edodm85 | 0:94c821cb7f80 | 3 | * |
edodm85 | 0:94c821cb7f80 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
edodm85 | 0:94c821cb7f80 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
edodm85 | 0:94c821cb7f80 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
edodm85 | 0:94c821cb7f80 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
edodm85 | 0:94c821cb7f80 | 8 | * furnished to do so, subject to the following conditions: |
edodm85 | 0:94c821cb7f80 | 9 | * |
edodm85 | 0:94c821cb7f80 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
edodm85 | 0:94c821cb7f80 | 11 | * substantial portions of the Software. |
edodm85 | 0:94c821cb7f80 | 12 | * |
edodm85 | 0:94c821cb7f80 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
edodm85 | 0:94c821cb7f80 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
edodm85 | 0:94c821cb7f80 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
edodm85 | 0:94c821cb7f80 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
edodm85 | 0:94c821cb7f80 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
edodm85 | 0:94c821cb7f80 | 18 | */ |
edodm85 | 0:94c821cb7f80 | 19 | |
edodm85 | 0:94c821cb7f80 | 20 | |
edodm85 | 0:94c821cb7f80 | 21 | |
edodm85 | 0:94c821cb7f80 | 22 | |
edodm85 | 0:94c821cb7f80 | 23 | #include "LM75A.h" |
edodm85 | 0:94c821cb7f80 | 24 | |
edodm85 | 0:94c821cb7f80 | 25 | |
edodm85 | 0:94c821cb7f80 | 26 | |
edodm85 | 0:94c821cb7f80 | 27 | LM75A::LM75A(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) |
edodm85 | 0:94c821cb7f80 | 28 | { |
edodm85 | 1:19dc98c810a5 | 29 | reg_addr = TEMP_REG_ADDR; |
edodm85 | 0:94c821cb7f80 | 30 | } |
edodm85 | 0:94c821cb7f80 | 31 | |
edodm85 | 0:94c821cb7f80 | 32 | |
edodm85 | 0:94c821cb7f80 | 33 | LM75A::~LM75A() |
edodm85 | 0:94c821cb7f80 | 34 | { |
edodm85 | 0:94c821cb7f80 | 35 | |
edodm85 | 0:94c821cb7f80 | 36 | } |
edodm85 | 0:94c821cb7f80 | 37 | |
edodm85 | 0:94c821cb7f80 | 38 | |
edodm85 | 0:94c821cb7f80 | 39 | |
edodm85 | 0:94c821cb7f80 | 40 | float LM75A::read_T() |
edodm85 | 0:94c821cb7f80 | 41 | { |
edodm85 | 0:94c821cb7f80 | 42 | |
edodm85 | 1:19dc98c810a5 | 43 | m_i2c.write(m_addr, ®_addr, 1); // Pointer to the temperature register |
edodm85 | 0:94c821cb7f80 | 44 | char cmd[2] = {0,0}; |
edodm85 | 0:94c821cb7f80 | 45 | m_i2c.read(m_addr, cmd, 2); // read temperature register |
edodm85 | 0:94c821cb7f80 | 46 | |
edodm85 | 0:94c821cb7f80 | 47 | unsigned short val = ((cmd[0] << 8) + cmd[1]) >> 7; //val = (cmd[ 1 ] << 1) | ( cmd[ 0 ] >> 7 ) ; |
edodm85 | 0:94c821cb7f80 | 48 | |
edodm85 | 0:94c821cb7f80 | 49 | float temp = (float) ((float)val * 0.5); |
edodm85 | 0:94c821cb7f80 | 50 | |
edodm85 | 0:94c821cb7f80 | 51 | return temp; |
edodm85 | 0:94c821cb7f80 | 52 | } |
edodm85 | 0:94c821cb7f80 | 53 | |
edodm85 | 0:94c821cb7f80 | 54 | |
edodm85 | 0:94c821cb7f80 | 55 | |
edodm85 | 0:94c821cb7f80 | 56 | char LM75A::read_reg(char addr) |
edodm85 | 0:94c821cb7f80 | 57 | { |
edodm85 | 0:94c821cb7f80 | 58 | |
edodm85 | 0:94c821cb7f80 | 59 | char data[1] = {0}; |
edodm85 | 0:94c821cb7f80 | 60 | char tmp = addr; |
edodm85 | 0:94c821cb7f80 | 61 | m_i2c.write(m_addr, &tmp, 1); |
edodm85 | 0:94c821cb7f80 | 62 | m_i2c.read(m_addr, data, 1); // Read register content |
edodm85 | 0:94c821cb7f80 | 63 | |
edodm85 | 0:94c821cb7f80 | 64 | return data[0]; |
edodm85 | 0:94c821cb7f80 | 65 | |
edodm85 | 0:94c821cb7f80 | 66 | } |
edodm85 | 0:94c821cb7f80 | 67 | |
edodm85 | 0:94c821cb7f80 | 68 | |
edodm85 | 0:94c821cb7f80 | 69 | |
edodm85 | 0:94c821cb7f80 | 70 | void LM75A::write_reg(char addr, char data) |
edodm85 | 0:94c821cb7f80 | 71 | { |
edodm85 | 0:94c821cb7f80 | 72 | |
edodm85 | 0:94c821cb7f80 | 73 | char data2[2] = {0, 0}; |
edodm85 | 0:94c821cb7f80 | 74 | |
edodm85 | 0:94c821cb7f80 | 75 | data2[0] = addr; |
edodm85 | 0:94c821cb7f80 | 76 | data2[1] = data; |
edodm85 | 0:94c821cb7f80 | 77 | |
edodm85 | 0:94c821cb7f80 | 78 | m_i2c.write(m_addr, data2, 2); |
edodm85 | 0:94c821cb7f80 | 79 | |
edodm85 | 0:94c821cb7f80 | 80 | } |
edodm85 | 0:94c821cb7f80 | 81 |