Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
abstandsmesser.cpp
00001 /***************************************************************************************/ 00002 /***************** Facharbeit Jannick Pötzel 23.12.2011 **********************/ 00003 /***************** SRF08 Ultrasonic Range Finder **********************/ 00004 /***************************************************************************************/ 00005 00006 #include "mbed.h" 00007 #include "TextLCD.h" 00008 00009 00010 // allocation of variables 00011 TextLCD lcd(p24, p25, p26, p27, p28, p29, p30); 00012 00013 DigitalOut mled0(LED1); 00014 DigitalOut mled1(LED2); 00015 DigitalOut mled2(LED3); 00016 DigitalOut mled3(LED4); 00017 00018 I2C sonar(p9, p10); // Define SDA, SCL pins 00019 Serial pc(USBTX, USBRX); // Define Tx, Rx for PC 00020 const int addr = 0xE0; // I2C device address for SRF08 00021 char cmd[2]; 00022 char echo[2]; 00023 00024 // start amin programm 00025 int main() { 00026 00027 // Set up SRF08 max range and receiver sensitivity over I2C bus 00028 cmd[0] = 0x02; // Range register 00029 cmd[1] = 0x1C; // Set max range about 100cm 00030 sonar.write(addr, cmd, 2); 00031 cmd[0] = 0x01; // Receiver gain register 00032 cmd[1] = 0x1B; // Set receiver gain 00033 sonar.write(addr, cmd, 2); 00034 00035 while(1) 00036 { 00037 00038 // Get range data from SRF08 00039 // Send Tx burst command over I2C bus 00040 cmd[0] = 0x00; // Command register 00041 cmd[1] = 0x51; // Ranging results in cm 00042 sonar.write(addr, cmd, 2); // Send ranging burst 00043 00044 wait(0.07); // Wait for return echo 00045 00046 // Read back range over I2C bus 00047 cmd[0] = 0x02; // Address of first echo 00048 sonar.write(addr, cmd, 1, 1); // Send address of first echo 00049 sonar.read(addr, echo, 2); // read two-byte echo result 00050 00051 // calculate distance 00052 int x = (echo[0]<<8)+echo[1]; 00053 00054 // clear LCD 00055 lcd.cls(); 00056 // if distance bigger than 99 cm write out of R to LCD 00057 if (x > 99) 00058 { 00059 lcd.writeData(' '); 00060 lcd.writeData('o'); 00061 lcd.writeData('u'); 00062 lcd.writeData('t'); 00063 lcd.writeData(' '); 00064 lcd.writeData('o'); 00065 lcd.writeData('f'); 00066 lcd.writeData(' '); 00067 lcd.writeData('R'); 00068 } 00069 // write distance to LCD 00070 else 00071 { 00072 int x1=0x30+x%10; 00073 int x2=x/10; 00074 x2=0x30+x2%10; 00075 lcd.writeData(' '); 00076 lcd.writeData(x2); 00077 lcd.writeData(x1); 00078 lcd.writeData(' '); 00079 lcd.writeData('c'); 00080 lcd.writeData('m'); 00081 } 00082 00083 // Power in LEDs in dependence of measured distance 00084 00085 if (x <= 40) 00086 { 00087 mled0 = 1; 00088 mled1 = 0; 00089 mled2 = 0; 00090 mled3 = 0; 00091 } 00092 if (x <= 30) 00093 { 00094 mled0 = 1; 00095 mled1 = 1; 00096 mled2 = 0; 00097 mled3 = 0; 00098 } 00099 if (x <= 20) 00100 { 00101 mled0 = 1; 00102 mled1 = 1; 00103 mled2 = 1; 00104 mled3 = 0; 00105 } 00106 if (x <= 10) 00107 { 00108 mled0 = 1; 00109 mled1 = 1; 00110 mled2 = 1; 00111 mled3 = 1; 00112 } 00113 else if (x > 40) 00114 { 00115 mled0 = 0; 00116 mled1 = 0; 00117 mled2 = 0; 00118 mled3 = 0; 00119 } 00120 } 00121 }
Generated on Tue Jul 26 2022 23:43:13 by
1.7.2