Guest User

Untitled

a guest
May 4th, 2016
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.81 KB | None | 0 0
  1. #include "stm32f4xx_hal.h"
  2. #include "spi.h"
  3. #include "gpio.h"
  4.  
  5. void SystemClock_Config(void);
  6.  
  7. int main(void)
  8. {
  9.     HAL_Init();
  10.     SystemClock_Config();
  11.     MX_GPIO_Init();
  12.     MX_SPI1_Init();
  13.  
  14.     while (1) {
  15.         uint8_t zeroes = 0;
  16.         uint8_t zero = 0b11100000;
  17.         uint8_t one = 0b11111000;
  18.  
  19.         uint8_t buffer[744];
  20.  
  21.         for (int i = 0; i < 24; i++)
  22.             buffer[i] = zeroes;
  23.  
  24.         uint8_t color = 0;
  25.         for (int i = 24; i < 744; i += 24) {
  26.             for (int j = 0; j < 24; j++)
  27.                 buffer[i+j] = zero;
  28.  
  29.             if (color == 0) {
  30.                 buffer[i] = one;
  31.                 color++;
  32.             } else if (color == 1) {
  33.                 buffer[i + 8] = one;
  34.                 color++;
  35.             } else if (color == 2) {
  36.                 buffer[i + 16] = one;
  37.                 color = 0;
  38.             }
  39.         }
  40.  
  41.         HAL_SPI_Transmit(&hspi1, &buffer, 744, 1000);
  42.         HAL_Delay(10000);
  43.     }
  44. }
  45.  
  46. //
  47. // KONFIGURACJA PĘTLI PLL WYGENEROWANA PRZEZ CUBEMX
  48. //
  49. void SystemClock_Config(void) {
  50.     RCC_OscInitTypeDef RCC_OscInitStruct;
  51.     RCC_ClkInitTypeDef RCC_ClkInitStruct;
  52.  
  53.     __HAL_RCC_PWR_CLK_ENABLE();
  54.  
  55.     __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  56.  
  57.     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  58.     RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  59.     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  60.     RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  61.     RCC_OscInitStruct.PLL.PLLM = 5;
  62.     RCC_OscInitStruct.PLL.PLLN = 64;
  63.     RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  64.     RCC_OscInitStruct.PLL.PLLQ = 4;
  65.     HAL_RCC_OscConfig(&RCC_OscInitStruct);
  66.  
  67.     RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  68.                                 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  69.     RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  70.     RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  71.     RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  72.     RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  73.     HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
  74.  
  75.     HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  76.  
  77.     HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  78.  
  79.     /* SysTick_IRQn interrupt configuration */
  80.     HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  81. }
  82.  
  83. //
  84. // SPI.C (WYGENEROWANE PRZEZ CUBEMX)
  85. //
  86. [...]
  87. SPI_HandleTypeDef hspi1;
  88.  
  89. /* SPI1 init function */
  90. void MX_SPI1_Init(void)
  91. {
  92.   hspi1.Instance = SPI1;
  93.   hspi1.Init.Mode = SPI_MODE_MASTER;
  94.   hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  95.   hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  96.   hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  97.   hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  98.   hspi1.Init.NSS = SPI_NSS_SOFT;
  99.   hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  100.   hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  101.   hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  102.   hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  103.   hspi1.Init.CRCPolynomial = 10;
  104.   HAL_SPI_Init(&hspi1);
  105.  
  106. }
  107.  
  108. void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
  109. {
  110.   GPIO_InitTypeDef GPIO_InitStruct;
  111.   if(hspi->Instance==SPI1)
  112.   {
  113.     /* Peripheral clock enable */
  114.     __HAL_RCC_SPI1_CLK_ENABLE();
  115.  
  116.     /**SPI1 GPIO Configuration    
  117.     PA5     ------> SPI1_SCK
  118.     PA7     ------> SPI1_MOSI
  119.     */
  120.     GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7;
  121.     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  122.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  123.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  124.     GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
  125.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  126.   }
  127. }
  128.  
  129. void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
  130. {
  131.   if(hspi->Instance==SPI1)
  132.   {
  133.     /* Peripheral clock disable */
  134.     __HAL_RCC_SPI1_CLK_DISABLE();
  135.  
  136.     /**SPI1 GPIO Configuration    
  137.     PA5     ------> SPI1_SCK
  138.     PA7     ------> SPI1_MOSI
  139.     */
  140.     HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_7);
  141.   }
  142. }
  143. [...]
  144.  
  145. //
  146. // GPIO.C
  147. //
  148. [...]
  149. void MX_GPIO_Init(void)
  150. {
  151.   /* GPIO Ports Clock Enable */
  152.   __HAL_RCC_GPIOC_CLK_ENABLE();
  153.   __HAL_RCC_GPIOH_CLK_ENABLE();
  154.   __HAL_RCC_GPIOA_CLK_ENABLE();
  155. }
  156. [...]
Advertisement
Add Comment
Please, Sign In to add comment