
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
【本文正在参与优质创作者激励】
今天是毛主席诞辰128周年,让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);