|
今天開始玩單片機(jī),選的是Arduino。因?yàn)閍rduino作為單片機(jī)入門級別的,使用起來很方便。
今天做的是加了超聲波傳感器。
一般來說,市面上的超聲波傳感器分為兩種:一種是trigger型;一種是尋址發(fā)指令型。(這是我自己的定義,如果有錯(cuò)請勿見怪。。
trigger型比較簡單,就是通過一個(gè)IO口作為Input,通過高低電壓驅(qū)動這個(gè)trigger,超聲波開始工作。然后再用pulseIn(EchoPin, HIGH)計(jì)算往返時(shí)間,再用公式計(jì)算距離。這種方法比較單一,簡單,這樣的超聲波價(jià)格較低,精度還不錯(cuò)。可以說是物美價(jià)廉。下面貼上代碼:
const int TrigPin = 2; //用arduino的數(shù)字口2定義為Trigpin,用于輸入觸發(fā)
const int EchoPin = 3; //用arduino的數(shù)字口3定義為EchoPin,用于返回時(shí)間
float cm; //距離
void setup()
{
Serial.begin(9600); //波特率,一般為9600,這里的是串口的,這里用串口實(shí)際上是為了用到串口監(jiān)視器看得到的數(shù)據(jù)
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
}
void loop()
{
digitalWrite(TrigPin, LOW); //低高低電平發(fā)一個(gè)短時(shí)間脈沖去TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
cm = pulseIn(EchoPin, HIGH) / 58.0; //將回波時(shí)間換算成cm
cm = (int(cm * 100.0)) / 100.0; //保留兩位小數(shù)
Serial.print(cm); //用串口監(jiān)視器。在arduino IDE的tool的serial monitor那里可以看到監(jiān)視器。上面
//是send,發(fā)送數(shù)據(jù),下面是數(shù)據(jù)print的內(nèi)容(這是串口發(fā)送數(shù)據(jù)出去的內(nèi)容,這里就用這個(gè)來看收到的數(shù)
//據(jù))。rx的串口的接收端,tx是發(fā)送端。
Serial.print("cm");
Serial.println();
delay(1000);
}
具體連線:將超聲波trigger端和arduino的數(shù)字接口2相連,EchoPin端和數(shù)字接口3相連,VCC連5v,gnd連地。接著燒入代碼,就可以檢測到距離了。
另一種是尋址發(fā)指令類型的:
這里要用到TTL串口(RX和TX)或者I2C(wire型,總線類型?梢耘獛资畟(gè)超聲波)。
在這里我就舉TTL串口的例子吧!使用的超聲波是KS103
超聲波工作原理是主機(jī)(就是Uno了)發(fā)送指令給超聲波,首先是發(fā)送超聲波的地址,找到地址。如KS103的地址是0xe8,但是uno只能用7位地址的話,就要右移一位了。接著發(fā)送指令0x02,這是寄存器,返回的數(shù)據(jù)就存在里面。然后是指令超聲波的工作模式,具體看超聲波的文檔。下面是代碼實(shí)現(xiàn):
#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才可以都會數(shù)據(jù)
}
delay(250); //wait to be read on the screen of PC.
}
連線如下:將超聲波RX與主機(jī)TX相連,TX與主機(jī)RX相連。VCC和GND就不用說了。
這樣就完成了TTL串口的超聲波。當(dāng)然,這里只用到了測距,沒有用到溫度傳感器和亮度傳感器。這種超聲波傳感器是比較高級的,這些傳感器上面也都有。
接下來我說一下I2C的超聲波使用:限于篇幅,我就簡單地貼貼代碼:
#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
}
void loop()
{
digitalWrite(6,LOW);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(7,HIGH);
Serial.println(KS103_read_command(read_distance)); // measure the distance
//Serial.print(" ");
reading = KS103_read_command(read_temperature); // measure the temerature
reading2 = reading & 0x0f;
reading = reading >> 4; // shift low byte 4 bits
Serial.print(reading);
//Serial.print('.');
Serial.print(reading2);
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
}
|
|