Go Down

Topic: Sensor an der I2C Schnittstelle (Read 1 time) previous topic - next topic

GekoCH

#10
Mar 22, 2012, 08:55 am Last Edit: Mar 22, 2012, 09:06 am by GekoCH Reason: 1
uuppps Ich *!*?# ok so ich habs :)

Code: [Select]

byte decToBcd(byte val){
 return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val){
 return ( (val/16*10) + (val%16) );
}


void adresseAendern(){
 //Sensor starten
 digitalWrite(13, HIGH);
 delay(2);

 //in Command Mode
 Wire.beginTransmission(adress);
 Wire.send(0xA0);
 Wire.send(0x00);
 Wire.send(0x00);
 Wire.endTransmission();
 delay(100);

 //Adress anfragen
 Wire.beginTransmission(adress);
 Wire.send(0x1C);
 Wire.send(0x00);
 Wire.send(0x00);
 Wire.endTransmission();
 delay(100);

 //Adresse auslesen
 Wire.requestFrom(adress, 3);
 if(Wire.available() > 2){
   byte b1 = bcdToDec(Wire.receive());
   byte b2 = bcdToDec(Wire.receive());
   byte b3 = bcdToDec(Wire.receive());
   /*
   Serial.println((b1), DEC);
    Serial.println((b2), DEC);
    Serial.println((b3), DEC);
    */
   int i2cAdress = b2 << 8 | b3;
   Serial.println((i2cAdress));
 }
 

 //Adress neu setzten
 Wire.beginTransmission(adress);
 Wire.send(0x5C);
 Wire.send(0x00);
 Wire.send(adressNew);
 Wire.endTransmission();
 delay(100);


 //normal Mode
  digitalWrite(13, LOW);
 delay(100);

}

Go Up