RISC-V MCU中文社区

【分享】 RV-STAR开发板蜂鸣器播放音乐

发表于 GD32VF103 MCU 2021-03-30 16:04:47
0
211
9

下载:music_player


本工程由nucleistudio建立,连接蜂鸣器引脚为PA8,具体代码见附件。


#include "beep.h"

void beep_init(void)
{
	timer_oc_parameter_struct oc_para;
	timer_parameter_struct    init_para;

	//rcu_periph_clock_enable(RCU_GPIOA | RCU_AF | RCU_TIMER0);
	rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_AF);
	rcu_periph_clock_enable(RCU_TIMER0);

	oc_para.outputstate = TIMER_CCX_ENABLE;
	oc_para.outputnstate = TIMER_CCXN_DISABLE;
	oc_para.ocpolarity = TIMER_OC_POLARITY_HIGH;
	oc_para.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
	oc_para.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
	oc_para.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;

	init_para.prescaler         = 107U;
	init_para.alignedmode       = TIMER_COUNTER_EDGE;
	init_para.counterdirection  = TIMER_COUNTER_UP;
	init_para.period            = 100U;
	init_para.clockdivision     = TIMER_CKDIV_DIV1;
	init_para.repetitioncounter = 0U;

	timer_init(TIMER0, &init_para);
	timer_channel_output_config(TIMER0, TIMER_CH_0, &oc_para);
	timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM0);
	timer_primary_output_config(TIMER0, ENABLE);
	timer_auto_reload_shadow_enable(TIMER0);

	gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
}

void beep_on(void)
{
	timer_enable(TIMER0);
}

void beep_off(void)
{
	timer_disable(TIMER0);
}

void beep_set(uint16_t freq, uint8_t volume)
{
	uint32_t period,pulse;
	period = 1000000 / freq;
	pulse = period - period / 100 * volume;
	timer_autoreload_value_config(TIMER0, period - 1);
	timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, pulse);
}

#include "nuclei_sdk_soc.h"
#include 
#include "gd32vf103v_rvstar.h"
#include "beep.h"
#include "decode.h"

#define THREAD_PRIORITY 2
#define THREAD_STACK_SIZE 512
#define THREAD_TIMESLICE 5

static rt_uint8_t thread_stack[THREAD_STACK_SIZE];
static struct rt_thread tid;


/* Thread demo */
int thread_led(void)
{
    gd_led_init(LED1);
    gd_led_init(LED2);
    gd_led_init(LED3);

    while(1)
    {
    	gd_led_toggle(LED1);
	gd_led_toggle(LED2);
	gd_led_toggle(LED3);
	rt_thread_mdelay(1000);
    }
    return 0;
}

const struct beep_song song1 =
{
    .name = "两只老虎",
    .data = {
        0x15, 0x02, 0x16, 0x02, 0x17, 0x02, 0x15, 0x02, 0x15, 0x02,
        0x16, 0x02, 0x17, 0x02, 0x15, 0x02, 0x17, 0x02, 0x18, 0x02,
        0x19, 0x01, 0x17, 0x02, 0x18, 0x02, 0x19, 0x01, 0x19, 0x03,
        0x1A, 0x03, 0x19, 0x03, 0x18, 0x03, 0x17, 0x02, 0x15, 0x16,
        0x19, 0x03, 0x1A, 0x03, 0x19, 0x03, 0x18, 0x03, 0x17, 0x02,
        0x15, 0x16, 0x15, 0x02, 0x0F, 0x02, 0x15, 0x01, 0x15, 0x02,
        0x0F, 0x02, 0x15, 0x01, 0x00, 0x00
    }
};

int main(void)
{
    struct beep_song_data data;
    int len, i = 0;
    char name[20];

    rt_thread_init(&tid, "thread", thread_led, NULL, thread_stack, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
    rt_thread_startup(&tid);

    beep_init();

    beep_song_decode_init();
	beep_song_get_name(&song1, name);
	rt_kprintf("正在播放:%s\n",name);
	len = beep_song_get_len(&song1);
	while (i < len)
	{
	   /* 解码音乐数据 */
	   beep_song_get_data(&song1, i, &data);
	   beep_set(data.freq, 3);
	   beep_on();
	   rt_thread_mdelay(data.sound_len);

	   beep_off();
	   rt_thread_mdelay(data.nosound_len);
	   i++;
	}
}


参考链接:https://www.rt-thread.org/document/site/tutorial/beep-player/

喜欢9
用户评论
关于作者
槑

未通过人工认证

懒的都不写签名

问答
粉丝
0
关注
0
  • 早春营|RISC-V处理器嵌入式开发
  • RV-STAR 开发板
  • RISC-V处理器设计系列课程
  • 培养RISC-V大学土壤 共建RISC-V教育生态
早春营|RISC-V处理器嵌入式开发