当前位置:网站首页>Introduction to 51 single chip microcomputer - LED light

Introduction to 51 single chip microcomputer - LED light

2022-06-22 06:46:00 In the morning, the rain shines

1. LED

1.1. What is? LED?

LED(light-emitting diode), Light emitting diodes , Be commonly called LED Small lights , It is made of gallium phosphide (GaP) And other semiconductor materials 、 A luminous display device that can directly convert electric energy into light energy . When LED There is a... Inside When the constant current passes , It will glow , Different LED Can emit different colors of light , Common ones are red 、 Yellow, etc , Pictured :
 Insert picture description here

About LED The outline features mainly describe the following points .
(1) monochrome LED The color of the shell indicates its luminous color .LED The shell of is transparent .
(2) monochrome LED Only two pins , These two pins are positive 、 Negative pole . Multicolor LED by 3 Root pin .
(3)LED The appearance is very distinctive , So you can easily identify LED.

1.2.LED The type of

 Insert picture description here

1.3.LED Main parameters of

  1. Electrical parameters
    (1) Forward operating current IF. It refers to the forward current value when the LED normally emits light . The working current of LED is generally 10~20mA.
    (2) Forward working voltage UF. It is the forward working voltage at both ends of the LED under a given forward current . Usually in IF=20mA When measuring , The forward working voltage of the LED is 1.4~3V . When the outside temperature rises , The forward operating voltage of the LED will drop .
    (3) v - Safety features . It refers to the LED voltage And current .

  2. Limit parameters
    (1) Allowable power consumption Pm. It is the maximum value of the product of the forward DC voltage applied to both ends of the LED and the current flowing through it , When this value is exceeded, the LED is heated 、 damage .
    (2) Maximum forward DC current IFm. It is the maximum forward DC current allowed , Exceeding this value will damage the diode .
    (3) Maximum reverse voltage URm. It is the maximum reverse voltage allowed , Beyond this value, the LED may be hit Wear damage .
    (4) The work environment topm. It is the ambient temperature range in which the LED can work normally . Below or above this temperature range , The LED will not work properly , The efficiency is greatly reduced .

1.4.LED The main characteristics of

  1. v - Safety features
    LED And the voltage of ordinary diode - Safety characteristics are similar , It's just LED The forward conduction voltage of is large . Small current LED The reverse breakdown voltage of is very small , by 6V To more than ten volts , Smaller than ordinary diodes . The picture below shows LED Positive volt - Safety characteristic curve .
     Insert picture description here

The picture below is LED v - Safety characteristic curve , It contains forward and reverse characteristics .LED It has the input voltage similar to the general semiconductor triode - Safety characteristic curve .
 Insert picture description here
About volt - Each section of the safety characteristic curve is described as follows .
(1)OA paragraph . This is a positive dead band .UA For opening LED Luminous voltage .
(2)AB paragraph . This is the workspace . In this section , Generally, as the voltage increases, the current also increases , The luminous intensity also increases .

In this section, special attention should be paid to , Without any protection , When the forward voltage increases to a certain value , that LED Forward voltage decrease of , And the forward current increases . If there is no protection circuit , It will burn out due to the increase of current LED.

