These LED simulation experiments take you to play 51 single-chip computer

Keywords: Single-Chip Microcomputer Proteus

Let's first see what we need to do:

  1. Light up an LED lamp with 51 single-chip computer
  2. Using 51 Single-chip Computer to Realize Horselight
  3. Using 51 Single-chip Computer to Realize Streaming Light
  4. Using 51 Single-chip Computer to Realize Pattern Lamp
  5. Heart LED Display Using 51 Single-chip Computer

The above five experiments are simulated by Proteus. This paper will describe how to play LED on Proteus step by step.
The download paths for the Keil and Proeus projects for all the experiments are placed at the end of the article and you can download them for free to play with.

The following pictures are illustrations of the experiment. Of course, they are detailed in the body section, so you can skip over here).
Pattern lamps:

Horselight:

Heart lamp:

1.Light up an LED

The first experiment is very simple, mainly for those students who are weak in the single-chip computer. If you are not very clear about the concept of LED, you can refer to this article. Getting Started with Hardware - Understanding LED It gives a brief introduction to the characteristics of LED.

Code

The code for lighting is fairly simple. sbit led = P2 ^ 0 defines the port of P2.0 as led, so long as the LED is assigned 0 or 1, the level of P2.0 can be controlled.
Since I connected the LED negative to the P2.0 of the single chip computer and the positive to the VCC (5V), the P2.0 IO pin must be set low in order for the light to go on.

#include "reg52.h" //This file defines some special function registers for single-chip computers

sbit led = P2 ^ 0;	   //Define the P2.0 port of single-chip computer as led

/*******************************************************************************
* Function name: main
* Functional Functions: Main Functions
* Input: None
* Output: None
*******************************************************************************/
void main()
{
	led = 0;	//P2.0 port set to low level
	while(1)
	{
						
	}		
}

Proteus simulation

Below is the complete circuit diagram. As the first experiment, I will tell you a little more about it. Here is a brief description of how to find these components and how to import the MCU program into Proteus MCU.

Proteus Common Device Name (for this experiment)

The Proteus version I am currently using is proteus 8.9 sp2, which has many downloads and is easy to crack (for learning only).
There are two ways to open the Proteus device library:

After opening, there are the following main contents:

Some common devices, such as Resistors, Capacitors, Transistors, Inductors, Diodes, and Microprocessor ICs, can be found in the category options box.
The keywords for the devices used in this paper (except resistive capacitance) are:

deviceKey word
SinglechipAT59C52
KeyBUTTON
Crystal oscillatorCRYSTAL
LEDLED-RED(LED-Color)
ExclusionRESPACK-8

In addition to the devices, there are also power (POWER) and ground (GROUND), which can be found in the following figure:

How Proteus Imports Single-chip Computer Program

The precondition of importing MCU program is that there are already binary (or hex) program files, double-click the Proteus MCU, or right-click it to select editing properties, then click the program file selection button to select the.Hex file of MCU program:

At this time, the program has been "burned" into the single-chip Flash, click the simulation button in the lower left corner to start the simulation.

Simulation effect

This is the basic part. Here is the simulation. The red dot indicates high level and the blue dot indicates low level.

2.horse race lamp

Running horse lights and running water lights may not be distinguishable, there is a lot of controversy, and I don't go into these too, so as I understand them, I call them running horses when they are on and off and running water when they are on and off.

Code

Here's the code for the street lamp. I've commented on almost every line, so I don't have to explain much. Simulate it directly:

/******************************************************************************
 * horse race lamp
 * P2.0 - P2.7 One LED after another
 ******************************************************************************/

#include "reg52.h"//This file defines some special function registers for single-chip computers
#Include <intrins.h> //Add this header file because the left-right shift function is used

typedef unsigned int u32;	  //Declarative definition of data types
typedef unsigned char u8;

#define led P2 //After defining a P2 port as led, you can use led instead of a P2 port

