Advertisement
Guest User

STM32F4 Discovery test

a guest
Oct 22nd, 2014
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.80 KB | None | 0 0
  1. #include <stm32f4xx.h>
  2. #include "stm32f4xx_hal.h"
  3.  
  4. void Delay(uint32_t n)
  5. {
  6.     while(n--);
  7. }
  8.  
  9. static void SystemClock_Config(void);
  10.  
  11. void initGPIO()
  12. {
  13.     GPIO_InitTypeDef GPIO_InitStruct;
  14.     __GPIOD_CLK_ENABLE();
  15.     GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
  16.     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  17.     GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  18.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  19.     HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  20.    
  21.     __GPIOA_CLK_ENABLE();
  22.     //RCC->AHBENR |= (1 << 17);
  23.     GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2;
  24.     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  25.     GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  26.     GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  27.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  28.    
  29. }
  30.  
  31. int main (void)
  32. {
  33.     HAL_Init();
  34.     initGPIO();
  35.     SystemClock_Config();
  36.    
  37.     while(1)
  38.     {
  39.         /*if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET)
  40.         {
  41.             HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15, GPIO_PIN_SET);
  42.         }
  43.         else
  44.         {
  45.             HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15, GPIO_PIN_RESET);
  46.         }*/
  47.         HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15, HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0));
  48.     }
  49.     return 0;
  50. }
  51.  
  52. static void SystemClock_Config(void)
  53. {
  54.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  55.   RCC_OscInitTypeDef RCC_OscInitStruct;
  56.  
  57.   /* Enable Power Control clock */
  58.   __PWR_CLK_ENABLE();
  59.  
  60.   /* The voltage scaling allows optimizing the power consumption when the device is
  61.      clocked below the maximum system frequency, to update the voltage scaling value
  62.      regarding system frequency refer to product datasheet.  */
  63.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  64.  
  65.   /* Enable HSE Oscillator and activate PLL with HSE as source */
  66.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  67.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  68.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  69.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  70.   RCC_OscInitStruct.PLL.PLLM = 8;
  71.   RCC_OscInitStruct.PLL.PLLN = 336;
  72.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  73.   RCC_OscInitStruct.PLL.PLLQ = 7;
  74.   if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  75.   {
  76.     //Error_Handler();
  77.   }
  78.  
  79.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  80.      clocks dividers */
  81.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  82.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  83.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  84.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  
  85.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  
  86.   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  87.   {
  88.     //Error_Handler();
  89.   }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement