实验目的:播放音乐
代码:
#include "stm32f10x.h"
#define Buzzer_TOGGLE {GPIOB->ODR ^=GPIO_Pin_8;}
int B;
void key_init(void);
void TIM_4(void);
void Buzzer_init(void);
void SysTick_Init(int n);
void delay_us(unsigned int nus);
void delay_ms(unsigned int nms);
int main(void)
{
enum Low_frequency{l_dao=262,l_re=286,l_mi=311,l_fa=349,l_sao=392,l_la=440,l_xi=494};
enum
Normal_frequency{dao=523,re=587,mi=659,fa=698,sao=784,la=880,xi=987};
int j;
int i_f;
unsigned int f[]={l_sao,l_sao,l_la,l_sao,dao,l_xi,
l_sao,l_sao,l_la,l_sao,re,dao,
l_sao,l_sao,sao,mi,dao,l_la,l_xi,
fa,fa,mi,dao,re,dao,
};
unsigned char JP[]={ 1,2,2,2,2,4,
1,2,2,2,2,4,
1,2,2,2,2,4,4,
1,2,2,2,2,4
};
TIM_4();
Buzzer_init();
while(1)
{
i_f=0;
while(f[i_f]!=0xff)
{
B=(1000*1000/2)/f[i_f];
SysTick_Init(B);
for(j=0;j<JP[i_f];j++)
delay_ms(208);
i_f++;
}
}
}
void key_init(void)
{
GPIO_InitTypeDef keyInit;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
keyInit.GPIO_Pin=GPIO_Pin_0;
keyInit.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&keyInit);
}
void TIM_4(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
TIM_TimeBaseStructure.TIM_Period = 1;
TIM_TimeBaseStructure.TIM_Prescaler=(72-1);
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Down;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
}
void Buzzer_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void SysTick_Init(int n)
{
if (SysTick_Config(SystemCoreClock/1000000*n)) {
/* Capture error */
while(1);
}
}
此代码有参考,但是时间太久了,已找不到原博文了,如果有侵权,立删。