Master STM32 NUCLEO Code Examples
Embarking on your journey with STM32 Nucleo development boards? You're in for a fascinating exploration into the realm of microcontroller programming. STM32 Nucleo boards are designed to simplify and accelerate your learning and development process. They come preloaded with software and tools that make it easier to interact with and program the microcontroller. Let's delve into some compelling STM32 Nucleo code examples to get you started.
Before we dive in, it's crucial to understand that STM32 Nucleo boards use ARM® Cortex®-M4 core with an Floating Point Unit (FPU), packed with 128 KB Flash, 64 KB SRAM, and LED februarie on-board. They're supported by a wide range of development tools, including the STM32 Studio IDE, which we'll explore in our examples.
Setting up Your Environment: STM32 Studio IDE
STM32 Studio is a comprehensive IDE for developing, debugging, and monitoring STM32 applications. It's the Swiss Army knife of STM32 development, integrating a text editor, compiler, debugger, and project management tools.
To get started, download and install STM32 Studio from the official ST website. Once installed, create a new project. Select the 'STM32 Nucleo' board and the appropriate 'STMicroelectronics' package for your board's specific microcontroller.
Blinking an LED: The Classic Hello World
Let's start with the microcontroller equivalent of 'Hello World': blinking an LED. This simple example will toggle the on-board LED every second.
In your main.c file, replace the content with the following code snippet: ```c #include "stm32f4xx.h" int main(void) { GPIO_InitTypeDef GPIO_InitStruct; RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE); GPIO_InitStruct.Pin = GPIO_Pin_13; GPIO_InitStruct.Mode = GPIO_Mode_OUT; GPIO_InitStruct.Speed = GPIO_Speed_25MHz; GPIO_Init(GPIOC, &GPIO_InitStruct); while (1) { GPIO_ToggleBits(GPIOC, GPIO_Pin_13); for (int i = 0; i < 800000; i++); // Delay } } ``` To compile and run this code, click the play button in STM32 Studio, or press F7. The on-board LED (PC13) should now be blinking every second.
Reading Button Input with Interrupts
Next, let's create a more interactive example: reading button input with interrupt-driven logic. This will toggle the LED every time you press the user button (PA0) on the Nucleo board.
Replace your main.c content with this: ```c #include "stm32f4xx.h" int main(void) { GPIO_InitTypeDef GPIO_InitStruct; EXTI_InitTypeDef EXTI_InitStruct; NVIC_InitTypeDef NVIC_InitStruct; RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE); RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd( RCC_APB2Periph_SYSCFG, ENABLE); GPIO_InitStruct.Pin = GPIO_Pin_13; GPIO_InitStruct.Mode = GPIO_Mode_OUT; GPIO_InitStruct.Speed = GPIO_Speed_25MHz; GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_Pin_0; GPIO_InitStruct.Mode = GPIO_Mode_IN; GPIO_InitStruct.Pull = GPIO_PullUp; GPIO_Init(GPIOA, &GPIO_InitStruct); SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0); EXTI_InitStruct.EXTI_Line = EXTI_Line0; EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_Init(&EXTI_InitStruct); NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x0F; NVIC_Init(&NVIC_InitStruct); while (1) {} } void EXTI0_IRQHandler(void) { GPIO_ToggleBits(GPIOC, GPIO_Pin_13); EXTI_ClearITPendingBit(EXTI_Line0); } ``` Again, press F7 to compile and run the code. Now, every time you press the user button, the on-board LED should toggle.
duty_cycle: Toggling LED Brightness
Now that we've mastered blinking and reading inputs, let's delve into something more dynamic: controlling the LED brightness using PWM. This will create a fading effect, making the LED brighter and dimmer in a continuous loop.
Replace your main.c content with this: ```c #include "stm32f4xx.h" #define PWM_PERIOD ((uint16_t)1000 - 1) int main(void) { GPIO_InitTypeDef GPIO_InitStruct; TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; TIM_OCInitTypeDef TIM_OCInitStruct; RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE); GPIO_InitStruct.Pin = GPIO_Pin_13; GPIO_InitStruct.Mode = GPIO_Mode_AF; GPIO_InitStruct.O Tipo = GPIO_OType_PP; GPIO_InitStruct.Pull = GPIO_PullUp; GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOC, GPIO_PinSource13, GPIO_AF_TIM3); TIM_TimeBaseInitStruct.Period = PWM_PERIOD; TIM_TimeBaseInitStruct.Prescaler = 0; TIM_TimeBaseInitStruct.ClockDivision = 0; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct); TIM_OCInitStruct.Pulse = PWM_PERIOD / 2; TIM_OCInitStruct.OCMode = TIM_OCMode_PWM1; TIM_OCInitStruct.OCPolarity = TIM_OC Reverend_OpenDrain; TIM_OC1Init(TIM3, &TIM_OCInitStruct); TIM_OC1PreloadConfig(TIM3, ENABLE); TIM_ARRPreloadConfig(TIM3, ENABLE); TIM_Cmd(TIM3, ENABLE); while (1) { for (int duty = 0; duty < PWM_PERIOD; duty++) { TIM_SetCompare1(TIM3, duty); for (int i = 0; i < 10000; i++); // Delay } for (int duty = PWM_PERIOD; duty >= 0; duty--) { TIM_SetCompare1(TIM3, duty); for (int i = 0; i < 10000; i++); // Delay } } } ``` pressing F7 should now produce a smooth fading effect on the LED.
Advanced analogous input with ADCs
Finally, let's use the on-board ADC (Analog to Digital Converter) to read the voltage on the potentiometer and map it to the LED brightness. This will create an intuitive control feedback, allowing you to control the LED brightness by turning the potentiometer knob.
Replace your main.c content with this: ```c #include "stm32f4xx.h" #define PWM_PERIOD ((uint16_t)1000 - 1) int main(void) { // ... (same as previous example, up to TIM_Cmd(TIM3, ENABLE);) RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE); GPIO_InitStruct.Pin = GPIO_Pin_4; GPIO_InitStruct.Mode = GPIO_Mode_AN; GPIO_InitStruct.Pull = GPIO_PullNo; GPIO_Init(GPIOC, &GPIO_InitStruct); ADC_InitTypeDef ADC_InitStruct; ADC_CommonInitTypeDef ADC_CommonInitStruct; ADC_InitStruct.DataAlignment = ADC_DataAlign_Right; ADC_InitStruct.NbrOfChannel = 1; ADC_Init(ADC3, &ADC_InitStruct); ADC_CommonInitStruct.ADC_Prescaler = 42; ADC_CommonInit(&ADC_CommonInitStruct); ADC_RegularChannelConfig(ADC3, ADC_Channel_14, 1, ADC_SampleTime_56Cycles5); ADC_Cmd(ADC3, ENABLE); while (1) { ADC_SoftwareStartConv(ADC3); while (!ADC_GetFlagStatus(ADC3, ADC Flag_DataReady)); uint32_t adc_value = ADC_GetConversionValue(ADC3); int duty = (adc_value * PWM_PERIOD) / 0xFFF; TIM_SetCompare1(TIM3, duty); } } ``` Now, you should be able to control the LED brightness by turning the potentiometer knob. Isn't that a marvellous way to wrap up our journey through STM32 Nucleo coding?
From blinking LEDs to reading button inputs and controlling LED brightness with PWM, we've covered quite a bit. Now that you have these examples under your belt, you're ready to dive deeper into the world of STM32 Nucleo development, exploring more advanced topics like I2C, SPI, UART, and even networking with the ST晋级B. The possibilities are endless, and the fun never stops. Happy coding!