简体   繁体   English

STM32裸金属C - 无法使LED工作

[英]STM32 Bare Metal C - Can't get LED to work

So I'm currently following a course for the STM32 Microprocessor however, I seem to fail at even the most basic thing: turning a LED on. 所以我现在正在学习STM32微处理器的课程,但是,即使是最基本的东西,我似乎也失败了:开启LED。 The complete code is at the bottom of this post. 完整的代码位于这篇文章的底部。

Important: 重要:

  • The hardware is functioning properly. 硬件运行正常。
  • I am using a STM32L432KC. 我使用的是STM32L432KC。

First of all, we have to figure out on what pin the built-in LED is. 首先,我们必须弄清楚内置LED是什么引脚。 According to the manufacturers manual, the LED should be on pin D13 (PB3). 根据制造商手册,LED应位于引脚D13(PB3)上。 在此输入图像描述


Okay so we're looking for PB3. 好的,我们正在寻找PB3。 According to the datasheet of the STM32L432KC, PB3 is on the B port and therefore connected to the high performance bus as suggested in the image below. 根据STM32L432KC的数据表,PB3位于B端口,因此连接到高性能总线,如下图所示。 在此输入图像描述


Cool. 凉。 So our bus is AHB2 and we're working with GPIOB. 所以我们的巴士是AHB2,我们正在与GPIOB合作。 Now we have to enable the clock on that bus using the RCC_AHB3ENR register. 现在我们必须使用RCC_AHB3ENR寄存器在该总线上启用时钟。 Now, this is the part where I'm probably going to make mistakes (as this post otherwise wouldn't exist), so please pay close attention. 现在,这是我可能会犯错的部分(因为这篇文章不会存在),所以请密切注意。 If I understand correctly, I want bit 1 to be set to 1 as this indicates that 'GPIOBEN' is set to 'IO port B clock enabled.'. 如果我理解正确,我希望将第1位设置为1,因为这表示“GPIOBEN”设置为“启用了IO端口B时钟”。 在此输入图像描述 在此输入图像描述

This leads me to believe that I should set the bus register as follows: 这让我相信我应该按如下方式设置总线寄存器:

RCC->AHB2ENR |= 0x2;

Next up I have to set the mode of the GPIO pin to output. 接下来我必须将GPIO引脚的模式设置为输出。 According to the course and my documentation, this is done using GPIOx_MODER. 根据课程和我的文档,这是使用GPIOx_MODER完成的。 在此输入图像描述

This leads me to believe that I should set the GPIO mode as follows: 这让我相信我应该按如下方式设置GPIO模式:

GPIOB->MODER |= 0x40;

And last but not least to turn the actual LED on we have to set the output data register which is GPIOx_ODR. 最后但并非最不重要的是打开实际的LED,我们必须设置输出数据寄存器GPIOx_ODR。 在此输入图像描述

This leads me to believe that I should set the data as follows: 这让我相信我应该将数据设置如下:

GPIOB->ODR = 0x8;

I'm not sure where I'm going wrong but this is the first time I'm working with registers on such a deep level. 我不知道我哪里出错了,但这是我第一次在如此深层次的寄存器上工作。 I must be overlooking something but I've tried multiple examples and had no success. 我必须忽视一些事情,但我尝试了多个例子但没有成功。 All help is appreciated. 所有帮助表示赞赏。 This is the complete code: 这是完整的代码:

// PB3 - User LED
// RCC->AHB2ENR
// GPIOx_MODER
// GPIOx_ODR

#include "stm32l4xx.h"

int main(void)
{
    RCC->AHB2ENR |= 0x2;
    GPIOB->MODER |= 0x40;

    while(1)
    {
        GPIOB->ODR = 0x8;
    }
}

Your mode register is not configured correctly. 您的模式寄存器配置不正确。 Your line of code 你的代码行

GPIOB->MODER |= 0x40;

can only set bits, it cannot clear them. 只能设置位,它不能清除它们。 And you have too many bits set, as the reset value of each pair is 11 and the entire register is FFFF FFFF for ports CE, FFFF FEBF for port B. 并且您设置的位数太多,因为每对的复位值为11 ,端口CE的整个寄存器为FFFF FFFF ,端口B的FFFF FEBFFFFF FEBF

You should use 你应该用

GPIOB->MODER = (GPIOB->MODER & 0xFFFFFF3F) | 0x00000040;

although because the reset state is guaranteed, this will also work: 虽然因为重置状态得到保证,但这也有效:

 
 
 
  
  GPIOB->MODER &= 0xFFFFFF7F; // equivalently, ~0x0080
 
  

The note in the documentation of 11 analog mode (reset state) is not accurate for all pins. 11 analog mode (reset state)文档中的注释对于所有引脚都不准确。 Several are in 10 alternate function mode at reset, including PB3. 有几个在复位时处于10 alternate function模式,包括PB3。 So you need to both clear one bit and set one. 所以你需要清除一位并设置一位。

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

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