2025年12月6日 星期六

VS Code LED Blink Project with ESP-IDF

 VS Code LED Blink Project with ESP-IDF

源自於 https://esp32tutorials.com/esp32-gpio-esp-idf-led-blinking-example/

main.c

#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#define LED_PIN 13

void app_main(void)
{
    // 配置 GPIO
    gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
   
    // 使用一個整數來追蹤 LED 的狀態 (1: ON, 0: OFF)
    int led_state = 0;

    while(true)
    {
        // 1. 切換 LED 狀態
        // 使用 XOR 運算符 '!' 來切換 0 和 1 (0 -> 1, 1 -> 0)
        led_state = !led_state;
       
        // 2. 設定 GPIO 輸出電平
        gpio_set_level(LED_PIN, led_state);
       
        // 3. 透過序列埠顯示狀態
        if (led_state == 1) {
            printf("LED=ON\n");
        } else {
            printf("LED=OFF\n");
        }
       
        // 4. 延遲 1000 毫秒 (1 秒)
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}



diagram.json


{
  "version": 1,
  "author": "alex wu",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-breadboard-mini", "id": "bb1", "top": -59, "left": -184.8, "attrs": {} },
    {
      "type": "board-esp32-devkit-c-v4",
      "id": "esp",
      "top": -153.6,
      "left": 33.64,
      "attrs": { "builder": "esp-idf" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r2",
      "top": -24.85,
      "left": -105.6,
      "attrs": { "value": "1000" }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -70.8,
      "left": -130.6,
      "attrs": { "color": "red" }
    }
  ],
  "connections": [
    [ "esp:TX", "$serialMonitor:RX", "", [] ],
    [ "esp:RX", "$serialMonitor:TX", "", [] ],
    [ "r2:1", "bb1:8t.d", "", [ "$bb" ] ],
    [ "r2:2", "bb1:14t.d", "", [ "$bb" ] ],
    [ "led1:A", "bb1:8t.c", "", [ "$bb" ] ],
    [ "led1:C", "bb1:7t.c", "", [ "$bb" ] ],
    [ "esp:13", "bb1:14t.e", "green", [ "h0" ] ],
    [ "esp:GND.1", "bb1:7t.d", "black", [ "h-38.25", "v-96", "h-134.4", "v9.6" ] ]
  ],
  "dependencies": {}
}

沒有留言:

張貼留言

8-QAM Signal 4 Phases 2 Amplitudes + 8PSK

 8-QAM Signal 4 Phases 2 Amplitudes + 8PSK import tkinter as tk from tkinter import messagebox import math import cmath # --- 8-QAM 參數設定 ---...