Skip to content

单片机_stm32f103c8t6_GPIO输出

点亮LED

c #include "stm32f10x.h"

int main(void) {
// 1.使能GPIOA的时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

// 2.初始化GPIOA
GPIO_InitTypeDef GPIO_InitStructure = 
{
    .GPIO_Mode = GPIO_Mode_Out_PP, // 推挽输出
    .GPIO_Pin = GPIO_Pin_0,
    .GPIO_Speed = GPIO_Speed_50MHz,
};
GPIO_Init(GPIOA,&GPIO_InitStructure);

// 3. 0管脚低电平点亮
GPIO_ResetBits(GPIOA, GPIO_Pin_0);
//GPIO_ResetBits(GPIOA, GPIO_Pin_0);

while(1)
{
    
}

}

## LED流水灯
> 推挽输出高低电平都可以驱动,开漏输出只能低电平驱动
![](../publics/images/GIF_20260208_223933.gif)es/GIF_20260208_223933.gif)

```c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"

int main(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    
    GPIO_InitTypeDef GPIO_InitStructure = 
    {
        // 开漏输出只有低电平的输出能力,推挽输出高低电平的能力都有
        .GPIO_Mode = GPIO_Mode_Out_PP, // 推挽输出
        .GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 |
                    GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5,
        .GPIO_Speed = GPIO_Speed_50MHz,
    };
    GPIO_Init(GPIOA,&GPIO_InitStructure);


    unsigned int tmp = 0x0001;
    while(1)
    {
        GPIO_Write(GPIOA, tmp);       // 低电平 亮 一次写入32位
        Delay_ms(500);

        tmp <<= 1;
        if (tmp > GPIO_Pin_5) 
            tmp = 0x0001;
    }
}

蜂鸣器

PB3、PB4、PA15 为Jtag调试端口,当作普通gpio来使用,需要额外的配置 #注意事项

c
#include "stm32f10x.h"                  // Device header
#include "Delay.h"

int main(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    
    GPIO_InitTypeDef GPIO_InitStructure = 
    {
        .GPIO_Mode = GPIO_Mode_Out_PP, // 推挽输出
        .GPIO_Pin = GPIO_Pin_8, 
        .GPIO_Speed = GPIO_Speed_50MHz,
    };
    GPIO_Init(GPIOB,&GPIO_InitStructure);

    while(1)
    {
        GPIO_WriteBit(GPIOB, GPIO_Pin_8, Bit_RESET);
        Delay_ms(100);
    }
}
最近更新