Upload files to "3-5 GPIO_光敏传感器/Hardware"
This commit is contained in:
47
3-5 GPIO_光敏传感器/Hardware/Buzzer.c
Normal file
47
3-5 GPIO_光敏传感器/Hardware/Buzzer.c
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#include "stm32f10x.h" // Device header
|
||||||
|
#include "stdio.h"
|
||||||
|
|
||||||
|
void Buzzer_Init(void)
|
||||||
|
{
|
||||||
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
|
||||||
|
|
||||||
|
GPIO_InitTypeDef GPIO_InitStruct; //初始化GPIO,定义GPIO结构体,以下设置其参数
|
||||||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//GPIO_Mode_Out_OD 开漏模式,低电平驱动,高电平时为高阻态;GPIO_Mode_Out_PP 推挽模式,高低电平都有驱动
|
||||||
|
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
|
||||||
|
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
||||||
|
GPIO_Init(GPIOB,&GPIO_InitStruct);
|
||||||
|
|
||||||
|
GPIO_SetBits(GPIOB,GPIO_Pin_12);//初始化Buzzer
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*!< Pin x selected ON or OFF
|
||||||
|
parameter opt is high or low;表示高低电平
|
||||||
|
*/
|
||||||
|
|
||||||
|
enum V_opt{//高低电压
|
||||||
|
high,
|
||||||
|
low
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 选择GPIOB外设进行Buzzer控制 ,opt有 high 和 low */
|
||||||
|
|
||||||
|
void GPIOB_Buzzer(uint16_t GPIO_Pin_x,enum V_opt opt)
|
||||||
|
{
|
||||||
|
if(opt == high) GPIO_SetBits(GPIOB,GPIO_Pin_x);
|
||||||
|
if(opt == low) GPIO_ResetBits(GPIOB,GPIO_Pin_x);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
控制GPIOB输出翻转
|
||||||
|
GPIO_Pin_x表示为哪个按键引脚,GPIO_Pin_Buzzer表示为哪个灯泡引脚
|
||||||
|
*/
|
||||||
|
void GPIOB_Buzzer_Turn(uint16_t GPIO_Pin_x)
|
||||||
|
{
|
||||||
|
if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_x) == 0)
|
||||||
|
GPIO_SetBits(GPIOB,GPIO_Pin_x);
|
||||||
|
else
|
||||||
|
GPIO_ResetBits(GPIOB,GPIO_Pin_x);
|
||||||
|
}
|
||||||
|
|
||||||
10
3-5 GPIO_光敏传感器/Hardware/Buzzer.h
Normal file
10
3-5 GPIO_光敏传感器/Hardware/Buzzer.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef __BUZZER_R
|
||||||
|
#define __BUZZER_R
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void Buzzer_Init(void);
|
||||||
|
void GPIOB_Buzzer(uint16_t GPIO_Pin_x,enum V_opt opt);
|
||||||
|
void GPIOB_Buzzer_Turn(uint16_t GPIO_Pin_x);
|
||||||
|
|
||||||
|
#endif
|
||||||
47
3-5 GPIO_光敏传感器/Hardware/LED.c
Normal file
47
3-5 GPIO_光敏传感器/Hardware/LED.c
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#include "stm32f10x.h" // Device header
|
||||||
|
#include "stdio.h"
|
||||||
|
|
||||||
|
void LED_Init(void)
|
||||||
|
{
|
||||||
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
|
||||||
|
|
||||||
|
GPIO_InitTypeDef GPIO_InitStruct; //初始化GPIO,定义GPIO结构体,以下设置其参数
|
||||||
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//GPIO_Mode_Out_OD 开漏模式,低电平驱动,高电平时为高阻态;GPIO_Mode_Out_PP 推挽模式,高低电平都有驱动
|
||||||
|
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_All;
|
||||||
|
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
||||||
|
GPIO_Init(GPIOA,&GPIO_InitStruct);
|
||||||
|
|
||||||
|
GPIO_SetBits(GPIOA,GPIO_Pin_1 | GPIO_Pin_2);//初始化LED后不点亮
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*!< Pin x selected ON or OFF
|
||||||
|
parameter opt is high or low;表示高低电平
|
||||||
|
*/
|
||||||
|
|
||||||
|
enum V_opt{//高低电压
|
||||||
|
high,
|
||||||
|
low
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 选择GPIOA外设进行LED控制 ,opt有 high 和 low */
|
||||||
|
//这个函数有点没用
|
||||||
|
void GPIOA_LED(uint16_t GPIO_Pin_x,enum V_opt opt)
|
||||||
|
{
|
||||||
|
if(opt == high) GPIO_SetBits(GPIOA,GPIO_Pin_x);
|
||||||
|
if(opt == low) GPIO_ResetBits(GPIOA,GPIO_Pin_x);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
控制GPIOA灯泡亮灭
|
||||||
|
GPIO_Pin_x表示为哪个按键引脚,GPIO_Pin_LED表示为哪个灯泡引脚
|
||||||
|
*/
|
||||||
|
void GPIOA_LED_Turn(uint16_t GPIO_Pin_x)
|
||||||
|
{
|
||||||
|
if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_x) == 0)
|
||||||
|
GPIO_SetBits(GPIOA,GPIO_Pin_x);
|
||||||
|
else
|
||||||
|
GPIO_ResetBits(GPIOA,GPIO_Pin_x);
|
||||||
|
}
|
||||||
|
|
||||||
41
3-5 GPIO_光敏传感器/Hardware/key.c
Normal file
41
3-5 GPIO_光敏传感器/Hardware/key.c
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#include "stm32f10x.h" // Device header
|
||||||
|
#include "Delay.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void KeyInit(void)
|
||||||
|
{
|
||||||
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
|
||||||
|
|
||||||
|
GPIO_InitTypeDef GPIO_InitStructure;
|
||||||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
|
||||||
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_11;
|
||||||
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||||
|
|
||||||
|
GPIO_Init(GPIOB,&GPIO_InitStructure);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Key_GetNum(void)
|
||||||
|
{
|
||||||
|
uint8_t Key_Num = 0;
|
||||||
|
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 0)
|
||||||
|
{
|
||||||
|
Delay_ms(20);//消抖
|
||||||
|
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 0) ;//按键未弹起,循环等待
|
||||||
|
Delay_ms(20);
|
||||||
|
Key_Num = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11) == 0)
|
||||||
|
{
|
||||||
|
Delay_ms(20);//消抖
|
||||||
|
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11) == 0) ;//按键未弹起,循环等待
|
||||||
|
Delay_ms(20);
|
||||||
|
Key_Num = 2;
|
||||||
|
};
|
||||||
|
return Key_Num;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
15
3-5 GPIO_光敏传感器/Hardware/key.h
Normal file
15
3-5 GPIO_光敏传感器/Hardware/key.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef __KEY_H
|
||||||
|
#define __KEY_H
|
||||||
|
|
||||||
|
void KeyInit(void);//init PB1 and PB11
|
||||||
|
|
||||||
|
|
||||||
|
/* PB1 按下弹起返回1 PB11 按下弹起返回2 */
|
||||||
|
uint8_t Key_GetNum(void);//获取键位值
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user