The single-chip microcomputer (AT89C51) button controls the LED light to realize the running water lamp, flashing running water lamp

table of Contents

Preface 

Experiment requirements and purpose

Experimental circuit diagram 

experiment procedure

experiment one

Experiment two

Experiment Three

Experiment summary

Afterword


 Preface 

It’s been a long time since I wrote the MCU series, so let’s go straight to the topic! The experiment to be explained and shared this time is a well-known water lamp experiment, but this experiment is to achieve "flowing water" by clicking the button by myself, and one of my simulation experiments ( microcontroller (AT89C51)-water lamp and one by one) Flashing lights (input and output) ) Blogs are different. Those who are interested in purely coded running lights can also read this article. In addition, I have launched a series of SCM knowledge points summary and experiment sharing: SCM Encyclopedia . Those who are interested can also pay attention to it to facilitate subsequent learning.

Experiment requirements and purpose

  1. Experiment 1: Every time you press the S1 key of the independent keyboard , one of the eight LEDs connected to the P1 port will light up and move down one bit

  2. Experiment 2: When the power is turned on, the light-emitting diode connected to the P1.0 pin of L1 is flickering. Press the switch once to make the next light start to flicker and light up, and so on.

  3. Experiment 3: Different switch button operations have different effects. I designed an experiment to achieve different effects when two different buttons are pressed. Combining the above two experiments, I got the-press the first When one button is pressed, it will normally jump to the next LED light (not flashing), when the second button is pressed, it will jump to the next LED light and blink.

Experimental circuit diagram 

      This experiment is mainly composed of single chip microcomputer, light emitting diode, 1K exclusion, and the realization circuit is as follows:

experiment procedure

experiment one

Experimental analysis: What needs to be noted during the experiment is to write the function in two parts, a function key() that recognizes whether the button is pressed and the move() function that executes the water lamp cycle . There is a global variable in the key function that will be added, mainly for the shift operation in the move function.

Experiment code:

#include<reg51.h>
sbit BY1=P3^4;	   //定义按键的输入端S建
unsigned char count;      //按键计数,每按一次加1
unsigned char temp;
unsigned char a,b;


void delay10ms(void)		  //延时程序
{
	unsigned char i,j;
	for(i=20;i>0;i--)
	 for(j=248;j>0;j--);
}

key()						 //判断按键是否按下
{
 	if(BY1==0)				 //判断是否按下按钮
	{
		delay10ms();		 //延时去抖动
		if(BY1==0)			 //确认按键是否按下
		{
			count++;		 //按键计数加1
			if(count==8)
			{
				count=0;
			}
		}
		while(BY1==0);            //按键锁定,每按一次count+1
	}
}

move()
{
	a=temp<<count;
	b=temp>>(8-count);
	P1=a|b;
}

main()
{
	count=0;
	temp=0xfe;
	P1=0xff;
	P1=temp;
	while(1)				//永远循环,扫描判断按键是否按下
	{
		key();			    //调用按键识别函数
		move();			    //调用移动函数
	}

}

The screenshot does not show the dynamic effect, but the video cannot be uploaded. It is recommended that CSDN modify the mechanism, so that we can also upload the video we recorded, and care about us niche bloggers

Experiment two

The difference between the second experiment and the experiment is to realize the flickering of the lamp, and then the operation of the switch shift is exactly the same as the previous experiment. We need to modify the move() function. For the sake of comparison, I will use the move function. Rewritten into move1() function.

The complete code of the experiment:

#include<reg51.h>
sbit BY1=P3^4;	   //定义按键的输入端S建
unsigned char count;      //按键计数,每按一次加1
unsigned char temp;
unsigned char a,b;


void delay10ms(void)		  //延时程序
{
	unsigned char i,j;
	for(i=20;i>0;i--)
	 for(j=248;j>0;j--);
}

key()						 //判断按键是否按下
{
 	if(BY1==0)				 //判断是否按下按钮
	{
		delay10ms();		 //延时去抖动
		if(BY1==0)			 //确认按键是否按下
		{
			count++;		 //按键计数加1
			if(count==8)
			{
				count=0;
			}
		}
		while(BY1==0);            //按键锁定,每按一次count+1
	}
}


move()
{
	a=temp<<count;
	b=temp>>(8-count);
	P1=a|b;
}
//带闪烁的移动
move1()
{
	a=temp<<count;
	b=temp>>(8-count);
	while(1)					  //进入死循环,按键按下时跳出
	{
	  P1=a|b;
	  delay10ms();				  //延时
	  P1=P1|0xff;				  //使得LED的状态相反
	  delay10ms();				  //延时
	  if(BY1==0)				  //按键按下的时候跳出循环,进入执行key()函数
	  break;
	}
	
}

