Electronics - STM32 - Initializing and enabling all pins at the same time on the STM32F103ZE-SK board

You probably won't use directly this code, but it's always interesting to know how to initialize all pins and activate them.
So let's see how to do this, in this STM32F103ZE-SK tutorial.

We are of course free to plug or weld LEDs on some ports in order to see then blink.

Explanation

In the example below, an array of GPIOs is created in order to handle them easily in the code.

Then I made a badprogDelay() function to use the system's clock.

I initialize all pins with badprogInitAllGPIO().
In this function is important to enable the clock of each peripheral.

After, our structGPIO has to be initialized (meaning set all elements structure by a default value).
But as we need a specific value for the GPIO_Mode, we set it manually to GPIO_Mode_Out_PP.

Finally we initialize all GPIOs with our structure.

In the lightningAll() function we disable all pins during 4000 microseconds before enabling them for the same time.
This, in an infinite loop, of course.

Code

#include "stm32f10x.h"
#include "stxng.h"

// define min and max for loops
#define LOOP_GPIOX_MIN 0
#define LOOP_GPIOX_MAX 7

// array of GPIO, from A to G
GPIO_TypeDef *arrayGPIOx[7] =
        { GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG };

/**
 We use the RCC_Clock to retrieve the system frequency.
 Then we divide this frequency by 2^20.
 Value for the bitwise shift of a 72MHz:

 Example:
 72 000 000 / 20 = 68.
 72 000 000 / 3 = 9 000 000.

 Because 20 in this case is equal in reality to 2^20.
 And 2^20 = 1048576.
 So (72000000 / 2^20) = (72000000 / 1048576) = (68.6645507813) = (68).

 And sooooo on.
 ______________
 >> | Value    |
 ___|__________|
 -1 |         0|
 -10|         0|
 0  |72 000 000|
 1  |36 000 000|
 2  |18 000 000|
 3  | 9 000 000|
 4  | 4 500 000|
 5  | 2 250 000|
 ...|       ...|
 19 |       137|
 20 |        68| <----- Here
 21 |        34|
 22 |        17|
 23 |         8|
 24 |         4|
 25 |         2|
 26 |         1|
 27 |         0|
 30 |         0|
____|__________|
*/
void badprogDelay(u32 myTime)
{
      u32 i;
      RCC_ClocksTypeDef RCC_Clocks;

      RCC_GetClocksFreq(&RCC_Clocks);
      i = myTime * (RCC_Clocks.SYSCLK_Frequency >> 20);

      for(; i != 0; i--);
}

/**
 * Initializing all GPIOs
 */
void badprogInitAllGPIO() {
    // declaring a structure
    GPIO_InitTypeDef structGPIO;

    // enabling all GPIO clock
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE);

    // seting GPIO_Speed and GPIO_Pins by default (respectively 2MHz and All)
    GPIO_StructInit(&structGPIO);

    // setting all pins port to OUTPUT PUSH-PULL
    structGPIO.GPIO_Mode = GPIO_Mode_Out_PP;

    // init all GPIOx with our structure
    GPIO_Init(GPIOA, &structGPIO);
    GPIO_Init(GPIOB, &structGPIO);
    GPIO_Init(GPIOC, &structGPIO);
    GPIO_Init(GPIOD, &structGPIO);
    GPIO_Init(GPIOE, &structGPIO);
    GPIO_Init(GPIOF, &structGPIO);
    GPIO_Init(GPIOG, &structGPIO);
}

/**
 * Lightning all LEDs on the board
 */
void lightningAll() {
    int i = 0;

    // resetting all LEDs
    while (i < LOOP_GPIOX_MAX) {
        GPIO_WriteBit(arrayGPIOx[i++], GPIO_Pin_All, Bit_RESET);
        badprogDelay(4000);
    }

    // turning on all LEDs
    while (i > LOOP_GPIOX_MIN) {
        GPIO_WriteBit(arrayGPIOx[--i], GPIO_Pin_All, Bit_SET);
        badprogDelay(4000);
    }
}

/**
 * Main, what else?
 */
int main(void) {
    badprogInitAllGPIO();
    while (1337) { // put a number there, except zero (0)
        lightningAll();
    }
    return 0;
}

Conclusion

A great exercise to understand how GPIOs work on the STM32F103ZE-SK board.
Well done, you've made it! cool

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.