申 请 I D:RayC02
1、申 请 I D:RayC022、个人邮箱:ssr_s4@163.com
3、原创文章:STM32L475使用PWM方式控制舵机和超声波测距模块
开发板:STM32L475VET6 (潘多拉lot开发板)
模块:SG90、HC-SR04
使用STM32CubeMX进行配置生成MDK代码,STM32CubeMX 是 ST 意法半导体近几年来大力推荐的STM32 芯片图形化配置工具,目的就是为了方便开发者, 允许用户使用图形化向导生成C 初始化代码,可以大大减轻开发工作,时间和费用,提高开发效率。
STM32CubeMX几乎覆盖了STM32 全系列芯。IDE为Keil5集成开发环境,主要采用C语言编程,库函数使用stm32l4xx_hal库。
系统组成结构示意图:
电源接入后,系统首先初始化时钟、定时器、LED灯、LCD屏幕、外接模块,串口的引脚以及配置,这时屏幕亮起,舵机旋转到0°准备工作,然后使能PWM输出同时使能捕获,经过计算得到测量的距离且输出到串口以及LCD屏幕,然后将采集到的距离进行判断是否小于70mm,如果小于70mm则将舵机旋转135°同时亮起LED灯,否则保持不动,将循环获取距离且判断。
HC-SR04超声波传感器的工作原理:
(1)采用IO口Trig触发测距,给至少10us的高电平信号;
(2)模块自动发送8个40khz的方波,自动检测是否有信号返回;
(3)有信号返回,通过IO口Echo输出一个高电平,高电平持续的时间就是超声波从发射到返回的时间。测试距离=(高电平时间*声速(340M/S))/2;
SG90舵机工作原理:
SG90舵机通过接收PWM信号,使其进入内部电路产生一个偏置电压,触发电机通过减速齿轮带动电位器移动,使电压差为零时,电机停转,从而达到伺服的效果。即给舵机一个特定的PWM信号,舵机旋转到特定角度。
舵机的控制,需要一个20us的时基脉冲,控制高电平时间为0.5ms~2.5ms范围内即可控制舵机在0 ~ 180°转动。
keil工程中主函数主要实现代码:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2021 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>
#include "lcd.h"
/*
KEY0 - PD10
KEY1 - PD9
KEY2 - PD8
WK_UP - PC13
*//*
#define KEY0 HAL_GPIO_ReadPin(GPIOD,GPIO_PIN_10)
#define KEY1 HAL_GPIO_ReadPin(GPIOD,GPIO_PIN_9)
#define KEY2 HAL_GPIO_ReadPin(GPIOD,GPIO_PIN_8)
#define WK_UP HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13)
#define KEY0_PRES 1
#define KEY1_PRES 2
#define KEY2_PRES 3
#define WKUP_PRES 4
uint8_t KEY_Scan(uint8_t mode);
*/
/* 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 */
uint8_t iscapup = 1;//捕获上升沿状态
uint16_t valueUp = 0;
uint16_t valueDown = 0;
uint16_t width = 0;
uint8_t updateCount = 0;
uint8_t echoFlag = 0;
/* 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 */
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(iscapup)
{
valueUp=HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_2); //获取当前捕获
__HAL_TIM_SET_CAPTUREPOLARITY(htim,TIM_CHANNEL_2,TIM_ICPOLARITY_FALLING); //设置为下降沿捕获
iscapup = 0;
}
else{
valueDown=HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_2);
__HAL_TIM_SET_CAPTUREPOLARITY(htim,TIM_CHANNEL_2,TIM_ICPOLARITY_RISING);//设置为上升沿捕获
iscapup = 1;
width = valueDown + updateCount * 65536 - valueUp;
echoFlag = 1;
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
updateCount++;
}
uint32_t width2dist(uint16_t width)
{
return width*170/1000;
}
/* USER CODE END 0 */
/**
* @briefThe application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* PB11 T2C4 SG90 两个传感器引脚位怕自己忘了咋插的
PD13 T4C2 ECHO
PC7 T3C2 TRIG*/
uint32_t dist = 0;
char printString;
//uint8_t key;
/* 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();
MX_USART1_UART_Init();
MX_TIM3_Init();
MX_SPI3_Init();
/* USER CODE BEGIN 2 */
LCD_Init();
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);
HAL_UART_Transmit(&huart1,"HC_SR04 print Start\r\n",22,HAL_MAX_DELAY);
HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_2); //使能输出
HAL_TIM_IC_Start_IT(&htim4,TIM_CHANNEL_2); //启动输入捕获
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(dist<= 70)
{
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9,GPIO_PIN_RESET);
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4,2000);
LCD_ShowString(0, 140, 240, 24, 24, " OPEN!");
HAL_Delay(3000);
}
else{
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9,GPIO_PIN_SET);
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4,500);
LCD_ShowString(0, 140, 240, 24, 24, " CLOSE!");
}
HAL_Delay(10);
if(echoFlag){
dist = width2dist(width);
sprintf(printString,"Distance:%u mm\r\n",dist);
HAL_UART_Transmit(&huart1,printString,strlen(printString),HAL_MAX_DELAY);
POINT_COLOR = RED;
BACK_COLOR = WHITE;
LCD_ShowString(0, 60, 240, 32, 32, "Distance");
LCD_ShowString(0, 100, 240, 24, 24, printString);
POINT_COLOR = BLUE;
LCD_ShowString(0, 160, 240, 32, 32, "2020090103");
echoFlag = 0;
}
/*key = KEY_Scan(0); //按键扫描
switch(key)
{
caseWKUP_PRES:
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9,GPIO_PIN_RESET);
HAL_Delay(200);
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9,GPIO_PIN_SET);
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4,2500);
break;
caseKEY2_PRES:
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9,GPIO_PIN_RESET);
HAL_Delay(200);
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9,GPIO_PIN_SET);
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4,3000);
break;
caseKEY1_PRES:
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9,GPIO_PIN_RESET);
HAL_Delay(200);
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9,GPIO_PIN_SET);
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4,500);
break;
caseKEY0_PRES:
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9,GPIO_PIN_RESET);
HAL_Delay(200);
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_9,GPIO_PIN_SET);
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4,100);
break;
default:
break;
}*/
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** 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.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
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_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/*uint8_t KEY_Scan(uint8_t mode)
{
static uint8_t key_up = 1;
if(mode == 1)key_up = 1;
if(key_up && (KEY0 == 0 || KEY1 == 0 || KEY2 == 0 || WK_UP == 1))
{
HAL_Delay(10);
key_up = 0;
if(KEY0 == 0) return KEY0_PRES;
else if(KEY1 == 0)return KEY1_PRES;
else if(KEY2 == 0)return KEY2_PRES;
else if(WK_UP == 1) return WKUP_PRES;
}
else if(KEY0 == 1 && KEY1 == 1 && KEY2 == 1 && WK_UP == 0)key_up = 1;
return 0;
}
*/
/* USER CODE END 4 */
/**
* @briefThis 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 */
}
#ifdefUSE_FULL_ASSERT
/**
* @briefReports the name of the source file and the source line number
* where the assert_param error has occurred.
* @paramfile: pointer to the source file name
* @paramline: 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 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
作品实现图:
完整程序如有需要可以私我
感觉有点牛批! 耐心等待审批吧。 这是原创吗?网上搜到类似的了。 Hmily 发表于 2022-1-26 17:27
这是原创吗?网上搜到类似的了。
后面的图是电脑卡住了 把别的图上传出来了 关于stm32的教程有很多,但是l475系列的开发板教程却很少 RayC02 发表于 2022-1-29 07:58
后面的图是电脑卡住了 把别的图上传出来了 关于stm32的教程有很多,但是l475系列的开发板教程却很少
我连部分文字都能搜到一样的,所以在质疑文章的原创性,明白吗? Hmily 发表于 2022-1-29 15:22
我连部分文字都能搜到一样的,所以在质疑文章的原创性,明白吗?
因为上述介绍大部分是单片机或者模块的工作原理 原理这种东西不可能是个人原创的吧?但是如何把这个原理实现成为我的作品就是我个人所做了 谢谢。 RayC02 发表于 2022-1-30 14:30
因为上述介绍大部分是单片机或者模块的工作原理 原理这种东西不可能是个人原创的吧?但是如何把这个原理 ...
抱歉了,我确实不懂这方面内容,那具体原创的部分画板子? Hmily 发表于 2022-1-30 22:50
抱歉了,我确实不懂这方面内容,那具体原创的部分画板子?
新年快乐。原创的部分是在stm32中使用cubeMX图形化配置,以及部分代码。因为我还没上传完整工程源码所以没那么直观 RayC02 发表于 2022-2-1 09:52
新年快乐。原创的部分是在stm32中使用cubeMX图形化配置,以及部分代码。因为我还没上传完整工程源码所以 ...
新年快乐,很抱歉目前内容不足以申请,要不等下个月3月13日周年开放注册来注册吧。
页:
[1]