按键控制LED
按键抖动,电平发生变化,可用延时函数抵消按键抖动对系统的影响
传感器电路图
按键电路图
c语言对应类型
“_t”后缀表示使用typedef重命名的数据类型
枚举类型
# include <iostream>
using namespace std;
typedef enum { Mon= 1 , Tue= 2 , Wed= 3
} week_t ;
int main ( ) { week_t w; w= Wed; cout << w << endl; return 0 ;
}
按键控制LED灯
主函数
# include "stm32f10x.h"
# include "MyDelay.h"
# include "Delay.h"
# include "Button.h"
# include "stdio.h" uint8_t KeyNum ;
int main ( void ) { Led_Init ( ) ; Button_Init ( ) ; while ( 1 ) { KeyNum = Key_GetNum ( ) ; if ( KeyNum== 1 ) { Led_One_Turn ( ) ; } if ( KeyNum== 2 ) { Led_Two_Turn ( ) ; } } return 0 ;
}
Button
# ifndef Led_h
# define led_h
void Led_Init ( void ) ;
void Button_Init ( void ) ;
uint8_t Key_GetNum ( void ) ;
void Led_One_Turn ( void ) ;
void Led_Two_Turn ( void ) ;
# endif
# include "stm32f10x.h"
# include "Delay.h"
# include "stdio.h"
void Led_Init ( void ) { RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOA, ENABLE) ; GPIO_InitTypeDef GI; GI. GPIO_Mode = GPIO_Mode_Out_PP; GI. GPIO_Pin = GPIO_Pin_All; GI. GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init ( GPIOA, & GI) ; GPIO_SetBits ( GPIOA, GPIO_Pin_0| GPIO_Pin_1) ;
}
void Button_Init ( void ) { RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOB, ENABLE) ; GPIO_InitTypeDef Button; Button. GPIO_Mode = GPIO_Mode_IPU; Button. GPIO_Pin = GPIO_Pin_1| GPIO_Pin_11; Button. GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init ( GPIOB, & Button) ;
}
uint8_t Key_GetNum ( void ) { uint8_t KeyNum= 0 ; if ( GPIO_ReadInputDataBit ( GPIOB, GPIO_Pin_1) == 0 ) { Delay_ms ( 20 ) ; while ( GPIO_ReadInputDataBit ( GPIOB, GPIO_Pin_1) == 0 ) ; Delay_ms ( 20 ) ; KeyNum= 1 ; } if ( GPIO_ReadInputDataBit ( GPIOB, GPIO_Pin_11) == 0 ) { Delay_ms ( 20 ) ; while ( GPIO_ReadInputDataBit ( GPIOB, GPIO_Pin_11) == 0 ) ; Delay_ms ( 20 ) ; KeyNum= 2 ; } return KeyNum;
}
void Led_One_Turn ( void ) { uint8_t Light_Status = GPIO_ReadInputDataBit ( GPIOA, GPIO_Pin_0) ; if ( Light_Status== 0 ) GPIO_WriteBit ( GPIOA, GPIO_Pin_0, Bit_SET) ; else GPIO_WriteBit ( GPIOA, GPIO_Pin_0, Bit_RESET) ;
}
void Led_Two_Turn ( void ) { uint8_t Light_Status = GPIO_ReadInputDataBit ( GPIOA, GPIO_Pin_1) ; if ( Light_Status== 0 ) GPIO_WriteBit ( GPIOA, GPIO_Pin_1, Bit_SET) ; else GPIO_WriteBit ( GPIOA, GPIO_Pin_1, Bit_RESET) ;
}
光敏传感蜂鸣器(绿灯常亮,光线变暗,红灯亮,警报响起,光线恢复,警报解除)
# ifndef Buzzer
# define Buzzer
uint8_t Read_Buzzer_Data ( void ) ;
void Buzzer_PhotoResitors_Init ( void ) ;
void Reaction_PhotoResitors ( void ) ;
void Buzzer_Ring ( void ) ;
void Buzzer_Slient ( void ) ;
# endif
# include "stm32f10x.h"
# include "Button.h"
# include "Delay.h"
uint8_t Read_Buzzer_Data ( void ) { return GPIO_ReadInputDataBit ( GPIOB, GPIO_Pin_13) ;
}
void Buzzer_PhotoResitors_Init ( void ) { RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOB, ENABLE) ; GPIO_InitTypeDef Buzzer; Buzzer. GPIO_Mode = GPIO_Mode_IPU; Buzzer. GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_12; Buzzer. GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init ( GPIOB, & Buzzer) ; GPIO_WriteBit ( GPIOB, GPIO_Pin_12, Bit_SET) ;
}
void Buzzer_Ring ( void ) { GPIO_WriteBit ( GPIOA, GPIO_Pin_0, Bit_RESET) ; GPIO_WriteBit ( GPIOA, GPIO_Pin_1, Bit_SET) ; while ( 1 ) { GPIO_WriteBit ( GPIOB, GPIO_Pin_12, Bit_RESET) ; Delay_ms ( 200 ) ; GPIO_WriteBit ( GPIOB, GPIO_Pin_12, Bit_SET) ; Delay_ms ( 300 ) ; GPIO_WriteBit ( GPIOB, GPIO_Pin_12, Bit_RESET) ; Delay_ms ( 300 ) ; GPIO_WriteBit ( GPIOB, GPIO_Pin_12, Bit_SET) ; Delay_ms ( 100 ) ; GPIO_WriteBit ( GPIOB, GPIO_Pin_12, Bit_RESET) ; Delay_ms ( 200 ) ; GPIO_WriteBit ( GPIOB, GPIO_Pin_12, Bit_SET) ; Delay_ms ( 300 ) ; break ; } }
void Buzzer_Slient ( void ) { GPIO_WriteBit ( GPIOA, GPIO_Pin_0, Bit_SET) ; GPIO_WriteBit ( GPIOA, GPIO_Pin_1, Bit_RESET) ; GPIO_WriteBit ( GPIOB, GPIO_Pin_12, Bit_SET) ;
}
void Reaction_PhotoResitors ( void ) { uint8_t RP = GPIO_ReadInputDataBit ( GPIOB, GPIO_Pin_13) ; if ( RP== 1 ) Buzzer_Ring ( ) ; else Buzzer_Slient ( ) ;
}
项目结构