Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f4xx_hal.h"
- #include "spi.h"
- #include "gpio.h"
- void SystemClock_Config(void);
- int main(void)
- {
- HAL_Init();
- SystemClock_Config();
- MX_GPIO_Init();
- MX_SPI1_Init();
- while (1) {
- uint8_t zeroes = 0;
- uint8_t zero = 0b11100000;
- uint8_t one = 0b11111000;
- uint8_t buffer[744];
- for (int i = 0; i < 24; i++)
- buffer[i] = zeroes;
- uint8_t color = 0;
- for (int i = 24; i < 744; i += 24) {
- for (int j = 0; j < 24; j++)
- buffer[i+j] = zero;
- if (color == 0) {
- buffer[i] = one;
- color++;
- } else if (color == 1) {
- buffer[i + 8] = one;
- color++;
- } else if (color == 2) {
- buffer[i + 16] = one;
- color = 0;
- }
- }
- HAL_SPI_Transmit(&hspi1, &buffer, 744, 1000);
- HAL_Delay(10000);
- }
- }
- //
- // KONFIGURACJA PĘTLI PLL WYGENEROWANA PRZEZ CUBEMX
- //
- void SystemClock_Config(void) {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- __HAL_RCC_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 = 5;
- RCC_OscInitStruct.PLL.PLLN = 64;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 4;
- 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_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- //
- // SPI.C (WYGENEROWANE PRZEZ CUBEMX)
- //
- [...]
- SPI_HandleTypeDef hspi1;
- /* SPI1 init function */
- void MX_SPI1_Init(void)
- {
- hspi1.Instance = SPI1;
- hspi1.Init.Mode = SPI_MODE_MASTER;
- hspi1.Init.Direction = SPI_DIRECTION_2LINES;
- hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
- hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
- hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
- hspi1.Init.NSS = SPI_NSS_SOFT;
- hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
- hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
- hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
- hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
- hspi1.Init.CRCPolynomial = 10;
- HAL_SPI_Init(&hspi1);
- }
- void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- if(hspi->Instance==SPI1)
- {
- /* Peripheral clock enable */
- __HAL_RCC_SPI1_CLK_ENABLE();
- /**SPI1 GPIO Configuration
- PA5 ------> SPI1_SCK
- PA7 ------> SPI1_MOSI
- */
- GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- }
- }
- void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
- {
- if(hspi->Instance==SPI1)
- {
- /* Peripheral clock disable */
- __HAL_RCC_SPI1_CLK_DISABLE();
- /**SPI1 GPIO Configuration
- PA5 ------> SPI1_SCK
- PA7 ------> SPI1_MOSI
- */
- HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_7);
- }
- }
- [...]
- //
- // GPIO.C
- //
- [...]
- void MX_GPIO_Init(void)
- {
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOH_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- }
- [...]
Advertisement
Add Comment
Please, Sign In to add comment