(3)OC paragraph . This is the reverse deadband , Led plus reverse voltage does not emit light ( Don't work ), But there is a reverse current . This reverse current is usually very small , Generally within a few microamps . stay 1990~1995 year , The reverse current is set as 10μA; stay 1996~2000 year , The reverse current is set as 5μA, At present, the reverse current is generally at 3μA following , But basically 0μA.
(4)CD paragraph . This is the reverse breakdown region ,LED The reverse voltage of one Do not exceed 10V, The maximum shall not exceed 15V. Over this voltage , Reverse breakdown will occur , Led scrapping .

In this section ,LED There is a large reverse breakdown current ( A few milliamps ). This breakdown is not a thermal breakdown , No damage , Some AC outlets 、 The AC indicator light on the AC power switch uses LED, During the negative half cycle of alternating current LED Is working in this section .

  1. Forward and reverse resistance characteristics
    LED The forward and reverse resistances are much larger than those of ordinary diodes , Understanding this point has important guiding significance for detecting diodes .
  2. Relation between working current and relative luminous intensity
    The following figure shows the characteristic curve of the relationship between the working current and the relative luminous intensity of the LED . about Red LED for , When the forward working current increases, the relative luminous intensity also increases , When the working current reaches a certain level , The curve flattens ( saturated ), It indicates that the relative intensity of luminescence tends to be saturated ; about green LED for , The working current increases , The relative intensity of luminescence increases , But there is no saturation .
     Insert picture description here
  3. Relationship between luminous intensity and ambient temperature
    The following figure shows the characteristic curve of the relationship between the luminous intensity of the LED and the ambient temperature . The lower the temperature , The greater the luminous intensity . When the ambient temperature rises , The luminous intensity will decrease significantly .
     Insert picture description here
    5. Relationship between maximum allowable working current and ambient temperature
    The following figure shows the characteristic curve of the relationship between the maximum allowable working current and the ambient temperature . When the ambient temperature reaches a certain level , The maximum allowable operating current decreases rapidly , The result is zero , It means that when the ambient temperature is high ,LED Easier to damage , This is also LED Fear of scalding .
     Insert picture description here
    6. Other characteristic curves

 Insert picture description here

1.5.LED Pin polarity identification method

In order not to affect LED Normal lighting of , The model and polarity are not marked on the housing . So identify LED just 、 The negative pin mainly depends on the shape characteristics and the detection of the multimeter .

  1. Pin length identification method
    The following figure shows the difference between positive and negative by pin length 、 Negative polarity pin LED, Its two pins are long and short , The long one is the positive pole , The short one is the negative pole .
     Insert picture description here
  2. Identification method of protruding key mode
    As shown in the figure below, the protruding key method indicates the positive pin method ,LED There is a protruding key on the base , by The nearest pin of this key is positive . Insert picture description here
  3. 3 Root pin LED Pin identification method
    The following figure shows a kind of 3 Root pin LED Pin distribution rule and internal circuit diagram . There are two different Color LED .K Is a common pin . Insert picture description here
    The figure below shows another 3 Discoloration of root pin LED Schematic diagram of pin identification method , It has a tab , According to this feature, the pins can be easily determined .
     Insert picture description here
  4. 6 Root pin LED Pin identification method
    The picture below shows 6 Root pin LED Pin distribution rule and internal circuit diagram . There are two groups in it 3 Root pin LED.
     Insert picture description here

1.6. Voltage control type and flashing type LED

  1. Voltage control type LED
    LED It is a current control device , That is, it will glow when an electric current flows , When in use, the current limiting resistor with appropriate resistance shall be connected in series , For the convenience of use LED And current limiting resistor , When in use, it can be directly connected to both ends of the power supply , Thus, a voltage control type is formed LED. The following figure shows the voltage control type LED Physical diagram and internal circuit . Voltage control type LED The luminous color of has red 、 yellow 、 Green, etc , The operating voltage has 5V、9V、12V、18V、 19V、24V common 6 Species specification , What is commonly used is BTV series .
     Insert picture description here

  2. Scintillation type LED
    Scintillation type LED It's a kind of CMOS Integrated circuits and LED Special light emitting devices composed of . The following figure shows a flashing type LED Physical diagram and internal circuit , It can be used for alarm indication and undervoltage 、 Overpressure indication, etc .
     Insert picture description here
    The following figure shows the flashing type LED Internal circuit block diagram . Scintillation type LED When use , No need to connect other components , As long as the appropriate DC working voltage is applied to both ends of its pin (5V) It will flash , The commonly used scintillation LED is BTS series . Insert picture description here

2. MCU control LED

Wrote so much LED Introduce , We are right. LED Should have a clear understanding , Now we begin to program the single chip microcomputer to control LED.

2.1. Light up a LED

2.1.1 Programming

First of all we have Proteus 8 Professional Build a circuit schematic diagram as shown in the figure
 Insert picture description here

 In the picture R1 Current limiting resistance , Prevent excessive current from causing LED damage .

Then start writing programs . If you've learned c The language of words , It should be easy to program , It doesn't matter if you haven't learned it , Copy first .

#include<reg52.h> //  Header file containing special function register definitions 

sbit LED = P2^3 ;     //  Bit address declaration  , Be careful :sbit  Must be in lowercase  ,P Capital must be written. 

void main()			  //  Any one of them c There must be only one program  main  function 
{
    					  // {}  In pairs , Here is the beginning and end of the program 
	LED = 0 ;		  //  A semicolon indicates the end of a statement ,"1" High level ,"0"  Indicates low level 
}

First, analyze the syntax of the program :
(1)main Is the function name of the main function , Every c There must be only one program main function .
(2)void Is the return type of the function , This program has no return value , use void Express .
(3){} Here are the beginning and end of the function , Do not omit .
(4) Every one of them c The statement is semicolon (;) end .
Logically , This is how the program is written , however There will be a problem in the practical application of single chip microcomputer . For example, the program space can accommodate 1000 Line code , But the actual program only uses 100 Line code , When the operation is finished 100 That's ok , While continuing to run , The first 101 Line program is not the program you want to run , It's an unknown thing , Once executed, the program may make mistakes , This may cause the single chip microcomputer to reset automatically , So you usually add an dead loop to your program , Let the program stay in the desired state , Don't run around , There are two ways to refer to :
Method 1 :

#include<reg52.h>

sbit LED = P2^3 ;

void mian()
{
    
	while(1)
	{
    
		LED = 0 ;
	}
}

Method 2 :

#include<reg52.h>

sbit LED = P2^3 ;

void mian()
{
    
	LED = 0 ;
	while(1) ;
}

Program (1) The function of is to execute repeatedly "LED = 1" This sentence , Program (2) The function of is to execute "LED = 1" Then stop and wait .

2.1.2 Compilation of program

After programming , The program should be compile , Generate .hex The file can burn the program into the MCU , Before compiling , You need to check an option first , Click Project (Project)-> Target “ The goal is 1” set an option …(Options for Target ‘Target1’…) Pictured :
 Insert picture description here
Or click the shortcut icon in the box as shown in the figure
 Insert picture description here
In the pop-up dialog , single click Output Options page , Check one of the Create HEX File , Then click OK (OK) button , Pictured :
 Insert picture description here
Once you set it up , Click Project (Project)-> compile (rebuild all target files) , Or click the shortcut icon shown in the following figure  Insert picture description here
After compiling , stay Keil Output below (Output) The corresponding prompt will appear in the window , Pictured : Insert picture description here
data = 9.0, It refers to that the program uses the internal 256 byte RAM In resources 9 Bytes .
code = 18 It means to use 8K Code Flash In resources 18 Bytes .
When prompted “ 0 Error(s) ,Warning(s) ” Indicates that the program has no errors and warnings , Will appear “ creating hex file from “LED”… ”, This means that a is generated from the current project HEX file , This is what you want to download to the MCU HEX file . If there are errors and warnings , Namely Error and Warning No 0, Then check the program , Found the problem , After the problem is solved, compile and generate HEX Can only be .
Here we are , The program compiles successfully , After that, the program is burned into the MCU .

2.1.3. Program burning

If you don't have hardware, you can use Proteus 8 Professional To simulate the program .
stay Proteus 8 Professional Double click the MCU that needs to burn the program , Click on the folder icon , Found compiled . hex file , Click to open , Pictured : Insert picture description here
 Insert picture description here
 Insert picture description here
Then you can start the simulation .

If you have a small partner with hardware equipment , have access to STC-ISP ——STC Series SCM download software , Burn the program into the development board , Pictured : Insert picture description here
Be careful : because STC Series MCU needs cold start download ( Click download first , Then supply power to the single chip computer ), So turn off the power switch on the development board first , Then click download (Download) Button , Then power on the development board , You can burn the program into the MCU . Insert picture description here
The above figure shows that the successful operation means that the program has been burned into the single-chip microcomputer .
 Insert picture description here

The figure above shows the simulation results in the simulation software .
thus , Light up a LED The experiment is finished .

2.2. Flashing light

stay 2.1. Light one in the LED Is to make “LED = 0” that will do , Put out one LED The procedure is also very simple “LED = 1”. Now it will go out and light up , So how to make LED Flashing ? It's also very simple. , Delay for a few seconds after lighting and then turn off , Just keep repeating .

2.2.1. The time delay function

There are usually two ways to achieve delay : One is Hardware delay , You need a timer / Counter , This method can improve CPU Work efficiency , It can also achieve accurate delay ; The other is Software delay , This method is mainly carried out by circulating body . Here we mainly study software latency .

In many cases , Timer / Counters are often used for other purposes , At this time, the software method can only be used to delay . Here are some methods of software delay .
Method 1 :
Short delay : Can be in C In the document, by using the tape _NOP_( ) Statement , Define a series of different delay functions , Such as Delay10us( )、Delay25us( )、Delay40us( ) Etc. are stored in a custom C In file , Call directly in the main program when necessary . Such as delay 10 μs The delay function of can be written as follows :

void Delay10us()
{
    
	_NOP_( ) ;
	_NOP_( ) ;
	_NOP_( ) ;
	_NOP_( ) ;
	_NOP_( ) ;
	_NOP_( ) ;
}

Delay10us( ) Functions are used together 6 individual _NOP_( ) sentence , The execution time of each statement is 1 μs. Main function call Delay10us( ) when , First execute a LCALL Instructions (2 μs), And then execute 6 individual _NOP_( ) sentence (6 μs), Finally, we executed a RET Instructions (2 μs), Therefore, a total of 10 μs.
Method 2 :
Loop empty statement delay : Write an empty loop statement in the program , Let the SCM in the empty loop “ In a daze ”. Write about 1ms The code for is as follows :

void delay_ms(int n)
{
    
	int i , j ;
	for( i = 0 ; i < n ; i ++ )
	{
    
		for( j = 0 ; j < 123 ; j ++) ;
	}
}

stay n by 1 when , The code probably runs 1ms about .
This delay method is the most commonly used method in imprecise delay .
Or both :

void Delay1ms()		//@11.0592MHz  Crystal vibration is 11.0592MHz
{
    
	unsigned char i, j;

	_nop_();
	i = 2;
	j = 199;
	do
	{
    
		while (--j);
	} while (--i);
}

2.2.2. The realization of flashing lamp

We have learned how to write the delay function , You can start writing blinking lights .
Circuit schematic diagram and 2.1 The same as , The code is as follows :

#include<reg52.h> //  Header file containing special function register definitions 

sbit LED = P2^3;     //  Bit address declaration  , Be careful :sbit  Must be in lowercase  ,P Capital must be written. 

void delay_ms(int n)	  //  The time delay function   About time delay  n ms
{
    
	int i , j ;			  //  Define two integer variables  i,j
	for( i = 0 ; i < n ; i ++ )
	{
    
		for( j = 0 ; j < 123 ; j ++ ) ;			 
	}
}

void main()			  //  Any one of them c There must be only one program  main  function 
{
    					  // {}  In pairs , Here is the beginning and end of the program 
	while(1)
	{
    
		LED = 1 ;	  //  A semicolon indicates the end of a statement  "1" High level ,"0"  Indicates low level 
		delay_ms( 10 ) ;
		LED = 0 ;
		delay_ms( 10 ) ;
	}		  
} 

Simulation video link of the above program

2.3. Running water lamp

seeing the name of a thing one thinks of its function , The running water lamp is to light up one by one like running water , Extinguish one by one .
We first in Proteus 8 Professional The following schematic diagram is established in :
 Insert picture description here
The picture shows LED The public end is GND( Low level ), So the program should make P1 Port output high level , To light up LED.
The code is as follows :

#include<reg52.h> // This file defines some special function registers of MCU 
#define uchar unsigned char // Declare and define data types 
#define uint unsigned int // Declare and define data types 

void delay(uint n)		//  The time delay function , About time delay  n ms
{
    
	uchar i,j;		// Define unsigned character variables  unsigned char  The data range of is 0~255(2^8 - 1)
	for(i = 0 ; i < n ; i ++)
	{
    
		for(j = 0 ; j < 123 ; j ++);
	}
}

void main()
{
    
	P1 = 0x00; //0000 0000  initialization P1 port , Make it output low level 
	while(1)
	{
    
		P1 = 0x01 ;   //0000 0001  namely P0^0 High level , The rest are low level 
		delay(200) ;
	   	P1 = 0x02 ;  // 0000 0010  namely P0^1 High level , The rest are low level 
		delay(200) ;
		P1 = 0x04 ;	 // 0000 0100  namely P0^2 High level , The rest are low level 
		delay(200) ;
	   	P1 = 0x08 ;  // 0000 1000  namely P0^3 High level , The rest are low level 
		delay(200) ;
		P1 = 0x10 ;	 // 0001 0000  namely P0^4 High level , The rest are low level 
		delay(200) ;
	   	P1 = 0x20 ;	  //0010 0000  namely P0^5 High level , The rest are low level 
		delay(200) ;
		P1 = 0x40 ;	 // 0100 0000  namely P0^5 High level , The rest are low level 
		delay(200) ;
	   	P1 = 0x80 ;  // 1000 0000  namely P0^6 High level , The rest are low level 
		delay(200) ;
	}
}

Or use the left shift method to write

void main()
{
    
	uint i ;     // Define an unsigned integer variable  unsigned int  The data range of is  0~65535(2^16 - 1)
	P1 = 0x00; //0000 0000  initialization P1 port , Make it output low level 
	while(1)  //  Dead cycle ( There is no end to the infinite cycle )
	{
    
		for( i = 0 ; i < 8 ; i ++ )
		{
    
		 	P1 = ( 0x01 << i ) ;  // i = 1  Move left when 1 position ;i = 2  Move left when 2 position  ...  When  i= 8  Stop the cycle when  for  End of the function  .
			delay(200) ;
		}
	}
}

Simulation video link of the above program

Here is the left shift of the reference program, and the right shift refers to the... Of the variable 8 Bit binary numbers are shifted one bit to the left in turn , Or move one bit to the right and fill in the high or low position 0. such as , take 8 digit , Give Way 0000 0001 Cycle one bit left , Namely 0000 0010.

原网站

版权声明
本文为[In the morning, the rain shines]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220545510963.html