STM32 Programming from Scratch

Welcome to Think Electronic Circuits!
In this tutorial you can learn how  to make a project for STM32 microcontroller using CMSIS and after that  how to write a  code for LED  blink.
Requirement for this project:-

Software:-
  • Keil uvision5
Hardware used:-
  • STM32L476 Nucleo board(any other nucleo baord from ST will also work)
  • USB
Steps to make a new project in keil:-

  • Open Keil in your PC:

  • Go to Project->New uvision Project

  • Give  a Project Name and save it.
  • Select the Microcontroller you are using (on your board)and select ok.
  • Manage Run time environment will be opened.Select two option a shown in below figure.(shown by tick mark).Core (under CMSIS) and Startup(under Device) and select OK.

  • Right click on Source group1 and select "Add new item to  Source group1"







  • Select C file and give name of   file as main and select "ADD" option.



  • The coding part:-

    1)Include the header file of microcontroller in main.c

    In main  write the relevant code:-

    start with declaring the declaring the structure pointer to RCC_TypeDef  and pointer to GPIO_TypeDef

    RCC_TypeDef *clock;

    GPIO_TypeDef *GPIO;

    Assign the address of  GPIOA and RCC to respective pointers.

    clock=RCC;
    GPIO=GPIOA;

    Select the clock source of microcontroller unit.I am using HSE in this code

    clock->CR &=0x00000000;
    clock->CR |=(1<<16);//clock source is on board 8 MHz crytal



    Initialise the other required register also.

    while(!((clock->CR)&(1<<17)));//Wait till HSERDY FLAG IS SET
    clock->CFGR &=0x00000000;
    clock->CFGR|=0xA0;//1MHz clock set in RCC,clock divider is 8
    clock->AHB2ENR&=0x00000000;
    clock->AHB2ENR|=0x01;//clock of port A is set

    Initialize the GPIO registers also.

    GPIO->MODER &=(1<<10);
    GPIO->OTYPER &=0x00;
    GPIO->OSPEEDR|=0x1100;
    GPIO->PUPDR|=0x00000000;
    GPIO->ODR &=0x00000000;

    add code in infinite  while loop also

    while(1){
    GPIO->ODR^=(1<<5);
    for(long unsigned int  i=0;i<1000000;i++)__nop();/*1SEC DELAY  1 MHZ CLOCK TO THE         PORT A 1/1000000=10^-6;10^-6 *10^6=1sec*/

    }

    The complete source code:-



    #include "stm32l4xx.h"

    int main(){
    RCC_TypeDef *clock;//pointer to RXX
    GPIO_TypeDef *GPIO;
    clock=RCC;
    GPIO=GPIOA;
            clock->CR &=0x00000000;
    clock->CR |=(1<<16);//clock source is on board 8 MHz crytal    //
    while(!((clock->CR)&(1<<17)));//Wait till HSERDY FLAG IS SET
    clock->CFGR &=0x00000000;
    clock->CFGR|=0x0A;//1MHz clock set in RCC,clock divider is 8
    clock->AHB2ENR&=0x00000000;
    clock->AHB2ENR|=0x01;//clock of port A is set
    GPIO->MODER &=(1<<10);
    GPIO->OTYPER &=0x00;
    GPIO->OSPEEDR|=0x1100;
    GPIO->PUPDR|=0x00000000;
    GPIO->ODR &=0x00000000;
    while(1){
    GPIO->ODR^=(1<<5);
    for(long unsigned int  i=0;i<1000000;i++)__nop();//1SEC DELAY  /*1 MHZ CLOCK TO //THE PORT A   1/1000000=10^-6;10^-6 *10^6=1sec*/

    }
    //return 0;
    }

    This code will toggle the on board LED of STM32 Nucleo with 1 second delay.

    Note:This code will work for all STM32 microcontrollers with change in registers value.Refer your microcontroller's  reference manual and datasheet.This code have been tested for STM32L476RG nucleo board.
    Amazon link to Stm32 nucleo board:https://amzn.to/3BBuNvi Amazon link to Hantek oscilloscope:https://amzn.to/3ocbo0S



    Link to the video tutorial:
    Video link

    download code

    Comments