参考:
https://blog.csdn.net/haigear/article/details/84895381
https://edu.51cto.com/center/course/lesson/index?id=722561
//超声波模块HC-SR04
/*
VCC--DC?5V
GND--GND
trig--A11
echo--A12
//超声波模块HC-SR04
/*
VCC--DC 5V
GND--GND
trig--A11
echo--A12
//https://edu.51cto.com/center/course/lesson/index?id=722561
*/
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "hi_io.h"
#include "iot_pwm.h"
#include "hi_pwm.h"
#include "hi_time.h"
__attribute__((__unused))
static time_t get_us(void)
{
struct timeval current_time;
gettimeofday(¤t_time,NULL);
time_t t=current_time.tv_sec*1000000+current_time.tv_usec;
return t;
}
static void ultrasonicsTask_txwtech(void *arg)
{
(void) arg;
__attribute__((__unused))
time_t startTime = 0;
int count = 0;
int state = 0;
time_t endTime = 0.0;
float distance = 0.0;
//超声波triger pin11
IoTGpioInit(HI_IO_NAME_GPIO_11);
IoTGpioSetDir(HI_IO_NAME_GPIO_11,IOT_GPIO_DIR_OUT);
IoTGpioSetOutputVal(HI_IO_NAME_GPIO_11,IOT_GPIO_VALUE0);
IotGpioValue val=IOT_GPIO_VALUE0;
IoTGpioInit(HI_IO_NAME_GPIO_12);
IoTGpioSetDir(HI_IO_NAME_GPIO_12,IOT_GPIO_DIR_IN);
while(1)
{
IoTGpioSetOutputVal(HI_IO_NAME_GPIO_11,IOT_GPIO_VALUE0);
usleep(8);
val=IOT_GPIO_VALUE1;
IoTGpioSetOutputVal(HI_IO_NAME_GPIO_11,IOT_GPIO_VALUE1);
usleep(10);
IoTGpioSetOutputVal(HI_IO_NAME_GPIO_11,IOT_GPIO_VALUE0);
// usleep(10000);
usleep(10);
startTime=get_us();
while(val==IOT_GPIO_VALUE1)
{
IoTGpioGetInputVal(HI_IO_NAME_GPIO_12,&val);
}
endTime=get_us();
distance=(endTime-startTime)*340/2/1000;//毫米
printf("distance: %f\n",distance);
if(state==0)
{
if(distance<=400)
{
state=1;
}
}
else if(state==1)
{
if(distance>400)
{
count++;
state=0;
char scount[10];
// memset(scount,0,sizeof(scount));
sprintf(scount,"%d\n",scount);
// printf("distance: %f\n",distance);
}
}
osDelay(20);
//usleep(40000);//40毫秒
}
//usleep(40000);//40毫秒
}
SYS_RUN(ultrasonicsTask_txwtech);
c_cpp_properties.json
{
"configurations": [
{
"name": "!!! WARNING !!! AUTO-GENERATED FILE, PLEASE DO NOT MODIFY IT"
},
{
"name": "Win32-debug",
"includePath": [
//"F:/code-2.0-canary/include",
"F:/code-2.0-canary/src",
"F:/utils/native/lite/include",
"F:/domains/iot/link/libbuild",
"F:/base/iot_hardware/peripheral/interfaces/kits",
"F:/third_party/cmsis/CMSIS/RTOS2/Include",
"F:/code-2.0-canary/device/hisilicon/hispark_pegasus/sdk_liteos/include",
//"F:/code-2.0-canary/third_party/mingw-w64/mingw-w64-headers/crt",
""
],
"browse": {
"limitSymbolsToIncludedHeaders": true,
"path": [
"F:/code-2.0-canary/include",
"F:/code-2.0-canary/src",
"F:/code-2.0-canary/utils/native/lite/include",
""
]
},
"defines": [
""
],
"intelliSenseMode": "clang-x64",
"compilerPath": "F:/hcc_riscv32_win/bin/riscv32-unknown-elf-gcc.exe",
"compilerArgs": [
""
]
}
],
"version": 4
}
build.gn
static_library("ultrasonics_demo_txwtech") {
sources = [ "ultrasonics_demo_txwtech.c"]
include_dirs = [
"//third_party/cmsis/CMSIS/RTOS2/Include",
"//utils/native/lite/include",
"//kernel/liteos_m/components/cmsis/2.0",
"//base/iot_hardware/peripheral/interfaces/kits",
"//device/hisilicon/hispark_pegasus/sdk_liteos/include",
]
}
app/build.gn
# Copyright (c) 2020 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/lite/config/component/lite_component.gni")
lite_component("app") {
features = [
"ultrasonics_demo_txwtech",
]
}
|