基于stm32的超声波感应灯、
时间: 2023-11-03 11:16:45 浏览: 109
基于STM32的超声波测距报警仪
超声波感应灯可以在人体接近时自动开启,人离开后自动关闭。使用STM32单片机可以实现对超声波传感器的控制和LED灯的控制。下面是一个基于STM32的超声波感应灯的示例代码:
```
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_exti.h"
#include "misc.h"
#define TRIG_PIN GPIO_Pin_0
#define ECHO_PIN GPIO_Pin_1
#define LED_PIN GPIO_Pin_12
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = TRIG_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = ECHO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = LED_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void EXTI_Config(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI1_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line1) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line1);
GPIO_SetBits(GPIOB, LED_PIN);
TIM_Cmd(TIM2, ENABLE);
}
}
void TIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 1000;
TIM_TimeBaseStructure.TIM_Prescaler = 7200;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_Cmd(TIM2, DISABLE);
GPIO_ResetBits(GPIOB, LED_PIN);
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
}
void delay_us(uint32_t us)
{
uint32_t i;
while (us--)
{
i = 10;
while (i--)
{
asm("nop");
}
}
}
void Send_Trigger(void)
{
GPIO_SetBits(GPIOA, TRIG_PIN);
delay_us(10);
GPIO_ResetBits(GPIOA, TRIG_PIN);
}
float Get_Distance(void)
{
uint32_t temp = 0;
float distance = 0;
while (GPIO_ReadInputDataBit(GPIOA, ECHO_PIN) == RESET);
TIM_Cmd(TIM3, ENABLE);
while (GPIO_ReadInputDataBit(GPIOA, ECHO_PIN) == SET)
{
temp++;
delay_us(10);
if (temp > 5000)
{
break;
}
}
TIM_Cmd(TIM3, DISABLE);
distance = temp * 0.034 / 2;
return distance;
}
int main(void)
{
GPIO_Config();
EXTI_Config();
TIM_Config();
TIM_Cmd(TIM3, ENABLE);
while (1)
{
Send_Trigger();
float distance = Get_Distance();
if (distance > 0 && distance < 30)
{
GPIO_SetBits(GPIOB, LED_PIN);
TIM_Cmd(TIM2, ENABLE);
}
else
{
GPIO_ResetBits(GPIOB, LED_PIN);
TIM_Cmd(TIM2, DISABLE);
}
delay_us(500);
}
}
```
上述代码中使用了PA0作为超声波传感器的触发引脚,PA1作为接受引脚,PB12作为LED控制引脚。在程序中使用了定时器控制LED的开关,当有人接近时,开启定时器,一段时间后关闭LED。在主函数中使用了Send_Trigger()函数发送超声波信号,Get_Distance()函数返回测量到的距离,根据测量到的距离判断是否有人接近,从而控制LED的开关。
阅读全文