跟视频学到外部中断这一块,程序是按照例程写的,区别是中断的配置是由stm32cubemx配置的。下载板子上,按键不能正常开关小灯

2019-07-20 03:33发布

本帖最后由 leigentle 于 2019-3-1 16:57 编辑

#include "stm32f4xx_hal.h"
#define KEY0        HAL_GPIO_ReadPin(GPIOH,GPIO_PIN_3)  //KEY0°´¼üPH3
#define KEY1        HAL_GPIO_ReadPin(GPIOH,GPIO_PIN_2)  //KEY1°´¼üPH2
#define KEY2        HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13) //KEY2°´¼üPC13
#define WK_UP       HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)  //WKUP°´¼üPA0
#define LED0 PBout(1)   //LED0
#define LED1 PBout(0)   //LED1
#define PBout(n)   BIT_ADDR(GPIOB_ODR_Addr,n)  //Êä³ö
#define PBin(n)    BIT_ADDR(GPIOB_IDR_Addr,n)  //ÊäÈë
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr)  *((volatile unsigned long  *)(addr))
#define BIT_ADDR(addr, bitnum)   MEM_ADDR(BITBAND(addr, bitnum))
#define GPIOB_ODR_Addr    (GPIOB_BASE+20) //0x40020414
#define GPIOB_IDR_Addr    (GPIOB_BASE+16) //0x40020410
#define KEY0_PRES  1
#define KEY1_PRES  2
#define KEY2_PRES  3
#define WKUP_PRES   4
uint32_t fac_us=0;
void delay_us(uint32_t nus)
{  
uint32_t ticks;
uint32_t told,tnow,tcnt=0;
uint32_t reload=SysTick->LOAD;    //LOAD&micro;&Auml;&Ouml;&micro;      
ticks=nus*fac_us;       //&ETH;è&Ograve;&ordf;&micro;&Auml;&frac12;&Uacute;&Aring;&Auml;&Ecirc;&yacute;
told=SysTick->VAL;            //&cedil;&Otilde;&frac12;&oslash;&Egrave;&euml;&Ecirc;±&micro;&Auml;&frac14;&AElig;&Ecirc;&yacute;&AElig;÷&Ouml;&micro;
while(1)
{
  tnow=SysTick->VAL;
  if(tnow!=told)
  {     
   if(tnow<told)tcnt+=told-tnow; //&Otilde;&acirc;&Agrave;&iuml;×&cent;&Ograve;&acirc;&Ograve;&raquo;&Iuml;&Acirc;SYSTICK&Ecirc;&Ccedil;&Ograve;&raquo;&cedil;&ouml;&micro;&Yacute;&frac14;&otilde;&micro;&Auml;&frac14;&AElig;&Ecirc;&yacute;&AElig;÷&frac34;&Iacute;&iquest;&Eacute;&Ograve;&Ocirc;&Aacute;&Euml;.
   else tcnt+=reload-tnow+told;     
   told=tnow;
   if(tcnt>=ticks)break;   //&Ecirc;±&frac14;&auml;&sup3;&not;&sup1;&yacute;/&micro;&Egrave;&Oacute;&Uacute;&Ograve;&ordf;&Ntilde;&Oacute;&sup3;&Ugrave;&micro;&Auml;&Ecirc;±&frac14;&auml;,&Ocirc;ò&Iacute;&Euml;&sup3;&ouml;.
  }  
};
}
void delay_ms(uint16_t nms)
{
uint32_t i;
for(i=0;i<nms;i++) delay_us(1000);
}

/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
//uint8_t KEY_Scan(uint8_t mode)
//{
//    static uint8_t key_up=1;     //°&acute;&frac14;ü&Euml;&Eacute;&iquest;&ordf;±ê&Ouml;&frac34;
//    if(mode==1)key_up=1;    //&Ouml;§&sup3;&Ouml;&Aacute;&not;°&acute;
//    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;   //&Icirc;&THORN;°&acute;&frac14;ü°&acute;&Iuml;&Acirc;
//}
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
void LED_Init()
{
    GPIO_InitTypeDef GPIO_Initure;
    __HAL_RCC_GPIOB_CLK_ENABLE();           //&iquest;&ordf;&AElig;&ocirc;GPIOB&Ecirc;±&Ouml;&Oacute;

    GPIO_Initure.Pin=GPIO_PIN_0|GPIO_PIN_1; //PB1,0
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;  //&Iacute;&AElig;&Iacute;ì&Ecirc;&auml;&sup3;&ouml;
    GPIO_Initure.Pull=GPIO_PULLUP;          //&Eacute;&Iuml;&Agrave;&shy;
    GPIO_Initure.Speed=GPIO_SPEED_HIGH;     //&cedil;&szlig;&Euml;&Ugrave;
    HAL_GPIO_Init(GPIOB,&GPIO_Initure);

    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET); //PB0&Ouml;&Atilde;1
    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET); //PB1&Ouml;&Atilde;1  
}
int main(void)
{
  uint8_t mode;
  /* USER CODE BEGIN 1 */
  /* USER CODE END 1 */
  /* MCU Configuration----------------------------------------------------------*/
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
//  KEY_Scan(mode);
  /* Configure the system clock */
  SystemClock_Config();
  /* Initialize all configured peripherals */
  MX_GPIO_Init();

  LED_Init();
  /* USER CODE BEGIN 2 */
  /* USER CODE END 2 */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */
  /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
delay_ms(100);
switch(GPIO_Pin)
{
  case GPIO_PIN_0:
   if(WK_UP==1)
   {
    LED1=!LED1;
    LED0=!LED1;
   }
   break;
   case GPIO_PIN_2:
   if(KEY1==1)
   {
    LED1=!LED1;
   }
   break;  
  case GPIO_PIN_3:
   if(KEY0==1)
   {
    LED0=!LED0;
    LED1=!LED1;
   }
   break;
  case GPIO_PIN_13:
   if(KEY2==1)
   {
    LED0=!LED0;
   }
   break;
   
}
}
/** Configure pins as
        * Analog
        * Input
        * Output
        * EVENT_OUT
        * EXTI
*/
void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  /*Configure GPIO pin : PC13 */
  GPIO_InitStruct.Pin = GPIO_PIN_13;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  /*Configure GPIO pin : PA0 */
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  /*Configure GPIO pins : PH2 PH3 */
  GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI0_IRQn, 2, 0);
  HAL_NVIC_EnableIRQ(EXTI0_IRQn);
  HAL_NVIC_SetPriority(EXTI2_IRQn, 2, 1);
  HAL_NVIC_EnableIRQ(EXTI2_IRQn);
  HAL_NVIC_SetPriority(EXTI3_IRQn, 2, 2);
  HAL_NVIC_EnableIRQ(EXTI3_IRQn);
  HAL_NVIC_SetPriority(EXTI15_10_IRQn, 2, 3);
  HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
#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 ", file, line) */
  /* USER CODE END 6 */
}
#endif

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。