"); //-->
使用STM8L101F3P6
驱动蜂鸣器:滴滴 两下,然后关闭3s. 关键程序如下:
void main(void)
{
GPIO_Init(BUZZER_GPIO_PORT,BUZZER_GPIO_PINS,GPIO_Mode_Out_PP_Low_Slow);//PB2 TIM2_CH2
CLK_PeripheralClockConfig (CLK_Peripheral_TIM2,ENABLE); //使能外设时钟,STM8L外设时钟默认关闭,使用前需使能
TIM2_DeInit();
//TIM2_TimeBaseInit(TIM2_Prescaler_128, 0xff);//16M/8/128=15.625K,0xff=255,255*(1/15.625)=0.01632S,大约61次中断是1S
TIM2_TimeBaseInit(TIM2_Prescaler_1,TIM2_CounterMode_Up,999);//16M/8/1=2M,0xff=255,255*(1/15.625)=0.01632S,大约61次中断是1S 2KHz
TIM2_OC2Init(TIM2_OCMode_PWM1,
TIM2_OutputState_Enable,
500,
TIM2_OCPolarity_High,
TIM2_OCIdleState_Set);
TIM2_OC2PreloadConfig(ENABLE);
}
void music (void)
{
TIM2_CtrlPWMOutputs(ENABLE);
Delayms(100);
TIM2_CtrlPWMOutputs(DISABLE);
Delayms(100);
TIM2_CtrlPWMOutputs(ENABLE);
Delayms(100);
TIM2_CtrlPWMOutputs(DISABLE);
DelayS(3);
}
//延时微妙
void Delayus(void)
{
asm("nop");
// asm("nop");
// asm("nop");
// asm("nop");
}
//延时毫秒
void Delayms(unsigned int time)
{
unsigned int i;
while(time--){
// for(i=900;i>0;i--)
for(i=100;i>0;i--)
{
Delayus();
}
}
}
//秒延时
void DelayS(unsigned int time)
{
while(time--)
{
Delayms(1000);
}
}
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。