初次接触单片机,点亮人生中的第一盏灯
学会了各种各样的灯的闪烁,课后自己编写了一点花样灯的操作
#include <reg51.h>
sbit LED0=P1^0;
sbit LED1=P1^1;
sbit LED2=P1^2;
sbit LED3=P1^3;
sbit LED4=P1^4;
sbit LED5=P1^5;
sbit LED6=P1^6;
sbit LED7=P1^7;
void DelayUS2X(unsigned char n);
void DelayMS(unsigned char n);
void Delay(unsigned int t);
void main (void)
{
unsigned char i,a,b;
while(1)
{
a = 0x10;
b = 0x08;
for(i = 0; i < 4; i++)
{
P1 = ~(a + b);
a <<= 1;
b >>= 1;
DelayMS(100);
}
DelayMS(100);
a = 0x80;
b = 0x01;
for(i = 0; i< 4; i++)
{
P1 = ~(a + b);
a >>= 1;
b <<= 1;
DelayMS(100);
}
DelayMS(100);
//DelayMS(100);
a = 0x10;
b = 0x08;
for(i = 0; i < 4; i++)
{
P1 = ~(a + b);
a <<= 1;
b >>= 1;
DelayMS(100);
}
DelayMS(100);
a = 0x80;
b = 0x01;
for(i = 0; i< 4; i++)
{
P1 = ~(a + b);
a >>= 1;
b <<= 1;
DelayMS(100);
}
DelayMS(100);
a = 0x30;
b = 0x0c;
for(i = 0; i< 4; i++)
{
P1 = ~(a + b);
DelayMS(80);
P1 = 0xff;
DelayMS(80);
P1 = a + b;
DelayMS(80);
}
DelayMS(80);
}
}
void DelayUS2X(unsigned char n)
{
while(--n);
}
void DelayMS(unsigned char n)
{
while(n--)
{
DelayUS2X(245);
DelayUS2X(245);
}
}
void Delay(unsigned int t)
{
while(t--);
}