ARM——蜂鸣器、风扇

文章提供了一组C语言代码,用于初始化和控制GPIO来操作蜂鸣器和风扇。通过设置GPIO引脚的模式、速度和输出状态,实现了5秒间隔的蜂鸣器响动和风扇转动功能。代码中包含了蜂鸣器和风扇的初始化函数以及开关控制函数。
摘要由CSDN通过智能技术生成

实现C语言控制蜂鸣器和风扇,5秒蜂鸣器响,5秒风扇转动。

蜂鸣器原理图 

 

 

风扇原理图 

 

 

代码如下 

gpio.h

#ifndef __GPIO_H__
#define __GPIO_H__

/* 封装RCC外设寄存器 */
#define RCC_MP_AHB4ENSETR (*((volatile unsigned int*)0x50000A28))

/* 封装GPIOA~GPIOF外设寄存器 */
typedef struct {
    volatile unsigned int MODER;
    volatile unsigned int OTYPER;
    volatile unsigned int OSPEEDR;
    volatile unsigned int PUPDR;
    volatile unsigned int IDR;
    volatile unsigned int ODR;
} gpio_t;

#define GPIOA ((gpio_t*)0x50002000)
#define GPIOB ((gpio_t*)0x50003000)
#define GPIOC ((gpio_t*)0x50004000)
#define GPIOD ((gpio_t*)0x50005000)
#define GPIOE ((gpio_t*)0x50006000)
#define GPIOF ((gpio_t*)0x50007000)

/* 枚举类型表示引脚的输出状态 */
typedef enum {
    GPIO_RESET = 0,
    GPIO_SET
} gpio_statu_t;

#endif

beep.h

#ifndef __BEEP_H
#define __BEEP_H

#include "gpio.h"

/* 蜂鸣器初始化函数 */
void hal_beep_init(void);

/* 控制蜂鸣器开关函数 */
void hal_beep_switch(gpio_statu_t statu);

#endif

beep.c

#include "beep.h"

void hal_beep_init(void)
{
    /* 使能GPIOB外设控制器的时钟 RCC_MP_AHB4ENSETR[1] 写0b1 */
    RCC_MP_AHB4ENSETR |= (0x1 << 1);

    /* 设置PB6引脚为输出模式  GPIOB_MODER[13:12]  写0b01 */
    GPIOB->MODER &= (~(0x3 << 12));
    GPIOB->MODER |= ((0x1 << 12));

    /* 设置PB6引脚为推挽输出  GPIOB_OTYPER[6]  写0b0 */
    GPIOB->OTYPER &= (~(0x1 << 6));

    /* 设置PB6引脚为低速模式  GPIOB_OSPEEDR[13:12]  写0b00 */
    GPIOB->OSPEEDR &= (~(0x3 << 12));

    /* 设置PB6引脚禁止上拉和下拉电阻  GPIOB_PUPDR[13:12]  写0b00 */
    GPIOB->PUPDR &= (~(0x3 << 12));

    /* 设置蜂鸣器(PB6)引脚输出低电平,关闭蜂鸣器  GPIOB_ODR[6] 写0b0 */
    GPIOB->ODR &= (~(0x1 << 6));
}

void hal_beep_switch(gpio_statu_t statu)
{
    /* 设置蜂鸣器(PB6)引脚输出高电平,打开蜂鸣器  GPIOB_ODR[6] 写0b1 */
    if (statu == GPIO_SET) {
        GPIOB->ODR |= (0x1 << 6);
    } else { /* 设置蜂鸣器(PB6)引脚输出低电平,关闭蜂鸣器  GPIOB_ODR[6] 写0b0 */
        GPIOB->ODR &= (~(0x1 << 6));
    }
}

fan.h

#ifndef __FAN_H
#define __FAN_H

#include "gpio.h"

/* 风扇初始化函数 */
void hal_fan_init(void);

/* 控制风扇开关函数 */
void hal_fan_switch(gpio_statu_t statu);

#endif

fan.c

#include "fan.h"

void hal_fan_init(void)
{
    /* 使能GPIOE外设控制器的时钟 RCC_MP_AHB4ENSETR[4] 写0b1 */
    RCC_MP_AHB4ENSETR |= (0x1 << 1);

    /* 设置PE9引脚为输出模式  GPIOE_MODER[19:18]  写0b01 */
    GPIOE->MODER &= (~(0x3 << 18));
    GPIOE->MODER |= ((0x1 << 18));

    /* 设置PE9引脚为推挽输出  GPIOE_OTYPER[9]  写0b0 */
    GPIOE->OTYPER &= (~(0x1 << 9));

    /* 设置PE9引脚为低速模式  GPIOE_OSPEEDR[19:18]  写0b00 */
    GPIOE->OSPEEDR &= (~(0x3 << 18));

    /* 设置PE9引脚禁止上拉和下拉电阻  GPIOE_PUPDR[19:18]  写0b00 */
    GPIOE->PUPDR &= (~(0x3 << 18));

    /* 设置风扇(PE9)引脚输出低电平,关闭风扇  GPIOE_ODR[9] 写0b0 */
    GPIOE->ODR &= (~(0x1 << 9));
}

void hal_fan_switch(gpio_statu_t statu)
{
    /* 设置风扇(PE9)引脚输出高电平,打开风扇  GPIOE_ODR[9] 写0b1 */
    if (statu == GPIO_SET) {
        GPIOE->ODR |= (0x1 << 9);
    } else { /* 设置蜂鸣器(PE9)引脚输出低电平,关闭风扇  GPIOE_ODR[9] 写0b0 */
        GPIOE->ODR &= (~(0x1 << 9));
    }
}

main.c

#include "beep.h"

#include "fan.h"

#include "gpio.h"

#include "led.h"



void delay_ms(unsigned int ms)

{

    int i, j;

    for (i = 0; i < ms; i++)

        for (j = 0; j < 1800; j++)

            ;

}



int main()

{

    /* 初始化蜂鸣器 */

    hal_beep_init();



    /* 初始化风扇 */

    hal_fan_init();



    while (1) {

        /* 蜂鸣器 */

        hal_beep_switch(GPIO_SET);

        delay_ms(5000);

        hal_beep_switch(GPIO_RESET);



        /* 风扇 */

        hal_fan_switch(GPIO_SET);

        delay_ms(5000);

        hal_fan_switch(GPIO_RESET);

    }

    return 0;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值