52 single-chip microcomputer four methods to achieve water lamp


You are welcome! Here are my MCU study notes, I hope my notes can be helpful to you on your way of learning!

Analysis of Water Lamp Circuit

Water lamp circuit diagram

Insert picture description here

Power supply→current limiting resistor→light emitting diode→74HC573 output terminal→SCM P1 group IO port

Because the output current of the IO port of the single-chip microcomputer is very small, it is impossible to directly drive the light-emitting diode with the IO port, so we need to use the 74HC573 chip (which can be understood as a current amplification chip).

74HC573

Chip map

Chip map

It can be seen from the figure that 74HC573 has 20 pins, Vcc and GND are the positive and negative terminals of the power supply, the pins 2-9 are the input terminals, and the pins 12-19 are the output terminals.

Electrode characteristics

Electrode characteristics

① The general working voltage of our circuit board is 5V. According to the data in the table, the high-level input voltage of the chip is ≥3.15V, and the low-level input voltage is 0~1.35V. The output voltage of the single-chip microcomputer STC89C52 we use is 5V or 0V, which meets the above conditions.
②Look at the conversion time of high and low levels. Vcc=4.5V corresponds to 500ns. The minimum time for the single-chip microcomputer to execute an instruction is 1.08 microseconds, which is greater than 500 nanoseconds. It can meet the requirements of the chip without additional delay.
③The working temperature is between -45 degrees Celsius and 85 degrees Celsius.

Truth table

Truth table

L means low point is flat, H means high level, X means any
Q0 means keep the last state, Z means keep high impedance state

It can be obtained from the figure:
①When the input OE level is low and LE is high, the output level is the same as the input level.
②When the input OE level is low and LE is also low, the output level maintains the original level.
③When the input OE level is high, the output level is high impedance.
Jumper cap

Because my OE is always grounded, so OE is always low, and the third situation above will not occur.
I connected the jumper cap to 1 and 2 (as shown in the figure above), and LE is high, which conforms to the above Case ①

Binary decimal and hexadecimal conversion

Base chart

Base table

Representation method:
Binary: 0B Hexadecimal: 0X Decimal number directly expressed

Example:
Decimal: 15➡ Binary: 0B1111➡ Hexadecimal: 0X0F

Binary to Hexadecimal

A hexadecimal number is expressed by a four-digit binary number and is calculated by the 8421 code. (Key)
Calculation example:
2 to 16

Binary to decimal

Two to ten

Decimal to binary

Ten to two

Decimal to Hexadecimal

Ten to sixteen

Hexadecimal to Decimal

Sixteen to ten

Total thread control mode of IO port

MCU IO port control mode

Bit control: control each individual IO port. Example: led0=0 (the method used in my last article to light up the led)
Bus control: control eight IO port notifications. Example: P1=0XFE
below Is the hexadecimal number corresponding to a single led illuminated by bus control?
Bus control

Reference Code

1. Simple and crude method

I mentioned above that the time for the microcontroller to run this piece of code is in the microsecond level, and our human eyes can barely perceive a flash of about 83 Hz every 11 milliseconds. So we have to add a delay to the above code

int i = 40000;
while(i--);//延时

We know that the program takes time to run, and the time to run once is very short, but we can use the while loop to make it run several times to achieve the effect of delay

#include<reg52.h>
#define led_time 40000
void main()
{
    
    
	unsigned int i;
	while(1)
	{
    
    
		P1 = 0XFE;//点亮第1个led
		i = led_time;
		while(i--);//延时(下同)
		P1 = 0XFD;//点亮第2个led
		i = led_time;
		while(i--);
		P1 = 0XFB;//点亮第3个led
		i = led_time;
		while(i--);
		P1 = 0XF7;//点亮第4个led
		i = led_time;
		while(i--);
		P1 = 0XEF;//点亮第5个led
		i = led_time;
		while(i--);
		P1 = 0XDF;//点亮第6个led
		i = led_time;
		while(i--);
		P1 = 0XBF;//点亮第7个led
		i = led_time;
		while(i--);
		P1 = 0X7F;//点亮第8个led
		i = led_time;
		while(i--);
	}
}

2. Simplified procedure by shifting left

#include<reg52.h>
void main()
{
    
    
	unsigned int X = 0;
	unsigned int i;
	while(1)
	{
    
    
		P1 = ~( 0X01 << X );//左移并取反
		X++;
		if( X >= 8 )
		{
    
    
			X = 0;    //左移7次后清零
		}
		i = 40000;
		while(i--);
	}
}

3. Call library function method

#include<intrins.h>
This library function contains subroutines that rotate left or right and the _nop_ delay function. The
_nop_();//空操作,延迟一个机器周期时间
machine cycle time depends on the crystal oscillator frequency

#include<reg52.h>
#include<intrins.h>
void main()
{
    
    
	unsigned int i;
	P1 = 0XFE;//点亮第1个led
	while(1)
	{
    
    
		i = 40000;
		while(i--);//延时
		P1 = _crol_(P1,1);//每次循环左移一格
	}
}

4. Array method

#include<reg52.h>
unsigned char code LED[8]={
    
    0XFE,0XFD,0XFB,0XF7,0XEF,0XDF,0XBF,0X7F};
//点亮led的数组(加code为只读,不可修改,节省内存)
void main()
{
    
    
	unsigned int i;
	unsigned char j=0;//自加变量
	while(1)
	{
    
    	
	  P1 = LED[j++];
		if(j>=8)
			j=0;
		i = 40000;
		while(i--);//延时
	}
}

to sum up

Four ways to realize the running water lamp
. The first one: simple and crude method.
Features: The idea is simple and clear, and the program is too complicated.
The second method : Left shift method
Features: The operation of left shift is used, which simplifies the procedure of the first method.
The third type: call library functions.
Features: The easiest method in the program, a statement realizes the function of the water lamp.
Fourth: Array Method
Features: The most flexible way to control the water lamp, can achieve the effect of any pattern of water lamps.

Reference content link: https://www.bilibili.com/video/BV1Kt411Q7aV?t=6824&p=5 .

If there is a mistake, look forward to it! Thank you for watching!

嘿~我亲爱的伙计!我想慷慨大方的你一定不会吝啬给我这个可怜的博主点赞的。

Guess you like

Origin blog.csdn.net/chuxiqianye2020/article/details/112802999