/*******************************************************************************
* Function name: delay
* Functional function: Delay function, i=1, approximately 10us delay
*******************************************************************************/
void delay(u32 i)
{
	while(i--);	
}

/*******************************************************************************
* Function name: main
* Functional Functions: Main Functions
* Input: None
* Output: None
*******************************************************************************/
void main()
{
	u8 i = 0;
	led = ~0x01;  //LED low level lights up, so reverse
	delay(50000); //Delay of approximately 450ms	
	while(1)
	{	
		/* 8 Circular lights on and off */
		for(i = 0; i < 8; i++) 
		{
			led = ~(0x01 << i);	 //Move 1 left by i bit and assign the result to P2 port, which is the LED with the switch point bright
			delay(35000);        //Rough delay, custom duration, now about 350ms
		}
	}		
}

Proteus simulation

3.Streaming light

Code

Here is the code for the running light, similar to the code for the horse-running light:

/******************************************************************************
 * Streaming light
 * P2.0 - P2.7 One LED after another
 ******************************************************************************/

#include "reg52.h"//This file defines some special function registers for single-chip computers
#Include <intrins.h> //Add this header file because the left-right shift function is used

typedef unsigned int u32;	  //Declarative definition of data types
typedef unsigned char u8;

#define led P2 //After defining a P2 port as led, you can use led instead of a P2 port

/*******************************************************************************
* Function name: delay
* Functional function: Delay function, i=1, approximately 10us delay
*******************************************************************************/
void delay(u32 i)
{
	while(i--);	
}

/*******************************************************************************
* Function name: main
* Functional Functions: Main Functions
* Input: None
* Output: None
*******************************************************************************/
void main()
{
	u8 i = 0;
	led = 0xff;  //IO is high, LED is off
	delay(50000); //Delay of approximately 450ms	
	while(1)
	{	
		/* 8 Circular lights on and off */
		for(i = 0; i < 8; i++) 
		{
			/* Move 0 left to i bit, and then with P2 bit by bit, that is, light up the next LED, leaving the LED illuminated in front unchanged */
			led = led & ~(0x01 << i);	 
			delay(35000);        //Rough delay, custom duration, now about 350ms
		}
		led = 0xff;   //Turn off all LED s
	}		
}

Proteus simulation

4.Pattern lamp

There's nothing more defining a pattern lamp than how it should be spent.

Code

Here's my code, because I'm taking care of beginners, and I'm lazy, delaying or using a while loop. Key detection is also placed in the main function's polling (while(1). Because there is a delay in the main function (100ms-600ms)So sometimes pressing a key does not respond. The program can only react completely if the key is pressed for more than 600 ms, but this experiment is just a demonstration. These are minor problems that can be solved by adding an interrupt (but only 2 external interrupts are available for 51.). The code is a bit confusing, just for reference (freely written).😁),Interpretations are in notes, [Functional introduction in simulation section]

/******************************************************************************
 * Patterned LED
 * P2.0 - P2.7 One LED after another
 * P1.0 - P1.3 Corresponds to K1-K4 four keys
 ******************************************************************************/

#include "reg52.h"//This file defines some special function registers for single-chip computers
#Include <intrins.h>//Add this header file because the left-right shift function is used

typedef unsigned int u32;	  //Declarative definition of data types
typedef unsigned char u8;

#define led P2 //After defining a P2 port as led, you can use led instead of a P2 port

sbit k1 = P1 ^ 0;
sbit k2 = P1 ^ 1;
sbit k3 = P1 ^ 2;
sbit k4 = P1 ^ 3;	 //Define key port

//All variables
u8 LED_Speed = 4; //LED switching speed, 100ms units, default 5x100ms, adjustable range (100ms-600ms, not allowed above 700ms) [coarse delay]
u8 LED_Mode = 0; //LED mode, 0: Full bright; 1: Full out; 2: Running horse light; 3. Running water light; 4. Mirrored running horse light (half-run horse)
										 //5: mirror running water lamp; 6;Auto-switch (4 modes in turn)
