标签:
功能
使蜂鸣器发出响声
分析
代码
1 //beep.h 2 #ifndef _BEEP_H_ 3 #define _BEEP_H_ 4 5 #define BEEP_ON (GPIOF->ODR |= (1<<8)) 6 #define BEEP_OFF (GPIOF->ODR &= ~(1<<8)) 7 8 void Beep_Init(void); 9 void Delay(int n); 10 11 #endif
1 //beep.c 2 #include "stm32f4xx.h" 3 #include "beep.h" 4 5 void Beep_Init(void) 6 { 7 //管脚外设时钟 8 RCC->AHB1ENR |= (1<<5); 9 //PF8管脚状态 10 //模式配置 11 GPIOF->MODER &= ~(0X3<<16); //清零 12 GPIOF->MODER |= (0X1<<16); //PF8配置为通用输出状态 13 //输出类型配置 14 GPIOF->OTYPER &= ~(0X1<<8); //推挽输出 15 //速率配置 16 GPIOF->OSPEEDR &= ~(0X1<<16); 17 GPIOF->OSPEEDR |= (0X2<<16); 18 //上下拉配置 19 GPIOF->PUPDR &= ~(0X3<<16); 20 GPIOF->PUPDR |= (0X1<<16); 21 } 22 23 void Delay(int n) 24 { 25 while(n--); 26 }
1 //main.c 2 #include "stm32f4xx.h" 3 #include "beep.h" 4 5 int main() 6 { 7 while(1) 8 { 9 Beep_Init(); 10 BEEP_ON; 11 Delay(1000000); 12 BEEP_OFF; 13 Delay(1000000); 14 } 15 }
标签:
原文地址:http://www.cnblogs.com/maxin/p/5857865.html