开发需求是:
1. 刚开始LED1和LED2均灭
2. LED1渐亮渐灭一次,然后LED1灭。
3. LED2渐亮渐灭一次,然后LED2灭。
就这样一直循环
我写的代码为何实现不了,求大神解答,折腾很久了
#include "reg51.h"
sbit LED1=P2^0;
sbit LED2=P2^1;
unsigned char cycle=0;
unsigned int step=1;
unsigned int bright=0;
unsigned char cycle2=0;
unsigned int step2;
unsigned int bright2=0;
void test1(void);
void test2(void);
void delay(unsigned int time_i);
void main()
{
while(1)
{
test1();
test2();
}
}
void test1(void)
{
if(++cycle >250)
{
cycle = 0;
switch (step)
{
case 1 :
bright++;
if(bright > 250)
{
step ++;
}break;
case 2 :
bright--;
if(bright < 1)
{
step2 = 1;
}break;
}
}
if(cycle < bright)
{
LED1=0; //灯亮
}
else
{
LED1=1; // 灯灭
}
}
void test2(void)
{
if(++cycle2 >250)
{
cycle2 = 0;
switch (step2)
{
case 1 :
bright2++;
if(bright2 > 250)
{
step2 ++;
}break;
case 2 :
bright2--;
if(bright2 < 1)
{
step = 1;
}break;
}
}
if(cycle2 < bright2)
{
LED2=0; //灯亮
}
else
{
LED2=1; //
}
} |