豆皮 - STM32开发板入门教程(二) - 按键 + 蜂鸣器 (原创)
版权所有 STMFANS 原创,转载请保留出处
http://www.stmfans.com/bbs/viewthread.php?tid=1064&extra=page%3D1
呵呵 在教程一的基础上 继续做偶们的按键和蜂鸣器 呵呵 豆皮板子上一共有5个按键(4个功能按键 + 一个RESET按键) 那么 选择 LED5 LED6 LED7 LED8 与按键 KEY1 KEY2 KEY3 KEY4 一一对应 按键按下的时候 对应的LED灯就亮起来 然后只要有按键安下 蜂鸣器就响 呵呵 初始化
//配置KEY使用的GPIO口 KEY_GPIO_Configuration();
和
//配置buzzer蜂鸣器使用的端口 BUZZER_GPIO_Configuration();
分别对应的函数体
按键的初始化函数体 /******************************************************************************* * Function Name : KEY_GPIO_Configuration * Description : Configures the KEY GPIO ports. * Input : None * Output : None * Return : None *******************************************************************************/ void KEY_GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; //按键使用的GPIOC 的 Pin6 Pin7 Pin8 端口 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 ; // GPIO口的速度 作为按键 10MHz 对于一般的用途 足以 呵呵 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //按键端口设置为 浮空输入 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOC, &GPIO_InitStructure);
//按键使用的GPIOB 的 Pin15 端口 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 ; // GPIO口的速度 作为按键 10MHz 对于一般的用途 足以 呵呵 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //按键端口设置为 浮空输入 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); } 蜂鸣器的初始化函数体 /******************************************************************************* * Function Name : BUZZER_GPIO_Configuration * Description : Configures the BUZZER GPIO ports. * Input : None * Output : None * Return : None *******************************************************************************/ void BUZZER_GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; //蜂鸣器使用的GPIOB 的 Pin9 端口 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ; //蜂鸣器的IO口速度暂时也设置为10MHz 如果以后想用PWM控制蜂鸣器发出各种不同声音 就需要把速度设置高一点 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //蜂鸣器端口设置为 推挽输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); } 蜂鸣器的接法 也是标准接法 呵呵 如下图所示
|