点亮小灯代码[基础]

点亮其中一个灯

#include <reg52.h>

//LED2亮灯

sbit ADDR0 = P1 ^ 0;
sbit ADDR1 = P1 ^ 1;
sbit ADDR2 = P1 ^ 2;
sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;
sbit LED2 = P0 ^ 0;


void main()
{
	ENLED = 0;
	ADDR3 = 1;

	ADDR2 = 1;
	ADDR1 = 1;
	ADDR0 = 0;

	LED2 = 0;
	

	while (1)
		;
} 

点亮所有灯,且不灭


#include <reg52.h>
 
//LED2~LED9依次亮灯后不灭

sbit LED2 = P0 ^ 0;
sbit LED3 = P0 ^ 1;
sbit LED4 = P0 ^ 2;
sbit LED5 = P0 ^ 3;
sbit LED6 = P0 ^ 4;
sbit LED7 = P0 ^ 5;
sbit LED8 = P0 ^ 6;
sbit LED9 = P0 ^ 7;

sbit ADDR0 = P1 ^ 0;
sbit ADDR1 = P1 ^ 1;
sbit ADDR2 = P1 ^ 2;
sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;

void main()
{
	int i = 0;

	ENLED = 0;
	ADDR3 = 1;

	ADDR2 = 1;
	ADDR1 = 1;
	ADDR0 = 0;

	LED2 = 0;

	for (i = 0; i < 10000; i++)
		;
	LED3 = 0;

	for (i = 0; i < 10000; i++)
		;
	LED4 = 0;

	for (i = 0; i < 10000; i++)
		;
	LED5 = 0;

	for (i = 0; i < 10000; i++)
		;
	LED6 = 0;

	for (i = 0; i < 10000; i++)
		;
	LED7 = 0;

	for (i = 0; i < 10000; i++)
		;
	LED8 = 0;

	for (i = 0; i < 10000; i++)
		;
	LED9 = 0;

	while (1)
		;
}

依次点亮所有灯且灭


#include <reg52.h>

//LED2~LED9依次亮灯后灭灯

sbit LED2 = P0 ^ 0;
sbit LED3 = P0 ^ 1;
sbit LED4 = P0 ^ 2;
sbit LED5 = P0 ^ 3;
sbit LED6 = P0 ^ 4;
sbit LED7 = P0 ^ 5;
sbit LED8 = P0 ^ 6;
sbit LED9 = P0 ^ 7;

sbit ADDR0 = P1 ^ 0;
sbit ADDR1 = P1 ^ 1;
sbit ADDR2 = P1 ^ 2;
sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;

void main()
{
	int i = 0;

	ENLED = 0;
	ADDR3 = 1;

	ADDR2 = 1;
	ADDR1 = 1;
	ADDR0 = 0;

	LED2 = 0;

	for (i = 0; i < 10000; i++)
		;
	LED2 = 1;
	LED3 = 0;

	for (i = 0; i < 10000; i++)
		;
	LED3 = 1; 
	LED4 = 0;


	for (i = 0; i < 10000; i++)
		;
	LED4 = 1;
	LED5 = 0;

	for (i = 0; i < 10000; i++)
		;
	LED5 = 1;
	LED6 = 0;

	for (i = 0; i < 10000; i++)
		;
	LED6 = 1;
	LED7 = 0;

	for (i = 0; i < 10000; i++)
		;
	LED7 = 1;
	LED8 = 0;

	for (i = 0; i < 10000; i++)
		;
	LED8 = 1;
	LED9 = 0;

	for (i = 0; i < 10000; i++)
		;
	LED9 = 1;

	while (1)
		;
}	

跑马灯


#include <reg52.h>

//LED2~LED9依次亮灯后灭灯再次亮灯..跑马灯

sbit LED2 = P0 ^ 0;
sbit LED3 = P0 ^ 1;
sbit LED4 = P0 ^ 2;
sbit LED5 = P0 ^ 3;
sbit LED6 = P0 ^ 4;
sbit LED7 = P0 ^ 5;
sbit LED8 = P0 ^ 6;
sbit LED9 = P0 ^ 7;

sbit ADDR0 = P1 ^ 0;
sbit ADDR1 = P1 ^ 1;
sbit ADDR2 = P1 ^ 2;
sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;

void main()
{
	int i = 0;

	ENLED = 0;
	ADDR3 = 1;

	ADDR2 = 1;
	ADDR1 = 1;
	ADDR0 = 0;

	while (1)
	{
		LED2 = 0;

		for (i = 0; i < 10000; i++)
			;
		LED2 = 1;
		LED3 = 0;

		for (i = 0; i < 10000; i++)
			;
		LED3 = 1; 
		LED4 = 0;


		for (i = 0; i < 10000; i++)
			;
		LED4 = 1;
		LED5 = 0;

		for (i = 0; i < 10000; i++)
			;
		LED5 = 1;
		LED6 = 0;

		for (i = 0; i < 10000; i++)
			;
		LED6 = 1;
		LED7 = 0;

		for (i = 0; i < 10000; i++)
			;
		LED7 = 1;
		LED8 = 0;

		for (i = 0; i < 10000; i++)
			;
		LED8 = 1;
		LED9 = 0;
   	
		for (i = 0; i < 10000; i++)
			;
		LED9 = 1;
	}
}