鸿蒙开发板Hi3861_超声波测距实验code-2.0-CANARY

参考:

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(&current_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",
    ]
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

txwtech笛克特科

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值