STM32

03实现LED流水灯和蜂鸣器

2022-12-02  本文已影响0人  MJUNy
#include "stm32f10x.h"
#include "Delay.h"

int main(void) 
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//APB2时钟使能
    
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;//PA0
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;//速度
    GPIO_Init(GPIOA, &GPIO_InitStruct);//GPIO初始化
    //对单个或多位进行操作
    //GPIO_SetBits(GPIOA, GPIO_Pin_0);//灭
    //GPIO_ResetBits(GPIOA, GPIO_Pin_0);//亮,低电平驱动
    //对单个位进行操作
    //GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_RESET);
    //GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET);
    while(1)
    {
        GPIO_ResetBits(GPIOA, GPIO_Pin_0);
        Delay_ms(1000);
        GPIO_SetBits(GPIOA, GPIO_Pin_0);
        Delay_ms(1000);
    }
}

6、验证开漏输出和推挽输出的驱动能力
将LED的长脚接PA0,断脚接+极,LED则为高电平驱动,在推挽模式下,LED仍然能正常点亮,说明推挽模式高低电平均有驱动能力。
而对于开漏模式,当LED为高电平驱动时,LED无法被点亮,只有当LED为低电平驱动时,才能被点亮,说明开漏模式下高电平有高阻态,低电平才有驱动能力。
7、流水灯(PA0~PA7)

#include "stm32f10x.h"
#include "Delay.h"

int main(void) 
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//APB2时钟使能
    
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_All;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;//速度
    GPIO_Init(GPIOA, &GPIO_InitStruct);//GPIO初始化

    while(1)
    {
        GPIO_Write(GPIOA, ~0x0001);//0000 0000 0000 0001
        Delay_ms(1000);
        GPIO_Write(GPIOA, ~0x0002);//0000 0000 0000 0010
        Delay_ms(1000);
        GPIO_Write(GPIOA, ~0x0004);//0000 0000 0000 0100
        Delay_ms(1000);
        GPIO_Write(GPIOA, ~0x0008);//0000 0000 0000 1000
        Delay_ms(1000);
        GPIO_Write(GPIOA, ~0x0010);//0000 0000 0001 0000
        Delay_ms(1000);
        GPIO_Write(GPIOA, ~0x0020);//0000 0000 0010 0000
        Delay_ms(1000);
        GPIO_Write(GPIOA, ~0x0040);//0000 0000 0100 0000
        Delay_ms(1000);
        GPIO_Write(GPIOA, ~0x0080);//0000 0000 1000 0000
        Delay_ms(1000);
    }
}

也可以写成GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | ... ;


或操作实现

8、蜂鸣器

上一篇 下一篇

猜你喜欢

热点阅读