久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 312|回復(fù): 4
收起左側(cè)

求一個(gè)STM32的8個(gè)流水燈

[復(fù)制鏈接]
ID:1151234 發(fā)表于 2025-5-22 00:42 | 顯示全部樓層 |閱讀模式
通過按鍵能夠?qū)崿F(xiàn)對(duì)燈光的啟動(dòng)、關(guān)閉、效果花樣切換功能 3.設(shè)計(jì)要求至少控制8盞LED燈,能夠完成至少四種花樣的切換加分項(xiàng):能夠?qū)崿F(xiàn)燈光能與蜂鳴器的配合
回復(fù)

使用道具 舉報(bào)

ID:1155444 發(fā)表于 2025-6-27 09:31 | 顯示全部樓層
#include "stm32f10x.h"

// 硬件定義
#define LED_PORT GPIOA
#define LED_PINS (GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | \
                 GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7)

#define KEY_PORT GPIOB
#define KEY_PIN GPIO_Pin_0

#define BEEP_PORT GPIOB
#define BEEP_PIN GPIO_Pin_1

// 系統(tǒng)狀態(tài)
typedef enum {
    SYSTEM_OFF,
    SYSTEM_RUNNING
} SystemState;

// 燈光模式
typedef enum {
    MODE_1_RIGHT_LEFT,
    MODE_2_LEFT_RIGHT,
    MODE_3_INSIDE_OUT,
    MODE_4_BLINK_ALL,
    MODE_COUNT
} LightMode;

// 全局變量
volatile SystemState sysState = SYSTEM_OFF;
volatile LightMode currentMode = MODE_1_RIGHT_LEFT;
uint8_t ledPosition = 0;
uint16_t delayCounter = 0;
uint8_t blinkState = 0;

// 函數(shù)原型
void RCC_Configure(void);
void GPIO_Configure(void);
void TIM_Configure(void);
void SysTick_Handler(void);
void UpdateLEDs(void);
void Beep(uint16_t duration, uint8_t times);
void ProcessKey(void);

int main(void) {
    // 初始化
    RCC_Configure();
    GPIO_Configure();
    TIM_Configure();
   
    // 系統(tǒng)啟動(dòng)提示
    Beep(100, 2);
   
    while(1) {
        ProcessKey();
        __WFI();  // 進(jìn)入低功耗模式等待中斷
    }
}

// 時(shí)鐘配置
void RCC_Configure(void) {
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
                           RCC_APB2Periph_AFIO, ENABLE);
   
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}

// GPIO配置
void GPIO_Configure(void) {
    GPIO_InitTypeDef GPIO_InitStructure;
   
    // LED配置 - PA0~PA7
    GPIO_InitStructure.GPIO_Pin = LED_PINS;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(LED_PORT, &GPIO_InitStructure);
    LED_PORT->ODR = 0x00;  // 初始關(guān)閉所有LED
   
    // 按鍵配置 - PB0 (外部上拉)
    GPIO_InitStructure.GPIO_Pin = KEY_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(KEY_PORT, &GPIO_InitStructure);
   
    // 蜂鳴器配置 - PB1
    GPIO_InitStructure.GPIO_Pin = BEEP_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(BEEP_PORT, &GPIO_InitStructure);
    GPIO_ResetBits(BEEP_PORT, BEEP_PIN);
}

// 定時(shí)器配置 - 10ms中斷一次
void TIM_Configure(void) {
    TIM_TimeBaseInitTypeDef TIM_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
   
    TIM_InitStructure.TIM_Period = 1000 - 1;      // 自動(dòng)重裝載值
    TIM_InitStructure.TIM_Prescaler = 720 - 1;     // 72MHz/720 = 100kHz
    TIM_InitStructure.TIM_ClockDivision = 0;
    TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &TIM_InitStructure);
   
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    TIM_Cmd(TIM2, ENABLE);
   
    // 配置中斷
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

// 蜂鳴器控制
void Beep(uint16_t duration, uint8_t times) {
    for(uint8_t i = 0; i < times; i++) {
        GPIO_SetBits(BEEP_PORT, BEEP_PIN);
        for(volatile uint32_t j = 0; j < duration * 1000; j++);
        GPIO_ResetBits(BEEP_PORT, BEEP_PIN);
        for(volatile uint32_t j = 0; j < 10000; j++); // 短暫間隔
    }
}

// 處理按鍵
void ProcessKey(void) {
    static uint8_t keyLastState = 1;  // 假設(shè)初始高電平
    static uint32_t pressTime = 0;
   
    uint8_t currentState = GPIO_ReadInputDataBit(KEY_PORT, KEY_PIN);
   
    // 檢測(cè)按鍵按下 (下降沿)
    if((keyLastState == 1) && (currentState == 0)) {
        pressTime = 0;
    }
   
    // 檢測(cè)按鍵釋放 (上升沿)
    if((keyLastState == 0) && (currentState == 1)) {
        if(pressTime < 50) {  // 短按 (<0.5s)
            if(sysState == SYSTEM_OFF) {
                sysState = SYSTEM_RUNNING;  // 啟動(dòng)系統(tǒng)
                Beep(100, 1);
            } else {
                // 切換燈光模式
                currentMode = (currentMode + 1) % MODE_COUNT;
                Beep(150, currentMode + 1);
                ledPosition = 0;  // 重置燈光位置
            }
        } else {  // 長(zhǎng)按 (>0.5s)
            sysState = (sysState == SYSTEM_OFF) ? SYSTEM_RUNNING : SYSTEM_OFF;
            Beep(100, (sysState == SYSTEM_ON) ? 1 : 2);
            LED_PORT->ODR = 0x00;  // 關(guān)閉所有LED
        }
    }
   
    keyLastState = currentState;
    pressTime++;
}

