Design 1: 51 MCU water lamp control

Table of contents

1. Design content

Second, hardware circuit analysis

3. Schematic diagram of simulation

4. Programming

5. Simulation results

6. Thinking questions

The author has something to say


1. Design content

       This design uses 4 buttons. When KEY1 is pressed, the light-emitting diodes (D1~D8) connected to the P0 port will light up 3 times from bottom to top at an interval of 100ms. The interval is 3 circles from top to bottom; when KEY3 is pressed, the LED is cyclically lit from both sides to the middle, the number of cycles is 3 circles, and the time interval is 1000ms; when KEY4 is pressed, the LED is cyclically lit from the middle to both sides On, the number of cycles is 3 circles, and the time interval is 1000ms. Normally, the status of the LEDs is all off.

Second, hardware circuit analysis

1. Chip 74HC245 function, pin meaning

The function of 74HC245 is to be a buffer. Its OE pin controls the output enable, which is active at low level. The DIR pin controls the data transmission direction. If DIR is high level, the data transmission direction is A->B, otherwise it is B -> A, A0->A7 and B0->B7 pins are used for data input/output, VCC and GND are the power supply terminal and ground terminal of the chip respectively.

2. Chip 74HC138 function, pin meaning

The function of 74HC138 is to decode 3-bit binary into 8 output states, and there are 8 output I/Os in total , A0->A2 are data input pins, Y 0- >Y7 are data output pins, E1, E2, E control chip enable, only when E1=0, E2=0, E3=1 , the chip can be enabled , otherwise the chip will not have any effective output , VCC and GND are the power supply terminal and ground terminal of the chip respectively .

3. Pay attention when P0 port is used as a general-purpose I/O port

Since the internal pull-up resistor of the P0 port is large, it is a "weak pull-up", so the high-level output current of the P0 port is very small, and when the output is low, the pull-down MOS transistor is turned on, the sink current is large, and the load capacity is strong. Therefore, in the design, the low-level drive mode is generally adopted.

4. Light up the light-emitting diode hardware to meet the conditions

To turn on the light-emitting diode, the triode needs to be turned on, that is, Q3 is at a low level, that is, the cathode of the diode is at a low level.

3. Schematic diagram of simulation

4. Programming

/*******************************************************************************
* 函 数 名  				: main
* 函数功能		        : 按键流水灯
* 创作者  				: 薄情书生
* 创作时间  				: 2022.9.15
*******************************************************************************/

#include "reg52.h"         
#include "intrins.h"

/*********类型定义*********/
typedef unsigned char u8;
typedef unsigned int u16;

/*********按键定义*********/
sbit KEY0 = P3^2;
sbit KEY1 = P3^3;
sbit KEY2 = P3^4;
sbit KEY3 = P3^5;

/*********毫秒延时*********/
void delay_ms(u16 ms)
{
	u16 x,y;
	for(x=ms;x>0;x--)
		for(y=114;y>0;y--);
}

/*************38译码器**************/
void SelectHC138(unsigned char channel)
{
   switch(channel)
	 {
		 case 0:
        P1 = (P1 & 0x1f)|0x00;break;    //发光二极管(LEDS0)
   }
}

/*************LED由下至上**************/
void Bottom_Up()
{
	u8 i,j;
	for(j=0;j<3;j++)
	{
		for(i=0,P0 = 0xff;i<8;i++)
		{
			P0 = P0<<1;	 
			delay_ms(200); 
		}
	}
	P0 = 0xff;
}

/*************LED由上至下**************/
void Up_Bottom()
{
	u8 i,j;
	for(j=0;j<3;j++)
	{
		for(i=0,P0 = 0xff;i<8;i++)
		{
			P0 = P0>>1;	 
			delay_ms(100); 
		}
	}
	P0 = 0xff;
}

/*************两边至中间**************/
void LED_sides()
{
	u8 i,j,ri,lf;
	for(j=0;j<3;j++)
  {
		for(i=0,ri=0x7f,lf=0xfe;i<4;i++)
		{
			P0 = ri&lf;
			ri = _cror_(ri,1);
			lf = _crol_(lf,1);
			delay_ms(1000);
		}
  }
	P0 = 0xff;
}

/*************中间至两边**************/
void LED_middle()
{
	u8 i,j,ri,lf;
	for(j=0;j<3;j++)
	{
		for(i=0,ri=0xef,lf=0xf7;i<4;i++)
		{
			P0 = ri&lf;
			ri = _crol_(ri,1);
			lf = _cror_(lf,1);
			delay_ms(1000);
		}
	}
	P0=0xff;
}

/*******************主程序************************/
void main()
{
    SelectHC138(0);
	while(1)
	{
		if(KEY0==0)
		{
			delay_ms(10); 
			if(KEY0==0)
			{
               Up_Bottom();
			}
	    }
		if(KEY1==0)
		{
            delay_ms(10); 
			if(KEY1==0)
			{
               Bottom_Up();
		    }
	    }
		if(KEY2==0)
		{
            delay_ms(10); 
			if(KEY2==0)
		    {
               LED_sides();
			}
			
		}
		if(KEY3==0)
		{
            delay_ms(10); 
			if(KEY3==0)
		    {
                LED_middle();
			}
		}
	}
}

5. Simulation results

When KEY1 is pressed, the light-emitting diodes (D1~D8) connected to the P0 port will light up 3 times from bottom to top at an interval of 100ms.

 When KEY2 is pressed, it will light up 3 circles from top to bottom at an interval of 200ms.

 When KEY3 is pressed, the LED lights up from both sides to the middle cycle, the number of cycles is 3, and the time interval is 1000ms.

 When KEY4 is pressed, the LED lights up from the middle to both sides, the number of cycles is 3, and the time interval is 1000ms.

Normally, the status of the LEDs is all off.

6. Thinking questions

Modify the program to realize 4 button control, from top to bottom, from bottom to top, the LED flashes 3 times, the time interval is 500ms, all the LEDs light up for 3 seconds and then turn off, and then return to the normal state that all LEDs are off. Please write and debug the program by yourself. The reference program for thinking questions will be given in Design 2: 51 MCU External Interrupt Experiment.

The author has something to say

This column is a practical tutorial of 51 single-chip microcomputer, which aims to promote learning through practice , and help friends quickly get started with 51 single-chip microcomputer. Please learn the corresponding theoretical knowledge of 51 single-chip microcomputer by yourself. The relevant theoretical knowledge involved in this design: 51 MCU IO port control, the use of independent buttons, please refer to the materials for learning.

This design source program and simulation source files Baidu network disk link: https://pan.baidu.com/s/17PtMjM5y-YSfdHlqnqAUnA Extraction code: 4ma5 
 

Guess you like

Origin blog.csdn.net/weixin_53402301/article/details/130907233