STM32單片機TIM3蜂鳴器播放音樂
void delay1(uchar i)
{ long j; for(;i>0;i--) { for(j=0;j<6000;j++); }
}
void delay(uchar i)
{ unsigned long j; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_Cmd(TIM3, DISABLE); for(;i>0;i--) { for(j=0;j<60000;j++); }
//TIM_Cmd(TIM3, ENABLE);
}
void song()
{
// TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// TIM_TimeBaseStructure.TIM_Period = reload_time;
// reload_time=FREQH[k]*256+FREQL[k]; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; reload_time=get_time; TIM_TimeBaseStructure.TIM_Period = reload_time; TIM_Cmd(TIM3, ENABLE);
delay(time);
}
int main (void)
{ uchar i=0; RCC_Configuration(); //配置時鐘 NVIC_Configuration(); GPIO_Configuration(); //配置IO口 TIM3_Configuration(); GPIO_WriteBit(GPIOD, GPIO_Pin_9, (BitAction)1); GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)1); delay1(10); GPIO_WriteBit(GPIOD, GPIO_Pin_9, (BitAction)0); GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)0);
while(1) { time=1; i=0; while(i<75) { k=sszymmh[i]+7*sszymmh[i+1]-1;
get_time=FREQH[k]*256+FREQL[k];
time = sszymmh [i+ 2]; //讀出時間長度數(shù)值
i=i+3;
song(); } }}
void TIM3_IRQHandler (void)
{ // TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_Cmd(TIM3, DISABLE); if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)//判斷是否為定時器3溢出中斷 { (bool)beep=!beep;
reload_time=get_time;
GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)beep); GPIO_WriteBit(GPIOD, GPIO_Pin_9, (BitAction)beep); } TIM_ClearFlag(TIM3, TIM_FLAG_Update); //清中斷標記 TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
評論