还不是很懂,给点建议
#include <reg52.h>
typedef unsigned char uchar;
#define Ton 50
#define LED P0
unsigned char Maikuan=0;
uchar Occupy_led; //占空比越大,灯越亮
bit light;
uchar m;
void Inittime() //初始化函数
{
TMOD = 0x02; //定时器0,工作方式2,自动重装
TH0 = 256-Ton;
TL0 = 256-Ton;
ET0 = 1;
EA = 1;
TR0 = 1;
} //定时器0中断
void time0() interrupt 1 using 0
{
Maikuan++;
}
void main()
{
Inittime();
while (1)
{
//所有的灯逐渐从暗到全亮,再由全亮到暗
for (m=254; m>0; m--)
{
while (Maikuan!=Occupy_led)
{
LED = 0x00; //亮
}
while (Maikuan!=127)
{
LED = 0xff; //灭
}
Maikuan = 0;
if (light)
{
Occupy_led++;
}
else
{
Occupy_led--;
}
if (Occupy_led==128)
{
Occupy_led = 127;
light = !light;
}
if (Occupy_led>128)
{
Occupy_led = 0;
light = !light;
}
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>