// 更新LED顯示
void UpdateLEDs(void) {
    if(sysState != SYSTEM_RUNNING) return;
   
    if(delayCounter++ < 10) return;  // 100ms延遲
    delayCounter = 0;
   
    switch(currentMode) {
        case MODE_1_RIGHT_LEFT:  // 從左向右流水燈
            LED_PORT->ODR = (1 << ledPosition);
            ledPosition = (ledPosition + 1) % 8;
            break;
            
        case MODE_2_LEFT_RIGHT:  // 從右向左流水燈
            LED_PORT->ODR = (0x80 >> ledPosition);
            ledPosition = (ledPosition + 1) % 8;
            break;
            
        case MODE_3_INSIDE_OUT:  // 從中間向兩邊擴(kuò)展
            if(ledPosition < 4) {
                LED_PORT->ODR = (1 << (3 - ledPosition)) | (1 << (4 + ledPosition));
            } else {
                LED_PORT->ODR = 0;
            }
            ledPosition = (ledPosition + 1) % 5;
            break;
            
        case MODE_4_BLINK_ALL:  // 所有LED同步閃爍
            LED_PORT->ODR = blinkState ? 0xFF : 0x00;
            blinkState = !blinkState;
            break;
    }
}

// 定時(shí)器中斷 (10ms)
void TIM2_IRQHandler(void) {
    if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) {
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
        UpdateLEDs();
        ProcessKey();
    }
}
回復(fù)

使用道具 舉報(bào)

ID:1152291 發(fā)表于 2025-6-27 14:47 | 顯示全部樓層
這種的程序網(wǎng)上開源的很多,你需要去網(wǎng)上找找,在這個(gè)網(wǎng)站學(xué)習(xí)STM32完全不夠用的哦,你要去別的網(wǎng)站再去搜尋點(diǎn)STM32的資料哦
回復(fù)

使用道具 舉報(bào)

ID:1155481 發(fā)表于 2025-6-27 17:21 | 顯示全部樓層
Hi! Your project sounds like a fun and practical challenge—controlling 8 LEDs with multiple switching patterns using a button, and even syncing with a buzzer for bonus points. I'd be happy to offer some ideas to help you get started.  ✅ Suggested Approach: &#128257; 1. Use a Microcontroller (like Arduino or STM32): It’s the easiest way to handle multiple LEDs, button input, and a buzzer with flexible logic.  &#128280; 2. Button-Control Logic: Use one button to cycle through 4 different modes.  Each press can increment a mode counter (0–3), and after the 4th, reset back to 0.  c Copy Edit // Pseudocode for button press logic if (buttonPressed) {   mode = (mode + 1) % 4; } &#128161; LED Pattern Ideas (at least 4): All LEDs ON  Chase effect (like running lights, one LED on at a time in sequence)  Blink all LEDs together  Even/Odd pattern (alternate between even-numbered and odd-numbered LEDs)  &#128276; Bonus: Add Buzzer Coordination You can sync a short beep with every button press or  Make the buzzer follow the LED pattern (e.g., beep when LEDs blink)  c Copy Edit // Example: Turn on buzzer with LED digitalWrite(buzzerPin, HIGH); delay(100); digitalWrite(buzzerPin, LOW); &#129504; Additional Tips: Use debounce logic or delay() after button press to avoid false triggers.  Store the mode state in a variable and use a switch or if statements to control LED patterns.  You could even add PWM dimming if you want to level up!  Let me know what platform or microcontroller you’re using—I’d be glad to share example code or schematics tailored to your setup. Good luck with your design!
回復(fù)

使用道具 舉報(bào)

ID:1154703 發(fā)表于 2025-6-30 13:38 | 顯示全部樓層
硬件可以用SC09B觸摸芯片來實(shí)現(xiàn)觸摸按鍵、觸摸滑條滾輪的任意組合控制燈光開關(guān)和功能效果的切換~
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 亚洲欧美精品 | 国产做爰免费观看视频 | 在线观看国产黄色 | 欧美不卡一区二区三区 | 久久精品福利视频 | 国产精品av一区二区 | 国产女人18毛片18精品 | 欧美国产在线观看 | 少妇精品| 日韩免费一区二区 | 三级黄网站 | 国产一区在线播放 | 看黄色大片 | 久久免费看片 | 亚洲网站在线 | 日韩精品综合 | 一级免费片 | 国产盗摄一区二区 | 日韩欧美国产精品 | 国产视频www | 97超碰免费 | a级片免费 | 日韩成人免费视频 | 美女操操操 | 中文字幕av在线 | 亚洲精品aaa | 日韩黄色录像 | 久视频在线 | 亚洲精品乱码久久久久 | 五月天激情视频 | 91久久久久久久久久 | 欧美激情国产精品 | 青青草视频在线观看 | 色综合久久88色综合天天 | 中文字幕免费在线观看 | 免费网站观看www在线观看 | 日日夜夜精品免费 | 日韩a视频 | 欧美激情国产精品 | 亚洲伊人色 | 羞羞的视频在线观看 |