1. #include "Beep.h"
2.
3. void Beep_Init(void)
4. {
5. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
6.
7. GPIO_InitTypeDef GPIO_InitStructure;
8. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
9. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
10. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
11. GPIO_Init(GPIOF, &GPIO_InitStructure);
12.
13. GPIO_SetBits(GPIOF, GPIO_Pin_0); //置高,蜂鸣器不响
14. }
15.
16. void TIM_UserConfig (uint16_t Period, uint16_t Prescaler)
17. {
18. RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
19.
20. TIM_TimeBaseInitTypeDef TIM_InitStructure;
21. TIM_InitStructure.TIM_Period = Period;
22. TIM_InitStructure.TIM_Prescaler = Prescaler;
23. TIM_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //不分割时钟
24. TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
25. TIM_InitStructure.TIM_RepetitionCounter = 0; //重复计数次数,就是计数溢出即申请中断
26. TIM_TimeBaseInit(TIM1, &TIM_InitStructure); //初始化定时器
27.
28. TIM_ClearITPendingBit(TIM1, TIM_IT_Update);//清除 TIM1 的中断待处理位,先清除,再开启中断
29. TIM_ITConfig(TIM1, TIM_IT_Update | TIM_IT_Trigger, ENABLE);//开启计数器中断(定时器1,计数|触发中断源,使能)
30. TIM_Cmd(TIM1, ENABLE);//使能计数器
31.
32. NVIC_InitTypeDef NVIC_InitStructure;
33. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //设置中断分组0,主优先级0,抢占优先级3
34. NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;//中断入口
35. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
36. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
37. NVIC_Init(&NVIC_InitStructure);
38.
39. }
40.
41. void TIM1_UP_IRQHandler(void)
42. {
43. //获取中断状态
44. if( TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET ) //只要不等于0,就代表进来中断了
45. {
46. GPIO_WriteBit(GPIOF, GPIO_Pin_0, (BitAction)(!GPIO_ReadOutputDataBit(GPIOF, GPIO_Pin_0)));
47. //进入中断,先把这个TIM_IT_Update清零
48. TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
49. }
50. }
51.