简体   繁体   English

在 stm32f103c8t6 Blue Pill 中启用 output 端口

[英]Enabling an output port in stm32f103c8t6 Blue Pill

I'm trying to enable the PC13 in the Blue Pill (stm32f103c8t6) which is connected to an LED, not sure if it is active low or active high so i tried both still doesn't work.我正在尝试启用连接到 LED 的蓝色药丸(stm32f103c8t6)中的 PC13,不确定它是低电平有效还是高电平有效,所以我尝试了两者仍然不起作用。

RCC->APB2ENR |= 0x10;
is used for enabling the clock in Port C.用于使能端口 C 中的时钟。

在此处输入图像描述


GPIOC->CRH = (GPIOC->CRH & 0xFF0FFFFF) | 0x00100000;
is used to configure the port C to be in Output mode and Push-Pull.用于将端口 C 配置为 Output 模式和 Push-Pull。

在此处输入图像描述


GPIOC->ODR &=;(1<<13); is used to drive the C13 pin to LOW.用于将 C13 引脚驱动为低电平。

The whole code:整个代码:

#include "stm32f10x.h"

int main(){
    RCC->APB2ENR |= 0x10;
    GPIOC->CRH = (GPIOC->CRH & 0xFF0FFFFF) | 0x00100000;  
    while(1) {
        GPIOC->ODR = ~(1<<13);//if it is Active Low
        for (int i = 0; i < 1000000; ++i) __asm__("nop");
        GPIOC->ODR |= 1<<13; //if it is Active High
        for (int i = 0; i <  500000; ++i) __asm__("nop");
    }

}

Edit:编辑:
After some investigation, i discovered that the code is working perfectly if run a debugging session, could it be a software problem?经过一番调查,我发现如果运行调试 session,代码运行良好,这可能是软件问题吗? or the code for debugging sets things i didn't?或者调试代码设置了我没有设置的东西? and as i said, I'm using uVision to compile and flash.正如我所说,我正在使用 uVision 编译和 flash。

If you are running Keil as you've said in your comments than it is quite certain that you are using the ST HAL.如果您按照评论中所说的那样运行 Keil,那么可以确定您使用的是 ST HAL。 As such you should use it.因此,您应该使用它。

Use this to enable the GPIOC clock使用它来启用 GPIOC 时钟

__GPIOC_CLK_ENABLE();

It also sounds like you haven't enabled another clock. 听起来您还没有启用另一个时钟。 I can't figure out at this time which one it is but it is probably one high up in the chain. 我目前无法弄清楚它是哪一个,但它可能是链条中的一个高位。 (This probably happens because you enabled the clock using APB2ENR instead of the macro) (这可能是因为您使用 APB2ENR而不是宏启用了时钟)

Another solution to figure it out would be to use STM32CubeMX it is a multiplatform tool proved by ST to create base initialized projects for several IDEs.另一种解决方案是使用STM32CubeMX ,它是 ST 证明的多平台工具,可以为多个 IDE 创建基本初始化项目。

Download and run it, create a project for your MCU, add the pin you want to drive as an output and generate a Keil project which should compile and run.下载并运行它,为您的 MCU 创建一个项目,将您要驱动的引脚添加为 output 并生成一个应该编译和运行的 Keil 项目。 If it works you can then reverse engineer the steps that STM32CubeMX took to create working code.如果它有效,您可以对 STM32CubeMX 创建工作代码所采取的步骤进行逆向工程。


Your underlying problem is that you haven't enabled reset and run in the configuration.您的根本问题是您尚未启用reset and run As such after programming the board needs to be reset before the newly flashed code will run.因此,在编程后,需要在新闪存代码运行之前重置电路板。 Starting the debugger does this.启动调试器会执行此操作。

带有重置和运行的配置窗口

This configuration is found inside the utilities->settings menu.此配置可在实用程序-> 设置菜单中找到。 (ignore the red marker, I couldn't capture the image myself and as such got it from the internet) (忽略红色标记,我自己无法捕捉图像,因此从互联网上获取)

在此处输入图像描述

If you use "volatile" before "int", it will work如果您在“int”之前使用“volatile”,它将起作用

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

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