Keil Logo

General Purpose I/O

Microcontrollers provide a number of I/O ports that can be used in target applications. Pins can be configured as a GPIO. However, some ports or single port pins can have functionalities that could prevent their generic usage.

The steps to configure GPIO ports and pins are, basically, identical on every microcontroller:

  • Enable the clock signal for the GPIO.
  • Configure the Alternate Function to use a GPIO (usually standard after reset).
  • Configure the GPIO pins as input or output.
  • Set remaining parameters like speed, pull-up/down.
  • Enable the GPIO.
  • Read from or write to the GPIO.

For this example the following steps are required:

  1. Create a new project.
  2. Select the STM32F107VC microcontroller from the Device Database.
  3. Include the files:
    • startup_stm32f10x_cl.s
    • system_stm32f10x_cl.c
    • core_cm3.c
    • Serial.c
    • Retarget.c
  4. Create and add a new file, GPIO.c, to the project.
  5. Use the code below as a reference.
    /*-----------------------------------------------------------------------------
     * Name:    GPIO.c
     * Purpose: Blink a led on GPIOE
     * Notes:
    *----------------------------------------------------------------------------*/
    #include <stm32f10x_cl.h>
    
    volatile uint32_t msTicks;                      // Counts 1ms timeTicks
    /*-----------------------------------------------------------------------------
      SysTick_Handler
    *----------------------------------------------------------------------------*/
    void SysTick_Handler (void)  {
      msTicks++;                                    // increment Delay()-counter
    }
    
    /*-------------------------------------------------------------------------------
      delays number of tick Systicks (happens every 1 ms)
    *------------------------------------------------------------------------------*/
    __INLINE static void Delay (uint32_t dlyTicks)  {
      uint32_t curTicks;
    
      curTicks = msTicks;
      while ((msTicks - curTicks) < dlyTicks);
    }
    
    /*-------------------------------------------------------------------------------
      configer LED pins
    *------------------------------------------------------------------------------*/
    __INLINE static void LED_Config (void)  {
    
      RCC->APB2ENR |=  1 <<  6;                     // Enable GPIOE clock
      GPIOE->CRH    = 0x33333333;                   // Configure the GPIO for LEDs
    }
    
    /*-------------------------------------------------------------------------------
      Switch on LEDs
    *------------------------------------------------------------------------------*/
    __INLINE static void LED_On (uint32_t led) {
    
      GPIOE->BSRR = (led);                          // Turn On  LED
    }
    
    
    /*-------------------------------------------------------------------------------
      Switch off LEDs
    *------------------------------------------------------------------------------*/
    __INLINE static void LED_Off (uint32_t led)  {
    
      GPIOE->BRR  = (led);                          // Turn Off LED
    }
    
    /*-----------------------------------------------------------------------------
      MAIN function
    *----------------------------------------------------------------------------*/
    int main (void)  {
    
      SystemInit ();                                // Initialize clocks
      SysTick_Config (SystemFrequency/1000);
      LED_Config ();
    
      while(1)  {
        LED_On (0x100);                             // Turn on the LED.
        Delay (100);                                // Delay  100 Msec
        LED_Off (0x100);                            // Turn off the LED.
        Delay (100);                                // Delay  100 Msec
    
      }
    
    }
    
  6. Build the project.
  7. Connect a debug adapter to the PC and to the board.
  8. Configure µVision for Flash downloading.
  9. Download to Flash and debug the application.
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.