STM32
直播中

刘超

8年用户 1529经验值
私信 关注
[问答]

想通过一个IO口上升沿触发, 控制另一个IO输出2秒的高电平,控制的那个IO口一直都是高电平,为什么?

本人小白一枚,想通过一个IO口上升沿触发, 控制另一个IO输出2秒的高电平,然后输出2秒低电平,我编了程序,中断是触发了,进入中断处理函数后只执行了第一句,控制的那个IO口一直都是高电平,高手帮我看看,谢谢了
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  ** This notice applies to any and all portions of this file
  * that are not between comment pairs USER CODE BEGIN and
  * USER CODE END. Other portions of this file, whether
  * inserted by the user or by software development tools
  * are owned by their respective copyright owners.
  *
  * COPYRIGHT(c) 2019 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "flash.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

uint32_t state = 0, count = 0;

/* 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 */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* 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 */
//        state = Flash_Read(FLASH_USER_START_ADDR);
  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
               
//                HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);
//                HAL_Delay(1000);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

/*                switch(state)
                {
                        case 0:
                                //µÈ´ýPB0ÖжÏ
                                break;
                        case 1: //PB0ÒÑÖжÏ
                                if(count == 0)
                                {
                                        Flash_Write(FLASH_USER_START_ADDR,state);
                                }
                                if(count++ > 20)
                                {
                                        state = 2;
                                        count = 0;
                                        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
                                }
                                break;
                                
                        case 2:        //µÈ´ýPA12ÖжÏ
                                break;
                        
                        case 3:
                                if(count == 0)
                                {
                                        Flash_Write(FLASH_USER_START_ADDR,state);
                                }
                                if(count++ > 20)
                                {
                                        state = 0;
                                        count = 0;
                                        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET);
                                }
                                break;
                }
               
                HAL_Delay(100);                //100ms*/
  }
  /* 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 CPU, AHB and APB busses clocks
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig( RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /**Initializes the CPU, AHB and APB busses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig( RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @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_GPIOF_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);

  /*Configure GPIO pin : PA3 */
  GPIO_InitStruct.Pin = GPIO_PIN_3;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA,  GPIO_InitStruct);

  /*Configure GPIO pin : PB0 */
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  HAL_GPIO_Init(GPIOB,  GPIO_InitStruct);

  /*Configure GPIO pin : PA12 */
  GPIO_InitStruct.Pin = GPIO_PIN_12;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(GPIOA,  GPIO_InitStruct);

  /*Configure GPIO pin : PB3 */
  GPIO_InitStruct.Pin = GPIO_PIN_3;
  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);

  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI0_1_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI0_1_IRQn);

  HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);

}

/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
        if(GPIO_Pin == GPIO_PIN_0)
        {
                  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
                        HAL_Delay(1000);        
                  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);
                  HAL_Delay(1000);
                /*
                if(state == 0)
                {
                        state = 1;
                        count = 0;
                        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
                        HAL_Delay(2000);        
                }                        
        }
        else if(GPIO_Pin == GPIO_PIN_12)
        {
                if(state == 2)
                {
                        state = 3;
                        count = 0;
                        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET);
                        HAL_Delay(2000);        
                }
        }
        else
        {
        
        }*/
}
}
/* 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 */

  /* 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(char *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %drn", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/




回帖(2)

刘悦竹

2024-4-23 10:12:27
中断里面延时,这中断时间太长了吧?Delay(1000)也是中断方式,是不是优先级配置有问题,导致Delay执行不下去?
举报

生龙活虎3

2024-5-9 16:43:32
首先,我会尝试理解您的问题并提供一些建议。您想要通过一个输入/输出(IO)口的上升沿触发,控制另一个IO口输出2秒的高电平,然后输出2秒的低电平。您提到您的程序中的中断被触发了,但是只执行了第一句,导致控制的IO口一直保持高电平。

为了解决这个问题,我们需要确保以下几点:

1. 确保您的硬件连接正确,即输入信号可以正确地触发中断。

2. 在中断服务程序中,确保您正确地设置了定时器或其他计时机制,以便在2秒后切换输出电平。

3. 确保您的中断服务程序中的代码是按照预期执行的,包括清除中断标志位,以防止重复触发。

以下是一个简化的示例,展示了如何使用STM32微控制器实现您的需求:

```c
#include "stm32f1xx_hal.h"

// 定义输入和输出IO口
#define INPUT_PIN GPIO_PIN_0
#define OUTPUT_PIN GPIO_PIN_1

// 定义定时器和通道
#define TIMER_HANDLE htim1
#define TIMER_CHANNEL TIM_CHANNEL_1

// 全局变量,用于存储定时器的开始时间
uint32_t startTime;

void SystemClock_Config(void);
void MX_TIM1_Init(void);

int main(void) {
  HAL_Init();
  SystemClock_Config();
  MX_TIM1_Init();

  // 初始化GPIO
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  __HAL_RCC_GPIOA_CLK_ENABLE();
  GPIO_InitStruct.Pin = INPUT_PIN | OUTPUT_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  // 初始化定时器
  TIMER_HANDLE.Instance = TIM1;
  TIMER_HANDLE.Init.Prescaler = (uint32_t)((SystemCoreClock / 2) - 1);
  TIMER_HANDLE.Init.CounterMode = TIM_COUNTERMODE_UP;
  TIMER_HANDLE.Init.Period = 1000 - 1;
  TIMER_HANDLE.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  TIMER_HANDLE.Init.RepetitionCounter = 0;
  HAL_TIM_Base_Init(&TIMER_HANDLE);

  // 配置定时器通道
  TIM_HandleTypeDef *htim = &TIMER_HANDLE;
  TIM_OC_InitTypeDef sConfigOC = {0};
  TIM_OC_ChannelConfigTypeDef sConfig = {0};
  sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
  sConfigOC.Pulse = 1000;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  HAL_TIM_OC_ConfigChannel(htim, &sConfigOC, TIMER_CHANNEL);
  HAL_TIM_OC_Start_IT(htim, TIMER_CHANNEL);

  // 配置外部中断
  HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI0_IRQn);

  while (1) {
    // 空循环,等待中断
  }
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
  if (htim->Instance == TIM1) {
    // 切换输出电平
    HAL_GPIO_TogglePin(GPIOA, OUTPUT_PIN);
    // 重新启动定时器
    HAL_TIM_Base_Start_IT(&TIMER_HANDLE);
  }
}

void EXTI0_IRQHandler(void) {
  // 清除中断标志位
  __HAL_GPIO_EXTI_CLEAR_IT(INPUT_PIN);
  // 启动定时器
  HAL_TIM_Base_Start_IT(&TIMER_HANDLE);
}
```

请注意,这个示例仅用于演示目的,您可能需要根据您的硬件和需求进行调整。希望这可以帮助您解决问题。如果您有任何疑问或需要进一步的帮助,请随时提问。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分