STC89C52单片机实验 DAY1 LED模块

点亮第一个LED

#include "reg52.h"    
sbit led=P2^0;	  
void main()
{
	while(1)
	{
		led=0;	
	}		
}

LED闪烁

#include
sbit LED=P2^0;

void delay(int i){
	while(i--);
}
void main()
{
	while(1){
		LED=0;
		delay(5000);
		LED=1;
		delay(5000);
	}
}

流水灯

#include
#include
#define led P2

void delay(int i){
	while(i--);
}
void main()
{
	int i;
	led=0xfe;  //1111 1110 
	while(1){
		for(i=0;i<7;i++){
			led=_crol_(led,1);
			delay(5000);
		}
		for(i=0;i<7;i++){
			led=_cror_(led,1);
			delay(5000);
		}
	}
}

LED中心开花

#include
#include
#define led P2

void delay(int i){
	while(i--);
}
void main()
{
	int i;
	//led=0x7e;  //0111 1110 
	while(1){
		led=0xe7;         //1110 0111
		delay(50000);
		led=0xdb;              //1101 1011
		delay(50000);
		led=0xbd;             //1011 1101
		delay(50000);
		led=0x7e;            //0111 1110
		delay(50000);
	}
}

全部点亮,左边先灭,右边后灭,从中间开始灭

#include
#include
#define led P2

void delay(int i){
	while(i--);
}
void main()
{
	int i;
	while(1){
		led=0x00;         //0000 0000
		delay(50000);
		led=0x01;              //0000 0001
		delay(50000);
		led=0x03;             //0000 0011
		delay(50000);
		led=0x07;            //0000 0111
		delay(50000);
		led=0x0f;              //0000 1111
		delay(50000);
		led=0x1f;             //0001 1111
		delay(50000);
		led=0x3f;            //0011 1111
		delay(50000);
		led=0x7f;             //0111 1111
		delay(50000);
		led=0xff;            //1111 1111
		delay(50000);
		
		led=0x00;         //0000 0000
		delay(50000);
		led=0x80;              //1000 0000
		delay(50000);
		led=0xc0;             //1100 0000
		delay(50000);
		led=0xe0;            //1110 0000
		delay(50000);
		led=0xf0;              //1111 0000
		delay(50000);
		led=0xf8;             //1111 1000
		delay(50000);
		led=0xfc;            //1111 1100
		delay(50000);
		led=0xfe;             //1111 1110
		delay(50000);
		led=0xff;            //1111 1111
		delay(50000);
		
		led=0x00;         //0000 0000
		delay(50000);
		led=0x18;              //0001 1000
		delay(50000);
		led=0x3c;             //0011 1100
		delay(50000);
		led=0x7e;            //0111 1110
		delay(50000);
		led=0xff;            //1111 1111
		delay(50000);
	}
}

你可能感兴趣的:(单片机,单片机,STC89C52,LED流水灯)