鸿蒙开发板Hi3861_通过GPIO12 PWM蜂鸣器播放<涛声依旧>-——基于code-2.0-CANARY

本文介绍了如何使用A12接口连接蜂鸣器,并通过PWM技术实现不同频率的音调控制,包括《两只老虎》和《涛声依旧》简谱演奏。教程详细到代码和频谱配置,适合初学者学习GPIO和PWM应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//通过PWM蜂鸣器实验-A12连接, A12接长脚,蜂鸣器短脚接GND

编译报错参考:

https://blog.csdn.net/txwtech/article/details/119853772

//通过PWM蜂鸣器实验-A12连接, A12接长脚,蜂鸣器短脚接GND
//,by txwtech
#include <stdio.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"
int count1=0;

//音符对照表:
//https://www.cnblogs.com/developer-huawei/p/13959852.html
static const uint16_t g_tuneFreqs[]={
    0,
    
    38223, // 1 1046.5,-
    34052, // 2 1174.7,--
    30338, // 3 1318.5,--
    28635, // 4 1396.4,--
    25511, // 5 1568,---
    22728, // 6 1760,---
    20249, // 7 1975.5,---  
    51021,//  8,5_低音 783.99,   40M=40*1000*1000, 40M/783.99=51021
    45454,//  9,6_     880.00
    40495,//  10,7_     987.77    
    
};
static const uint8_t g_scoreNotes[] = {
    // 《两只老虎》简谱
    1, 2, 3, 1,    1, 2, 3, 1,   3, 4, 5,  3, 4, 5,
    5, 6, 5, 4, 3, 1,  5, 6, 5, 4, 3, 1,   1, 8, 1,  1, 8, 1,
};
static const uint8_t g_scoreNotes_txwtech[] = {
    // 《涛声依旧》简谱
    6,6,3,5,  6,5,5,
    1,2,3,3,  3,5,6,5,3,2,3,0,
    6,5,3,5, 1, 9,8,8,
    9,1,2,2,   2,6,3,      5,6,5,
   
    

};

// 曲谱时值
static const uint8_t g_scoreDurations[] = {
    4, 4, 4, 4,    4, 4, 4, 4,   4, 4, 8,  4, 4, 8,
    3, 1, 3, 1, 4, 4,  3, 1, 3, 1, 4, 4,   4, 4, 8,  4, 4, 8,
};
//txwtech
static const uint8_t g_scoreDurations_txwtech[] = {
    4, 4, 4, 4,  2,2,4,    
    2, 2, 4, 4,   2, 4, 1, 1, 2,2, 8,1,
    4,4,2,2,     4, 2, 2, 4,
    2,2,4,4,       2,4, 2,    2, 2, 8,
};

static void GpioTask(void *arg)
{
    (void) arg;

    uint32_t tune;
    uint16_t freqDivisor;
    uint32_t tuneInterval;
   hi_gpio_init();
   hi_io_set_func(HI_IO_NAME_GPIO_12,HI_IO_FUNC_GPIO_12_PWM3_OUT);  
  // 
   hi_pwm_init(HI_PWM_PORT_PWM3);  
  //  hi_pwm_set_clock(PWM_CLK_160M);   
   hi_pwm_set_clock(PWM_CLK_XTAL);  
   //while(1)
   {
    
       IotGpioValue val=IOT_GPIO_VALUE0;
       IoTGpioGetOutputVal(HI_IO_NAME_GPIO_12,&val);
        count1=sizeof(g_tuneFreqs)/sizeof(g_tuneFreqs[0]);
       printf("count is:  %d\n",count1);
       

       printf("execute pwm,value: %d\n",val);
       if(0)
       {
         for(size_t i=0;i<sizeof(g_scoreNotes)/sizeof(g_scoreNotes[0]);i++)
          {
            tune=g_scoreNotes[i];//简谱标记
            freqDivisor=g_tuneFreqs[tune];//对应的频率
            tuneInterval=g_scoreDurations[i]*(125 * 1000);//声音的延迟
            hi_pwm_start(HI_PWM_PORT_PWM3,freqDivisor/2,freqDivisor);
          //osDelay(tuneInterval);
            hi_udelay(tuneInterval);
            hi_pwm_stop(HI_PWM_PORT_PWM3);
          }
       }

       if(1)
       {
        for(size_t i=0;i<sizeof(g_scoreNotes_txwtech)/sizeof(g_scoreNotes_txwtech[0]);i++)
        {
           tune=g_scoreNotes_txwtech[i];//简谱标记---
           freqDivisor=g_tuneFreqs[tune];//对应的频率
           tuneInterval=g_scoreDurations_txwtech[i]*(125 * 1000);//声音的延迟---
          hi_pwm_start(HI_PWM_PORT_PWM3,freqDivisor/2,freqDivisor);
          
          hi_udelay(tuneInterval);
          hi_pwm_stop(HI_PWM_PORT_PWM3);
         }
       }


       //验证音准
      // for(size_t i=0;i<sizeof(g_tuneFreqs)/sizeof(g_tuneFreqs[0]);i++)
      //  {
          // tune=g_tuneFreqs[i];//简谱标记---
         // freqDivisor=tune;
         // hi_pwm_start(HI_PWM_PORT_PWM3,freqDivisor/2,freqDivisor);
          
         // hi_udelay(3*125 * 1000);
         // hi_pwm_stop(HI_PWM_PORT_PWM3);
        // }


       
      
       
       
       
   }
}
static void GpioEntry(void)
{
    osThreadAttr_t attr={0};
    attr.name="GpioTask";
    attr.stack_size=4096;
    attr.priority=osPriorityNormal;
    if(osThreadNew(GpioTask,NULL,&attr)==NULL)
    {
        printf("[GpioEntry] create GpioTask failed!\n");
    }
}
SYS_RUN(GpioTask);
//SYS_RUN(GpioEntry);

build.gn

static_library("pwm_tone_test") {
    sources = [ "pwm_tone_test.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 = [
        "pwm_tone_test",
    ]
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

txwtech笛克特科

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

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

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

打赏作者

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

抵扣说明:

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

余额充值