u8 LED_Auto_Flag = 0; //Automatic switch mode open flag, 1 means open
u8 i = 0;             //Global loop variable, 0-7 for 8 LED s

/*******************************************************************************
* Function name: delay
* Functional function: Delay function, i=1, approximately 10us delay
*******************************************************************************/
void delay(u32 i)
{
	while(i--);	
}

/*******************************************************************************
* Function name: Key_Process()
* Function Function: Key Processing Function
* Input: None
* Output: None
*******************************************************************************/
void Key_Process()
{
	if(k1 == 0) //K1 is pressed to switch display mode
	{
		delay(1000);  //Dithering
		if(k1 == 0)
		{
			if(LED_Mode >= 1) //Not all bright or all extinct
			{
				LED_Mode++; //Switch LED display mode
				if(LED_Mode > 6)
					LED_Mode = 2; //Back to Horselight Mode
			}
			else  			//LED is in full light or out state
				LED_Mode = 2;
			if(LED_Mode != 6) //Turn off automatic switch mode if mode is manually modified
			{
				LED_Auto_Flag = 0;
			}
			if(LED_Auto_Flag == 1) //If the current mode is automatic, go directly back to the first mode
				LED_Mode = 2;
		}
		while(!k1);
	}
	if(k2 == 0) //K2 is pressed to speed up LED blinking (e.g. horse-running lights run faster)
	{
		delay(1000);  //Dithering
		if(k2 == 0)
		{
			if(LED_Speed > 1) //Minimum is 1x100ms (100ms)
				LED_Speed--;
		}
		while(!k2);
	}
	if(k3 == 0) //K3 is pressed to slow down LED blinking (e.g. running a horse-running light more slowly)
	{
		delay(1000);  //Dithering
		if(k3 == 0)
		{
			if(LED_Speed < 6) //Maximum 6x100ms (600ms)
				LED_Speed++;
		}
		while(!k3);
	}
	if(k4 == 0) //K4 is pressed to turn off all LEDs and light up all LEDs if they are already turned off
	{
		delay(1000);  //Dithering
		if(k4 == 0)
		{
			if(LED_Mode == 1)
				LED_Mode = 0;  //Full Bright
			else 
				LED_Mode = 1;  //Total extinction
		}
		while(!k4);
	}		
}


/*******************************************************************************
* Function Name: Led_Show
* Functional Functions: LED Display
* Input: None
* Output: None
*******************************************************************************/
void Led_Show()
{
	u8 tmp_L = 0;
	u8 tmp_H = 0;
	if(LED_Mode == 0) //Full Light Mode
	{
		led = 0x00;  //LED IO low level light, LED full light
	}
	else if(LED_Mode == 1) //Total extinction mode
	{
		led = 0xff;  //LED IO High Level Off, LED Full Off
	}
	else if(LED_Mode == 2) //Running Horselight Mode (changes the status of only one lamp)
	{
		led = ~(0x01 << i);	     //Move 1 left by i bit and assign the result to P2 port, which is the LED with the switch point bright
	}
	else if(LED_Mode == 3) //Streaming light mode (changes the state of only one light)
	{
		/* Move 0 left to i bit, and then with P2 bit by bit, that is, light up the next LED, leaving the LED illuminated in front unchanged */
		led = led & ~(0x01 << i);	 
	}
	else if(LED_Mode == 4) //Mirror running lights, lights 0-3 run in 0-3 order, lights 4-7 run in 7-4 order
	{
		if(i >= 4) //Two lights are controlled at the same time, so only four cycles are needed
			i -= 4;
		tmp_L = ~(0x01 << i); //Select one of lights 0-3 to light up
		tmp_H = ~(0x80 >> i); //Select one of lights 7-4 to light up
		led = (tmp_L & 0x0f) | (tmp_H & 0xf0);
	}
	else if(LED_Mode == 5) //Mirror running light, lights 0-3 run in 0-3 order, lights 4-7 run in 7-4 order P2 = P2 & ~ (0x01 < < i);	 
	{
		if(i >= 4) //Two lights are controlled at the same time, so only four cycles are needed
		{
			i -= 4;
			led = 0xff; //Streaming light mode performs a complete extinction after a cycle
		}
		/* Move 0 left to i bit, and then with P2 bit by bit, that is, light up the next LED, leaving the LED illuminated in front unchanged */
		led = led & ~(0x01 << i); 
		/* Move 0 to the right by i bit, and then with P2 bit by bit, that is, light up the next LED, leaving the LED illuminated in front unchanged */
		led = led & ~(0x80 >> i); 
	}
	else if(LED_Mode == 6)
	{
		LED_Auto_Flag = 1; //Turn on automatic switch mode
		i = 0;
		led = 0xff; //Initialize LED status
	}

	/* If automatic switch mode is turned on */
	if(LED_Auto_Flag == 1 && i == 0) 
	{
		if(LED_Mode > 1) //Not all bright or all extinct
		{
			LED_Mode++; //Switch LED display mode
			if(LED_Mode > 5)
				LED_Mode = 2; //Back to Horselight Mode
		}
		else  			//LED is in full light or out state
		{ 
						//No Operation
		}            
	}
}

