今天开始玩单片机,选的是Arduino。因为arduino作为单片机入门级别的,使用起来很方便。
今天做的是加了超声波传感器。
一般来说,市面上的超声波传感器分为两种:一种是trigger型;一种是寻址发指令型。(这是我自己的定义,如果有错请勿见怪啊!)
trigger型比较简单,就是通过一个IO口作为Input,通过高低电压驱动这个trigger,超声波开始工作。然后再用pulseIn(EchoPin, HIGH)计算往返时间,再用公式计算距离。这种方法比较单一,简单,这样的超声波价格较低,精度还不错。可以说是物美价廉。下面贴上代码:
const int TrigPin = 2; //用arduino的数字口2定义为Trigpin,用于输入触发
const int EchoPin = 3; //用arduino的数字口3定义为EchoPin,用于返回时间
float cm; //距离
void setup()
{
Serial.begin(9600); //波特率,一般为9600,这里的是串口的,这里用串口实际上是为了用到串口监视器看得到的数据
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
}
void loop()
{
digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
cm = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm
cm = (int(cm * 100.0)) / 100.0; //保留两位小数
Serial.print(cm); //用串口监视器。在arduino IDE的tool的serial monitor那里可以看到监视器。上面
//是send,发送数据,下面是数据print的内容(这是串口发送数据出去的内容,这里就用这个来看收到的数
//据)。rx的串口的接收端,tx是发送端。
Serial.print("cm");
Serial.println();
delay(1000);
}
具体连线:将超声波trigger端和arduino的数字接口2相连,EchoPin端和数字接口3相连,VCC连5v,gnd连地。接着烧入代码,就可以检测到距离了。
另一种是寻址发指令类型的:
这里要用到TTL串口(RX和TX)或者I2C(wire型,总线类型。可以弄几十个超声波)。
在这里我就举TTL串口的例子吧!使用的超声波是KS103
超声波工作原理是主机(就是Uno了)发送指令给超声波,首先是发送超声波的地址,找到地址。如KS103的地址是0xe8,但是uno只能用7位地址的话,就要右移一位了。接着发送指令0x02,这是寄存器,返回的数据就存在里面。然后是指令超声波的工作模式,具体看超声波的文档。下面是代码实现:
#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
//数据一般都是16位的,要两次read才可以都会数据
}
delay(250); //wait to be read on the screen of PC.
}
连线如下:将超声波RX与主机TX相连,TX与主机RX相连。VCC和GND就不用说了。
这样就完成了TTL串口的超声波。当然,这里只用到了测距,没有用到温度传感器和亮度传感器。这种超声波传感器是比较高级的,这些传感器上面也都有。
接下来我说一下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
}