单片机+超声波实时测距
我写的代码:
下面是代码:
#include
LCD4Bit_mod lcd = LCD4Bit_mod(2);
int inputPin=15;
int outputPin=16;
int ledPin = 17;
void setup() {
pinMode(inputPin, INPUT);
pinMode(outputPin, OUTPUT);
pinMode(ledPin,OUTPUT);
lcd.init();
lcd.clear();
lcd.cursorTo(1, 0);
lcd.printIn("www.geeetech.com");
}
void display(int number,unsigned char address)
{
unsigned char a,b,c,d;
if(number <100)
digitalWrite(ledPin,LOW);
if(number >=100 )
digitalWrite(ledPin,HIGH);
a=(number/1000);
b=(number/100)%10;
c=(number/10)%10;
d=number%10;
lcd.commandWrite(0x80+address);
lcd.print(a+48);
lcd.print(b+48);
lcd.print(c+48);
lcd.print(46);
lcd.print(d+48);
lcd.print(99);
lcd.print(109);
}
void loop() {
digitalWrite(outputPin, LOW);
delayMicroseconds(2);
digitalWrite(outputPin, HIGH);
delayMicroseconds(10);
digitalWrite(outputPin, LOW);
float distance = pulseIn(inputPin, HIGH);
distance= distance/5.8;
lcd.cursorTo(2, 0);
lcd.printIn("Distance:");
display(distance,0x49);
}
单片机超声波实时测 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)