跳转至

无源蜂鸣器

无源蜂鸣器

接线要求

  • GND连接开发板GND

  • VCC连接开发板3.3V或5V

  • OUT连接GPIO14引脚(其它引脚也可以)

项目编写

  1. applications/genkipi/app 下新建 buzzer_passive 文件夹
  2. buzzer_passive 下新建 main.c 文件
  3. buzzer_passive 下新建 BUILD.gn文件

代码部分

main.c 文件内容

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

#include "iot_gpio.h"
#include "iot_pwm.h"
#include "iot_io.h"
#include "genki_pin.h"


static void start(void) {
    //初始化GPIO口
    IoTGpioInit(IOT_IO_NAME_14);
    //设置IO口功能为GPIO
    IoTIoSetFunc(IOT_IO_NAME_14, IOT_IO_FUNC_14_PWM5_OUT);
    //设置IO口输出方向:输出
    IoTGpioSetDir(IOT_IO_NAME_14, IOT_GPIO_DIR_OUT);
    //初始化PWM功能
    IoTPwmInit(IOT_PWM_NAME_5);
    //不断输出pwm方波
    while (1) {
        for (int i = 0; i < 20; i++) {

            IoTPwmStart(IOT_PWM_NAME_5, i, 10000);

            usleep(0.05 * 1000 * 1000);
        }

        for (int i = 20; i > 0; i--) {
            IoTPwmStart(IOT_PWM_NAME_5, i, 10000);

            usleep(0.05 * 1000 * 1000);
        }
    }
}

APP_FEATURE_INIT(start);

项目Build.gn

buzzer_passive 目录下 BUILD.gn 内容为

static_library("buzzer_passive") {
    sources = [
        "main.c"
    ]

    include_dirs = [
        "//utils/native/lite/include",
        "//base/iot_hardware/peripheral/interfaces/kits",
        "//device/itcast/genkipi/interfaces/kits"
    ]
}

外部Build.gn

buzzer_passive文件夹上一级目录下BUILD.gn内容为

import("//build/lite/config/component/lite_component.gni")

lite_component("app") {
    features = [
        "buzzer_passive"
    ]
}