点亮LED灯
#include <REGX52.H>
void main()
{
P0=0xFE;
}
LED灯的闪烁
#include <REGX52.H>
#include <INTRINS.H>
void Delay500ms() //@12.000MHz
{
unsigned char i, j, k;
_nop_();
i = 4;
j = 205;
k = 187;
do 绿色的不重要只是记时的
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main ()
{
while(1)
{
P0 = 0xFE;
Delay500ms();
P0 = 0xFF;
Delay500ms();
}
}
LED流水灯
#include <REGX52.H>
#include <INTRINS.H>
void Delay500ms() //@12.000MHz
{
unsigned char i, j, k;
_nop_();
i = 4;
j = 205;
k = 187;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
while(1)
{
P0 = 0xFE;
Delay500ms();
P0 = 0xFD;
Delay500ms();
P0 = 0xFB;
Delay500ms();
P0 = 0xF7;
Delay500ms();
P0 = 0xEF;
Delay500ms();
P0 = 0xDF;
Delay500ms();
P0 = 0xBF;
Delay500ms();
P0 = 0x7F;
Delay500ms();
}
}
LED流水灯改良
#include <REGX52.H>
void Delay1ms(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms)
{
i = 2;
j = 239;
do 这个在sta中搞的1ms,这样能按照自己的需要调时间
{
while (--j);
} while (--i);
xms = xms--;
}
}
void main ()
{
P0 = 0xFE;
Delay1ms(500);
P0 = 0xFD;
Delay1ms(500);
P0 = 0xFB;
Delay1ms(500);
P0 = 0xF7;
Delay1ms(500);
P0 = 0xEF;
Delay1ms(500);
P0 = 0xDF;
Delay1ms(500);
P0 = 0xBF;
Delay1ms(500);
P0 = 0x7F;
Delay1ms(500);
}