2015-09-04 206 views
2

我剛開始玩stm32f4發現板上的HAL。 最基本的眨眼不起作用,任何關於此事的暗示都會有幫助?代碼本身編譯沒有任何問題。STM32F4探索板hal閃爍不閃爍

我正在測試STM32CubeMX並使用預生成的主文件來處理它。 裏面沒有什麼先進的可能是問題的原因。

我正在使用CooCox CoIDE開發環境。

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

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

/* USER CODE BEGIN PFP */ 
/* Private function prototypes -----------------------------------------------*/ 

/* USER CODE END PFP */ 

/* USER CODE BEGIN 0 */ 

/* USER CODE END 0 */ 

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(); 

    /* Configure the system clock */ 
    SystemClock_Config(); 

    /* Initialize all configured peripherals */ 
    MX_GPIO_Init(); 

    /* USER CODE BEGIN 2 */ 

    /* USER CODE END 2 */ 

    /* Infinite loop */ 
    /* USER CODE BEGIN WHILE */ 
    while (1) 
    { 
    /* USER CODE END WHILE */ 
     HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14,GPIO_PIN_SET); 
     HAL_Delay(2000); 
     HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14,GPIO_PIN_RESET); 
     HAL_Delay(2000); 

    /* USER CODE BEGIN 3 */ 

    } 
    /* USER CODE END 3 */ 

} 

/** System Clock Configuration 
*/ 
void SystemClock_Config(void) 
{ 

    RCC_OscInitTypeDef RCC_OscInitStruct; 
    RCC_ClkInitTypeDef RCC_ClkInitStruct; 

    __PWR_CLK_ENABLE(); 

    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); 

    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 = 8; 
    RCC_OscInitStruct.PLL.PLLN = 336; 
    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; 
    RCC_OscInitStruct.PLL.PLLQ = 7; 
    HAL_RCC_OscConfig(&RCC_OscInitStruct); 

    RCC_ClkInitStruct.ClockType = 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_DIV4; 
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; 
    HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5); 

    HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); 

    HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); 

} 

