2023/6/7

#使用C实现基于STM32的蜂鸣器与风扇交替执行5s

fan.c

#include"fan.h"
//延时函数
void delay_ms(int ms)
{
	int i,j;
	for(i = 0; i < ms;i++)
		for (j = 0; j < 1800; j++);
}



//led灯设置初始化 
void all_fan_init()
{
	RCC |=(0x9<<1);
	//蜂鸣器
	GPIOB->moder &= (~(0X3<<12));
	GPIOB->moder |= (0X1<<12);
	GPIOB->otyper &=(~(0X1<<6));
	GPIOB->ospeedr &= (~(0X3<<12));
	GPIOB->pupdr &= (~(0X3<<12));
	//风扇
	GPIOE->moder &= (~(0X3<<18));
	GPIOE->moder |= (0X1<<18);
	GPIOE->otyper &=(~(0X1<<9));
	GPIOE->ospeedr &= (~(0X3<<18));
	GPIOE->pupdr &= (~(0X3<<18));
}
void  all_fan_flash()
{
	//蜂鸣器响
	GPIOB->odr |=(0X1<<6);
	GPIOE->odr &= (~(0X1<<9));
	delay_ms(5000);

	//风扇转
	GPIOB->odr &=(~(0X1<<6));
	GPIOE->odr |=(0X1<<9);
	delay_ms(5000);

} 


main.c

#include "fan.h"
int main()
{
	
	all_fan_init();

	while(1)
	{
		all_fan_flash();
	}
	return 0;
}

fan.h

#ifndef __FAN_H__
#define __FAN_H__

//定义寄存器组织结构体
typedef struct{
	unsigned int moder;
	unsigned int otyper;
	unsigned int ospeedr;
	unsigned int pupdr;
	unsigned int idr;
	unsigned int odr;
}gpio_t;



//定义GPIOB和GPIOE
#define GPIOB ((gpio_t *)0X50003000)
#define GPIOE ((gpio_t *)0X50006000)

#define RCC (*(volatile unsigned int *)0X50000A28)





void delay_ms(int ms);
void all_fan_init();
void  all_fan_flash();




#endif


你可能感兴趣的:(单片机,stm32,嵌入式硬件)