main()
{
	count=0;
	temp=0xfe;
	P1=0xff;
	P1=temp;
	while(1)				//永远循环,扫描判断按键是否按下
	{
		key();			    //调用按键识别函数
		//move();			    //调用移动函数
		move1();		    //使用移动加闪烁函数
	}
}

Detailed explanation of move1() function:

//带闪烁的移动
move1()
{
	a=temp<<count;
	b=temp>>(8-count);
	while(1)					  //进入死循环,按键按下时跳出
	{
	  P1=a|b;
	  delay10ms();				  //延时
	  P1=P1|0xff;				  //使得LED的状态相反
	  delay10ms();				  //延时
	  if(BY1==0)				  //按键按下的时候跳出循环,进入执行key()函数
	  break;
	}
	
}

The previous assignment of a and b is to make the corresponding lights light up, for example :

When the button is pressed for the first time, count is assigned a value of 1, then a is equal to 0xfe(temp) shifted one bit to the left, then a is represented in binary as 1111 1100 , and b is represented in binary as: 0000 0001   , then the initial value of P1 is a|b, which is expressed in binary: 1111 1101   corresponds to the circuit diagram and the second light is on, with a delay of 10ms. The operation of P1=P1|0xff is to reverse the current light. In this reciprocating infinite loop, there will be a flickering effect, when a button is pressed, it will jump out of the loop, and execute other codes to achieve the experimental purpose.

Experiment Three

Based on the previous two experiments, it is not difficult to conclude that in the third experiment, the key function needs to be changed to realize the control of even a button, plus some code fine-tuning.

Experiment code:

#include<reg51.h>
sbit BY1=P3^4;	          //定义按键的输入端S建
sbit BY2=P3^5;
unsigned char count;      //按键计数,每按一次加1
unsigned char temp;
unsigned char a,b;


void delay10ms(void)		  //延时程序
{
	unsigned char i,j;
	for(i=20;i>0;i--)
	 for(j=248;j>0;j--);
}

key()						 //判断按键是否按下
{
 	if(BY1==0)				 //判断是否按下按钮
	{
		delay10ms();		 //延时去抖动
		if(BY1==0)			 //确认按键是否按下
		{
			count++;		 //按键计数加1
			if(count==8)
			{
				count=0;
			}
		}
		while(BY1==0);            //按键锁定,每按一次count+1
	}
}

key2()						 //判断按键是否按下
{
 	if(BY2==0)				 //判断是否按下按钮
	{
		delay10ms();		 //延时去抖动
		if(BY2==0)			 //确认按键是否按下
		{
			count++;		 //按键计数加1
			if(count==8)
			{
				count=0;
			}
		}
		while(BY2==0);            //按键锁定,每按一次count+1
	}
}
move()
{
	a=temp<<count;
	b=temp>>(8-count);
	P1=a|b;
	while(1)				     //加入循环使得这个状态持续
	{
	   if(BY1==0||BY2==0)				  //按键按下的时候跳出循环,进入执行key()函数
	   break;
	}

}
//带闪烁的移动
move1()
{
	a=temp<<count;
	b=temp>>(8-count);
	while(1)					  //进入死循环,按键按下时跳出
	{
	  P1=a|b;
	  delay10ms();				  //延时
	  P1=P1|0xff;				  //使得LED的状态相反
	  delay10ms();				  //延时
	  if(BY1==0||BY2==0)				  //按键按下的时候跳出循环,进入执行key()函数
	  break;
	}
	
}

main()
{
	count=0;
	temp=0xfe;
	P1=0xff;
	P1=temp;
	while(1)				//永远循环,扫描判断按键是否按下
	{
		key();			    //调用按键识别函数
		move();			    //调用移动函数
		key2();
		move1();		    //使用移动加闪烁函数
	}
}

The key2() function is basically the same as the key() function, but the most basic parameters are changed. The key difference from the above is that an infinite loop is added to the move function so that the bulb can be continuously lit without flickering, otherwise there will be a little problem.

Experiment summary

This experiment mainly explained how to control the on and off of our LED lights through an external button command, and realized the running of the water lamp. The experiment code is relatively simple, I believe you can see it thoroughly. After all, those who came in were not considered mortals, they were all masters.

Afterword

This experiment may be implemented in a simpler way. I hope to get the guidance of the big guys. Learning is to learn under the condition of mutual guidance and communication. I am a little white. If there is a mistake, please ask the big guys for guidance! ! ! Greatful! ! !

         

No pains No results

Guess you like

Origin blog.csdn.net/weixin_45629315/article/details/106348140