|
硬件上與流水燈沒有不同,軟件上就是PWM調(diào)光。
- #include<reg52.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit D0 = P1^0;
- sbit D1 = P1^1;
- sbit D2 = P1^2;
- sbit D3 = P1^3;
- sbit D4 = P1^4;
- sbit D5 = P1^5;
- sbit D6 = P1^6;
- sbit D7 = P1^7;
- uchar PWM0 = 15;
- uchar PWM1 = 17;
- uchar PWM2 = 19;
- uchar PWM3 = 21;
- uchar PWM4 = 23;
- uchar PWM5 = 25;
- uchar PWM6 = 27;
- uchar PWM7 = 29;
- uchar count=0;
- uchar num=0;
- void main()
- {
- TMOD=0x01;
- TH0=(65536-500)/256;
- TL0=(65536-500)%256;
- EA=1;
- ET0=1;
- TR0=1;
- while(1)
- {
- if(num==50)
- {
- num=0;
- PWM7++;PWM6++;PWM5++;PWM4++;
- PWM3++;PWM2++;PWM1++;PWM0++;
- if(PWM7==30) PWM7=0;
- if(PWM6==30) PWM6=0;
- if(PWM5==30) PWM5=0;
- if(PWM4==30) PWM4=0;
- if(PWM3==30) PWM3=0;
- if(PWM2==30) PWM2=0;
- if(PWM1==30) PWM1=0;
- if(PWM0==30) PWM0=0;
- }
- }
- }
- void Timer0(void) interrupt 1
- {
- TH0=(65536-500)/256;
- TL0=(65536-500)%256;
- count++;
- num++;
- if(count >= 15) count = 0;
- if(count >= PWM7) D0 = 0; else D0 = 1;
- if(count >= PWM6) D1 = 0; else D1 = 1;
- if(count >= PWM5) D2 = 0; else D2 = 1;
- if(count >= PWM4) D3 = 0; else D3 = 1;
- if(count >= PWM3) D4 = 0; else D4 = 1;
- if(count >= PWM2) D5 = 0; else D5 = 1;
- if(count >= PWM1) D6 = 0; else D6 = 1;
- if(count >= PWM0) D7 = 0; else D7 = 1;
- }
復(fù)制代碼 |
|