51 microcontroller buttons to control the LED light on and off N ways to play

51 microcontroller buttons to control the LED light on and off N ways to play

1 Overview

This article introduces the use of buttons, and through a small experiment of controlling LED lights, discovers problems in the buttons, and then thinks about and solves these problems. Achieve proficiency in using button control components.

2. Build a hardware environment

1.Hardware preparation
name model quantity
Microcontroller STC12C2052 1
LED lanterns none 2
crystal oscillator 12MHZ 1
capacitance 30pf 2
resistance 100 ohms 2
Breadboard none 1
Connecting line none 5
2. Circuit diagram schematic diagram

Insert image description here

  • 1. Connect a 100 ohm resistor in series to the positive pole of the LED lamp, and connect it to pin 20 VCC of the microcontroller.
  • 2. One end of the connecting cable is connected to pin 12 of the microcontroller, and the other end is connected to one pin of the button.
  • 3. The other pin of the button is connected to pin 10 GND of the microcontroller through a connecting wire.
  • 4. The two pins of the crystal oscillator are connected to pins 4 and 5 of the microcontroller respectively. At the same time, each pin of the crystal oscillator is connected in series with a 30pf capacitor to pin 10 of the microcontroller, GND.

Insert image description here

3. Experimental topics

3.1. When the button is pressed, the light turns on, and when the button is released, the light turns off.

1. Topic description

The purpose of the project is to use buttons to control the on and off of LED lights. When the button is pressed, a low level is read and the light turns on. When the send-on button reads a high level, the light turns off.

2. Implement the program
/*************************************************************
* 程序名: 按键控制LED灯亮灭
* 编写人: bruce
* 硬件支持:TC12C2052 外部12MHZ晶振
* 日  期: 2023-11-14
* 修改日志: 
/*************************************************************/

#include<STC12C2052AD.H>

sbit LED = P1^7;
sbit KEY = P1^0;


void main(void){
    
    
	KEY = 1;
	LED = 1;

	while(1){
    
    
		/*
		使用if判断语句实现按下按键LED灯亮,
		松开按键LED灯灭
		*/

		/*
		if(KEY == 0){
			LED = 0;
		}else if(KEY == 1){
			LED = 1;
		}
		*/

		// 讨巧的方式实现按键控制灯亮灭
		LED = KEY;
		
	}

}

3.2. Button rotation control light switch

1. Topic description

When the button is pressed and released for the first time, the light is turned on, and when the button is released for the second time, the light is turned off.

2. Implement the program
/*************************************************************
* 程序名: 第一次按下按键LED灯亮,第二次按下按键LED灯灭,如此循环
* 编写人: bruce
* 硬件支持:TC12C2052 外部12MHZ晶振
* 日  期: 2023-11-14
* 修改日志: 
/*************************************************************/

#include<STC12C2052AD.H>

sbit LED = P1^7;
sbit KEY = P1^0;

/*********************************************************************************************
函数名:毫秒级CPU延时函数
调  用:DELAY_MS (?);
参  数:1~65535(参数不可为0)
返回值:无
结  果:占用CPU方式延时与参数数值相同的毫秒时间
备  注:应用于1T单片机时i<600,应用于12T单片机时i<125
/*********************************************************************************************/
void DELAY_MS (unsigned int a){
    
    
	unsigned int i;
	while( --a != 0){
    
    
		for(i = 0; i < 600; i++);
	}
}

void main(void){
    
    
	KEY = 1;
	LED = 1;

	while(1){
    
    

		if(KEY == 0){
    
    
			/*
			DELAY_MS()延时函数的作用是去掉按下按键抖动造成信号不稳定,导致程序失灵。
			当按下按键后让程序稍等一会跳过按键抖动时刻,再读取按键状态达到去抖动效果。
		  */
			DELAY_MS(120);
			if(KEY == 0){
    
    
				//当按键为0时,判断当前灯的状态,并取反。实现轮训控制灯的开关。
				if(LED == 0){
    
    
					LED = 1;
				}else{
    
    
					LED = 0;
				}
				
			}
		}	
	}

}

Although the above program realizes the button control function of turning on and off the light, it has two imperfections and needs to be optimized. The optimization points are as follows

  • Embedding three layers of if judgment statements makes the program complicated and difficult to read.
  • Only the delay is used to eliminate key jitter, and occasionally malfunctions may occur.

The following program has optimized these two points to make the program more nightly.

/*************************************************************
* 程序名: 第一次按下按键LED灯亮,第二次按下按键LED灯灭,如此循环
* 编写人: bruce
* 硬件支持:TC12C2052 外部12MHZ晶振
* 日  期: 2023-11-14
* 修改日志: 
/*************************************************************/

#include<STC12C2052AD.H>

sbit LED = P1^7;
sbit KEY = P1^0;

/*********************************************************************************************
函数名:毫秒级CPU延时函数
调  用:DELAY_MS (?);
参  数:1~65535(参数不可为0)
返回值:无
结  果:占用CPU方式延时与参数数值相同的毫秒时间
备  注:应用于1T单片机时i<600,应用于12T单片机时i<125
/*********************************************************************************************/
void DELAY_MS (unsigned int a){
    
    
	unsigned int i;
	while( --a != 0){
    
    
		for(i = 0; i < 600; i++);
	}
}

void main(void){
    
    
	KEY = 1;
	LED = 1;

	while(1){
    
    

		if(KEY == 0){
    
    
			/*
			DELAY_MS()延时函数的作用是去掉按下按键抖动造成信号不稳定,导致程序失灵。
			当按下按键后让程序稍等一会跳过按键抖动时刻,再读取按键状态达到去抖动效果。
		  */
			DELAY_MS(120);
			if(KEY == 0){
    
    
				//当按键为0时,判断当前灯的状态,并取反。实现轮训控制灯的开关。
				LED = ~LED;
				//当按键按下没有松开时则一直做空循环,直到松开,while(1)才开始新的一轮循环。增强了按键控制灯的稳定性。
				while(KEY == 0);	
			}
		}	
	}

}

Guess you like

Origin blog.csdn.net/m0_38039437/article/details/134560671