Hallo Floyd,
Daher nun meine Frage, ob jemand schon diese
Sensoren zufriedenstellend zum laufen bekommen hat?!
ja ich
Das angehängte Programm bedient 3 Module, die anderen lassen sich aber problemlos raustreichen und gibt die Werte auf ein LCD aus.
Mit dabei sind auch Funktionen zum ändern der I2C-Adresse des Moduls.
Allerdings verwende ich die geänderten I2C-Funktionen von cc2net.de.
HTH und Viele Grüße
Jörg
Code:
const addrSRFFrontLeft = 0xE0;
const addrSRFFront = 0xE2;
const addrSRFFrontRight = 0xE4;
string s;
int val1, val2, val3;
function ModulWrite (byte addr, byte reg, byte Val)
{
i2c.cstart (addr);
i2c.write (reg);
i2c.write (Val);
i2c.stop ();
}
function ModulRead (byte addr, byte reg) returns byte
{
byte val;
i2c.cstart (addr);
i2c.write (reg);
// i2c.stop ();
i2c.start (addr+1);
val = i2c.readlast();
i2c.stop();
return val;
}
function ModulReadWord (byte addr, byte reg) returns int
{
int val;
i2c.cstart (addr);
i2c.write (reg);
// i2c.stop ();
i2c.start (addr+1);
val = i2c.read();
val = val << 8;
val = val + i2c.readlast();
i2c.stop();
return val;
}
function GetVersion (byte addr) returns byte
{
return ModulRead (addr, 0);
}
function SetAddress (byte addrSrc, byte addrTarg)
{
i2c.cstart (addrSrc);
i2c.write (0); // Register 0 Command Register
i2c.write (0xa0); // 1. Byte Sequ.
i2c.start (addrSrc);
i2c.write (0); // Register 0 Command Register
i2c.write (0xaa); // 2. Byte Sequ.
i2c.start (addrSrc);
i2c.write (0); // Register 0 Command Register
i2c.write (0xa5); // 3. Byte Sequ.
i2c.start (addrSrc);
i2c.write (0); // Register 0 Command Register
i2c.write (addrTarg);
i2c.stop();
}
function Measure ()
{
ModulWrite (0, 0, 0x51); // Messung auslösen (broadcast)
sleep (70);
val1 = ModulReadWord (addrSRFFrontLeft, 2);
val2 = ModulReadWord (addrSRFFront, 2);
val3 = ModulReadWord (addrSRFFrontRight, 2);
}
thread main
{
lcd.clear();
s = "";
str.putint (s, GetVersion (addrSRFFrontLeft));
lcd.print(s, str.length(s));
sleep 1000;
s = s + " ";
lcd.clear();
str.putint (s, GetVersion (addrSRFFront));
lcd.print(s, str.length(s));
sleep 1000;
s = s + " ";
lcd.clear();
str.putint (s, GetVersion (addrSRFFrontRight));
lcd.print(s, str.length(s));
sleep 5000;
loop
{
Measure ();
lcd.clear();
s = "";
str.putint (s, val1);
s = s + " ";
str.putint (s, val2);
s = s + " ";
str.putint (s, val3);
lcd.print(s, str.length(s));
sleep 500;
}
sleep 5000;
lcd.clear();
quit 1;
}
Lesezeichen