Humidity and Temperature Sensor - Sensirion SHT1x driver
Dependents: ProgrammePrincipal Wetterstation project1 4180_Project ... more
i2c.cpp
00001 /** 00002 * Copyright (c) 2010 Roy van Dam <roy@negative-black.org> 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 00014 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 00015 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00016 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00017 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 00018 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00019 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00020 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00021 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00022 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00023 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00024 * SUCH DAMAGE. 00025 */ 00026 00027 #include "i2c.hpp" 00028 00029 namespace SHTx { 00030 I2C::I2C(PinName sda, PinName scl) : 00031 scl_pin(scl), sda_pin(sda), frequency(10) { 00032 this->sda_pin.output(); 00033 this->scl_pin.output(); 00034 } 00035 00036 void 00037 I2C::setFrequency(uint32_t hz) { 00038 this->frequency = (1000000 / hz); 00039 } 00040 00041 void 00042 I2C::start(void) { 00043 this->output(); 00044 this->sda(1); 00045 this->scl(1); 00046 this->sda(0); 00047 this->scl(0); 00048 this->scl(1); 00049 this->sda(1); 00050 this->scl(0); 00051 } 00052 00053 void 00054 I2C::stop(void) { 00055 this->output(); 00056 this->sda(0); 00057 this->scl(1); 00058 this->sda(1); 00059 } 00060 00061 bool 00062 I2C::wait(void) { 00063 bool ack = false; 00064 00065 this->input(); 00066 for (uint8_t i = 0; i < 500 && !ack; i++) { 00067 wait_ms(1); 00068 ack = !this->sda_pin; 00069 } 00070 00071 return ack; 00072 } 00073 00074 void 00075 I2C::reset(void) { 00076 this->output(); 00077 for (uint8_t i = 9; i; i--) { 00078 this->shift_out(1); 00079 } 00080 this->start(); 00081 this->scl(1); 00082 } 00083 00084 bool 00085 I2C::write(uint8_t data) { 00086 bool ack; 00087 00088 this->output(); 00089 for (uint8_t i = 8; i; i--) { 00090 this->shift_out(data & 0x80); 00091 data <<= 1; 00092 } 00093 00094 this->input(); 00095 ack = !this->shift_in(); 00096 00097 return ack; 00098 } 00099 00100 uint8_t 00101 I2C::read(bool ack) { 00102 uint8_t data = 0; 00103 00104 this->input(); 00105 for (uint8_t i = 8; i; i--) { 00106 data <<= 1; 00107 data |= this->shift_in(); 00108 } 00109 00110 this->output(); 00111 this->shift_out(!ack); 00112 00113 return data; 00114 } 00115 00116 void 00117 I2C::output(void) { 00118 this->sda_pin.output(); 00119 } 00120 00121 void 00122 I2C::input(void) { 00123 this->sda_pin.input(); 00124 } 00125 00126 void 00127 I2C::sda(bool value) { 00128 this->sda_pin = value; 00129 wait_us(this->frequency); 00130 } 00131 00132 void 00133 I2C::scl(bool value) { 00134 this->scl_pin = value; 00135 wait_us(this->frequency); 00136 } 00137 00138 void 00139 I2C::shift_out(bool bit) { 00140 this->sda(bit); 00141 this->scl(1); 00142 this->scl(0); 00143 } 00144 00145 bool 00146 I2C::shift_in(void) { 00147 wait_us(this->frequency); 00148 this->scl(1); 00149 bool bit = this->sda_pin; 00150 this->scl(0); 00151 return bit; 00152 } 00153 }
Generated on Wed Jul 13 2022 17:19:11 by 1.7.2