/*******************************************************************************
* Function name: main
* Functional Functions: Main Functions
* Input: None
* Output: None
*******************************************************************************/
void main()
{
	led = 0x00;  //LED low level light, full light after power on
	delay(50000); //Delay of approximately 450ms
	
	while(1)
	{	
		Key_Process();  //Key Processing
		Led_Show();     //LED status detection plus display
		i++;  			//Switch the LED to be controlled
		delay(LED_Speed * 10000);    //Rough delay, custom duration
		if(i >= 8) 
		{
			i = 0;
			if(LED_Mode == 3)
				led = 0xff; //Streaming light mode performs a complete extinction after a cycle
		}
		
	}		
}

Proteus simulation

Mode Switching

This program has four main modes:

  1. Running Horselight Mode
  2. Streaming light mode
  3. Mirror Running Horselight
  4. Mirror Streaming Light
  5. Automatic mode (loop the four modes above)

The following are simulation diagrams of these modes:

Horselight mode:

Streaming light mode:

Mirror Running Light Mode:

Mirror Streaming Light Mode:

Automatic switch mode:

Speed Switching

There are two keys in the program to control the "flow speed" of a running light, etc.

Acceleration:

Slow down:

Turn off/on all LED s

At any time, you can turn off all LEDs by pressing the K4 key, and then press K4 again to turn on all LEDs.

5.Heart lamp

If you are interested in heart-shaped lamps, you can find out about them.

Code

The code is for reference only, with comments on each line:

/******************************************************************************
 * horse race lamp
 * P0.0 - P0.7 One LED after another
 * P2.0 - P2.7  LED
 * P3.0 - P3.7  LED
 ******************************************************************************/

#include "reg52.h"//This file defines some special function registers for single-chip computers
#Include <intrins.h> //Add this header file because the left-right shift function is used

typedef unsigned int u32;	  //Declarative definition of data types
typedef unsigned char u8;

#define led1 P0 // After defining P0 port as led1, you can use LED1 instead of P0 port
#define led2 P2
#define led3 P3

/*******************************************************************************
* Function name: delay
* Functional function: Delay function, i=1, approximately 10us delay
*******************************************************************************/
void delay(u32 i)
{
	while(i--);	
}

