Open ALi-Natiq opened 4 years ago
We gebruiken STM32 CubeIDE als software om de STM32 NUCLEO-L4A6ZG kit te programmeren.
Eerst dus de MCU kiezen en dan het board (in onze geval STM32 NUCLEO-L4A6ZG)
Daarna naar de main.c om de code te plaatsen.
het is me nog altijd niet gelukt om het STM32L4A6ZG boord werkend te krijgen met ultrasoon sensor blijkbaar Omdat ik dit boord dat ik heb bestaat niet op de IDE van de STM software die ik heb.
Ik heb al een gelijkaardig boord gekozen en daarmee geprobeerd te werken , maar ik krijg het niet werkend.
`/ USER CODE BEGIN Header / /**
/ / USER CODE END Header */
/ Includes ------------------------------------------------------------------/
/ Private includes ----------------------------------------------------------/ / USER CODE BEGIN Includes /
/ USER CODE END Includes /
/ Private typedef -----------------------------------------------------------/ / USER CODE BEGIN PTD /
/ USER CODE END PTD /
/ Private define ------------------------------------------------------------/ / USER CODE BEGIN PD /
/ USER CODE END PD /
/ Private macro -------------------------------------------------------------/ / USER CODE BEGIN PM /
/ USER CODE END PM /
/ Private variables ---------------------------------------------------------/ TIM_HandleTypeDef htim4;
UART_HandleTypeDef huart2;
/ USER CODE BEGIN PV /
/ USER CODE END PV /
/ Private function prototypes -----------------------------------------------/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_TIM4_Init(void); static void MX_USART2_UART_Init(void); / USER CODE BEGIN PFP / void usDelay(uint32_t uSec); / USER CODE END PFP /
/ Private user code ---------------------------------------------------------/ / USER CODE BEGIN 0 / //Speed of sound in cm/usec const float speedOfSound = 0.0343/2; float distance;
char uartBuf[100];
/ USER CODE END 0 /
/**
@retval int / int main(void) { / USER CODE BEGIN 1 / uint32_t numTicks = 0; / USER CODE END 1 */
/ MCU Configuration--------------------------------------------------------/
/ Reset of all peripherals, Initializes the Flash interface and the Systick. / HAL_Init();
/ USER CODE BEGIN Init /
/ USER CODE END Init /
/ Configure the system clock / SystemClock_Config();
/ USER CODE BEGIN SysInit /
/ USER CODE END SysInit /
/ Initialize all configured peripherals / MX_GPIO_Init(); MX_TIM4_Init(); MX_USART2_UART_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 / //Set TRIG to LOW for few uSec HAL_GPIO_WritePin(TRIG_GPIO_Port, TRIG_Pin, GPIO_PIN_RESET); usDelay(3);
//*** START Ultrasonic measure routine ***//
//1. Output 10 usec TRIG
HAL_GPIO_WritePin(TRIG_GPIO_Port, TRIG_Pin, GPIO_PIN_SET);
usDelay(10);
HAL_GPIO_WritePin(TRIG_GPIO_Port, TRIG_Pin, GPIO_PIN_RESET);
//2. Wait for ECHO pin rising edge
while(HAL_GPIO_ReadPin(ECHO_GPIO_Port, ECHO_Pin) == GPIO_PIN_RESET);
//3. Start measuring ECHO pulse width in usec
numTicks = 0;
while(HAL_GPIO_ReadPin(ECHO_GPIO_Port, ECHO_Pin) == GPIO_PIN_SET)
{
numTicks++;
usDelay(2); //2.8usec
};
//4. Estimate distance in cm
distance = (numTicks + 0.0f)*2.8*speedOfSound;
//5. Print to UART terminal for debugging
sprintf(uartBuf, "Distance (cm) = %.1f\r\n", distance);
HAL_UART_Transmit(&huart2, (uint8_t *)uartBuf, strlen(uartBuf), 100);
HAL_Delay(1000);
} / USER CODE END 3 / }
/**
@retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/* Initializes the CPU, AHB and APB busses clocks / RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI; RCC_OscInitStruct.MSIState = RCC_MSI_ON; RCC_OscInitStruct.MSICalibrationValue = 0; RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI; RCC_OscInitStruct.PLL.PLLM = 1; RCC_OscInitStruct.PLL.PLLN = 71; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV6; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /* Initializes the CPU, AHB and APB busses clocks / 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;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2; PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } /* Configure the main internal regulator output voltage / if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK) { Error_Handler(); } }
/**
@retval None */ static void MX_TIM4_Init(void) {
/ USER CODE BEGIN TIM4_Init 0 /
/ USER CODE END TIM4_Init 0 /
TIM_ClockConfigTypeDef sClockSourceConfig = {0}; TIM_MasterConfigTypeDef sMasterConfig = {0};
/ USER CODE BEGIN TIM4_Init 1 /
/ USER CODE END TIM4_Init 1 / htim4.Instance = TIM4; htim4.Init.Prescaler = 47.33333-1; htim4.Init.CounterMode = TIM_COUNTERMODE_UP; htim4.Init.Period = 0; htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim4) != HAL_OK) { Error_Handler(); } sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK) { Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK) { Error_Handler(); } / USER CODE BEGIN TIM4_Init 2 /
/ USER CODE END TIM4_Init 2 /
}
/**
@retval None */ static void MX_USART2_UART_Init(void) {
/ USER CODE BEGIN USART2_Init 0 /
/ USER CODE END USART2_Init 0 /
/ USER CODE BEGIN USART2_Init 1 /
/ USER CODE END USART2_Init 1 / huart2.Instance = USART2; huart2.Init.BaudRate = 115200; huart2.Init.WordLength = UART_WORDLENGTH_8B; huart2.Init.StopBits = UART_STOPBITS_1; huart2.Init.Parity = UART_PARITY_NONE; huart2.Init.Mode = UART_MODE_TX_RX; huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart2.Init.OverSampling = UART_OVERSAMPLING_16; huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; if (HAL_UART_Init(&huart2) != HAL_OK) { Error_Handler(); } / USER CODE BEGIN USART2_Init 2 /
/ USER CODE END USART2_Init 2 /
}
/**
@retval None */ static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0};
/ GPIO Ports Clock Enable / HAL_RCC_GPIOC_CLK_ENABLE(); HAL_RCC_GPIOA_CLK_ENABLE();
/Configure GPIO pin Output Level / HAL_GPIO_WritePin(GPIOA, LED_Pin|TRIG_Pin, GPIO_PIN_RESET);
/Configure GPIO pin : Button_Pin / GPIO_InitStruct.Pin = Button_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(Button_GPIO_Port, &GPIO_InitStruct);
/Configure GPIO pins : LED_Pin TRIG_Pin / GPIO_InitStruct.Pin = LED_Pin|TRIG_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/Configure GPIO pin : ECHO_Pin / GPIO_InitStruct.Pin = ECHO_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(ECHO_GPIO_Port, &GPIO_InitStruct);
}
/ USER CODE BEGIN 4 / void usDelay(uint32_t uSec) { if(uSec < 2) uSec = 2; usTIM->ARR = uSec - 1; /sets the value in the auto-reload register/ usTIM->EGR = 1; /Re-initialises the timer/ usTIM->SR &= ~1; //Resets the flag usTIM->CR1 |= 1; //Enables the counter while((usTIM->SR&0x0001) != 1); usTIM->SR &= ~(0x0001); } / USER CODE END 4 /
/**
@retval None / void Error_Handler(void) { / USER CODE BEGIN Error_Handler_Debug / / User can add his own implementation to report the HAL error return state */
/ USER CODE END Error_Handler_Debug / }
/**
/**** (C) COPYRIGHT STMicroelectronics *END OF FILE/ `
Geprobeerd om gewoon een hallo world te sturen via de uart , maar het is niet gelukt. (Geen errors bij build of debug of run)
In plaats van Psoc4 pioneer gaan we nu de microcontroller STM32 NUCLEO-L4A6ZG gebruiken om eerst de ultrasoon sensor te programmeren en daarna de RGB LEDs aansturen.