Page 1 of 1

[STM] Tutorial: Getting started with STM32F3 Discovery board

PostPosted: 11-Nov-2014, 19:55
by Administrator
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_AHBPeriphClockCmdRCC_AHBPeriph_GPIOEENABLE );

    
// Init PortE (LEDS)
    
GPIO_StructInit( &gpio );
    
gpio.GPIO_Mode GPIO_Mode_OUT;
    
gpio.GPIO_Pin  GPIO_Pin_8 GPIO_Pin_9;
    
GPIO_InitGPIOE, &gpio );

    
// Blinking LEDS
    
while(1)
    {
        
// On
        
GPIO_SetBitsGPIOEGPIO_Pin_8 );
        
Delay(500000);
        
GPIO_SetBitsGPIOEGPIO_Pin_9 );
        
Delay(500000);
        
// All off
        
GPIO_ResetBitsGPIOEGPIO_Pin_8 |GPIO_Pin_9 );
        
Delay(1000000);
    }
}

void Delay(unsigned long i)
{
    while( 
i-- > ) ;
}
 

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.

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

PostPosted: 11-Nov-2014, 21:17
by Administrator
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_AHBPeriphClockCmdRCC_AHBPeriph_GPIOAENABLE );
    
RCC_AHBPeriphClockCmdRCC_AHBPeriph_GPIOEENABLE );

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

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

    
//---------------------------------------------------//
    
while(1)
    {
        
// Polling pushbutton state
        
if( GPIO_ReadInputDataBit(GPIOAGPIO_Pin_0) )
        {
            
GPIO_SetBitsGPIOELeds );
        }
        else
        {
            
GPIO_ResetBitsGPIOELeds );
        }
    }
}
 

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

PostPosted: 11-Nov-2014, 22:43
by Administrator
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( ++== 1000 )
    {
        
uint8_t bit GPIO_ReadOutputDataBit(GPIOEGPIO_Pin_8);
        
GPIO_WriteBitGPIOEGPIO_Pin_8bit Bit_RESET Bit_SET );
        
0;
    }
}

int main()
{
    
GPIO_InitTypeDef gpio;
    
RCC_AHBPeriphClockCmdRCC_AHBPeriph_GPIOEENABLE );

    
GPIO_StructInit( &gpio );
    
gpio.GPIO_Mode GPIO_Mode_OUT;
    
gpio.GPIO_Pin  GPIO_Pin_8;
    
GPIO_InitGPIOE, &gpio );

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