标题:
STM32三路超声波避障程序
[打印本页]
作者:
甘洋洋
时间:
2018-12-24 13:52
标题:
STM32三路超声波避障程序
单片机源程序如下:
#include "main.h"
#include "tim1.h"
#include "tim3.h"
#include "uart.h"
#include "share.h"
#include "delayus.h"
#include "direction.h"
#include "us_avoid.h"
__IO uint16_t Uart_Upload_Time;//串口上传数据周期ms
void SystemClock_Config(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_TIM1_Init();//电机驱动PWM
MX_TIM3_Init();//TIM3输入捕获配置
Init_DirectionGPIO();//电机驱动芯片IO口使能
MX_USART3_Init();//串口3使能
US_GPIOInit();//超声波GPIO口配置
// while(1)
// {
// TIM1->CCR1=L_PWM;
// TIM1->CCR2=R_PWM;
// ForWard1();
// }
while (1)
{
US_Obstacle_Avoid();//超声波避障
if(!Uart_Upload_Time)//串口数据周期发送
{
Uart_Upload_Time=300;//串口数据发送周期ms
printf("L=%dcm F=%dcm R=%dcm\r\n",US1_Distance,US2_Distance,US3_Distance);//串口打印数据
}
}
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
/**Initializes the CPU, AHB and APB busses clocks
*/
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(__FILE__, __LINE__);
}
/**Initializes the CPU, AHB and APB busses 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(__FILE__, __LINE__);
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV4;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
/**
* @brief This function is executed in case of error occurrence.
* @param file: The file name as string.
* @param line: The line in file as a number.
* @retval None
*/
// _Error_Handler(__FILE__, __LINE__);
void _Error_Handler(char *file, int line)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
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 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
所有资料51hei提供下载:
Program.7z
(2.9 MB, 下载次数: 43)
2018-12-24 15:43 上传
点击文件名下载附件
工程文件
下载积分: 黑币 -5
作者:
muse122334
时间:
2019-12-7 23:03
感谢老哥
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1