Navigation

    全志在线开发者论坛

    • Register
    • Login
    • Search
    • Categories
    • Tags
    • 在线文档
    • 社区主页

    【XR806开发板试用】 PWM播放《铃儿响叮当》

    Wireless & Analog Series
    1
    1
    1189
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • O
      oiouou123 LV 5 last edited by oiouou123

      PWM播放<<铃儿响叮当>>

      参考
      OpenHarmony LYEVK-3861开发板播放《蜜雪冰城》
      OpenHarmony Neptune开发板PWM驱动 播放《小星星》
      收到XR806开发板了,之前在公众号看到有人用开发板播放蜜雪冰城,快到圣诞节了,今天试下播放<<铃儿响叮当>>。

      环境搭建

      1. WSL2 Ubuntu 20.04 LTS开发环境搭建

      感觉和正常的Ubuntu环境配置(220119发现链接失效,我从提交记录里找到了当时参考的那一篇)没啥区别,区别有以下几个

      a.在 安装必要的库和工具的时候把libc6-dev-amd64替换为libc6-dev就成,这个方法参考github上边wsl给的遇到E: Unable to locate package libc6-dev-amd64的解决方案

      b.我没装LLVM也可以烧录

      1. XR806开发板、无源蜂鸣器

      开发

      曲谱

      微信截图_20211224153024.png

      流程

      流程图.png

      代码编写

      cd device/xradio/xr806/ohosdemo
      mkdir beeperMusic
      

      目录如下

      .
      ├── BUILD.gn
      ├── beeperMusic
      │   ├── BUILD.gn
      │   └── src
      │       └── main.c
      ├── hello_demo
      ├── iot_peripheral
      └── wlan_demo
      
      

      源代码

      //main.c
      #include <stdio.h>
      #include <string.h>
      #include <unistd.h>
      #include "kernel/os/os.h"
      #include "iot_pwm.h"
      #include "ohos_init.h"
      
      static OS_Thread_t g_main_thread;
      // 音符对应的频率
      static const uint16_t g_tuneFreqs[] = {523,587,659,698,784,880,988};
      
      // 曲谱音符
      static const uint8_t g_scoreNotes[] = {
        3,3,3, 3,3,3, 3,5,1,2, 3, 4,4,4, 4,4,3,3,
        3,2,2,1, 2,5,
        3,3,3, 3,3,3, 3,5,1,2, 3, 4,4,4, 4,4,3,3,
        5,5,4,2, 1,
      };
      
      // 曲谱时值
      static const uint8_t g_scoreDurations[] = {
        4,4,8, 4,4,8, 4,4,4,4, 16, 4,4,4, 4,4,4,8,
        4,4,4,4, 8,8,
        4,4,8, 4,4,8, 4,4,4,4, 16, 4,4,4, 4,4,4,8,
        4,4,4,4, 16,
      };
      
      
      static void BeeperMusicTask(const char *arg)
      {
          (void)arg;
          printf("BeeperMusicTask start!\r\n");
          IoTPwmInit(0);
          unsigned int pwm_duty_ratio = 30;
          for (size_t i = 0; i < sizeof(g_scoreNotes)/sizeof(g_scoreNotes[0]); i++) {
              unsigned int pwm_delay_cnt = 0;
              uint32_t tune = g_scoreNotes[i]; // 音符
              uint16_t freqDivisor = g_tuneFreqs[tune];
              uint32_t tuneInterval = g_scoreDurations[i] * 10; // 音符时间
              printf("%d %d %d %d\r\n", tune,freqDivisor, pwm_duty_ratio,tuneInterval);
              IoTPwmStart(0,30,freqDivisor);
              OS_MSleep(tuneInterval*5);
              IoTPwmStop(0);
          }
          IoTPwmDeinit(0);
      }
      
      void MusicTestMain(void)
      {
      	printf("Music Test Start\n");  
      	if (OS_ThreadCreate(&g_main_thread, "MainThread", BeeperMusicTask, NULL,
      			    OS_THREAD_PRIO_APP, 4 * 1024) != OS_OK) {
      		printf("[ERR] Create MainThread Failed\n");
      	}
      }
      
      SYS_RUN(MusicTestMain);
      

      beeperMusic目录的BUILD.gn

      import("//device/xradio/xr806/liteos_m/config.gni")
      
      static_library("app_music") {
         configs = []
      
         sources = [
            "src/main.c",
         ]
      
         cflags = board_cflags
      
         include_dirs = board_include_dirs
         include_dirs += [
            "//kernel/liteos_m/kernel/arch/include",
            "include",
            "//base/iot_hardware/peripheral/interfaces/kits",
         ]
      }
      

      主目录BUILD.gn

      group("ohosdemo") {
          deps = [
              #"hello_demo:app_hello",
              #"iot_peripheral:app_peripheral",
              #"wlan_demo:app_WlanTest",
              "beeperMusic:app_music"
          ]
      }
      

      效果

      B站视频链接

      1 Reply Last reply Reply Quote Share 1
      • 1 / 1
      • First post
        Last post

      Copyright © 2024 深圳全志在线有限公司 粤ICP备2021084185号 粤公网安备44030502007680号

      行为准则 | 用户协议 | 隐私权政策