嵌入式入门小作业(1)

博客围绕STM32展开,介绍了按键、LED灯、蜂鸣器库的初始化,包含按键库及其主代码、LED灯库和蜂鸣器库,还提及主函数,最后有相关视频展示。

STM32按键LED灯蜂鸣器

按键、LED灯、蜂鸣器库初始化

按键库

#ifndef __KEY_H
#define __KEY_H	 //使库没有被多次定义
#include "sys.h" 

#define KEY0 		GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)
#define KEY1 		GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)	
#define KEY2 		GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2) 
#define WK_UP   	GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)	 
//对每个按键进行宏定义


#define KEY0_PRES 	1
#define KEY1_PRES   2
#define KEY2_PRES	3
#define WKUP_PRES   4     //将每个按键按下的状态转化为数字

void KEY_Init(void);	
u8 KEY_Scan(u8);  		

#endif  
                    //多一行回车不会报warning

按键库主代码

#include "key.h"
#include "delay.h" 

void KEY_Init(void)
{
	
	GPIO_InitTypeDef  GPIO_InitStructure;

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOE, ENABLE);
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOE, &GPIO_InitStructure);   
	
	 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);  
 
}    					
//将按键所对应的引脚进行库函数的状态初始化,设置输出方式,输出速度

u8 KEY_Scan(u8 mode)
{	 
	static u8 key_up=1;
	if(mode)key_up=1;  		  
	if(key_up&&(KEY0==0||KEY1==0||KEY2==0||WK_UP==1))
	{
		delay_ms(10);
		key_up=0;
		if(KEY0==0)return 1;
		else if(KEY1==0)return 2;
		else if(KEY2==0)return 3;
		else if(WK_UP==1)return 4;
	}else if(KEY0==1&&KEY1==1&&KEY2==1&&WK_UP==0)key_up=1; 	    
	//写入连续按键和不连续按键的库函数
 	return 0;     
}

LED灯库和蜂鸣器库

#ifndef __LED_H
#define __LED_H
#include "sys.h"


#define LED0 PFout(9)	 
#define LED1 PFout(10)	   //将对应端口定义为变量,方便改变	 

void LED_Init(void);

#endif


#include "led.h" 

void LED_Init(void)
{    	 
  GPIO_InitTypeDef  GPIO_InitStructure;

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOF, &GPIO_InitStructure);
	
	GPIO_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);

}


#ifndef __BEEP_H
#define __BEEP_H	 
#include "sys.h" 

#define BEEP PFout(8)	

void BEEP_Init(void);		 				    
#endif


#include "beep.h" 

void BEEP_Init(void)
{   
  GPIO_InitTypeDef  GPIO_InitStructure;

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
  GPIO_Init(GPIOF, &GPIO_InitStructure);
	
  GPIO_ResetBits(GPIOF,GPIO_Pin_8);  
}


主函数

#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "beep.h"
#include "key.h"       //调用所需要的函数库

int main(void)
{ 
 
  u8 key;          
	delay_init(168); 
	LED_Init();				 
	BEEP_Init();      
	KEY_Init();       //按键、LED灯、蜂鸣器初始化
	LED0=1;				  	
	LED1=1;         //没按键时灯不亮
	while(1)
	{
		key=KEY_Scan(0);	
	   	if(key)
		{						   
			switch(key)
			{				 
				case 4:	
					BEEP=!BEEP;
					LED0=!LED0;
					LED1=!LED1;
					break;
				case 3:	
					BEEP=!BEEP;
					LED0=!LED0;
					LED1=!LED1;
					break;
				case 2:	
					BEEP=!BEEP;
					LED0=!LED0;
					LED1=!LED1;
					break;
				case 1:	 
					BEEP=!BEEP;
					LED0=!LED0;
					LED1=!LED1;
					break;
			}
		}else delay_ms(10); 
	}

}

视频展示

作业视频

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Atom明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值