//通过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",
]
}