OpenHarmony鸿蒙硬件-控制蜂鸣器播放音乐 作者:马育民 • 2025-09-29 10:47 • 阅读:10006 # 蜂鸣器介绍 https://www.malaoshi.top/show_1GW1wtTYVAmV.html # 开发 ### 步骤 1. 并初始化 2. 循环调用PwmStart、PwmStop方法 3. 配置模块构建脚本 4. 配置模块到应用子系统 5. 启用PWM功能 ### 代码 创建文件 `beeper_music_demo.c` 代码如下: ``` #include #include #include "ohos_init.h" #include "cmsis_os2.h" #include "wifiiot_gpio.h" #include "wifiiot_gpio_ex.h" #include "wifiiot_watchdog.h" #include "wifiiot_pwm.h" #include "hi_pwm.h" // 海思PWM驱动接口头文件 static volatile int g_buttonPressed = 0; static const uint16_t g_tuneFreqs[] = { 0, // 40M Hz 对应的分频系数: 38223, // 1046.5 34052, // 1174.7 30338, // 1318.5 28635, // 1396.9 25511, // 1568 22728, // 1760 20249, // 1975.5 51021 // 5_ 783.99 // 第一个八度的 5 }; // 曲谱音符 static const uint8_t g_scoreNotes[] = { // 《两只老虎》简谱:http://www.jianpu.cn/pu/33/33945.htm 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, // 最后两个 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, }; static void *BeeperMusicTask(const char *arg) { (void)arg; printf("BeeperMusicTask start!\r\n"); hi_pwm_set_clock(PWM_CLK_XTAL); // 设置时钟源为晶体时钟(40MHz,默认时钟源160MHz) while (1) { for (size_t i = 0; i < sizeof(g_scoreNotes) / sizeof(g_scoreNotes[0]); i++) { uint32_t tune = g_scoreNotes[i]; // 音符 uint16_t freqDivisor = g_tuneFreqs[tune]; uint32_t tuneInterval = g_scoreDurations[i] * (125 * 1000); // 音符时间 printf("%d %d %d %d\r\n", tune, (40 * 1000 * 1000) / freqDivisor, freqDivisor, tuneInterval); PwmStart(WIFI_IOT_PWM_PORT_PWM0, freqDivisor / 2, freqDivisor); usleep(tuneInterval); PwmStop(WIFI_IOT_PWM_PORT_PWM0); } } return NULL; } static void StartBeepMusicTask(void) { osThreadAttr_t attr; GpioInit(); // 蜂鸣器引脚 设置为 PWM功能 IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_PWM0_OUT); PwmInit(WIFI_IOT_PWM_PORT_PWM0); WatchDogDisable(); attr.name = "BeeperMusicTask"; attr.attr_bits = 0U; attr.cb_mem = NULL; attr.cb_size = 0U; attr.stack_mem = NULL; attr.stack_size = 1024; attr.priority = osPriorityNormal; if (osThreadNew((osThreadFunc_t)BeeperMusicTask, NULL, &attr) == NULL) { printf("[LedExample] Falied to create BeeperMusicTask!\n"); } } SYS_RUN(StartBeepMusicTask); ``` # 编辑BUILD.gn文件 ### 编辑1 在代码同级目录创建下面文件: ``` BUILD.gn ``` 内容: ``` static_library("traffic_demo") { sources = [ # "led_demo.c" # "traffic_light_demo.c" "beeper_music_demo.c", ] include_dirs = [ "//utils/native/lite/include", "//base/iot_hardware/interfaces/kits/wifiiot_lite", "//kernel/liteos_m/components/cmsis/2.0", ] } ``` ### 编辑2 编辑下面文件: ``` \\applications\sample\wifi-iot\app\BUILD.gn ``` 内容: ``` import("//build/lite/config/component/lite_component.gni") lite_component("app") { features = [ "code_v2.0/std_03:hello_demo", ] } ``` # 启用PWM功能 [OpenHarmony鸿蒙硬件-启用PWM功能(关键)](https://www.malaoshi.top/show_1GW1wuJn5Rze.html "OpenHarmony鸿蒙硬件-启用PWM功能(关键)") # 编译代码 详见链接: https://www.malaoshi.top/show_1GW1wTyUOMPk.html 原文出处:http://www.malaoshi.top/show_1GW1wtUraqlc.html