51 single-chip microcomputer realizes the control of running water lamp

51 single-chip microcomputer realizes the control of running water lamp

The following is the content of the text of this article, and the following cases are for reference

1. Turn on the first LED light

#include <reg52.h>
#define uint unsigned int //简化定义
#define uchar unsigned char//同上
sbit D1=P2^1;
void main(){
		D1=0;
}

In the code, D1 represents the bit definition, which is equivalent to giving a name to the position of the corresponding pin of the LED light. The initial state pin of the microcontroller is high level by default, that is, the corresponding value is 1. So if you want to light up the LED light, you only need to make its pin level low.

Two, water lamp

1. Bus type control

To design a running light, if you use bit operations, you need to define them one by one. This is undoubtedly very cumbersome. So here we use the bus type operation.

Bus type This bus system method can uniformly control pins with the same tens digit.

For example, if you want to control the level of P1.0-p1.7, the specific programming method is to mark the 01 value corresponding to each pin according to the required result, arrange it from front to back, and then convert it into hexadecimal , directly set P1=0x+ to correspond to the hexadecimal number. In this way, multiple pins can be controlled with only one line of code.

#include <reg52.h>
void main()
		P1=0x80;//对应十进制数字10000001,对应P1的首个管脚与最后一个管脚高电平。
}

2. Delay function

The code is as follows (example):

void delay(uint i){
while(i--);
}//此函数可以用来控制特定时长的延时,具体时间由单片机晶振频率决定。

3. _ crol _ function usage

_crol_ function function: shift c to the left by b bits, and return the value as unsigned char type;

#include <intrins.h>//_crol_函数在intrins.h函数库中。
unsigned int temp;
temp=0xfe;
P1=temp;
temp=_crol_(temp,1);//第一个变量用来控制位,第二个用来控制每次移动的位数。

4. Realize the running water lamp

#include <reg52.h>
#include <intrins.h>
#define uint unsigned int 
#define uchar unsigned char
void delay(uint i);
void main(){
	uint temp=0xfe;
	while(1){
		P2=temp;
		temp=_crol_(temp,1);
		delay(10000);
	}
}
void delay(uint i){
while(i--);
}

Finally, the operation of the single-chip running water lamp is realized.

Guess you like

Origin blog.csdn.net/yfw88888/article/details/130746115