The single chip microcomputer (AT89C51) button controls the LED light to realize the running light and flashing running light

Publisher:CyberJoltLatest update time:2022-05-30 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Preface 

It's been a long time since I wrote about the microcontroller series. Without further ado, let's get straight to the point! The experiment I'm going to explain and share this time is a famous running light experiment, but this experiment is to achieve "running water" by clicking a button yourself, which is different from my blog (Simulation Experiment of Microcontroller (AT89C51) - Running Light and Flashing Lights One by One (Input and Output)). If you are interested in pure coding running lights, you can also read this article. I have also launched a series of microcontroller knowledge point summaries and experimental sharing: Microcontroller Encyclopedia, and those who are interested can also pay attention to it for subsequent learning.


Experimental requirements and objectives

Experiment 1: Each time you press the S1 key on the independent keyboard, one of the eight LEDs connected to the P1 port lights up and moves down one position.


Experiment 2: When powered on, the LED connected to the P1.0 pin of L1 flashes. Pressing the switch once causes the next light to flash and light up, and the cycle continues.


Experiment 3: There are different effects when different switch button operations are implemented. I designed an experiment to achieve different effects when two different buttons are pressed. Combining the above two experiments, I obtained that when the first button is pressed, it jumps to the next LED light normally (without flashing), and when the second button is pressed, it jumps to the next LED light and flashes.


Experimental Circuit Diagram 

This experiment mainly consists of a single-chip microcomputer, a light-emitting diode, and a 1K resistor. The circuit is as follows:

experiment procedure


experiment one

Experimental analysis: During the experiment, we need to pay attention to writing functions in two parts: a key() function that identifies whether a key is pressed and a move() function that executes the water light loop. In the key function, there is a global variable that increments itself, mainly for the shift operation in the move function.


Experiment code:


#include

sbit BY1=P3^4;    //Define the input terminal S of the key

unsigned char count; //Key count, each press adds 1

unsigned char temp;

unsigned char a,b;

 

 

void delay10ms(void)   //delay program

{

unsigned char i,j;

for(i=20;i>0;i--)

for(j=248;j>0;j--);

}

 

key() // Check if the button is pressed

{

  if(BY1==0) //Judge whether the button is pressed

{

delay10ms(); //delay de-jitter

if(BY1==0) //Confirm whether the button is pressed

{

count++; //key count plus 1

if(count==8)

{

count=0;

}

}

while(BY1==0); //Key lock, count+1 each time you press it

}

}

 

move()

{

a=temp< b=temp>>(8-count);

P1=a|b;

}

 

main()

{

count=0;

temp=0xfe;

P1=0xff;

P1=temp;

while(1) //Loop forever, scan to see if the key is pressed

{

key();     //Call key recognition function

move();     //Call the move function

}

 

}


The screenshots don't show the dynamic effects, but the videos can't be uploaded. Here I suggest that CSDN modify the mechanism so that we can upload our own recorded videos, and care about us niche bloggers.


Experiment 2

The difference between Experiment 2 and Experiment 1 is that we need to make the light flash. The switch shifting operation is exactly the same as the previous experiment. We just need to modify the move() function. For the sake of comparison, I rewrote the move function into move1() function.


The complete code of the experiment:


#include

sbit BY1=P3^4;    //Define the input terminal S of the key

unsigned char count; //Key count, each press adds 1

unsigned char temp;

unsigned char a,b;

 

 

void delay10ms(void)   //delay program

{

unsigned char i,j;

for(i=20;i>0;i--)

for(j=248;j>0;j--);

}

 

key() // Check if the button is pressed

{

  if(BY1==0) //Judge whether the button is pressed

{

delay10ms(); //delay de-jitter

if(BY1==0) //Confirm whether the button is pressed

{

count++; //key count plus 1

if(count==8)

{

count=0;

}

}

while(BY1==0); //Key lock, count+1 each time you press it

}

}

 

 

move()

{

a=temp< b=temp>>(8-count);

P1=a|b;

}

//Move with flashing

move1()

{

a=temp< b=temp>>(8-count);

while(1)   //Enter an infinite loop and exit when a key is pressed

{

  P1=a|b;

  delay10ms();   //delay

  P1=P1|0xff;   // Make the LED state opposite

  delay10ms();   //delay

  if(BY1==0)   //When the key is pressed, it will exit the loop and execute the key() function

  break;

}

}

 

