灵动eMiniboard 系列MB-041 开发板入门5
开发板上自带了一个无源蜂鸣器,但是管脚上串联了一个电容器,所以不能直接拉高IO让它发声必须控制io按频率输出方波才行.(大意是这样,大佬勿喷.),这里没用PWM方式,只是简单的控制IO模拟.
1.硬件连接:(板载,无需额外连接)
2.按照一定计算方式输出方波:
void Sound(u16 frq)
{
u32 n;
if(frq != 1000)
{
n =50000/((u32)frq);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
DELAY_Us(n);
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
DELAY_Us(n);
}else
{
DELAY_Us(50);
}
}
3.设置频率,乐谱,时间:
uc16 tone[] = {247,262,294,330,349,392,440,294,523,587,659,698,784,1000};
const unsigned char music[]={
5,5,6,8,7,6,5,6,13,13,
5,5,6,8,7,6,5,3,13,13,
2,2,3,5,3,5,6,3,2,1,
6,6,5,6,5,3,6,5,13,13,
5,5,6,8,7,6,5,6,13,13,
5,5,6,8,7,6,5,3,13,13,
2,2,3,5,3,5,6,3,2,1,
6,6,5,6,5,3,6,1,
13,8,9,10,10,9,8,10,9,8,6,
13,6,8,9,9,8,6,9,8,6,5,
13,2,3,5,5,3,5,5,6,8,7,6,
6,10,9,9,8,6,5,6,8
};
const u8 time[] = {
2,4,2,2,2,2,2,8,4, 4,
2,4,2,2,2,2,2,8,4, 4,
2,4,2,4,2,2,4,2,2,8,
2,4,2,2,2,2,2,8,4 ,4,
2,4,2,2,2,2,2,8,4, 4,
2,4,2,2,2,2,2,8,4, 4,
2,4,2,4,2,2,4,2,2,8,
2,4,2,2,2,2,2,8,
4, 2,2,2, 4, 2,2,2, 2,2,8,
4, 2,2,2,4,2,2,2,2,2,8,
4, 2,2,2,4,2,2,5,2,6,2,4,
2,2 ,2,4,2,4,2,2,12
};
4.基本就是这样吧,完整代码见附件. 音频部分肯定有问题,音色不对,我对这方面没什么研究,这里也只是简单的演示一下,切勿深究.