[C language] 51 single-chip microcomputer running water lamp (electronic internship of Northwestern Polytechnical University)

[C language] 51 single-chip microcomputer running water lamp (electronic internship of Northwestern Polytechnical University)

Basic knowledge

&: bitwise and
|: bitwise OR
~: bitwise negation
^: bitwise exclusive OR

For example:
A=1 1 0 0 1 1 0 0
B=1 1 1 1 0 0 0 0

A&B=1 1 0 0 0 0 0 0
A|B=1 1 1 1 1 1 0 0
~A=0 0 1 1 0 0 1 1
A^B=0 0 1 1 1 1 0 0

Rotate left _crol_ and rotate right _cror_:
There is a library file of the microcontroller that has a shift function written, and it is stored in the #include<intrins.h> library file.
Variable = crol (variable name, number of shifts)
P0 = 0xfe; // 1111 1110
P0 = crol (P0,1);
At this time, P0=0xfd; // 1111 1101



The realization effect of the seven questions

Presentation rendering



Complete code

#include <reg51.h>              //51单片机头文件
#include <intrins.h>            //包含循环移位指令的头文件

#define uchar unsigned char     //宏定义 unsigned char 为 uchar
#define uint  unsigned int      //宏定义 unsigned int  为 uint

sbit k1 = P3 ^ 2;
sbit k2 = P3 ^ 3;
sbit k3 = P3 ^ 4;
sbit k4 = P3 ^ 5;

void delay_us(uint us)          //延时子程序
{
    
    
	uint a;                     //声明变量a取值为0-65535
	for(a=0;a<us;a++);
}

void start_1()                  //第一题
{
    
    
	uchar a,b;
    b=0xfe;                     //1111 1110
    for(a=0;a<7;a++)            //从右到左
    {
    
    
		P0 = b;
		delay_us(50000);
		b=_crol_(b,1);
	}
	for(a=0;a<7;a++)            //从左到右
	{
    
    
		P0 = b;
		delay_us(50000);
		b=_cror_(b,1);
	}
}

void start_2()                 //第二题
{
    
    
	uchar a,b;
	b=0xfe;                    //1111 1110
	for(a=0;a<7;a++)           //从右到左
	{
    
    
		P0 = 0xe7 & b;         //1110 0111 & b
		delay_us(50000);
		b=_crol_(b,1);
	}
	for(a=0;a<7;a++)           //从左到右
    {
    
    
		P0 = 0xe7 & b;         //1110 0111 & b
		delay_us(50000);
		b=_cror_(b,1);
	}
}

void start_3()                //第三题
{
    
     
	uchar a,b,c;
	b=0x7f;                   //0111 1111
	c=0xfe;                   //1111 1110
	for(a=0;a<3;a++)          //从两边到中间
    {
    
    
		P0 = b & c;
		delay_us(50000);
		b=_cror_(b,1);
		c=_crol_(c,1);
	}
	for(a=0;a<3;a++)          //从中间到两边
    {
    
    
		P0 = b & c;
		delay_us(50000);
		b=_crol_(b,1);
		c=_cror_(c,1);
	}
}

void start_4()               //第四题
{
    
    
	uchar a;
    for(a=0;a<8;a++)         //从左到右依次亮起
	{
    
    
		P0 = 0x7f >> a;      //0111 1111 >> a
		delay_us(50000);
	}
	for(a=0;a<8;a++)         //从右到左依次灭掉
    {
    
    
		P0 = ~(0xff<<a);     //1111 1111 << a 按位取反
		delay_us(50000);
	}
}

void start_5()               //第五题
{
    
    
	uchar a,b,c,d;
    c=0xff;                  //1111 1111
    for(i=0;i<9;i++)
	{
    
    
		b=0xfe;              //1111 1110
		for(a=0;a<8;a++)
		{
    
    
			P0= b & c;
			delay_us(50000);
			b=_cror_(b,1);
		}
		c=c<<1;
	}
}

void start_6()              //第六题
{
    
    
	uchar a,b,c,d,e;
	b=0xfe;                 //1111 1110
	for(d=0;d<9;d++)
	{
    
    
		if(e>0)
		{
    
    
			b=_cror_(b,1);
		}
		a=0xfe;             //1111 1110
		for(e=0;e<d+1;e++)
		{
    
    
			P0= c;
			delay_us(50000);
			a=_cror_(a,1);
			c= a & b;
		}
	}
}


void start_7()             //第七题
{
    
    
	uint  a=0;
	uint  b=500;
	uchar c=0x00;          //0000 0000
	uchar d=0xff;          //1111 1111

	while(b)
	{
    
    
		P0= c;
		delay_us(b--);
		P0= d;
		delay_us(a++);
	}

	while(a)
	{
    
    
		P0= c;
		delay_us(b++);
		P0= d;
		delay_us(a--);
	}
}


void main()
{
    
    
	if(k1==0)
	{
    
    
		start_1();
		start_2();
		start_3();
		start_4();
		start_5();
		start_6();
		start_7();
	}
}
		

Guess you like

Origin blog.csdn.net/weixin_43767920/article/details/108684799