Search
This exercise deals with NUCLEO I/O initialization of the User LED and User Button. The exercise also deals with usage of these resources. The definition of the LED and Button connection is described in the board user manual.
Task:
The NUCLEO board contains a green LED controlled by the CPU. The LED is connected to the CPU pin XX of the port GPIOXX. The output needs to be initialized through the GPIO_Init function as depicted below. The initialization should be placed into void GPIO_Configuration( void ) function of the project.
// PXX = USER LED GPIO_InitStructure.GPIO_Pin = GPIO_Pin_XX; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init( GPIOXX, &GPIO_InitStructure );
The user button is hardwired to a different port then LED and USART. This means the respective GPIO needs to be initialized and connected to the clock source. This operation should be done in void RCC_Configuration( void ) function: see below.
// GPIOC clock enable RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE );
The initialization of the input pin is performed in the same way as the output pin with small difference: GPIO_Mode_IN.
// PXX = USER Button GPIO_InitStructure.GPIO_Pin = GPIO_Pin_XX; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init( GPIOXX, &GPIO_InitStructure );
The setting of the output can be done through GPIO_SetBits and GPIO_ResetBits (or GPIO_ToggleBits). The input can be read through GPIO_ReadInputDataBit. The list of functions available for input output manipulation is described in stm32f4xx_gpio.h.
int main(void) { // Init RCC_Configuration(); GPIO_Configuration(); USART2_Configuration(); // Process OutString("Welcome to Nucleo F401RE\r\n"); while(1) // Don't want to exit { uint16_t Data; uint8_t btData; while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET); // Wait for Char Data = USART_ReceiveData(USART2); // Collect Char OutString("Welcome to Nucleo F401RE\r\n"); USART_SendData(USART2, Data); // Echo Char OutString("\n"); GPIO_ToggleBits( GPIOA, GPIO_Pin_5 ); btData = GPIO_ReadInputDataBit( GPIOC, GPIO_Pin_13 ); if( !btData ) { OutString("Butt press\r\n"); } } // END while(1) } // END main