/*******************************************************************************
* Function name: main
* Functional Functions: Main Functions
* Input: None
* Output: None
*******************************************************************************/
void main()
{
	u8 i = 0;
	u8 tmp_H = 0;
	u8 tmp_L = 0;
	
	led1 = 0x00;  //LED low level lighting
	led2 = 0x00;
	led3 = 0x00;
	delay(50000); //Delay of approximately 450ms	
	led1 = ~0x00;  //LED low level lights up, so reverse
	led2 = ~0x00;
	led3 = ~0x00;

	
	while(1)
	{	
		/* 8 Circular lights on and off */
		for(i = 0; i < 8; i++) 
		{
			led1 = ~(0x01 << i);	 //Move 1 left by i bit and assign the result to port P0, which is the LED with the switch point bright
			delay(10000);        //Rough delay, custom duration, about 100 ms
		}
		led1 = 0xff;
		/* 8 Circular lights on and off */
		for(i = 0; i < 8; i++) 
		{
			led2 = ~(0x01 << i);	 //Move 1 left by i bit and assign the result to P2 port, which is the LED with the switch point bright
			delay(10000);        //Rough delay, custom duration,
		}
		led2 = 0xff;
		/* 8 Circular lights on and off */
		for(i = 0; i < 8; i++) 
		{
			led3 = ~(0x01 << i);	 //Move 1 left by i bit and assign the result to port P3, which is the LED with the switch point bright
			delay(10000);        //Rough delay, custom duration,
		}
		led3 = 0xff;
		
		/* 24 Each lamp turns on and off (eight lights), and the running water lamp turns on and off. */
		for(i = 0; i < 24 + 8; i++)
		{
			if(i < 8)
			{
				/* Move 0 left by i bit, and then with P0 bit by bit, that is, light up the next LED, keep the status of the LED illuminated in front unchanged */
				led1 = led1 & ~(0x01 << i);
				delay(10000);
			}
			if(i >= 8 && i < 16)
			{
				/* Move 1 left to i bit, and then press or with 20 bit, that is, light up the next LED, leaving the LED illuminated in front unchanged */
				led1 = led1 | 0x01 << (i - 8); 	
				/* Move 0 left to i bit, and then with P2 bit by bit, that is, light up the next LED, leaving the LED illuminated in front unchanged */
				led2 = led2 & ~(0x01 << (i - 8)); 
				delay(10000);
			}
			if(i >= 16 && i < 24)
			{
				/* Move 1 left to i bit, and then either with P2 bit or, that is, light up the next LED, leaving the LED illuminated in front unchanged */
				led2 = led2 | 0x01 << (i - 16); 	
				/* Move 0 left by i bit and then with P3 bit by bit, that is, light up the next LED, keep the status of the LED illuminated in front unchanged */
				led3 = led3 & ~(0x01 << (i - 16)); 
				delay(10000);
			}
			if(i >= 24)
			{
				/* Move 1 left by bit i, and then press or P3, that is, light the next LED, leaving the LED illuminated in front unchanged */
				led3 = led3 | 0x01 << (i - 24); 	
				delay(10000);
			}
		}
			
		/* The troops are divided into two routes */
		for(i = 0; i < 12; i++)
		{
			if(i < 8)
			{
				led1 = ~(0x01 << i);	 //Move 1 left by i bit and assign the result to port P0, which is the LED with the switch point bright
				led3 = ~(0x80 >> i);     //Move 0x80 right by i bit
				delay(10000);        //Rough delay, custom duration,
			}
			if(i >= 8)
			{
				led1 = 0xff;  //Extinction
				led3 = 0xff;
				tmp_L = ~(0x01 << (i - 8)); //Select one of the lights 0-3 of LED 2 to light up
				tmp_H = ~(0x80 >> (i - 8)); //Select one of lights 7-4 to light up
				led2 = (tmp_L & 0x0f) | (tmp_H & 0xf0);
				delay(10000);        //Rough delay, custom duration,
			}
		}
		led2 = 0xff;
	}		
}

Proteus simulation

All the above code projects and Proeus projects have been source to the code cloud and GitHub at the following addresses:
Gitee: https://gitee.com/huang-xiaohui/led_test
GitHub:https://github.com/xiaohuisuper/led_test

Posted by lostprophetpunk on Tue, 05 Oct 2021 10:16:32 -0700