簡體   English   中英

STM32F103C8 LED 閃爍

[英]STM32F103C8 LED blink

我嘗試對 STM32F103C8 電路進行編程。 我使用 ST-LINK V2 編程器。 運行使用特殊庫的代碼示例后,我能夠看到內置 LED 亮起,但現在我想在不使用這些庫的情況下對電路板進行編程,我不知道為什么我什么都看不到,LED一直處於關閉狀態。 這是代碼:

#include "stm32f10x.h"                  // Device header

int main(){
    RCC->APB2ENR |= 1<<4;
    GPIOC->CRH &= ~0xFF0FFFF;
    GPIOC->CRH |= 0x00300000;
    GPIOC->ODR |= 1<<13;//turn PC_13 ON
    while(1){
        
    }
}

根據您在 PC13 上有 LED 的信息,我假設您使用的是著名的Blue Pill板。 在該板上,由於 PC13 的特殊限制,LED 連接被反轉 - 該引腳只能吸收電流,而不是源。 因此,為了打開它,您需要將 0 寫入 ODR 的相應位。

通常,我反對使用幻數 但對於 STM32F103,使用十六進制文字進行 GPIO 配置是可行的,因為一個十六進制數字唯一地定義了引腳模式。 人們可以記住 GPIO 設置的十六進制文字的含義,並在一次定義 GPIO 端口的 8 個引腳的緊湊分配中使用它們。 如果您只進行一次 GPIO 配置,則不需要&=|=運算符。

此外,更喜歡使用BSRR而不是ODR ,因為它允許對 output 引腳進行原子修改。

這是Blue Pill板的 LED 閃爍示例:

//========================================================================
// Includes
//========================================================================
#include "stm32f1xx.h"
#include <stdbool.h>
#include <stdint.h>

//========================================================================
// Local Variables
//========================================================================
volatile uint32_t sysTick = 0;

//========================================================================
// Local Function Prototypes
//========================================================================
void initialize(void);
void delayMs(uint32_t ms);

//========================================================================
// Initialization
//========================================================================
void initialize(void) {

    SystemCoreClock = 8000000U;

    RCC->APB2ENR |= RCC_APB2ENR_IOPCEN // Enable PortC
            | RCC_APB2ENR_AFIOEN; // Enable A.F.Z

    // Pin Remaps
    AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_JTAGDISABLE; // JTAG is disabled

    // GPIO Settings
    GPIOC->CRH = 0x44244444; // LED connected to PC13
    GPIOC->BSRR = (1 << 13); // LED is inverted, turn it off.

    // SysTick Settings
    SysTick_Config(SystemCoreClock / 1000);

    // Initialization Done Signal
    GPIOC->BSRR = (1 << (13 + 16));
    delayMs(200);
    GPIOC->BSRR = (1 << 13);
}

//========================================================================
// Main
//========================================================================
int main(void) {

    initialize();

    while (true) {
        delayMs(500);
        GPIOC->BSRR = (1 << (13 + 16));
        delayMs(500);
        GPIOC->BSRR = (1 << 13);
    }
}

//========================================================================
// Interrupt Handlers
//========================================================================

void SysTick_Handler(void)
{
    ++sysTick;
}

//========================================================================
// Local Functions
//========================================================================

void delayMs(uint32_t ms) {
    uint32_t start = sysTick;
    while (sysTick - start < ms);
}
#include "stm32f10x.h"                  // Device header

int main(void){
    RCC->APB2ENR |= 1<<4;
    GPIOC->CRH &= 0xFF0FFFF;//clear the necessary bit 
    GPIOC->CRH |= 0x00300000;//set the necessary bit
    GPIOC->ODR &= ~(1<<13);//turn PC_13 ON
        while(1){
        
    }
}

此代碼有效。 問題是 PC13 控制的是陰極,而不是內置 LED 的陽極。 這就是為什么我應該寫: GPIO->ODR &= ~(1 << 13); 為了打開 LED。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM