项目二:感应开关盖垃圾桶
项目需求
检测靠近时,垃圾桶自动开盖并伴随滴一声,
2
秒后关盖
发生震动时,垃圾桶自动开盖并伴随滴一声,
2
秒后关盖
按下按键时,垃圾桶自动开盖并伴随滴一声,
2
秒后关盖
项目框图
硬件清单
SG90
舵机,超声波模块,震动传感器,蜂鸣器
a. sg90
舵机介绍及实战
sg90
舵机介绍
PWM
波的频率不能太高,大约
50HZ
,即周期
=1/
频率
=1/50=0.02s
,
20ms
左右。
确定周期
/
频率
如果周期为
20ms
,则
PSC=7199
,
ARR=199
角度控制
0.5ms-------------0
度;
2.5%
对应函数中
CCRx
为
5
1.0ms------------45
度;
5.0%
对应函数中
CCRx
为
10
1.5ms------------90
度;
7.5%
对应函数中
CCRx
为
15
2.0ms-----------135
度;
10.0%
对应函数中
CCRx
为
20
2.5ms-----------180
度;
12.5%
对应函数中
CCRx为25
编程实现
需求:
每隔
1s
,转动一个角度:
0
度
--> 45
度
--> 90
度
--> 135
度
--> 180
度
--> 0
度
接线:
代码:
HAL_TIM_PWM_Start ( & htim4 , TIM_CHANNEL_3 );while ( 1 ){HAL_Delay ( 1000 );__HAL_TIM_SetCompare ( & htim4 , TIM_CHANNEL_3 , 5 );HAL_Delay ( 1000 );__HAL_TIM_SetCompare ( & htim4 , TIM_CHANNEL_3 , 10 );HAL_Delay ( 1000 );__HAL_TIM_SetCompare ( & htim4 , TIM_CHANNEL_3 , 15 );HAL_Delay ( 1000 );__HAL_TIM_SetCompare ( & htim4 , TIM_CHANNEL_3 , 20 );HAL_Delay ( 1000 );__HAL_TIM_SetCompare ( & htim4 , TIM_CHANNEL_3 , 25 );}
b. 超声波传感器介绍及实战
超声波传感器介绍
怎么让它发送波
Trig
,给
Trig
端口至少
10us
的高电平
怎么知道它开始发了
Echo
信号,由低电平跳转到高电平,表示开始发送波
怎么知道接收了返回波
Echo
,由高电平跳转回低电平,表示波回来了
怎么算时间
Echo
引脚维持高电平的时间!
波发出去的那一下,开始启动定时器
波回来的拿一下,我们开始停止定时器,计算出中间经过多少时间
怎么算距离
距离
=
速度 (
340m/s
)
* 时间/2
编程实战
需求:
使用超声波测距,当手离传感器距离小于
5cm
时,
LED1
点亮,否则保持不亮状态。
接线:
Trig --- PB6
Echo --- PB7
LED1 --- PB8
编写微秒级函数:
// 使用 TIM2 来做 us 级延时函数void TIM2_Delay_us ( uint16_t n_us ){/* 使能定时器 2 计数 */__HAL_TIM_ENABLE ( & htim2 );__HAL_TIM_SetCounter ( & htim2 , 0 );while ( __HAL_TIM_GetCounter ( & htim2 ) < (( 1 * n_us ) - 1 ) );/* 关闭定时器 2 计数 */__HAL_TIM_DISABLE ( & htim2 );}
主函数
//1. Trig ,给 Trig 端口至少 10us 的高电平//2. echo 由低电平跳转到高电平,表示开始发送波 // 波 发出去的那一下,开始启动定时器//3. 由高电平跳转回低电平,表示波回来了// 波回来的那一 下,我们开始停止定时器//4. 计算出中间经过多少时间//5. 距离 = 速度 ( 340m/s ) * 时间 /2 (计数1 次表示 1us ) // 每 500 毫秒测试一次距离
int cnt;
float distance;
while (1)
{
//1. Trig ,给Trig端口至少10us的高电平
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);//拉高
TIM2_Delay_us(20);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);//拉低
//2. echo由低电平跳转到高电平,表示开始发送波
//波发出去的那一下,开始启动定时器
while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7) == GPIO_PIN_RESET);//等待输入电平拉高
HAL_TIM_Base_Start(&htim2);
__HAL_TIM_SetCounter(&htim2,0);
//3. 由高电平跳转回低电平,表示波回来了
while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7) == GPIO_PIN_SET);//等待输入电平变低
//波回来的那一下,我们开始停止定时器
HAL_TIM_Base_Stop(&htim2);
//4. 计算出中间经过多少时间
cnt = __HAL_TIM_GetCounter(&htim2);
//5. 距离 = 速度 (340m/s)* 时间/2(计数1次表示1us)
distance = cnt*340/2*0.000001*100; //单位:cm
if(distance < 5)
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
//每500毫秒测试一次距离
HAL_Delay(500);
}
项目设计及实现
项目设计
超声波模块:
Trig -- PB6
Echo -- PB7
sg90
舵机:
PWM -- PB9
按键:
KEY1 -- PA0
LED
灯:
LED1 -- PB8
震动传感器:
D0 -- PB5
VCC -- 5V
蜂鸣器:
IO -- PB4
VCC -- 3V3
//main.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#define OPEN 1
#define CLOSE 0
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
char flag = CLOSE;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//使用TIM2来做us级延时函数
void TIM2_Delay_us(uint16_t n_us)
{
/* 使能定时器2计数 */
__HAL_TIM_ENABLE(&htim2);
__HAL_TIM_SetCounter(&htim2, 0);
while(__HAL_TIM_GetCounter(&htim2) < ((1 * n_us)-1) );
/* 关闭定时器2计数 */
__HAL_TIM_DISABLE(&htim2);
}
double get_distance()
{
int cnt = 0;
//1. Trig ,给Trig端口至少10us的高电平
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);//拉高
TIM2_Delay_us(20);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);//拉低
//2. echo由低电平跳转到高电平,表示开始发送波
//波发出去的那一下,开始启动定时器
while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7) == GPIO_PIN_RESET);//等待输入电平拉高
HAL_TIM_Base_Start(&htim2);
__HAL_TIM_SetCounter(&htim2,0);
//3. 由高电平跳转回低电平,表示波回来了
while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7) == GPIO_PIN_SET);//等待输入电平变低
//波回来的那一下,我们开始停止定时器
HAL_TIM_Base_Stop(&htim2);
//4. 计算出中间经过多少时间
cnt = __HAL_TIM_GetCounter(&htim2);
//5. 距离 = 速度 (340m/s)* 时间/2(计数1次表示1us)
return (cnt*340/2*0.000001*100); //单位:cm
}
void openStatusLight()
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
}
void closeStatusLight()
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
}
void initSG90_0()
{
HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_4);//启动定时器4
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_4, 5);//将舵机置为0度
}
void openDusbin()
{
if(flag == CLOSE){
flag = OPEN;
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_4, 15);//将舵机置为0度
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET);
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET);
}
HAL_Delay(2000);
}
void closeDusbin()
{
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_4, 5);//将舵机置为0度
flag = CLOSE;
HAL_Delay(150);
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == GPIO_PIN_0 ||GPIO_Pin == GPIO_PIN_5)
{
if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0) == GPIO_PIN_RESET/*||
HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_5) == GPIO_PIN_RESET*/)
{
openStatusLight();
openDusbin();
}
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM2_Init();
MX_TIM4_Init();
/* USER CODE BEGIN 2 */
float dis;
initSG90_0();
HAL_NVIC_SetPriority(SysTick_IRQn,0,0);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
dis = get_distance();
if(dis < 10)
{
openStatusLight();
openDusbin();
}
else
{
closeStatusLight();
closeDusbin();
}
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */