我制作的会唱歌的STM32实验板

pcwe   2008-10-10 21:06 楼主
可以用过PWM控制无源蜂鸣器唱歌:

回复评论 (14)

这是使用说明书

                                  
点赞  2008-10-10 21:20

主要程序

#include "Include.h"

//音调表
//C 262 
//D 294 
//E 330 
//F 349 
//G 392 
//A 440 
//B 494 
//C 523 
//D 587 
//E 659 
//F 698 
//G 784 
//A 880 
//B 988 
//C 1047 
//D 1175 
//E 1319 
//F 1397 
//G 1568 
//A 1760 
//B 1976 

//低音1~7
const unsigned int MusicDataD[7] = {61069,54422,48485,45845,40816,36364,32389};
//中音1~7
const unsigned int MusicData[7] = {30592,27257,24297,22922,20408,18182,16194};
//高音1~7
const unsigned int MusicDataG[7] = {15282,13617,12130,11453,10204,9091,8097};

//音乐数据格式:
//0x11~0x17 表示低音1~7
//0x21~0x27 表示中音1~7
//0x31~0x37 表示高音1~7
//0         表示静音
//每个音后面数据表示该音为多少个时间单位:1个16分音符定为1个时间单位

const unsigned char SuperStarData[] = //SuperStar S.H.E.
{
        0x22,4,0x22,2,0x23,2,0x21,4,0x15,2,0x15,2,
        0x22,2,0x21,2,0x22,2,0x23,2,0x21,4,0,2,0x23,2,
        0x24,2,0x23,2,0x21,2,0x21,1,0x22,1,0x22,4,0,2,0x23,2,
        0x24,2,0x23,2,0x21,2,0x21,2,0x21,2,0x22,1,0x21,1,0x17,4,
        0x22,2,0x22,2,0x22,2,0x23,2,0x21,4,0x15,2,0x15,2,
        0x22,2,0x21,2,0x22,2,0x23,2,0x21,4,0,2,0x23,2,
        0x24,2,0x23,2,0x21,2,0x21,1,0x22,1,0x22,4,0,2,0x23,2,
        0x24,2,0x23,2,0x24,2,0x25,2,0x25,4,0x15,2,0x16,2,
        
        0x21,4,0x22,2,0x23,2,0x25,4,0x23,2,0x25,2,
        0x26,2,0x27,2,0x26,2,0x25,2,0x23,4,0x22,2,0x23,2,
        0x25,2,0x23,2,0,2,0x23,2,0x22,2,0x23,2,0x22,2,0x21,2,
        0x15,8,0,4,0x15,2,0x16,2,
        0x21,4,0x22,2,0x23,2,0x25,4,0x23,2,0x25,2,
        0x26,2,0x27,2,0x26,2,0x25,2,0x23,4,0x22,2,0x23,2,
        0x25,2,0x23,2,0,2,0x23,2,0x22,2,0x23,2,0x22,2,0x21,2,
        //1
        0x15,16,
        0,32,
        
        0x22,4,0x22,2,0x23,2,0x21,4,0x15,2,0x15,2,
        0x22,2,0x21,2,0x22,2,0x23,2,0x21,4,0,2,0x23,2,
        0x24,2,0x23,2,0x21,2,0x21,1,0x22,1,0x22,4,0,2,0x23,2,
        0x24,2,0x23,2,0x21,2,0x21,2,0x21,2,0x22,1,0x21,1,0x17,4,
        0x22,2,0x22,2,0x22,2,0x23,2,0x21,4,0x15,2,0x15,2,
        0x22,2,0x21,2,0x22,2,0x23,2,0x21,4,0,2,0x23,2,
        0x24,2,0x23,2,0x21,2,0x21,1,0x22,1,0x22,4,0,2,0x23,2,
        0x24,2,0x23,2,0x24,2,0x25,2,0x25,4,0x15,2,0x16,2,
        0x21,4,0x22,2,0x23,2,0x25,4,0x23,2,0x25,2,
        0x26,2,0x27,2,0x26,2,0x25,2,0x23,4,0x22,2,0x23,2,
        0x25,2,0x23,2,0,2,0x23,2,0x22,2,0x23,2,0x22,2,0x21,2,
        0x15,8,0,4,0x15,2,0x16,2,
        0x21,4,0x22,2,0x23,2,0x25,4,0x23,2,0x25,2,
        0x26,2,0x27,2,0x26,2,0x25,2,0x23,4,0x22,2,0x23,2,
        0x25,2,0x23,2,0,2,0x23,2,0x22,2,0x23,2,0x22,2,0x21,2,

        //2
        0x15,16,
        0,4,0x26,2,0x25,6,0x22,2,0x23,10,0,8,
        0,4,0x26,2,0x25,6,0x22,2,0x23,4,0x24,2,0x23,1,0x22,1,0x21,1,0x21,9,
        0,4,0x26,2,0x25,6,0x27,2,0x27,6,0,2,0x27,2,0x31,4,0x27,2,0x26,2,
        0x26,4,0x22,2,0x23,2,0x21,4,0x15,2,0x15,2,
        0x22,2,0x21,2,0x22,2,0x23,2,0x21,4,0,2,0x23,2,
        0x24,2,0x23,2,0x21,2,0x22,2,0x22,4,0,2,0x23,2,

        0x24,2,0x23,2,0x21,2,0x21,2,0x21,2,0x22,1,0x21,1,0x17,4,
        0x22,2,0x22,2,0x22,2,0x23,2,0x21,4,0x15,2,0x15,2,
        0x22,2,0x21,2,0x22,2,0x23,2,0x21,4,0,2,0x23,2,
        0x24,2,0x23,2,0x21,2,0x22,2,0x22,4,0,2,0x23,2,
        0x24,2,0x23,2,0x24,2,0x25,2,0x25,20,0x15,2,0x16,2,

        0x21,4,0x22,2,0x23,2,0x25,4,0x23,2,0x25,2,
        0x26,2,0x27,2,0x26,2,0x25,2,0x23,4,0x22,2,0x23,2,
        0x25,2,0x23,2,0,2,0x23,2,0x22,2,0x23,2,0x22,2,0x21,2,
        0x15,8,0,4,0x15,2,0x16,2,
        0x21,4,0x22,2,0x23,2,0x25,4,0x23,2,0x25,2,
        0x26,2,0x27,2,0x26,2,0x25,2,0x23,4,0x22,2,0x23,2,
        0x25,2,0x23,2,0,2,0x23,2,0x22,2,0x23,2,0x22,2,0x21,2,
        0x15,12,0,4,
};

