|
1 | 1 | /* |
2 | | - Description: Display the distance measured by ultrasonic |
| 2 | +******************************************************************************* |
| 3 | +* Copyright (c) 2021 by M5Stack |
| 4 | +* Equipped with M5Core sample source code |
| 5 | +* 配套 M5Core 示例源代码 |
| 6 | +* Visit the website for more information:https://docs.m5stack.com/en/core/gray |
| 7 | +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/gray |
| 8 | +* |
| 9 | +* describe: Ultrasonic. 超声波测距传感器 |
| 10 | +* date:2021/8/19 |
| 11 | +******************************************************************************* |
| 12 | + Please connect to Port A,Display the distance measured by ultrasonic |
| 13 | + 请连接端口A,显示超声波测量的距离 |
3 | 14 | */ |
4 | 15 |
|
5 | 16 | #include <M5Stack.h> |
6 | 17 |
|
7 | | -TFT_eSprite Terminalbuff = TFT_eSprite(&M5.Lcd); |
8 | | - |
9 | 18 | void setup() { |
10 | | - // put your setup code here, to run once: |
11 | | - M5.begin(); |
12 | | - |
13 | | - Wire.begin(21,22); |
14 | | - |
15 | | - Terminalbuff.createSprite(160, 80); |
16 | | - Terminalbuff.fillRect(80,20,160,80,BLACK); |
17 | | - |
18 | | - Terminalbuff.pushSprite(80,20); |
19 | | - Terminalbuff.setTextFont(4); |
20 | | - M5.Lcd.setCursor(105, 0, 4); |
21 | | - |
22 | | - M5.Lcd.print("Ultrasonic"); |
| 19 | + M5.begin(); //Init M5Stack. 初始化M5Stack |
| 20 | + M5.Power.begin(); //Init power 初始化电源模块 |
| 21 | + Wire.begin(21, 22); |
| 22 | + M5.Lcd.setCursor(105, 0, 4); //Set the cursor at (105,0) and set the font to a 4 point font. 将光标设置在(105,0)处,且设置字体为4号字体 |
| 23 | + M5.Lcd.print("Ultrasonic\nDistance:"); |
23 | 24 | } |
24 | 25 |
|
25 | 26 | float readEUS() |
26 | 27 | { |
27 | | - uint32_t data; |
28 | | - Wire.beginTransmission(0x57); |
29 | | - Wire.write(0x01); |
30 | | - Wire.endTransmission(); |
31 | | - delay(120); |
32 | | - Wire.requestFrom(0x57,3); |
33 | | - data = Wire.read();data <<= 8; |
34 | | - data |= Wire.read();data <<= 8; |
35 | | - data |= Wire.read(); |
36 | | - return float(data) / 1000; |
37 | | - |
| 28 | + uint32_t data; |
| 29 | + Wire.beginTransmission(0x57); //Transfer data to 0x57. 将数据传输到0x57 |
| 30 | + Wire.write(0x01); |
| 31 | + Wire.endTransmission(); //Stop data transmission with the Ultrasonic Unit. 停止与Ultrasonic Unit的数据传输 |
| 32 | + delay(120); |
| 33 | + Wire.requestFrom(0x57,3); //Request 3 bytes from Ultrasonic Unit. 向Ultrasonic Unit请求3个字节。 |
| 34 | + data = Wire.read();data <<= 8; |
| 35 | + data |= Wire.read();data <<= 8; |
| 36 | + data |= Wire.read(); |
| 37 | + return float(data) / 1000; |
38 | 38 | } |
39 | 39 |
|
40 | 40 | void loop() { |
41 | | - |
42 | | - float newvalue = 0; |
43 | | - |
44 | | - while(1) |
45 | | - { |
46 | | - newvalue = readEUS(); |
47 | | - |
48 | | - Terminalbuff.fillRect(80,20,160,80,BLACK); |
49 | | - Terminalbuff.setCursor(30,50); |
50 | | - |
51 | | - if(( newvalue < 1500 )&&( newvalue > 20 )) |
52 | | - { |
53 | | - Terminalbuff.printf("%.2fmm",newvalue); |
54 | | - Terminalbuff.pushSprite(80,20); |
55 | | - } |
56 | | - delay(100); |
57 | | - } |
58 | | - |
| 41 | + static float newvalue = 0; |
| 42 | + newvalue = readEUS(); |
| 43 | + if(( newvalue < 1500 )&&( newvalue > 20 )){ |
| 44 | + M5.Lcd.setCursor(105,27); |
| 45 | + M5.Lcd.printf("%.2fmm",newvalue); |
| 46 | + } |
| 47 | + delay(100); |
59 | 48 | } |
0 commit comments