OpenHarmonyOS 3.0 HI3861播放《太阳最红,毛主席最亲》 原创 精华

发布于 2021-12-26 13:27
浏览
3收藏

【本文正在参与优质创作者激励】
今天是毛主席诞辰128周年,让Hi3861播放一首歌曲“太阳最红,毛主席最亲”

开发环境

  1. Windows 10 64位系统
  2. DevEco Device Tool 3.0
  3. 鸿联物联网操作系统3.02
  4. 润和wifi—iot开发套件

曲谱

OpenHarmonyOS 3.0  HI3861播放《太阳最红,毛主席最亲》-开源基础软件社区

源码

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

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "iot_watchdog.h"
#include "iot_pwm.h"
#include "hi_pwm.h"

static volatile int g_buttonPressed = 0;
static const uint16_t g_tuneFreqs[] = {
    0, // 523.3,        0
    0, // 523.3,        1
    0, // 587.4,        2
    659, // 659.3,      3
    699, // 698.5,      4
    784, // 784,        5
    880, // 880,        6
    988, // 987.8,      7
    0, // 
    0, // 
    0, //

    1047, // 1046.5,   11
    1175, // 1174.7,   12
    1319, // 1318.5,   13
    1397, // 1396.9,   14
    1568, // 1568,     15
    1760, // 1760,     16
    1976, // 1975.5,   17
    0, // 
    0, // 
    0, // 

    2093, // 2093,     21
    2349, // 2349.4,   22
    2637, // 2637,     23
    2794, // 2793.8,   24
    3136, // 3136,     25
    3520, // 3520,     26
    3951, // 3951,     27 
};

// 曲谱音符
static const uint8_t g_scoreNotes[] = {
    // 简谱:http://www.jianpu.cn/pu/41/41614.htm
    11,5,12,11, 6, 5,  11,5,13,12,11,   5,
    5,11,13,16,  15,13,11,   13,13,15,11,6,5,   12,
    11,5,12,11,  6,5,   11,5,13,12,11,   15,
    5,11,13,16,  15,11,13,   12,13,15,6,5,12,    11,

    5,11,13,16,   15,11,13,   12,13,15,6,5,12,   11,

    11,5,12,11, 6, 5,   11,5,13,12,11,   15,
    5,11,13,16,  15,13,11,   13,13,15,11,6,5,   12,
    11,5,12,11,6,5,   11,5,13,12,11,   15,
    5,11,13,16,  15,11,13,   12,13,15,6,5,12,11,
    
};

// 曲谱时值
static const uint8_t g_scoreDurations[] = {
    2,2,2,2, 1, 7,  2,2,2,1,1,   8,
    2,2,2,2,  2,2,4,   2,1,1,2,1,1,   8,
    2,2,2,2,  1,7,     2,2,2,1,1,8,
    2,2,2,2,  2,2,4,   2,1,1,1,1,2,   8,

    2,2,2,2,   2,2,4,  2,1,1,1,1,2,  8, 

    2,2,2,2,   1,7,    2,2,2,1,1,   8,
    2,2,2,2,   2,2,4,   2,1,1,2,1,1,  8,
    2,2,2,2,   1,7,     2,2,2,1,1,    8,
    2,2,2,2,   2,2,4,     2,1,1,1,1,2,  8,
};

static void *TaiyangzuihongTask(const char *arg)
{
    (void)arg;

    printf("TaiyangzuihongTask start!\r\n");

    hi_pwm_set_clock(PWM_CLK_XTAL); // 设置时钟源为晶体时钟(40MHz,默认时钟源160MHz)

    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] * (160); // 音符时间
        printf("%d %d %d %d\r\n", tune, (40*1000*1000) / freqDivisor, freqDivisor, tuneInterval);
        IoTPwmStart(0, 50, freqDivisor);
        msleep(tuneInterval);
        IoTPwmStop(0);
    }

    return NULL;
}

static void StartTaiyangzuihongTask(void)
{
    osThreadAttr_t attr;

    IoTGpioInit(9);

    // 蜂鸣器引脚 设置为 PWM功能
    IoTGpioSetFunc(9, 5);
    IoTPwmInit(0);

    IoTWatchDogDisable();

    attr.name = "TaiyangzuihongTask";
    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)TaiyangzuihongTask, NULL, &attr) == NULL) {
        printf("[Taiyangzuihong] Falied to create TaiyangzuihongTask!\n");
    }
}

SYS_RUN(StartTaiyangzuihongTask);

编译烧录

  1. PWM默认时钟源160MHz,设置时钟源为晶体时钟40MHz
  2. 设置烧录波特率为115200,参考DevEco Device Tool 3.0 Beta1在Windows环境下的烧录
  3. 歌曲镜像见附件,可以直接烧录。
    OpenHarmonyOS 3.0  HI3861播放《太阳最红,毛主席最亲》-开源基础软件社区

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
Hi3861_wifiiot_app_allinone.zip 453.02K 42次下载
已于2021-12-28 14:29:29修改
7
收藏 3
回复
举报
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
    相关推荐