void SetBellF(unsigned int SetV)
{
        TIM_OCInitTypeDef TIM_OCInitStructure;
        TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
        
        TIM_TimeBaseInitStructure.TIM_Period = SetV;
        TIM_TimeBaseInitStructure.TIM_Prescaler = 0;
        TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
        TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);

        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_Pulse = (SetV/2);
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
        TIM_OC2Init(TIM2, &TIM_OCInitStructure);
}


void Timer2Init(void)
{
        TIM_OCInitTypeDef TIM_OCInitStructure;
        TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
        
        TIM_TimeBaseInitStructure.TIM_Period = 10000;
        TIM_TimeBaseInitStructure.TIM_Prescaler = 0;
        TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
        TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);

        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_Pulse = 5000;
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
        TIM_OC2Init(TIM2, &TIM_OCInitStructure);
}

void Timer2OutEnable(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void Timer2OutDisable(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void Timer2Start(void)
{
        TIM_Cmd(TIM2, ENABLE);
}

void Timer2Stop(void)
{
        TIM_Cmd(TIM2, DISABLE);
}

//播放歌曲
void PlayMusicFile(unsigned int DataNumber,unsigned char* p,unsigned char UnitTime)
{
        unsigned int i = 0;
        unsigned int PlayWord;

        for(i=0;i<DataNumber;i++)
        {
                switch (*p)
                {
                        case 0x11:
                                PlayWord = MusicDataD[0];
                                break;
                        case 0x12:
                                PlayWord = MusicDataD[1];
                                break;
                        case 0x13:
                                PlayWord = MusicDataD[2];
                                break;
                        case 0x14:
                                PlayWord = MusicDataD[3];
                                break;
                        case 0x15:
                                PlayWord = MusicDataD[4];
                                break;
                        case 0x16:
                                PlayWord = MusicDataD[5];
                                break;
                        case 0x17:
                                PlayWord = MusicDataD[6];
                                break;
                        case 0x21:
                                PlayWord = MusicData[0];
                                break;
                        case 0x22:
                                PlayWord = MusicData[1];
                                break;
                        case 0x23:
                                PlayWord = MusicData[2];
                                break;
                        case 0x24:
                                PlayWord = MusicData[3];
                                break;
                        case 0x25:
                                PlayWord = MusicData[4];
                                break;
                        case 0x26:
                                PlayWord = MusicData[5];
                                break;
                        case 0x27:
                                PlayWord = MusicData[6];
                                break;
                        case 0x31:
                                PlayWord = MusicDataG[0];
                                break;
                        case 0x32:
                                PlayWord = MusicDataG[1];
                                break;
                        case 0x33:
                                PlayWord = MusicDataG[2];
                                break;
                        case 0x34:
                                PlayWord = MusicDataG[3];
                                break;
                        case 0x35:
                                PlayWord = MusicDataG[4];
                                break;
                        case 0x36:
                                PlayWord = MusicDataG[5];
                                break;
                        case 0x37:
                                PlayWord = MusicDataG[6];
                                break;
                        default:
                                PlayWord = 0;//表示无声音
                                break;
                }
                p++;
                i++;
                if(PlayWord != 0)
                {
                        Timer2OutEnable();
                        SetBellF(PlayWord);
                }
                else
                {
                        Timer2OutDisable();
                }
                DelayNmS(UnitTime*(*p));
                p++;

        }
}

void Timer2Test(void)
{
        unsigned char i = 0;

        Timer2Start();

        while(1)
        {
                while(NO_KEY == GetKey());
                PlayMusicFile(sizeof(SuperStarData),(unsigned char*)SuperStarData,100);
                Timer2OutDisable();
        }

        //一句简单的歌曲
        while(0)
        {
                Timer2OutEnable();
                SetBellF(MusicData[0]);
                DelayNmS(400);
                SetBellF(MusicData[0]);
                DelayNmS(400);
                SetBellF(MusicData[4]);
                DelayNmS(400);
                SetBellF(MusicData[4]);
                DelayNmS(400);
                SetBellF(MusicData[5]);
                DelayNmS(400);
                SetBellF(MusicData[5]);
                DelayNmS(400);
                SetBellF(MusicData[4]);
                DelayNmS(400);
                DelayNmS(400);
                SetBellF(MusicData[3]);
                DelayNmS(400);
                SetBellF(MusicData[3]);
                DelayNmS(400);
                SetBellF(MusicData[2]);
                DelayNmS(400);
                SetBellF(MusicData[2]);
                DelayNmS(400);
                SetBellF(MusicData[1]);
                DelayNmS(400);
                SetBellF(MusicData[1]);
                DelayNmS(400);
                SetBellF(MusicData[0]);
                DelayNmS(400);
                DelayNmS(400);
                Timer2OutDisable();
                DelayNmS(1000);
        }

        //从C播放到B
        while(1)
        {
                SetBellF(MusicData);
                Timer2OutEnable();
                DelayNmS(200);
                i++;
                if(i>=7)
                {
                        i = 0;
                        Timer2OutDisable();
                        DelayNmS(2000);
                }
        }
}
点赞  2008-10-10 21:28

卖的?

                                  
点赞  2008-10-10 21:30

是的

希望用STM32的人更多点
STM32是好东西啊
点赞  2008-10-13 17:14

我制作的会唱歌的STM32实验板

                                 顶
点赞  2008-10-13 20:41

32位玩音频数据才合适吧?

这个,这个,好像用8位也可以吧,为啥要用32位?
32位要有32位应用的特点,MP3解码加数据流播放才是正题吧?
点赞  2008-10-14 01:43

不错,不错,很好的东东

                                 顶
点赞  2008-10-14 06:09

呵呵,玩还是可以

                                 要音质,这种搞法不行。不过学习学习还是不错的。
点赞  2008-10-14 11:17

可以唱吉祥三宝吗?-嵌入式版的?歌词如下

嵌入式吉祥三宝

小女儿:  阿爸
爸爸:    哎
小女儿:  ARM公司就像那个谁呀?
爸爸  :  爸爸
小女儿:  KEIL公司又像那个谁呀?
爸爸  :  妈妈
小女儿:  那么MDK又是什么呀?
爸爸:    他们的孩子
合:      ARM KEIL MDK就是吉祥的一家
小女儿:  阿妈
妈妈:    哎
小女儿:  MDK有RealView编译器吗?
妈妈:    有啊
小女儿:  MDK有uVision3 IDE吗?
妈妈:    有啊
小女儿:  加起来不是很强大吗?
妈妈:    是啊
合:      ARM KEIL MDK就是如意的一家
爸爸妈妈:宝贝
小女儿:  啊
爸爸妈妈:你知道你支持Cortex-M3吗?
小女儿:  知道呀
爸爸妈妈:你知道你支持ARM7/9吗?
小女儿:  知道呀
爸爸妈妈:希望你能够快快地长大
小女儿:  呵呵呵
合:      我们三个就像吉祥如意的一家
点赞  2008-10-14 11:28

哈哈,做得不错,这年头用STM32时髦+实用

                                  
点赞  2008-10-14 11:45

哈哈 定位就是学习啦

                                 当然要放出mp3的声音是不可能啦
点赞  2008-10-14 12:14

放出mp3的声音怎么不可能呀

用它的PWM完全可以做出播放WAV的声音呀,如果是带有IIS总线的那就更不用说了,,IIS不就是专门的语音的总线吗,,LS说的应该就是普通的声音吧,WAV的音质比MP3的要好呀,,,,如果真正做MP3解码是挺难的,
点赞  2008-10-23 14:34

请教楼上

那PWM怎么变声音出来,直接RC滤波?那失真,噪声不是很大?
我试过WAV是可以读出来,可是做不到到声音的转换

下面是我淘宝上的地址:
http://auction1.taobao.com/auction/item_detail-0db1-b8d83af207f1916f6d6bfc886bd9f586.jhtml

欢迎大家捧场哦
点赞  2008-10-24 11:36
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复