1 // Copyright (c) 2018, Tobias Mueller tm(at)tm3d.de
\r
2 // All rights reserved.
\r
4 // Redistribution and use in source and binary forms, with or without
\r
5 // modification, are permitted provided that the following conditions are
\r
8 // * Redistributions of source code must retain the above copyright
\r
9 // notice, this list of conditions and the following disclaimer.
\r
10 // * Redistributions in binary form must reproduce the above copyright
\r
11 // notice, this list of conditions and the following disclaimer in the
\r
12 // documentation and/or other materials provided with the
\r
14 // * All advertising materials mentioning features or use of this
\r
15 // software must display the following acknowledgement: This product
\r
16 // includes software developed by tm3d.de and its contributors.
\r
17 // * Neither the name of tm3d.de nor the names of its contributors may
\r
18 // be used to endorse or promote products derived from this software
\r
19 // without specific prior written permission.
\r
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
\r
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
\r
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
\r
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
\r
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
\r
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
\r
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
\r
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
\r
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
33 //---------- Includes ----------------------------------------------------------
\r
34 #define F_CPU 8000000UL
\r
36 #include <avr/interrupt.h>
\r
37 #include <util/delay.h>
\r
38 #include <avr/wdt.h>
\r
39 #include <avr/sleep.h>
\r
40 #include "USI_TWI_Master.h"
\r
41 #include "LPS225HB.h"
\r
43 I2C_ADR_W = 0b10111000, // sensor I2C address + write bit //ADR=VDD
\r
44 I2C_ADR_R = 0b10111001 // sensor I2C address + read bit
\r
47 uint8_t LPS225HB_Init() {
\r
48 volatile uint8_t error=0;
\r
49 /*I2c_StartCondition();
\r
50 error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr
\r
51 error |= I2c_WriteByte (0x02); // Config
\r
52 error |= I2c_WriteByte (0x0); // 14 bit
\r
53 error |= I2c_WriteByte (0x0); //
\r
54 I2c_StopCondition();*/
\r
57 I2c_StartCondition();
\r
58 error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr
\r
59 error |= I2c_WriteByte (0x11); //
\r
60 //error |= I2c_WriteByte (0b01110001); //
\r
61 error |= I2c_WriteByte (0b00010100); //
\r
62 I2c_StopCondition();
\r
67 uint8_t LPS225HB_Readf(double * temperature, double * pressure) {
\r
71 error= LPS225HB_Readi(&t,&p);
\r
72 *temperature=(double)t/100.0;
\r
73 *pressure=(double)p/4096.0;
\r
77 uint8_t readReg(uint8_t reg) {
\r
78 volatile uint8_t error=0;
\r
80 I2c_StartCondition();
\r
81 error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr
\r
82 error |= I2c_WriteByte (reg); //
\r
83 I2c_StartCondition();
\r
84 error |= I2c_WriteByte (I2C_ADR_R); //I2C address
\r
85 ret= I2c_ReadByte(NO_ACK);
\r
86 I2c_StopCondition();
\r
90 uint8_t LPS225HB_Readi(int16_t * temperature, uint32_t * pressure) {
\r
91 volatile uint8_t error=0;
\r
92 I2c_StartCondition();
\r
93 error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr
\r
94 error |= I2c_WriteByte (0x11); //
\r
95 //error |= I2c_WriteByte (0b00000001); //
\r
96 error |= I2c_WriteByte (0b01010001); //
\r
97 I2c_StopCondition();
\r
99 // uint16_t status=0;
\r
100 // status=readReg(0x27);
\r
101 // status|=readReg(0x26)<<8;
\r
103 // *pressure|= readReg(0x28);
\r
104 // *pressure|= ((uint32_t)readReg(0x29))<<8;
\r
105 // *pressure|= ((uint32_t)readReg(0x2A))<<16;
\r
108 I2c_StartCondition();
\r
109 error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr
\r
110 error |= I2c_WriteByte (0x28); //
\r
111 I2c_StartCondition();
\r
112 error |= I2c_WriteByte (I2C_ADR_R); //I2C address
\r
113 // status= I2c_ReadByte(ACK)<<8;
\r
114 // status|= I2c_ReadByte(ACK);
\r
118 *pressure|= I2c_ReadByte(ACK);
\r
119 *pressure|= ((uint32_t)I2c_ReadByte(ACK))<<8;
\r
120 *pressure|= ((uint32_t)I2c_ReadByte(ACK))<<16;
\r
122 *temperature |= I2c_ReadByte(ACK);
\r
123 *temperature |= ((int16_t)I2c_ReadByte(NO_ACK)<<8);
\r
124 I2c_StopCondition();
\r
125 //*temperature=status;
\r