Electronics - STM32 - Initializing and enabling default LEDs, LCD screen and buzzer with STM32F103ZE-SK

In a previously STM32F103ZE-SK's tutorial we created a program to initialize and enable all GPIOs on the board.
This time, we will just initialize and enable default LEDs of the board, the LCD screen and the buzzer.
Making them blinking.

That's why we disable the GPIO D, E and G.
Indeed, there is no peripheral of LED, LCD or buzzer types on those ports.

So the code is the same, except that the badprogInitOnlyBoardDefault() function is replaced with badprogInitAllGPIO().

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|
 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 only GPIOs that we need
 */
void badprogInitOnlyBoardDefault() {

    // declaring a structure
    GPIO_InitTypeDef structGPIO;

    // enabling all GPIO clock for visual ouput on the board
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);

    // disabling the GPIO without any default visual output
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, DISABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, DISABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, DISABLE);

    // A - Init the GPIO with the structure
    structGPIO.GPIO_Pin = GPIO_Pin_12;
    structGPIO.GPIO_Mode = GPIO_Mode_Out_PP;
    structGPIO.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOA, &structGPIO);

    // B - Init the GPIO with the structure
    structGPIO.GPIO_Pin = GPIO_Pin_5;
    structGPIO.GPIO_Mode = GPIO_Mode_Out_PP;
    structGPIO.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOB, &structGPIO);

    // C - Init the GPIO with the structure
    structGPIO.GPIO_Pin = GPIO_Pin_7;
    structGPIO.GPIO_Mode = GPIO_Mode_Out_PP;
    structGPIO.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOC, &structGPIO);

    // F - Init the GPIO with the structure
    structGPIO.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8
            | GPIO_Pin_9;
    structGPIO.GPIO_Mode = GPIO_Mode_Out_PP;
    structGPIO.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOF, &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) {
    badprogInitOnlyBoardDefault();
    while (1337) { // put a number, except a 0 (zero)
        lightningAll();
    }
    return 0;
}

Conclusion

You are now able, with previously code and this one, to handle GPIOs like a professional.
Good job, you've made it. smiley

Add new comment

Plain text

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