Das I2C Zeug ist von hier
Code:
#ifndef _I2CMASTER_H
#define _I2CMASTER_H 1
/*************************************************************************
* Title: C include file for the I2C master interface
* (i2cmaster.S or twimaster.c)
* Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury
* File: $Id: i2cmaster.h,v 1.10 2005/03/06 22:39:57 Peter Exp $
* Software: AVR-GCC 3.4.3 / avr-libc 1.2.3
* Target: any AVR device
* Usage: see Doxygen manual
**************************************************************************/
Lesen und schreiben sieht so aus:
Code:
char bmp180_readByte(char address, char *value)
{
unsigned char data[1];
data[0] = address;
if (bmp180_readBytes(data,1))
{
*value = data[0];
return 1;
}
value = 0;
return 0;
}
char bmp180_readUByte(char address, unsigned char *value)
{
unsigned char data[1];
data[0] = address;
if (bmp180_readBytes(data,1))
{
*value = data[0];
return 1;
}
value = 0;
return 0;
}
char bmp180_readInt(char address, int *value)
{
unsigned char data[2];
data[0] = address;
if (bmp180_readBytes(data,2))
{
*value = (((int)data[0]<<8)|(int)data[1]);
return 1;
}
value = 0;
return 0;
}
char bmp180_readUInt(char address, unsigned int *value)
{
unsigned char data[2];
data[0] = address;
if (bmp180_readBytes(data,2))
{
*value = (((unsigned int)data[0]<<8)|(unsigned int)data[1]);
return 0;
}
value = 0;
return 1;
}
char bmp180_writeByte(char address, char value)
{
unsigned char data[2];
data[0] = address;
data[1] = value;
if (bmp180_writeBytes(data,1))
{
return 1;
}
value = 0;
return 0;
}
char bmp180_readBytes(unsigned char *values, char length)
{
unsigned char ret, res = 0, zc = 0;
unsigned char addr = values[0];
while(zc < length)
{
ret = i2c_start(BMP180+I2C_WRITE); // ret=0 -> Ok, ret=1 -> no ACK
if (!ret)
{
ret = i2c_write(addr + zc); // write Instruction Byte
res = res + ret;
i2c_stop(); // set stop conditon = release bus
}
res = res + ret;
ret = i2c_rep_start(BMP180+I2C_READ); // ret=0 -> Ok, ret=1 -> no ACK
if (!ret)
{
values[zc] = i2c_readNak(); // read one byte
res = res + ret;
i2c_stop(); // set stop conditon = release bus
}
res = res + ret;
if(res) return 0;
zc++;
}
return 1;
}
char bmp180_writeBytes(unsigned char *values, char length)
{
unsigned char ret, res = 0, zc = 0;
unsigned char addr = values[0];
while(zc < length)
{
ret = i2c_start(BMP180+I2C_WRITE); // ret=0 -> Ok, ret=1 -> no ACK
if (!ret)
{
ret = i2c_write(addr + zc); // write Instruction Byte
res = res + ret;
ret = i2c_write(values[zc + 1]); // Date Byte
res = res + ret;
i2c_stop(); // set stop conditon = release bus
}
res = res + ret;
if(res) return 0;
zc++;
}
return 1;
}
Ich hab auch nur 3 Stück in Benutzung. Wenn damit die selben Fehler auftreten, dann liegts an den Sensoren oder irgend etwas anderem. Ist auch schon wieder eine Weile her. Ich sammle zwar die Werte noch, aber werte die zZ nicht aus.
Die cal Werte je Sensor muss man auch nur einmal auslesen. Laut Hersteller ändern die sich ja lebenslang nicht mehr. Wichtig ist nur, dass du auch die richtigen cal Werte zur Berechnung des jeweiligen Sensors verwendest. Die unterscheiden sich ja doch etwas. Aber das hast du sicher beachtet.
Lesezeichen