[STM] Tutorial: Getting started with STM32F3 Discovery board

Hardware issues, electronic components, schemas, Arduino, STM32, Robots, Sensors
Post Reply
Administrator
Site Admin
Posts: 43
Joined: 26-Feb-2014, 17:54

[STM] Tutorial: Getting started with STM32F3 Discovery board

Post by Administrator » 11-Nov-2014, 19:55

Lesson #1.

This guide briefly describes how to get started programming SMT32F3 board using IAR Embedded Workbench.

First tutorial shows how to create a simple blinking LEDs firmware for this board.

Code: Select all

#include <stm32f30x.h>
#include <stm32f30x_rcc.h>
#include <stm32f30x_gpio.h>

int main()
{
    GPIO_InitTypeDef gpio;

    //Reset and Clock Control (Enable PortE, LEDS 8-15)
    RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOE, ENABLE );

    // Init PortE (LEDS)
    GPIO_StructInit( &gpio );
    gpio.GPIO_Mode = GPIO_Mode_OUT;
    gpio.GPIO_Pin  = GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_Init( GPIOE, &gpio );

    // Blinking LEDS
    while(1)
    {
        // On
        GPIO_SetBits( GPIOE, GPIO_Pin_8 );
        Delay(500000);
        GPIO_SetBits( GPIOE, GPIO_Pin_9 );
        Delay(500000);
        // All off
        GPIO_ResetBits( GPIOE, GPIO_Pin_8 |GPIO_Pin_9 );
        Delay(1000000);
    }
}

void Delay(unsigned long i)
{
    while( i-- > 0 ) ;
}
 
Comments to the code:
1) Include header files from SPL-library.
2) Enable Port_E with RCC_AHBPeriphClockCmd() function call.
3) Configure Port_E pins 8 and 9 (LEDs are connected to this pins). This requires to initialize GPIO_InitTypeDef structure with default values, then we set Mode and Pin fields and finally call GPIO_StructInit().

In the endless 'while(1)' loop pins Pin_8 and Pin_9 are switched on and off thus making LEDs blink.

Administrator
Site Admin
Posts: 43
Joined: 26-Feb-2014, 17:54

Re: [STM] Tutorial: Getting started with STM32F3 Discovery b

Post by Administrator » 11-Nov-2014, 21:17

Lesson #2.

In this tutorial we will devolop firmware for a STM32F303 board. This program polls the state of the user pusshbutton. When the button is pressed all LEDs are switched on. In the other case the LEDs are turned off.

User pushbutton is connected to Pin_0 on Port_A. There is a push-pull resistor 10k, thus initialization is simple. GPIO mode for Port A is set to INPUT, pin number is #0.

Code: Select all

int main()
{
    GPIO_InitTypeDef gpio;

    uint32_t Leds = GPIO_Pin_8  | GPIO_Pin_9  |
                    GPIO_Pin_10 | GPIO_Pin_11 |
                    GPIO_Pin_12 | GPIO_Pin_13 |
                    GPIO_Pin_14 | GPIO_Pin_15;
    RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE );
    RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOE, ENABLE );

    //---------------------------------------------------//
    // PortA - pushbutton
    GPIO_StructInit( &gpio );
    gpio.GPIO_Mode = GPIO_Mode_IN;
    gpio.GPIO_Pin  = GPIO_Pin_0;
    GPIO_Init( GPIOA, &gpio );

    //---------------------------------------------------//
    // Init PortE (LEDS)
    GPIO_StructInit( &gpio );
    gpio.GPIO_Mode = GPIO_Mode_OUT;
    gpio.GPIO_Pin  = Leds;
    GPIO_Init( GPIOE, &gpio );

    //---------------------------------------------------//
    while(1)
    {
        // Polling pushbutton state
        if( GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) )
        {
            GPIO_SetBits( GPIOE, Leds );
        }
        else
        {
            GPIO_ResetBits( GPIOE, Leds );
        }
    }
}
 

Administrator
Site Admin
Posts: 43
Joined: 26-Feb-2014, 17:54

Re: [STM] Tutorial: Getting started with STM32F3 Discovery b

Post by Administrator » 11-Nov-2014, 22:43

Lesson #3.

This lesson shows how to use System Timers to control the LED of an STM32 microcontroller.

System Timer is implemented as an interrupt handler. SysTick_Handler is called every N-ticks on an STM32 Discovery Board.
Cortex-M3 (M4) has a wide variaty of timers that generates different events. SysTick is very simple and very usefull.

Important note: The SysTick counter supports values between 1 and 0x00FFFFFF (1-24M) and the CPU frequency is 72MHz. We can configure CPU clock to 24MHz and set SysTick Reload Value to its maximov value 24M. Thus we will get SysTick interrupts every second.


Code: Select all

static int i = 0;
void SysTick_Handler(void)
{
    // 1ms x 1000 == 1 sec
    if( ++i == 1000 )
    {
        uint8_t bit = GPIO_ReadOutputDataBit(GPIOE, GPIO_Pin_8);
        GPIO_WriteBit( GPIOE, GPIO_Pin_8, bit ? Bit_RESET : Bit_SET );
        i = 0;
    }
}

int main()
{
    GPIO_InitTypeDef gpio;
    RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOE, ENABLE );

    GPIO_StructInit( &gpio );
    gpio.GPIO_Mode = GPIO_Mode_OUT;
    gpio.GPIO_Pin  = GPIO_Pin_8;
    GPIO_Init( GPIOE, &gpio );

    // 1 ms ticks
    SysTick_Config(72000);
    while(1)
    {
    }
}
 

Post Reply