//跳线设置:默认跳线位 #include <reg51.h> typedef unsigned char uchar; #define Ton 40 #define LED P1 sbit leds=P1^4; 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; unsigned char Maikuan=0; static unsigned char ledon[8] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; 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() { unsigned char i = 0,j = 0; //初始化i,j leds=0; Inittime(); while (1) { //第1,3,5,7个灯半亮,其余的全亮。 for(i=254; i>0; i--) { while ( Maikuan!=20) { LED = 0x00; } while ( Maikuan!=200) { LED = 0x55; } Maikuan = 0; } //最难的:8个灯从第一个开始依次渐亮,直到最后一个。再从最后一个起渐暗,直到第一个。如此循环往复。 for(j = 0;j<=7;j++) { i = 0; while(i!=255) // 由暗变亮 { if( Maikuan == 0) // 点亮 { LED = ledon[j]; } if(Maikuan == i) // 熄灭 { LED = 0xff; } if( Maikuan== 255) { i++; } } } for(j=7;j>=0;j--) { i = 0; while(i!=255) // 由亮变暗 { if(Maikuan == 0) // 熄灭 { LED = 0xff; } if( Maikuan == i) // 点亮 { LED = ledon[j]; } if( Maikuan == 255) { i++; } } } //所有的灯逐渐从暗到全亮,再由全亮到暗。 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; } } } }