RISC-V技术论坛
直播中

李晶

8年用户 1012经验值
私信 关注
[经验]

RV-STAR开发板蜂鸣器播放音乐

下载: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 5static 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("正在播放:%sn",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/

更多回帖

发帖
×
20
完善资料,
赚取积分