/** Configure pins as 
     * Analog 
     * Input 
     * Output 
     * EVENT_OUT 
     * EXTI 
    PC3 ------> I2S2_SD 
    PA4 ------> I2S3_WS 
    PA5 ------> SPI1_SCK 
    PA6 ------> SPI1_MISO 
    PA7 ------> SPI1_MOSI 
    PB10 ------> I2S2_CK 
    PC7 ------> I2S3_MCK 
    PA9 ------> USB_OTG_FS_VBUS 
    PA10 ------> USB_OTG_FS_ID 
    PA11 ------> USB_OTG_FS_DM 
    PA12 ------> USB_OTG_FS_DP 
    PC10 ------> I2S3_CK 
    PC12 ------> I2S3_SD 
    PB6 ------> I2C1_SCL 
    PB9 ------> I2C1_SDA 
*/ 
void MX_GPIO_Init(void) 
{ 

    GPIO_InitTypeDef GPIO_InitStruct; 

    /* GPIO Ports Clock Enable */ 
    __GPIOE_CLK_ENABLE(); 
    __GPIOC_CLK_ENABLE(); 
    __GPIOH_CLK_ENABLE(); 
    __GPIOA_CLK_ENABLE(); 
    __GPIOB_CLK_ENABLE(); 
    __GPIOD_CLK_ENABLE(); 

    /*Configure GPIO pin : PE3 */ 
    GPIO_InitStruct.Pin = GPIO_PIN_3; 
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 
    GPIO_InitStruct.Pull = GPIO_NOPULL; 
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW; 
    HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); 

    /*Configure GPIO pin : PC0 */ 
    GPIO_InitStruct.Pin = GPIO_PIN_0; 
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 
    GPIO_InitStruct.Pull = GPIO_NOPULL; 
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW; 
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); 

    /*Configure GPIO pin : PC3 */ 
    GPIO_InitStruct.Pin = GPIO_PIN_3; 
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 
    GPIO_InitStruct.Pull = GPIO_NOPULL; 
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW; 
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI2; 
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); 

    /*Configure GPIO pin : PA0 */ 
    GPIO_InitStruct.Pin = GPIO_PIN_0; 
    GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING; 
    GPIO_InitStruct.Pull = GPIO_NOPULL; 
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 

    /*Configure GPIO pin : PA4 */ 
    GPIO_InitStruct.Pin = GPIO_PIN_4; 
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 
    GPIO_InitStruct.Pull = GPIO_NOPULL; 
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW; 
    GPIO_InitStruct.Alternate = GPIO_AF6_SPI3; 
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 

    /*Configure GPIO pins : PA5 PA6 PA7 */ 
    GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7; 
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 
    GPIO_InitStruct.Pull = GPIO_NOPULL; 
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW; 
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI1; 
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 

    /*Configure GPIO pin : PB2 */ 
    GPIO_InitStruct.Pin = GPIO_PIN_2; 
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 
    GPIO_InitStruct.Pull = GPIO_NOPULL; 
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 

    /*Configure GPIO pin : PB10 */ 
    GPIO_InitStruct.Pin = GPIO_PIN_10; 
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 
    GPIO_InitStruct.Pull = GPIO_NOPULL; 
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW; 
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI2; 
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 

    /*Configure GPIO pins : PD12 PD13 PD14 PD15 
          PD4 */ 
    GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15 
          |GPIO_PIN_4; 
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 
    GPIO_InitStruct.Pull = GPIO_NOPULL; 
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW; 
    HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); 

    /*Configure GPIO pins : PC7 PC10 PC12 */ 
    GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_10|GPIO_PIN_12; 
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 
    GPIO_InitStruct.Pull = GPIO_NOPULL; 
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW; 
    GPIO_InitStruct.Alternate = GPIO_AF6_SPI3; 
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); 

    /*Configure GPIO pin : PA9 */ 
    GPIO_InitStruct.Pin = GPIO_PIN_9; 
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 
    GPIO_InitStruct.Pull = GPIO_NOPULL; 
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 

    /*Configure GPIO pins : PA10 PA11 PA12 */ 
    GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12; 
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 
    GPIO_InitStruct.Pull = GPIO_NOPULL; 
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW; 
    GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; 
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 

    /*Configure GPIO pin : PD5 */ 
    GPIO_InitStruct.Pin = GPIO_PIN_5; 
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 
    GPIO_InitStruct.Pull = GPIO_NOPULL; 
    HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); 

    /*Configure GPIO pins : PB6 PB9 */ 
    GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_9; 
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; 
    GPIO_InitStruct.Pull = GPIO_PULLUP; 
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW; 
    GPIO_InitStruct.Alternate = GPIO_AF4_I2C1; 
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 

    /*Configure GPIO pin : PE1 */ 
    GPIO_InitStruct.Pin = GPIO_PIN_1; 
    GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING; 
    GPIO_InitStruct.Pull = GPIO_NOPULL; 
    HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); 

} 

/* 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\r\n", file, line) */ 
    /* USER CODE END 6 */ 

} 

#endif 

/** 
    * @} 
    */ 

/** 
    * @} 
*/ 

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

您沒有定義很多外設,請爲開始嘗試刪除所有不必要的部分,並開始在綠地和/或嘗試調試它。從我的角度來看,STM32CumeMX承諾的事情可能會超過它。但是,我嘗試使用多維數據集生成的基本代碼工作正常。 – vlk

回答

1

您發佈的代碼沒有什麼明顯的錯誤 - 大部分是由stm32cubemx生成的,對吧?

嘗試檢查stm32f4xx_it.c文件的內部 - 你打電話給HAL_IncTick();在SysTick中斷內?沒有這一點,HAL_Delay功能將無法正常工作...

此外,你有沒有試過在調試模式下單步執行代碼,看看發生了什麼?至少可以告訴你是否掛在任何地方。

如果以上都沒有幫助,那麼我會推薦廢除MX_GPIO_Init函數並編寫自己的函數,它只是初始化要切換的GPIO引腳。

這是我對stm32cubmx的主要問題 - 輸出過於複雜!時鐘配置的東西是不錯,但我不希望在main.c中 - 在一個單獨的文件將是OK(ISH)...

亞歷

+0

我建議調試代碼並查看時鐘的寄存器和IO端口的寄存器。我同意Alex ...檢查HAL_IncTick() – Lorenzo