久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

標(biāo)題: arduino之a(chǎn)rduino uno與超聲波傳感器的使用 [打印本頁]

作者: liumei    時(shí)間: 2014-4-20 23:32
標(biāo)題: arduino之a(chǎn)rduino uno與超聲波傳感器的使用
    今天開始玩單片機(jī),選的是Arduino。因?yàn)閍rduino作為單片機(jī)入門級別的,使用起來很方便。

     今天做的是加了超聲波傳感器。

     一般來說,市面上的超聲波傳感器分為兩種:一種是trigger型;一種是尋址發(fā)指令型。(這是我自己的定義,如果有錯(cuò)請勿見怪。。

    trigger型比較簡單,就是通過一個(gè)IO口作為Input,通過高低電壓驅(qū)動這個(gè)trigger,超聲波開始工作。然后再用pulseIn(EchoPin, HIGH)計(jì)算往返時(shí)間,再用公式計(jì)算距離。這種方法比較單一,簡單,這樣的超聲波價(jià)格較低,精度還不錯(cuò)?梢哉f是物美價(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
}

作者: ITEAD創(chuàng)易工作室    時(shí)間: 2014-6-18 17:42
好貼怎么沒人頂。
作者: 451506709    時(shí)間: 2014-8-9 15:57
不錯(cuò),KS103超聲波還是不錯(cuò)的




歡迎光臨 (http://m.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 亚洲一区在线观看视频 | 成人免费视屏 | 日韩一区二区三区在线 | 国产日韩一区二区 | 视频一二三区 | 天堂网在线资源 | 97精品国产 | 不卡在线视频 | 国产aⅴ爽av久久久久成人 | 永久免费看片在线播放 | 午夜影院在线 | 一区二区三区成人 | 天天操天天看 | 免费网站黄色 | 国产精品婷婷 | 免费av网站在线观看 | 国产精品久久久久久久久久辛辛 | a级在线观看 | 日韩色在线 | 美女黄色大片 | 色综合五月天 | 午夜视频网站 | 影音先锋中文字幕在线 | 一区二区国产在线 | 国产精品一品二区三区的使用体验 | 黄片毛片在线观看 | 亚洲精品乱码 | 久久久久久91 | 日韩av免费在线观看 | 国产高潮在线 | 又色又爽又黄18网站 | 在线观看av网站 | 亚洲午夜18毛片在线看 | 蜜桃综合网 | 黄色在线观看网址 | 日韩欧美亚洲 | 80日本xxxxxxxxx96 国产成人在线免费视频 | 国内自拍xxxx18 | 日韩毛片网站 | 国产黄色免费看 | 视频一区二区在线 |