include/fan_buzzer.h
#ifndef __FAN_BUZZER_H__
#define __FAN_BUZZER_H__
//定义寄存器结构体
typedef struct{
unsigned int moder;
unsigned int otyper;
unsigned int ospeedr;
unsigned int pupdr;
unsigned int idr;
unsigned int odr;
}gpio_t;
//定义GPIOE,GPIOF,RCC
#define GPIOE ((gpio_t *)0x50006000)
#define GPIOB ((gpio_t *)0x50003000)
#define RCC (*(volatile unsigned int*)0x50000A28)
void delay_t(int t);
void fan_init();
void buzzer_init();
void fan_run();
void buzzer_run();
#endif
main.c
#include "fan_buzzer.h"
int main()
{
fan_init();
buzzer_init();
while(1)
{
fan_run();
buzzer_run();
}
return 0;
}
src/fan_buzzer.c
#include "fan_buzzer.h"
//延时函数
void delay_t(int t)
{
for(int i=0;i<t;i++)
{
for(int j=0;j<1800;j++);
}
}
//初始化风扇
void fan_init()
{
RCC &= (~(1<<4));
RCC |= (1<<4);
GPIOE->moder &= (~(0x3<<18));
GPIOE->moder |= (0x1<<18);
GPIOE->otyper &= (~(0x1<<9));
GPIOE->ospeedr &= (~(0x1<<18));
GPIOE->pupdr &= (~(0x1<<18));
}
//初始化蜂鸣器
void buzzer_init()
{
RCC &= (~(1<<1));
RCC |= (1<<1);
GPIOB->moder &= (~(0x3<<12));
GPIOB->moder |= (0x1<<12);
GPIOB->otyper &= (~(0x1<<6));
GPIOB->ospeedr &= (~(0x1<<12));
GPIOB->pupdr &= (~(0x1<<12));
}
//风扇转五秒
void fan_run()
{
GPIOE->odr |= (0x1<<9);
GPIOB->odr &= (~(0x1<<6));
delay_t(5000);
}
//蜂鸣器响五秒
void buzzer_run()
{
GPIOE->odr &= (~(0x1<<9));
GPIOB->odr |= (0x1<<6);
delay_t(5000);
}