打印

各位帮我看下为什么蜂鸣器会一直响 我的是PB5接蜂鸣器

[复制链接]
1379|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
tong1991海深|  楼主 | 2012-11-30 15:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "stm32f10x_lib.h"
vu8 flag=0;
void GPIO_Config()
{

GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
  //开GPIOA时钟

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD;

GPIO_Init(GPIOB,&GPIO_InitStructure);
}

void NVIC_Config()
{

NVIC_InitTypeDef NVIC_InitStructure;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQChannel;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

NVIC_Init(&NVIC_InitStructure);

}

void TIME_Config()
{

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);

TIM_DeInit(TIM2);

TIM_TimeBaseStructure.TIM_Period=2000;

TIM_TimeBaseStructure.TIM_Prescaler=(36000-1);

TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;

TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);

TIM_ClearFlag(TIM2,TIM_FLAG_Update);

TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);

TIM_Cmd(TIM2,ENABLE);
}

int main()
{

GPIO_Config();

NVIC_Config();

TIME_Config();

while(1);
}




void TIM2_IRQHandler()
{

    flag++;

if(flag==2)

flag=0;

if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)

{

TIM_ClearITPendingBit(TIM2,TIM_FLAG_Update);

if(flag==0)

{

GPIO_SetBits(GPIOB,GPIO_Pin_5);

}

else

{

GPIO_ResetBits(GPIOB,GPIO_Pin_5);

}


}

}
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

31

主题

136

帖子

1

粉丝