使用keil5及Proteus8.7的LED按键电路亮灭仿真

使用Proteus8.7软件实现按键控制对应LED工作设置电路仿真

  • 实验操作过程及内容
    • 实验程序代码
    • 使用Proteus8.7软件实现按键控制对应LED工作设置电路仿真图

实验操作过程及内容

按键是最重要的外部输入设备之一,可以通过按键阵列支持更多的按建输入,或者通过扩ZLG7289B芯片。按键直接与STM32F103ZET6芯片的管脚相连,三个按键均为常开按键,当按键被按下时,输入低电平;当按键弹出后,相应的管脚被内部上拉电路拉高,从而相当于输入高电平。设计工程项目,实现如下功能:
(1)KEY0按键按下抬起后LED0、LED1灯点亮;
(2)KEY1按键按下抬起后LED0熄灭、LED1灯点亮;
(3)KEY2按键按下抬起后LED0、LED1熄灭。

实验程序代码

  1. main.c
 //Filename: main.c  
   #include "includes.h"
    
    void Delay(Int32U);
    
    int main(void)
    {
    	LEDInit();
   		KEYInit();
   		LED(0,LED_OFF);
   	LED(1,LED_OFF);
   		 
   	for(;;)
   	{
   			if(KEY_Scan(0)==0)
   			{ while(KEY_Scan(0)==0)					
   						LED(0,LED_OFF);
   						LED(1,LED_OFF);
   			}
   			
   			if(KEY_Scan(2)==0)
   			{ while(KEY_Scan(2)==0)					
   						LED(1,LED_ON);
   						LED(0,LED_ON);
   			}
   			
   			if(KEY_Scan(1)==0)
   			{ while(KEY_Scan(1)==0)
   				
   						LED(1,LED_ON);
   						LED(0,LED_OFF);
   			}
   
   	}
   }
   
   void Delay(Int32U u)
   {
   	Int32U i,j;
   	for(i=0;i<u;i++)
   		for(j=0;j<12000;j++);
   }

2.key.h

    //Filename: key.h
    
    
    #include "vartypes.h"
   	 
   	 #ifndef _KEY_H
   	 #define _KEY_H
   	 
   	 void KEYInit(void);
   	 Int08U KEY_Scan(Int08U key);
   	 
   	 #endif

3.key.c

   //Filename: key.c
       #include "includes.h"
   
   void KEYInit(void)
   {
   	RCC->APB2ENR |= (1uL<<4);
  		 
   	GPIOC->CRL &=~((3uL<<8)|(3uL<<12)|(3uL<<16));
  		GPIOC->CRL |=(1uL<<10)|(1uL<<14)|(3uL<<18);
  		GPIOC->CRL &=~((1uL<<11)|(1uL<<15)|(1uL<<19));
  	
  	
  }
  
  	Int08U KEY_Scan(Int08U key)
  	{
  	switch(key)
  	{
  		case 0: return (GPIOC->IDR& GPIO_IDR_IDR4);
  		case 1: return (GPIOC->IDR& GPIO_IDR_IDR3);
  		case 2: return (GPIOC->IDR& GPIO_IDR_IDR2);
  		default:return 1;
  	}
  	}

4.vartypes.h

   //Filename: vartypes.h
   
   #ifndef _VARTYPES_H
   #define _VARTYPES_H
   
   typedef unsigned char  Int08U;
   typedef signed char    Int08S;
   typedef unsigned short Int16U;
   typedef signed short   Int16S;
  typedef unsigned int   Int32U;
  typedef signed int     Int32S;
  
  typedef float          Float32;
  
  typedef enum {LED_ON,LED_OFF} LEDState;
  
  #endif

5.include.h

   //Filename: includes.h
   
   #include "stm32f10x.h"
   
   #include "vartypes.h"
  	 #include "key.h"  
   #include "led.h"  

6.led.c

   //Filename: led.c
       #include "includes.h"
   
   void LEDInit(void)
   {
   	RCC->APB2ENR |= (1uL<<3) | (1uL<<2);
   	GPIOB->CRL |= (1uL<<20);
   	GPIOB->CRL &=~((1uL<<21) | (1uL<<22) | (1uL<<23));
  	
  	GPIOA->CRL |= (1uL<<20);
  	GPIOA->CRL &=~((1uL<<21) | (1uL<<22) | (1uL<<23));
  }
  
  void LED(Int08U w, LEDState s) 
  {
  	switch(w)
  	{
  		case 0:
  			if(s==LED_ON)
  			  GPIOB->BRR = (1uL<<5);
  			else
  			  GPIOB->BSRR = (1uL<<5);
  			break;
  		case 1:
  			if(s==LED_ON)
  			  GPIOA->BRR = (1uL<<5);	
  			else
  			  GPIOA->BSRR = (1uL<<5);
  			break;
  		default:
  			break;
  	}	
  }

7.led.h

   //Filename: led.h
   
   #include "vartypes.h"
   
   #ifndef  _LED_H
   #define  _LED_H
   
   void LEDInit(void);
   void LED(Int08U,LEDState);
  
  #endif

使用Proteus8.7软件实现按键控制对应LED工作设置电路仿真图

KEY0按键按下抬起后LED0、LED1灯点亮电路结果如下:
使用keil5及Proteus8.7的LED按键电路亮灭仿真_第1张图片

KEY1按键按下抬起后LED0熄灭、LED1灯点亮电路结果如下:
使用keil5及Proteus8.7的LED按键电路亮灭仿真_第2张图片

KEY2按键按下抬起后LED0、LED1熄灭电路结果如下:

使用keil5及Proteus8.7的LED按键电路亮灭仿真_第3张图片
实验程序下载链接[gitee](https://gitee.com/w1391830845/embedded-development.git)

你可能感兴趣的:(C,嵌入式,c++)