This tutorial may be seen as a HelloWorld project.
Indeed we are going to switch on the LED6 of the STM32F3-Discovery board with the push button.
The code has been reduced to its minimum in order to get the most important elements in this example.
You can find the whole project on my GitHub:
The project has been made with Atollic TrueStudio, but the code in the main.c file is the same, if you use another IDE such as Keil uVision, IAR EW and so on.
The function manager is: SystemClock_Config().
The oscillator and the clock have been set with the HSI (High Speed Internal) oscillator.
All the clock, such as SYSCLK, PCLK1 and PCLK2 have been set to have a classic using for this tutorial.
So don't try to understand really what is going on with these oscillator and clocks, just admit it works well like that for this tutorial.
To have a GPIO pin working, it's necessary to activate the clock wtih the specific pin.
For example, we want the LED6 is switched on when the pusb button is pressed.
The LED6 is on GPIO with port E and the number of the pin is 15.
This GPIO will be with mode output.
For the push button, it's quite the same except that the port is A and the pin is 0.
We want this GPIO as input and pull down.
In the while loop, we check if the GPIO A0 is set.
If yes it's mean that the push button has been pressed and then we set the GPIO E6 to switch on the LED.
When we stop pressing the push button, the A0 register is reset, so we switch off the LED 6.
/** * The famous main */ int main(void) { HAL_Init(); // resets periph. and inits the FLash and Systick SystemClock_Config(); // inits oscillators and clocks MX_GPIO_Init(); // inits GPIO while (1337) { // infinite loop if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0)) { // checks if PA0 is set HAL_GPIO_WritePin(GPIOE, GPIO_PIN_15, GPIO_PIN_SET); // switch on LED6 } else { HAL_GPIO_WritePin(GPIOE, GPIO_PIN_15, GPIO_PIN_RESET); // switch off LED6 } } } /** * System Clock Configuration */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = 16; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; 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_2); HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000); HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); /* SysTick_IRQn interrupt configuration */ HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); } /** * Pins configuration */ void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOE_CLK_ENABLE(); /*Configure GPIO pin : PushButton_Pin */ GPIO_InitStruct.Pin = PushButton_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLDOWN; HAL_GPIO_Init(PushButton_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pin : LED_6_Pin */ GPIO_InitStruct.Pin = LED_6_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(LED_6_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(LED_6_GPIO_Port, LED_6_Pin, GPIO_PIN_RESET); }
A good example in order to understand basics of STM32F3 libraries.
Good job, you got it!
Comments
Dipankar panda (not verified)
Tuesday, May 3, 2016 - 12:03pm
Permalink
Just to turn on an LED while
Just to turn on an LED while a button is pressed, this is the coding size and complexity.
This job can be easily done using Arduino ATMEGA 328P within a few line and simple coading.
Add new comment