Skip to main content

STM32F4 Discovery : GPIO

The most basic thing one would do with an MCU would be how to control the IO pins right? So how do we program the GPIO pins on STM32F4Discovery board?
Let's take a look at it now,

First and foremost we need to add stm32f4xx_gpio.h and stm32f4xx_rcc.h header files to be included in the project.

Once that is done we must enable clock to the port (whichever port you would like to use)

Each peripheral bus is detailed in the datasheet and we can use this information to enable system clock programmatically based on our application.

// enables HSE oscillator
RCC_HSEConfig(RCC_HSE_ON);
// wait till clock stabilizes
while(!RCC_WaitForHSEStartUp()) {
}
//enables clock to portD
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
Then we have to set parameters for configuring GPIO pins

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&GPIO_InitStructure);
The various options are self explanatory.You can adjust the settings based on your application.

Now lets try to toggle pin14 from PORTD which is an onboard LED on our discovery board.

// Toggle pin
while(1) {
GPIO_SetBits(GPIOD,GPIO_Pin_14);
delay(0x0000FFFF);
GPIO_ResetBits(GPIOD,GPIO_Pin_14);
delay(0x0000FFFF);
}
return(0);
}
// delay function
void delay(__IO uint32_t count) {
while(count>0) {
count--;
}
}
We've done it! Now you can compile it and see the onboard LED blinking ON and OFF!

So the final code looks something like
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
void delay(__IO uint32_t);
int main() {
RCC_HSEConfig(RCC_HSE_ON);
while(!RCC_WaitForHSEStartUp()) {
}
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&GPIO_InitStructure);
while(1) {
GPIO_SetBits(GPIOD,GPIO_Pin_14);
delay(0x0000FFFF);
GPIO_ResetBits(GPIOD,GPIO_Pin_14);
delay(0x0000FFFF);
}
return(0);
}
void delay(__IO uint32_t count) {
while(count>0) {
count--;
}
}

Comments

Popular posts from this blog

STM32F4 Discovery: Clock frequency

So you have got an STM32F4 discovery board in your hands. I can surely assume one of the reason you got this nifty little development board is it's computing capability at 168MHz. However if you are not cautious, you may not achieve the maximum clock speed the device could achieve.If u are not careful (sometimes it happens) the controller would run at lower clock.Obviously that is not a bad thing in power critical applications.Here are some steps to ensure the system is running at its peak performance STEP 1: Download clock configuration tool from ST, file named AN3988 http://www.st.com/internet/mcu/product/252149.jsp STEP 2: Open the clock configuration tool using excel and change HCLK to 168 MHz.You can select on other options and PLL frequencies also (Know what you are doing else leave it as it is).Click generate.If values are OK then no errors would be shown.Then click on generate.Then close the excel file.You should have got a system_stm32f4xx.c file in the folder.U...

Arduino as USB to Serial converter

Electronics prototyping was taken to new heights by the introduction of arduino. The small board could do many wonderful things,but the beauty that makes it stand apart from others is it`s versatility and easiness. Most of us while doing our projects would have searched for a communication method with PC, and ended up using USART by level converters (MAX232) or perhaps a USB-Serial cable.Serial ports are disappearing from systems these days. Obviously USB is the only choice..So what to do? First thing you would say is to use a USB to serial converter cable that is cheap to buy. Unfortunately as many of you would have observed these are not  reliable many a times. next option is to use a FTDI breakout boards like this https://www.sparkfun.com/products/9115 . So if you can`t reach out for an FTDI board or any of those serial converter cables, you`l think you are doomed just like i did at that time.So no way out? Nope there is a way! Do you have an arduino sitting ar...