|
| 1 | +/* |
| 2 | + * Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated |
| 3 | + * group companies. |
| 4 | + * |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * |
| 13 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 14 | + * this list of conditions and the following disclaimer in the documentation |
| 15 | + * and/or other materials provided with the distribution. |
| 16 | + * |
| 17 | + * 3. Neither the name of the copyright holder nor the names of its |
| 18 | + * contributors may be used to endorse or promote products derived from this |
| 19 | + * software without specific prior written permission. |
| 20 | + * |
| 21 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 25 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 29 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 30 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | + * POSSIBILITY OF SUCH DAMAGE. |
| 32 | + */ |
| 33 | + |
| 34 | +/** |
| 35 | + * \file |
| 36 | + * Thermo3 click driver. |
| 37 | + * http://www.mikroe.com/click/thermo3/ |
| 38 | + * |
| 39 | + */ |
| 40 | + |
| 41 | +#include <stdio.h> |
| 42 | +#include <stdint.h> |
| 43 | +#include <pic32_i2c.h> |
| 44 | +#include "thermo3-click.h" |
| 45 | +/*---------------------------------------------------------------------------*/ |
| 46 | +void tmp102_init(void) |
| 47 | +{ |
| 48 | + i2c1_init(); |
| 49 | + i2c1_set_frequency (I2C_FREQUENCY); |
| 50 | +} |
| 51 | +/*---------------------------------------------------------------------------*/ |
| 52 | +void tmp102_reg_select(uint8_t reg) |
| 53 | +{ |
| 54 | + i2c1_master_enable(); |
| 55 | + i2c1_send_start(); |
| 56 | + if(i2c1_byte_send (TMP102_REG_WRITE)) { |
| 57 | + i2c1_send_repeated_start(); |
| 58 | + if(i2c1_byte_send (TMP102_REG_WRITE)) { |
| 59 | + printf("Failed the connection to Thermo3\n"); |
| 60 | + } |
| 61 | + } |
| 62 | + switch (reg) { |
| 63 | + case TMP102_TEMP: |
| 64 | + i2c1_byte_send (TMP102_TEMP); |
| 65 | + break; |
| 66 | + case TMP102_CONF: |
| 67 | + i2c1_byte_send (TMP102_CONF); |
| 68 | + break; |
| 69 | + case TMP102_TLOW: |
| 70 | + i2c1_byte_send (TMP102_TLOW); |
| 71 | + break; |
| 72 | + case TMP102_THIGH: |
| 73 | + i2c1_byte_send (TMP102_THIGH); |
| 74 | + break; |
| 75 | + default: |
| 76 | + printf("Invalid Register Address\n"); |
| 77 | + break; |
| 78 | + } |
| 79 | + i2c1_send_stop(); |
| 80 | + i2c1_master_disable(); |
| 81 | +} |
| 82 | +/*---------------------------------------------------------------------------*/ |
| 83 | +void tmp102_reg_read(uint8_t *data) |
| 84 | +{ |
| 85 | + i2c1_master_enable(); |
| 86 | + i2c1_send_start(); |
| 87 | + /*command to TMP102 to Write data on bus */ |
| 88 | + if(i2c1_byte_send (TMP102_REG_READ)) { |
| 89 | + printf("Failed the connection to Thermo3\n"); |
| 90 | + } |
| 91 | + i2c1_byte_receive(data); |
| 92 | + i2c1_byte_receive((data + 1)); |
| 93 | + i2c1_send_stop(); |
| 94 | + i2c1_master_disable(); |
| 95 | +} |
| 96 | +/*---------------------------------------------------------------------------*/ |
| 97 | +void tmp102_reg_write(uint8_t msb, uint8_t lsb) |
| 98 | +{ |
| 99 | + i2c1_master_enable(); |
| 100 | + i2c1_send_start(); |
| 101 | + /*command to TMP102 to Read data from bus */ |
| 102 | + i2c1_byte_send (TMP102_REG_WRITE); |
| 103 | + i2c1_byte_send(msb); |
| 104 | + i2c1_byte_send(lsb); |
| 105 | + i2c1_send_stop(); |
| 106 | + i2c1_master_disable(); |
| 107 | +} |
| 108 | +/*---------------------------------------------------------------------------*/ |
| 109 | +float tmp102_read_temp(void) |
| 110 | +{ |
| 111 | + uint8_t data[2] = { 0, 0 }; |
| 112 | + float temp_c = 0; |
| 113 | + tmp102_reg_read(data); |
| 114 | + /* data[1] having LSB of Temp. Register */ |
| 115 | + if(data[1] & 0x10) { |
| 116 | + temp_c += 0.06250; |
| 117 | + } |
| 118 | + if(data[1] & 0x20) { |
| 119 | + temp_c += 0.12500; |
| 120 | + } |
| 121 | + if(data[1] & 0x40) { |
| 122 | + temp_c += 0.25000; |
| 123 | + } |
| 124 | + if(data[1] & 0x80) { |
| 125 | + temp_c += 0.50000; |
| 126 | + } |
| 127 | + /* data[0] having MSB of Temp. Register */ |
| 128 | + if((data[0] & 0x80) != 0x80) { |
| 129 | + temp_c += ((float) data[0]); |
| 130 | + } |
| 131 | + if((data[0] & 0x80) == 0x80) { |
| 132 | + data[0] = data[0] - 0x01; |
| 133 | + data[0] = ~data[0]; |
| 134 | + temp_c = temp_c - ((float) data[0]); |
| 135 | + } |
| 136 | + return temp_c; |
| 137 | +} |
| 138 | +/*---------------------------------------------------------------------------*/ |
0 commit comments