STM32点亮流水灯、驱动蜂鸣器

基于STM32F407ZE开发板的点亮流水灯、驱动蜂鸣器
LED原理图:
在这里插入图片描述

蜂鸣器BEEP原理图:
在这里插入图片描述

芯片原理图:
在这里插入图片描述

①由LED原理图可知:4个LED灯对应的引脚分别为:LED0 、 LED1 、 FSMC_D10 、FSMC_D11
②由蜂鸣器原理图可知:蜂鸣器对应的引脚为: BEEP
③对照芯片原理图,找到对应的引脚:
4个LED灯: PF9 — --- LED0
PF10 — --- LED1
PE13 — --- LED3
PE14 — --- LED4
蜂鸣器: PF8 — --- BEEP

实现代码如下:

#include <stm32f4xx.h>

void delay_ms(int ms)									//延时函数
{
    
    
	int i,j;
	for(i=0; i<ms; i++)
		for(j=0; j<10000;j++);
}


int main()
{
    
    
	GPIO_InitTypeDef led1;
	GPIO_InitTypeDef led2;
	GPIO_InitTypeDef FM;
	
	//先开启对应用到的模块时钟节拍
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);
	
	led1.GPIO_Pin 	= GPIO_Pin_9 | GPIO_Pin_10;
	led1.GPIO_Mode 	= GPIO_Mode_OUT;
	led1.GPIO_Speed = GPIO_Fast_Speed;
	led1.GPIO_OType = GPIO_OType_PP;
	led1.GPIO_PuPd  = GPIO_PuPd_UP;
	GPIO_Init(GPIOF,&led1);
	
	led2.GPIO_Pin 	= GPIO_Pin_13 | GPIO_Pin_14;
	led2.GPIO_Mode 	= GPIO_Mode_OUT;
	led2.GPIO_Speed = GPIO_Fast_Speed;
	led2.GPIO_OType = GPIO_OType_PP;
	led2.GPIO_PuPd  = GPIO_PuPd_UP;
	GPIO_Init(GPIOE,&led2);
	
	FM.GPIO_Pin 	= GPIO_Pin_8;
	FM.GPIO_Mode 	= GPIO_Mode_OUT;
	FM.GPIO_Speed 	= GPIO_Fast_Speed;
	FM.GPIO_OType 	= GPIO_OType_PP;
	FM.GPIO_PuPd  	= GPIO_PuPd_UP;
	GPIO_Init(GPIOF,&FM);
	
	int t = 1;
	while(t)
	{
    
    
		GPIO_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);	//置位、高电平(关灯)
		GPIO_SetBits(GPIOE,GPIO_Pin_13 | GPIO_Pin_14);	//置位、高电平(关灯)
		
		GPIO_ResetBits(GPIOF,GPIO_Pin_9);				//复位、低电平(开灯)
		delay_ms(100);
		
		GPIO_SetBits(GPIOF,GPIO_Pin_9);					//置位、高电平(关灯)
		GPIO_ResetBits(GPIOF,GPIO_Pin_10);				//复位、低电平(开灯)
		delay_ms(100);
		
		GPIO_SetBits(GPIOF,GPIO_Pin_10);				//置位、高电平(关灯)
		GPIO_ResetBits(GPIOE,GPIO_Pin_13);				//复位、低电平(开灯)
		delay_ms(100);
		
		GPIO_SetBits(GPIOE,GPIO_Pin_13);				//置位、高电平(关灯)
		GPIO_ResetBits(GPIOE,GPIO_Pin_14);				//复位、低电平(开灯)
		
		delay_ms(100);
		
		GPIO_ToggleBits(GPIOF,GPIO_Pin_8);				//蜂鸣器引脚电平取反
		//GPIO_WriteBit(GPIOF,GPIO_Pin_8,1);			//蜂鸣器置1高电平
		//GPIO_SetBits(GPIOF,GPIO_Pin_8);				//蜂鸣器响起
		delay_ms(3);
		//GPIO_ResetBits(GPIOF,GPIO_Pin_8);				//蜂鸣器静音
		//GPIO_WriteBit(GPIOF,GPIO_Pin_8,0);			//蜂鸣器置0低电平
		GPIO_ToggleBits(GPIOF,GPIO_Pin_8);				//蜂鸣器引脚电平取反
		
		t++;
		if(t == 6)
		{
    
    
			t = 0;
			GPIO_SetBits(GPIOE,GPIO_Pin_14);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43793181/article/details/109008629