#include <STC89C5xRC.H>
//定义
sbit LED =P2^0;
//延时
void Delay1ms(unsigned char t)
{
while (t–);
}
//延时
void Delay20ms(void) //@11.0592MHz
{
unsigned char data i, j;
i = 36;
j = 217;
do
{
while (--j);
} while (--i);
}
//主函数
void main()
{
unsigned char x=0;
while(1)
{
//从不亮到亮
for(x=0;x<100;x++)
{
Delay20ms();
LED=0;
Delay1ms(x);
LED=1;
Delay1ms(100-x);
}
//从亮到不亮
for(x=100;x>0;x–)
{
Delay20ms();
LED=0;
Delay1ms(x);
LED=1;
Delay1ms(100-x);
}
}
}
不断进行一个反转
另一种写法,除此之外,还可以用定时器写。