8. LED Blink

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

How do you 

 

#include <stdint.h>
/* ── Base Addresses ────────────────────────────── */
#define RCC_BASE   (0x40021000UL)
#define GPIOA_BASE (0x40010800UL)
/* ── RCC Registers ─────────────────────────────── */
#define RCC_APB2ENR  (*(volatile uint32_t *)(RCC_BASE   + 0x18))
/* ── GPIOA Registers ───────────────────────────── */
#define GPIOA_CRL    (*(volatile uint32_t *)(GPIOA_BASE + 0x00))
#define GPIOA_BSRR   (*(volatile uint32_t *)(GPIOA_BASE + 0x10))
/* ── Bit Definitions ───────────────────────────── */
#define RCC_APB2ENR_IOPAEN  (1U << 2)   /* GPIOA clock enable   */
#define LED_PIN              (5U)          /* PA5                  */
#define LED_SET              (1U << LED_PIN)       /* BSRR set bit         */
#define LED_RESET            (1U << (LED_PIN + 16)) /* BSRR reset bit       */
/* ── Delay Constants (8 MHz HSI) ───────────────── */
/*
 *  SysTick @ 8 MHz: 1 tick = 125 ns
  *  Simple loop delay: ~4 cycles/iteration (empirical)
   *  loops_per_ms = 8,000,000 / (4 * 1000) = 2000
    */
    #define LOOPS_PER_MS   (2000UL)
    #define ON_MS          (400UL)
    #define OFF_MS         (800UL)
    /* ── Helpers ───────────────────────────────────── */
    static void delay_ms(uint32_t ms) {
        volatile uint32_t count = ms * LOOPS_PER_MS;
            while (count--);
            }
            static void gpio_init(void) {
                /* 1. Enable GPIOA peripheral clock */
                    RCC_APB2ENR |= RCC_APB2ENR_IOPAEN;
                        /* 2. Configure PA5: Output 2MHz, Push-Pull
                             *    CRL[23:20] = CNF5[1:0]:MODE5[1:0] = 0b0010
                                  *    CNF=00 (GP Push-Pull), MODE=10 (Output 2MHz)  */
                                      GPIOA_CRL &= ~(0xFU << 20); /* Clear bits 23:20 */
                                          GPIOA_CRL |=  (0x2U << 20); /* MODE=10 (2 MHz)  */
                                          }
                                          /* ── Entry Point ───────────────────────────────── */
                                          int main(void) {
                                              gpio_init();
                                                  while (1) {
                                                          GPIOA_BSRR = LED_SET;        /* PA5 HIGH → LED ON  */
                                                                  delay_ms(ON_MS);              /* 400 ms             */
                                                                          GPIOA_BSRR = LED_RESET;      /* PA5 LOW  → LED OFF */
                                                                                  delay_ms(OFF_MS);             /* 800 ms             */
                                                                                      }
                                                                                      }

 

 

 

Output

Video

Add video of the output (know more)

 

 

Photo of Output

 

 

Was this helpful?
Upvote
Downvote