Thí nghiệm nhấp nháy LED trên STM32 sử dụng GPIO

1. Chuẩn bị phần cứng:

  • 1 bo mạch STM32F103C8T6 tối thiểu
  • 1 bộ ghi ST-LINK V2
  • Tuỳ chọn dây nối DuPont

LED tích hợp - chân GPIOC13

2. Hàm delay (Delay.h)

Xem mã nguồn

#ifndef _DELAY_H_
#define _DELAY_H_

#include "stm32f10x.h"                  // Device header

void delayMicroseconds(uint32_t us);
void delayMilliseconds(uint32_t ms);
void delaySeconds(uint32_t s);

#endif

3. Triển khai hàm delay (Delay.c)

Xem mã nguồn

#include "Delay.h"

/**
  * @brief  Delay microsecond
  * @param  microseconds: 0~233015
  * @retval None
  */
void delayMicroseconds(uint32_t microseconds)
{
    SysTick->LOAD = 72 * microseconds;                // Thiết lập giá trị reload
    SysTick->VAL = 0x00;                     // Xóa giá trị hiện tại
    SysTick->CTRL = 0x00000005;              // Khởi động timer với HCLK
    while(!(SysTick->CTRL & 0x00010000));    // Chờ đến khi đếm xong
    SysTick->CTRL = 0x00000004;              // Tắt timer
}
 
/**
  * @brief  Delay millisecond
  * @param  milliseconds: 0~4294967295
  * @retval None
  */
void delayMilliseconds(uint32_t milliseconds)
{
    while(milliseconds--)
    {
        delayMicroseconds(1000);
    }
}
  
/**
  * @brief  Delay second
  * @param  seconds: 0~4294967295
  * @retval None
  */
void delaySeconds(uint32_t seconds)
{
    while(seconds--)
    {
        delayMilliseconds(1000);
    }
} 

4. Khai báo LED (LED.h)

Xem mã nguồn

#ifndef __LED_H
#define __LED_H

#include "stm32f10x.h"  // Device header

// Cấu hình LED trên GPIOC13
#define LED_GPIO_PIN    GPIO_Pin_13
#define LED_GPIO_PORT   GPIOC
#define LED_GPIO_CLK    RCC_APB2Periph_GPIOC

void LED_Init(void);  // Khai báo hàm
void LED_Toggle(void);  // Hàm đảo trạng thái LED
void LED_Blink(uint32_t count);  // Hàm nhấp nháy LED

#endif

5. Triển khai LED (LED.c)

Xem mã nguồn

#include "LED.h"
#include "Delay.h"
#include "stm32f10x.h"                  // Device header

void LED_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); // Kích hoạt đồng hồ GPIOC
	
	GPIO_InitTypeDef GPIO_InitStruct; // Khai báo cấu trúc
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13; // Chọn chân GPIO
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; // Chế độ output push-pull
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // Tốc độ 50MHz
	GPIO_Init(GPIOC,&GPIO_InitStruct); // Khởi tạo GPIO
	GPIO_WriteBit(GPIOC, GPIO_Pin_13, Bit_RESET);
}

// Hàm đảo trạng thái LED
void LED_Toggle(void)
{
    // Đọc trạng thái hiện tại
    if(GPIO_ReadOutputDataBit(LED_GPIO_PORT, LED_GPIO_PIN) == Bit_SET)
    {
        // Nếu là cao → chuyển sang thấp
        GPIO_ResetBits(LED_GPIO_PORT, LED_GPIO_PIN);
    }
    else
    {
        // Nếu là thấp → chuyển sang cao
        GPIO_SetBits(LED_GPIO_PORT, LED_GPIO_PIN);
    }
}
 
/**
  * @brief  Nhấp nháy LED theo số lần
  * @param  count: Số lần nhấp nháy (1 lần = sáng 200ms + tắt 200ms)
  * @retval None
  */
void LED_Blink(uint32_t count)
{
    for(uint32_t i = 0; i < count; i++)
    {
        GPIO_ResetBits(LED_GPIO_PORT, LED_GPIO_PIN);  // Bật LED
        delayMilliseconds(200);                      // Giữ sáng 200ms
        GPIO_SetBits(LED_GPIO_PORT, LED_GPIO_PIN);    // Tắt LED
        delayMilliseconds(200);                      // Giữ tắt 200ms
    }
}

6. Hàm chính (main.c)

Xem mã nguồn

#include "stm32f10x.h"
#include "LED.h"
#include "Delay.h"

int main(void)
{
	LED_Init();
	while(1)
	{
//		// Bật LED
//		GPIO_WriteBit(GPIOC, GPIO_Pin_13,Bit_RESET);  // Ghi 0
//		
//		// Tắt LED
//		GPIO_WriteBit(GPIOC, GPIO_Pin_13, Bit_SET);  // Ghi 1
//		
//		// Nhấp nháy 500ms
//		GPIO_WriteBit(GPIOC, GPIO_Pin_13,Bit_RESET);  // Ghi 0
//		delayMilliseconds(500);
//		GPIO_WriteBit(GPIOC, GPIO_Pin_13, Bit_SET);  // Ghi 1
//		delayMilliseconds(500);
//		
//		// Đảo trạng thái
//		LED_Toggle();
//		delayMilliseconds(500);
//		
//		// Nhấp nháy 3 lần (200ms sáng/tắt)
//		LED_Blink(3);
//		delaySeconds(2);
	}
}

Thẻ: STM32 GPIO LED SysTick delay

Đăng vào ngày 19 tháng 7 lúc 10:56