简体   繁体   English

STM32F103C8 LED 闪烁

[英]STM32F103C8 LED blink

I try to program an STM32F103C8 circuit.我尝试对 STM32F103C8 电路进行编程。 I use an ST-LINK V2 programmer.我使用 ST-LINK V2 编程器。 After running a sample of code that used special libraries, I was able to see the built-in LED ON, but now I want to program the board without using those libraries, and I don't know why I cannot see anything, the LED is OFF the whole time.运行使用特殊库的代码示例后,我能够看到内置 LED 亮起,但现在我想在不使用这些库的情况下对电路板进行编程,我不知道为什么我什么都看不到,LED一直处于关闭状态。 Here is the code:这是代码:

#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){
        
    }
}

Based on the info that you have the LED on PC13, I assume you're using the famous Blue Pill board.根据您在 PC13 上有 LED 的信息,我假设您使用的是著名的Blue Pill板。 In that board, LED connection is inverted because of the special limitations of PC13 - that pin should only sink current, not source.在该板上,由于 PC13 的特殊限制,LED 连接被反转 - 该引脚只能吸收电流,而不是源。 So, in order to turn it on, you need to write 0 to corresponding bit of ODR.因此,为了打开它,您需要将 0 写入 ODR 的相应位。

Normally, I'm against the usage of magic numbers .通常,我反对使用幻数 But for STM32F103, using hex literals for GPIO configuration is practical, because one single hex digit uniquely defines the pin mode.但对于 STM32F103,使用十六进制文字进行 GPIO 配置是可行的,因为一个十六进制数字唯一地定义了引脚模式。 One can memorize the meaning of hex literals for GPIO settings, and use them in a compact assignment which defines the 8 pins of a GPIO port at once.人们可以记住 GPIO 设置的十六进制文字的含义,并在一次定义 GPIO 端口的 8 个引脚的紧凑分配中使用它们。 If you make GPIO configurations only once, you don't need &= or |= operators.如果您只进行一次 GPIO 配置,则不需要&=|=运算符。

Also, prefer using BSRR instead of ODR , because it allows atomic modification of output pins.此外,更喜欢使用BSRR而不是ODR ,因为它允许对 output 引脚进行原子修改。

Here is a LED blink example for Blue Pill board:这是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){
        
    }
}

This code works.此代码有效。 The problem was that PC13 controls the cathode, not the anode of the built-in LED.问题是 PC13 控制的是阴极,而不是内置 LED 的阳极。 That's why I should write: GPIO->ODR &= ~(1 << 13);这就是为什么我应该写: GPIO->ODR &= ~(1 << 13); in order to turn ON the LED.为了打开 LED。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM