初级会员
- 积分
- 67
- 金钱
- 67
- 注册时间
- 2023-11-22
- 在线时间
- 8 小时
|
楼主 |
发表于 2024-1-10 17:10:39
|
显示全部楼层
这几天加了一个定时器中断来控制蜂鸣器,但是也没有实现效果,
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_DMA_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
MX_TIM3_Init();
MX_TIM6_Init();
// 清除定时器初始化过程中的更新中断标志,避免定时器一启动就中断
__HAL_TIM_CLEAR_IT(&htim3, TIM_IT_UPDATE);
HAL_TIM_Base_Start_IT(&htim3);
/* USER CODE BEGIN 2 */
StartPic();
/* USER CODE END 2 */
HAL_Delay(5000);
/* Call init function for freertos objects (in freertos.c) */
MX_FREERTOS_Init();
/* Start scheduler */
osKernelStart();
/* We should never get here as control is now taken by the scheduler */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* 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
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** 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 = 4;
RCC_OscInitStruct.PLL.PLLN = 72;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
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 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
static uint16_t TIM3_Cnt1 = 0;
// 判断是定时器3发生的中断
if(htim->Instance == TIM3)
{
// 定时器3每中断一次,计数器自加1
TIM3_Cnt1 ++;
// 计数1000次,定时1s
if(TIM3_Cnt1 >= MIN*50)//1000
{
// 清除计数器
TIM3_Cnt1 = 0;
HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_0);
}
}
}
/* USER CODE END 4 */
main函数↑
void MX_FREERTOS_Init(void) {
osThreadDef(defaultTask, StartDefaultTask, osPriorityRealtime, 0, 512);
defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
/* definition and creation of myTask02 */
osThreadDef(myTask02, StartTask02, osPriorityRealtime, 0, 256);
myTask02Handle = osThreadCreate(osThread(myTask02), NULL);
/* definition and creation of myTask03 */
osThreadDef(myTask03, StartTask03, osPriorityHigh, 0, 256);
myTask03Handle = osThreadCreate(osThread(myTask03), NULL);
/* definition and creation of myTask04 */
osThreadDef(myTask04, StartTask04, osPriorityRealtime, 0, 256);
myTask04Handle = osThreadCreate(osThread(myTask04), NULL);
/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
/* USER CODE END RTOS_THREADS */
}
/* USER CODE BEGIN Header_StartDefaultTask */
/**
* @brief Function implementing the defaultTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void const * argument)
{
/* USER CODE BEGIN StartDefaultTask */
// portTickType xLastWakeTime;
// xLastWakeTime = xTaskGetTickCount();
/* Infinite loop */
for(;;)
{
memset(&RadarData.rangeOut[0],50,8);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
HAL_UART_Transmit(&huart2,sendReadUltraCmd,sizeof(sendReadUltraCmd),0XFFFF);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
HAL_UART_Receive_IT(&huart2,&RadarData.buffer[0],48);
HAL_Delay(450);
HAL_UART_AbortTransmit_IT(&huart2);// stop acq
__HAL_UART_CLEAR_IDLEFLAG(&huart2);
// Beep(10);
;
osDelay(100);
}
/* USER CODE END StartDefaultTask */
}
/* USER CODE BEGIN Header_StartTask02 */
/**
* @brief Function implementing the myTask02 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask02 */
void StartTask02(void const * argument)
{
uint8_t i,j;
uint8_t flag;
uint8_t id;
for(;;)
{
memset(&RadarData.status[0],0,8);
for (i = 0; i < 43; i++)
{
flag = ((RadarData.buffer[i] == 0xE5) &&
(RadarData.buffer[i + 1] == 0xE5) &&
(RadarData.buffer[i + 2] == 0xEA) &&
(RadarData.buffer[i + 5] == 0xFE));
if(flag)
{
id = RadarData.buffer[i + 3];
if(id > 0 && id < 9)
{
RadarData.status[id - 1] = 1;
RadarData.range[id - 1] = (float)RadarData.buffer[i + 4]/10;
for (j = 0; j < BUFFER_LENGTH - 1; j++)
{
RadarData.rangeBuffer[id-1][j] = RadarData.rangeBuffer[id-1][j + 1];
}
RadarData.rangeBuffer[id-1][BUFFER_LENGTH - 1] = RadarData.range[id - 1];
}
i = i + 5;
}
}
osDelay(100);
}
/* USER CODE END StartTask02 */
}
/* USER CODE BEGIN Header_StartTask03 */
/**
* @brief Function implementing the myTask03 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask03 */
void StartTask03(void const * argument)
{
/* USER CODE BEGIN StartTask03 */
uint8_t i,j;
uint8_t minRange = 5;
uint8_t m;
for(;;)
{
for ( i = 0; i < 8; i++)
{
for(j =0; j < BUFFER_LENGTH; j++)
{
RadarData.rangeTmp[j] = RadarData.rangeBuffer[i][j];
}
BubbleSort(RadarData.rangeTmp, BUFFER_LENGTH);
RadarData.rangeFilter[i] = RadarData.rangeTmp[BUFFER_LENGTH/2];
if( RadarData.status[i] == 0)
RadarData.rangeOut[i] = 50;
else
RadarData.rangeOut[i] = (uint8_t)(RadarData.rangeFilter[i] * 10 + 0.5f);
}
minRange = 50;
for ( i = 0; i < 4; i++)
{
if(minRange > RadarData.rangeOut[i])
{
minRange = RadarData.rangeOut[i];
}
}
RadarData.minRangeFront = minRange;//修改这里看八个雷达的反应速度
minRange = 50;
for ( i = 4; i < 8; i++)
{
if(minRange > RadarData.rangeOut[i])
{
minRange = RadarData.rangeOut[i];
}
}
RadarData.minRangeRear = minRange;
MIN = RadarData.minRangeFront < RadarData.minRangeRear?RadarData.minRangeFront:RadarData.minRangeRear;
for (m = 1; m < 9; m++)
{
Distance(m, RadarData.rangeOut[m-1]);
BLE_Display(m-1);
}
// min range front;
Radar_ShowMin(0, RadarData.minRangeFront);
// min range rear
Radar_ShowMin(1, RadarData.minRangeRear);
if(MIN <= 25 && MIN > 0)
HAL_TIM_Base_Start_IT(&htim3); //距离在2.5m内,开启定时器,蜂鸣器报警,
else
{
HAL_TIM_Base_Stop_IT(&htim3);
__HAL_TIM_CLEAR_IT(&htim3, TIM_IT_UPDATE); //否则关闭定时器,蜂鸣器不报警
}
osDelay(10);
}
/* USER CODE END StartTask03 */
}
/* USER CODE BEGIN Header_StartTask04 */
/**
* @brief Function implementing the myTask04 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask04 */
void StartTask04(void const * argument)
{
/* USER CODE BEGIN StartTask04 */
for(;;)
{
// Beep(20);
osDelay(1);
/* USER CODE END StartTask04 */
}
}
freertos.c↑ |
|