Categories
Examples STM32 STM32F103

STM32 LED blinking Different Techniques

#include "stm32f10x.h"

GPIO_InitTypeDef GPIO_InitStructure;

void Delay(__IO uint32_t nCount);

int main(void) {
  SystemInit();
  SystemCoreClockUpdate();

  // Enable peripheral clock for GPIOC
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

  // Configure GPIOC Pin 13 as Output push-pull (on-board LED)
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  while (1) {
    // Toggle the LED on and off
    GPIO_WriteBit(GPIOC, GPIO_Pin_13, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_13)));
    
    Delay(500000); // Delay for a while
  }
}

void Delay(__IO uint32_t nCount) {
  while (nCount--) {
  }
}
Code language: PHP (php)

Delay using Systick Timer

#include "stm32f10x.h"

GPIO_InitTypeDef GPIO_InitStructure;

volatile uint32_t TimingDelay;

void SysTick_Handler(void) {
  if (TimingDelay != 0) {
    TimingDelay--;
  }
}

void Delay_ms(__IO uint32_t nTime);
void Delay_us(__IO uint32_t nTime);

int main(void) {
  SystemInit();
  SystemCoreClockUpdate();

  // Enable peripheral clock for GPIOC
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

  // Configure GPIOC Pin 13 as Output push-pull (on-board LED)
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  // SysTick configuration for 1ms interrupts
  if (SysTick_Config(SystemCoreClock / 1000)) {
    while (1); // Handle error
  }

  while (1) {
    // Toggle the LED on and off every 500ms
    GPIO_WriteBit(GPIOC, GPIO_Pin_13, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_13)));
    Delay_ms(500);
  }
}

void Delay_ms(__IO uint32_t nTime) {
  TimingDelay = nTime;
  while (TimingDelay != 0);
}

void Delay_us(__IO uint32_t nTime) {
  // Assuming a 72 MHz clock, each instruction takes approximately 13.89 ns.
  nTime *= 13; // Adjust for the approximate number of instructions in the loop
  while (nTime--);
}
Code language: PHP (php)

LED blinking with Sleep

#include "stm32f10x_conf.h"
#include <stm32f10x.h>
#include <stm32f10x_gpio.h>

/* A Led blink lab for STM32 Discovry Disco                   *
 * Based on:                                                  *
 *   Discovering the STM32 Microcontroller by Geoffrey Brown. */

void setupLED(GPIO_InitTypeDef *LED_InitStruct);
void sleep(uint32_t nSec);

int main(int argc, char **argv)
{
    int LEDVal = 0;
    GPIO_InitTypeDef LED_InitStruct;

    /* Setup LED */
    setupLED(&LED_InitStruct);

    /* Setup timer interrupt interval to nano second */
    if(SysTick_Config(SystemCoreClock / 1000)) {
        while(1); /* Trap here if failed */
    }

    /* Blinking LED3 and LED4 */
    while(1) {
        GPIO_WriteBit(GPIOC, GPIO_Pin_13 , LEDVal);
        

        sleep(250);
        LEDVal = !LEDVal;
    }

    return 0;
}

void setupLED(GPIO_InitTypeDef *LED_InitStruct)
{
    /* Setup LED GPIO */
    // Enable peripheral clock for GPIOC (used for on-board LED)
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

    GPIO_StructInit(LED_InitStruct);
    LED_InitStruct->GPIO_Pin   = GPIO_Pin_13 | GPIO_Pin_14 ;
    LED_InitStruct->GPIO_Mode  = GPIO_Mode_Out_PP;
    LED_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOC, LED_InitStruct);
}

static __IO uint32_t g_timeToWakeUp;
void sleep(uint32_t nSec)
{
    g_timeToWakeUp = nSec;

    /* Busy waiting */
    while(g_timeToWakeUp != 0);
}

/* ISR for system tick */
void SysTick_Handler(void)
{
    if (g_timeToWakeUp != 0x00) {
        g_timeToWakeUp--;
    }
}

/* Trap here for gdb if asserted */
void assert_failed(uint8_t* file, uint32_t line)
{
    while(1);
}Code language: PHP (php)

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.