main()

{

count=0;

temp=0xfe;

P1=0xff;

P1=temp;

while(1) //Loop forever, scan to see if the key is pressed

{

key();     //Call key recognition function

//move();     //Call the move function

move1();     //Use the move and flash function

}

}

Detailed explanation of move1() function:


//Move with flashing

move1()

{

a=temp< b=temp>>(8-count);

while(1)   //Enter an infinite loop and exit when a key is pressed

{

  P1=a|b;

  delay10ms();   //delay

  P1=P1|0xff;   // Make the LED state opposite

  delay10ms();   //delay

  if(BY1==0)   //When the key is pressed, it will exit the loop and execute the key() function

  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 to 1, so a is equal to 0xfe(temp) shifted left by one bit, so a is represented by 1111 1100 in binary, and b is represented by 0000 0001 in binary, so the initial value of P1 is a|b, represented by 1111 1101 in binary. Corresponding to the circuit diagram, the second light is on, with a delay of 10ms, and the operation of P1=P1|0xff is to reverse the currently on light. In this way, in an endless loop, a flashing effect will appear. When a button is pressed, the loop will be jumped out, and other codes can be executed to achieve the purpose of the experiment.


Experiment 3

Combining the previous two experiments, it is not difficult to conclude that the third experiment requires changing the key function to control two buttons, plus some code fine-tuning.


Experiment code:


#include

sbit BY1=P3^4;           //Define the input terminal S of the key

sbit BY2=P3^5;

unsigned char count; //Key count, each press adds 1

unsigned char temp;

unsigned char a,b;

 

 

void delay10ms(void)   //delay program

{

unsigned char i,j;

for(i=20;i>0;i--)

for(j=248;j>0;j--);

}

 

key() // Check if the button is pressed

{

  if(BY1==0) //Judge whether the button is pressed

{

delay10ms(); //delay de-jitter

if(BY1==0) //Confirm whether the button is pressed

{

count++; //key count plus 1

if(count==8)

{

count=0;

}

}

while(BY1==0); //Key lock, count+1 each time you press it

}

}

 

key2() // Check if the button is pressed

{

  if(BY2==0) //Judge whether the button is pressed

{

delay10ms(); //delay de-jitter

if(BY2==0) //Confirm whether the button is pressed

{

count++; //key count plus 1

if(count==8)

{

count=0;

}

}

while(BY2==0); //Key lock, count+1 each time you press it

}

}

move()

{

a=temp< b=temp>>(8-count);

P1=a|b;

while(1)      //Add a loop to make this state continue

{

   if(BY1==0||BY2==0)   //When a key is pressed, it will exit the loop and execute the key() function

   break;

}

 

}

//Move with flashing

move1()

{

a=temp< b=temp>>(8-count);

while(1)   //Enter an infinite loop and exit when a key is pressed

{

  P1=a|b;

  delay10ms();   //delay

  P1=P1|0xff;   // Make the LED state opposite

  delay10ms();   //delay

  if(BY1==0||BY2==0)   //When a key is pressed, it will exit the loop and execute the key() function

  break;

}

}

 

main()

{

count=0;

temp=0xfe;

P1=0xff;

P1=temp;

while(1) //Loop forever, scan to see if the key is pressed

{

key();     //Call key recognition function

move();     //Call the move function

key2();

move1();     //Use the move and flash function

}

}


The key2() function is basically the same as the key() function, except for the most basic parameters. The main difference is that an infinite loop is added to the move function so that the light bulb can be continuously lit when it is not flashing, otherwise there will be some minor problems.


Experimental Summary

This experiment mainly explains how to control the on and off of our LED lights through an external button command, and realizes the operation of the running light. The experimental code is relatively simple, and I believe you can understand it thoroughly. After all, those who come in are not ordinary people, but masters.

Keywords:MCU Reference address:The single chip microcomputer (AT89C51) button controls the LED light to realize the running light and flashing running light

Previous article:Detailed explanation of the interrupt system of the single-chip microcomputer (AT89C51) and the application experiment of the interrupt system
Next article:Single chip microcomputer (AT89C51) peripheral I/O input and output experiment

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号