网站建设分工明细表,乐清市建设路小学网站,响站怎么建设网站,哪里有广告设计培训机构目录
项目需求
项目框图
编辑
硬件清单
sg90舵机介绍及实战
sg90舵机介绍
角度控制
SG90舵机编程实现 超声波传感器介绍及实战 超声波传感器介绍 超声波编程实战
项目设计及实现 项目需求
检测靠近时#xff0c;垃圾桶自动开盖并伴随滴一声#xff0c;2秒后关盖…目录
项目需求
项目框图
编辑
硬件清单
sg90舵机介绍及实战
sg90舵机介绍
角度控制
SG90舵机编程实现 超声波传感器介绍及实战 超声波传感器介绍 超声波编程实战
项目设计及实现 项目需求
检测靠近时垃圾桶自动开盖并伴随滴一声2秒后关盖 发生震动时垃圾桶自动开盖并伴随滴一声2秒后关盖 按下按键时垃圾桶自动开盖并伴随滴一声2秒后关盖
项目框图 硬件清单
SG90舵机超声波模块震动传感器蜂鸣器 sg90舵机介绍及实战 sg90舵机介绍 PWM波的频率不能太高大约50HZ即周期1/频率1/500.02s20ms左右。 确定周期/频率 如果周期为20ms则 PSC7199ARR199 角度控制 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 SG90舵机编程实现 需求 每隔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);
} 超声波传感器介绍及实战 超声波传感器介绍 怎么让它发送波Trig 给Trig端口至少10us的高电平 怎么知道它开始发了 Echo信号由低电平跳转到高电平表示开始发送波 怎么知道接收了返回波 Echo由高电平跳转回低电平表示波回来了 怎么算时间 Echo引脚维持高电平的时间 波发出去的那一下开始启动定时器 波回来的拿一下我们开始停止定时器计算出中间经过多少时间 怎么算距离 距离 速度 340m/s* 时间/2 超声波编程实战 需求 使用超声波测距当手离传感器距离小于5cm时LED1点亮否则保持不亮状态。 接线 Trig --- PB6 Echo --- PB7 LED1 --- PB8 定时器配置 使用 TIM2 只用作计数功能不用作定时。 将 PSC 配置为71则计数 1 次代表 1us 。 编写微秒级函数 //使用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次表示1usdistance cnt*340/2*0.000001*100; //单位cmif(distance 5)HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);elseHAL_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 /* 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/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes *//* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
#define OPEN 1
#define CLOSE 0
/* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
char flagCLOSE;
/* USER CODE END PM *//* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim2;
TIM_HandleTypeDef htim4;/* USER CODE BEGIN PV *//* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);
static void MX_TIM4_Init(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;//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次表示1usreturn (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(flagCLOSE){flagOPEN;__HAL_TIM_SetCompare(htim4, TIM_CHANNEL_4, 15);//将舵机置90°HAL_GPIO_WritePin(GPIOB,GPIO_PIN_4,GPIO_PIN_RESET);HAL_Delay(100);HAL_GPIO_WritePin(GPIOB,GPIO_PIN_4,GPIO_PIN_SET);}HAL_Delay(2000);
}void closeDusbin()
{__HAL_TIM_SetCompare(htim4, TIM_CHANNEL_4, 5);//将舵机置0°flagCLOSE;HAL_Delay(150);
}void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{if(GPIO_PinGPIO_PIN_0||GPIO_PinGPIO_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 */float distance;/* 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 */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 *///超声波测距distanceget_distance();if(distance 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();}
}/*** brief TIM2 Initialization Function* param None* retval None*/
static void MX_TIM2_Init(void)
{/* USER CODE BEGIN TIM2_Init 0 *//* USER CODE END TIM2_Init 0 */TIM_ClockConfigTypeDef sClockSourceConfig {0};TIM_MasterConfigTypeDef sMasterConfig {0};/* USER CODE BEGIN TIM2_Init 1 *//* USER CODE END TIM2_Init 1 */htim2.Instance TIM2;htim2.Init.Prescaler 71;htim2.Init.CounterMode TIM_COUNTERMODE_UP;htim2.Init.Period 65535;htim2.Init.ClockDivision TIM_CLOCKDIVISION_DIV1;htim2.Init.AutoReloadPreload TIM_AUTORELOAD_PRELOAD_ENABLE;if (HAL_TIM_Base_Init(htim2) ! HAL_OK){Error_Handler();}sClockSourceConfig.ClockSource TIM_CLOCKSOURCE_INTERNAL;if (HAL_TIM_ConfigClockSource(htim2, sClockSourceConfig) ! HAL_OK){Error_Handler();}sMasterConfig.MasterOutputTrigger TIM_TRGO_RESET;sMasterConfig.MasterSlaveMode TIM_MASTERSLAVEMODE_DISABLE;if (HAL_TIMEx_MasterConfigSynchronization(htim2, sMasterConfig) ! HAL_OK){Error_Handler();}/* USER CODE BEGIN TIM2_Init 2 *//* USER CODE END TIM2_Init 2 */}/*** brief TIM4 Initialization Function* param None* retval None*/
static void MX_TIM4_Init(void)
{/* USER CODE BEGIN TIM4_Init 0 *//* USER CODE END TIM4_Init 0 */TIM_ClockConfigTypeDef sClockSourceConfig {0};TIM_MasterConfigTypeDef sMasterConfig {0};TIM_OC_InitTypeDef sConfigOC {0};/* USER CODE BEGIN TIM4_Init 1 *//* USER CODE END TIM4_Init 1 */htim4.Instance TIM4;htim4.Init.Prescaler 7199;htim4.Init.CounterMode TIM_COUNTERMODE_UP;htim4.Init.Period 199;htim4.Init.ClockDivision TIM_CLOCKDIVISION_DIV1;htim4.Init.AutoReloadPreload TIM_AUTORELOAD_PRELOAD_ENABLE;if (HAL_TIM_Base_Init(htim4) ! HAL_OK){Error_Handler();}sClockSourceConfig.ClockSource TIM_CLOCKSOURCE_INTERNAL;if (HAL_TIM_ConfigClockSource(htim4, sClockSourceConfig) ! HAL_OK){Error_Handler();}if (HAL_TIM_PWM_Init(htim4) ! HAL_OK){Error_Handler();}sMasterConfig.MasterOutputTrigger TIM_TRGO_RESET;sMasterConfig.MasterSlaveMode TIM_MASTERSLAVEMODE_DISABLE;if (HAL_TIMEx_MasterConfigSynchronization(htim4, sMasterConfig) ! HAL_OK){Error_Handler();}sConfigOC.OCMode TIM_OCMODE_PWM1;sConfigOC.Pulse 0;sConfigOC.OCPolarity TIM_OCPOLARITY_HIGH;sConfigOC.OCFastMode TIM_OCFAST_DISABLE;if (HAL_TIM_PWM_ConfigChannel(htim4, sConfigOC, TIM_CHANNEL_4) ! HAL_OK){Error_Handler();}/* USER CODE BEGIN TIM4_Init 2 *//* USER CODE END TIM4_Init 2 */HAL_TIM_MspPostInit(htim4);}/*** brief GPIO Initialization Function* param None* retval None*/
static void MX_GPIO_Init(void)
{GPIO_InitTypeDef GPIO_InitStruct {0};/* GPIO Ports Clock Enable */__HAL_RCC_GPIOD_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();__HAL_RCC_GPIOB_CLK_ENABLE();/*Configure GPIO pin Output Level */HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4|GPIO_PIN_8, GPIO_PIN_SET);/*Configure GPIO pin Output Level */HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);/*Configure GPIO pin : PA0 */GPIO_InitStruct.Pin GPIO_PIN_0;GPIO_InitStruct.Mode GPIO_MODE_IT_RISING;GPIO_InitStruct.Pull GPIO_NOPULL;HAL_GPIO_Init(GPIOA, GPIO_InitStruct);/*Configure GPIO pins : PB4 PB6 PB8 */GPIO_InitStruct.Pin GPIO_PIN_4|GPIO_PIN_6|GPIO_PIN_8;GPIO_InitStruct.Mode GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull GPIO_NOPULL;GPIO_InitStruct.Speed GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(GPIOB, GPIO_InitStruct);/*Configure GPIO pin : PB5 */GPIO_InitStruct.Pin GPIO_PIN_5;GPIO_InitStruct.Mode GPIO_MODE_IT_RISING;GPIO_InitStruct.Pull GPIO_NOPULL;HAL_GPIO_Init(GPIOB, GPIO_InitStruct);/*Configure GPIO pin : PB7 */GPIO_InitStruct.Pin GPIO_PIN_7;GPIO_InitStruct.Mode GPIO_MODE_INPUT;GPIO_InitStruct.Pull GPIO_NOPULL;HAL_GPIO_Init(GPIOB, GPIO_InitStruct);/* EXTI interrupt init*/HAL_NVIC_SetPriority(EXTI0_IRQn, 2, 0);HAL_NVIC_EnableIRQ(EXTI0_IRQn);HAL_NVIC_SetPriority(EXTI9_5_IRQn, 2, 0);HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);}/* 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 */