001:闪烁的LED
#include
#define u8 unsigned char
#define u16 unsigned int
sbit LED=P2^0;
void delay(u16 x)
{
uchar i;
while(x--)
{
for(i=0;i<120;i++);
}
}
void main()
{
while(1)
{
LED=~LED;
delay(150);
}
}
002:从左到右的流水
#include
#include
#define u8 unsigned char
#define u16 unsigned int
void delay(u16 n)
{
while(n--);
}
void main()
{ u8 i=0;
P2=0xfe;
delay(50000);
P2=0xfe;
while(1)
{
P2=_crol_(P2,1); //P2的值向左循环移动
delay(50000);
}
}
003:8只LED左右来回点亮
#include
#include
typedef unsigned char u8;
typedef unsigned int u16;
#define led P2
void delay(u16 i)
{
while(i--);
}
void main()
{ u8 i=0;
led=0xfe;
delay(50000);
while(1)
{
for(i=0;i<7;i++)
{
led=_crol_(led,1);
delay(50000);
}
for(i=0;i<7;i++)
{
led=_cror_(led,1);
delay(50000);
}
}
}
004开关控制LED
#include
sbit SW1=P1^4;
sbit SW2=P1^5;
sbit LED1=P2^0;
sbit LED2=P2^1;
void main()
{
while(1)
{
LED1=SW1;
LED2=SW2;
}
}
005:花样流水灯
#include "reg51.h"
#define u8 unsigned char
#define u16 unsigned int
u8 code sng_P0[]={ 0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff,0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f,0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff};
u8 code sng_P3[]= {
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0xff,0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff,0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f,0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff};
void delay(u8 n)
{ u8 e=0;
while(n--) for(e=0;e<120;e++);
}
void main()
{ u8 i;
while(1)
{
for(i=0;i<136;i++)
{
P0=sng_P0[i];
P3=sng_P3[i];
delay(200) ;
}
} }
#include "reg51.h"
#define u8 unsigned char
#define u16 unsigned int
sbit RDX=P0^0; //东西
sbit YDX=P0^1;
sbit GDX=P0^2;
sbit RNB=P0^3; //南北
sbit YNB=P0^4;
sbit GNB=P0^5;
u16 s=0,c=1;
void delay(u16 n)
{
u8 i ;
while(n--)
for(i=0;i<120;i++);
}
void change()
{
switch(c)
{
case 1: RDX=1;YDX=1;GDX=0;
RNB=0;YNB=1;GNB=1;
delay(2000);
c=2;
break;
case 2: delay(300);
YDX=~YDX; GDX=1;
if(++s!=10)return;
s=0;
c=3;
break;
case 3: RDX=0;YDX=1;GDX=1;
RNB=1;YNB=1;GNB=0;
delay(2000);
c=4;break;
case 4: delay(300);
YNB=~YNB; GDX=1;
if(++s!=10) return;
s=0;
c=1;
}
}
void main()
{
while(1)
change();
}
007:数码管显示0~9
#include "reg51.h"
typedef unsigned int u16;
unsigned char s[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,};
void delay(u16 n)
{
while(n--);
}
void seg()
{
u16 i=0;
for(i=0;i<11;i++)
{
P2=s[i];
delay(50000);
}
}
void main()
{
while(1)
{
seg();
}
}
008:数码管显示HELLO
#include "reg51.h"
typedef unsigned int u16;
typedef unsigned char u8;
sbit LSA=P3^0;
sbit LSB=P3^1;
sbit LSC=P3^2;
u8 code smgduan[5]={0x76,0x79,0x38,0x38,0x3f};
void delay(u16 i)
{
while(i--);
}
void DigDisplay()
{
u8 i;
for(i=0;i<8;i++)
{
switch(i)
{
case(0):
LSA=0;LSB=0;LSC=0; break;
case(1):
LSA=1;LSB=0;LSC=0; break;
case(2):
LSA=0;LSB=1;LSC=0; break;
case(3):
LSA=1;LSB=1;LSC=0; break;
case(4):
LSA=0;LSB=0;LSC=1; break;
case(5):
LSA=1;LSB=0;LSC=1; break;
case(6):
LSA=0;LSB=1;LSC=0; break;
case(7):
LSA=1;LSB=1;LSC=1; break;
}
P2=smgduan[i];
delay(100);
P2=0x00;
}
}
void main()
{
while(1)
{
DigDisplay();
}
}