#define KS103 0xe8 // default address of KS103 is 0xe8
#define reduce_noise 0x71 // these lines define the data for commands. See data sheat.
#define turn_off_led 0xc1
#define read_distance 0xb0 // distance may be less than 5m .
#define read_brightness 0xa0
#define read_temperature 0xc9
word reading=0; // thease variables are for printing the temperture through Serial.print().
void setup()
{
Serial.begin(9600); // start Serial transaction
delaydelayMicroseconds(90);
Serial.print(KS103);
delaydelayMicroseconds(90);
Serial.print(0x02);
delaydelayMicroseconds(90);
Serial.print(reduce_noise); // send the noise reduction command to KS103
delay(2000); // wait for being stabilized 2seconds
}
void loop()
{
Serial.flush();//清空串口緩存
Serial.print(KS103); // measure the distance
delaydelayMicroseconds(90);
Serial.print(0x02);
delaydelayMicroseconds(90);
Serial.print(read_distance);
if(2 <= Serial.available()) // wait the register available
{
reading = Serial.read(); // read register 2
reading = reading << 8; // shift the data read to upper byte
reading |= Serial.read(); // read the register 2 to lower byte
//數(shù)據(jù)一般都是16位的,要兩次read才可以都會(huì)數(shù)據(jù)
}
delay(250); //wait to be read on the screen of PC.
}
#include
#define KS103 0xe8 // default address of KS103 is 0xe8
#define reduce_noise 0x71 // these lines define the data for commands. See data sheat.
#define turn_off_led 0xc1
#define read_distance 0xb0 // distance may be less than 5m .
#define read_brightness 0xa0
#define read_temperature 0xc9
word reading=0; // thease variables are for printing the temperture through Serial.print().
word reading2=0;
void setup()
{
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
Wire.begin(); // join the TWI as the master
Serial.begin(9600); // start Serial transaction
//Wire.begin(KS103);
KS103_command(reduce_noise); // send the noise reduction command to KS103
delay(2000); // wait for being stabilized 2seconds
KS103_command(turn_off_led); // turn off the LED
}
Serial.print("deg.");
Serial.print(" brightness ");
Serial.print(KS103_read_command(read_brightness)); // measure light
Serial.println(".");
delay(250); //wait to be read on the screen of PC.
}
String reading_temperature(){ // read the temperature data
word reading = KS103_read_command(read_temperature);
word reading2 = reading & 0x0f;
reading = reading >> 4; // shift low byte 4 bits
return (String)reading + (String)reading2;
}
word KS103_read_command(byte command){ // sending the command and read the data in the register
word reading =0;
// step 1: instruct sensor to read echoes
KS103_command(command); // send the command
// step 2: wait for readings to happen
delay(10); // wait 1 milliseconds // I'm not sure if this line is neccesary. I mimic the sketch for other TWI devices.
// step 3: instruct sensor to return a particular echo reading
Wire.beginTransmission(KS103); // start to transmit with KS103
Wire.write(byte(0x02)); // register 2 is the gate of receiving the data
Wire.endTransmission(); // stop transmitting
// step 4: request reading from sensor
Wire.requestFrom(KS103, 2); // request the data in the 2nd and thir register of KS103
// step 5: receive reading from sensor
if(2 <= Wire.available()) // wait the register available
{
reading = Wire.read(); // read register 2
reading = reading << 8; // shift the data read to upper byte
reading |= Wire.read(); // read the register 3 to lower byte
}
return reading; // return the 16bit data
}
void KS103_command(byte command){ // send the command to KS103
Wire.beginTransmission(KS103); // start the transmission with KS103
Wire.write(byte(0x02)); // register 2 Wire.write(byte(0x02));
Wire.write(command); // send the command to the register 2
Wire.endTransmission(); // end of transmission
delay(30);//added by chenyu
} 作者: ITEAD創(chuàng)易工作室 時(shí)間: 2014-6-18 17:42
好貼怎么沒(méi)人頂?作者: 451506709 時(shí)間: 2014-8-9 15:57
不錯(cuò),KS103超聲波還是不錯(cuò)的