|
|
发表于 2018-7-1 14:10:01
|
显示全部楼层
- #include<reg51.h>
- sbit led_r=P3^2;//É趨LED¡¢°´¼ü½Åλ
- sbit led_g=P3^3;
- sbit led_b=P3^4;
- sbit key1=P3^1;
- sbit key2=P3^0;
- unsigned char mode,key1_old,key1_action,key2_old,key2_action,light=100,light_back,count,second,duty_r,duty_g,duty_b;
- //É趨±äÁ¿ ģʽ£¬¼ü1¼Ç¼£¬¼ü1¾ÍÐ÷£¬¼ü2¼Ç¼£¬¼ü2¾ÍÐ÷£¬ÁÁ¶È³õʼֵ100£¬ÁÁ¶Èϵ÷±êÖ¾£¬10ºÁÃë¼ÆÊýÖÜÆÚ£¬ÃëÊý£¬ºìÉ«ÁÁ¶È¿ÕÕ¼±È100¼¶£¬ÂÌÉ«¿ÕÕ¼±È£¬À¶É«¿ÕÕ¼±È
- unsigned int count2,count3=3333,count4=6666;//É趨±äÁ¿ 1Ãë¼ÆÊýÖÜÆÚ
- void Delay100us() //@12.000MHz ¶¨Òå0.1ºÁÃëÑÓʱ
- {
- unsigned char i, j;
- i = 2;
- j = 39;
- do
- {
- while (--j);
- } while (--i);
- }
- void delay(unsigned int n)//100n us
- {
- while(n--)
- {
- Delay100us();
- }
- }
- main()
- {
- while(1)
- {
- Delay100us();//0.1ºÁÃë1¸öÑ»·
- count++;//¼ÆÊýÆ÷£¬Ò»¸öÖÜÆÚ10ºÁÃë 100¸öÑ»· 1Ãë100ÖÜÆÚ 100hz
- if(count>99)
- {
- count=0;
- }
- count2++;//¼ÆÊýÆ÷2£¬Ò»¸öÖÜÆÚ1Ãë
- if(count2>9999)
- {
- count2=0;
- second++;
- if(second>3)//ÃëÊý¼Ç¼£¬Ò»¸öÖÜÆÚ4Ãë
- {
- second=0;
- }
- }
-
- count3++;//¼ÆÊýÆ÷3£¬Ò»¸öÖÜÆÚ1Ãë
- if(count3>9999)
- {
- count3=0;
- }
-
- count4++;//¼ÆÊýÆ÷4£¬Ò»¸öÖÜÆÚ1Ãë
- if(count4>9999)
- {
- count4=0;
- }
- if(count==0)//°´¼ü¼ì²â£¬10ºÁÃë1´Î
- {
- if(key1==0&&key1_old==1)
- {
- key1_action=1;
- }
- if(key2==0&&key2_old==1)
- {
- key2_action=1;
- }
- key1_old=key1;//±£´æ°´¼ü
- key2_old=key2;
- if(key1_action==1)//°´¼ü1¸Ä±äģʽ
- {
- key1_action=0;
- mode++;
- if(mode>9)
- {
- mode=0;
- }
- }
- if(key2_action==1)//°´¼ü2¸Ä±äÁÁ¶È10¼¶£¬¹²100¼¶ÁÁ¶È
- {
- key2_action=0;
- if(light_back==0)
- {
- light+=10;
- if(light>99)
- {
- light_back=1;
- }
- }
- if(light_back==1)
- {
- light-=10;
- if(light==0)
- {
- light_back=0;
- }
- }
- }
- }
- if(mode==0)//ģʽ0¹ØµÆ
- {
- duty_r=0;
- duty_g=0;
- duty_b=0;
- }
-
- if(mode==1)//ģʽ1 È«ÁÁ
- {
- duty_r=light;
- duty_g=light;
- duty_b=light;
- }
-
- if(mode==2)//ģʽ2 ±¬ÉÁ
- {
- if(count2<1000)
- {
- duty_r=light;
- duty_g=light;
- duty_b=light;
- }
- else
- {
- duty_r=0;
- duty_g=0;
- duty_b=0;
- }
- }
-
- if(mode==3)//ģʽ3 Ë«±¬ÉÁ 1ÃëÖÜÆÚ
- {
- if(count2<1000)
- {
- duty_r=light;
- duty_g=light;
- duty_b=light;
- }
- else if(count2>=1000&&count2<2000)
- {
- duty_r=0;
- duty_g=0;
- duty_b=0;
- }
- else if(count2>=2000&&count2<3000)
- {
- duty_r=light;
- duty_g=light;
- duty_b=light;
- }
- else
- {
- duty_r=0;
- duty_g=0;
- duty_b=0;
- }
- }
- if(mode==4)//ģʽ4 ÁÁ°ëÃëÃð°ëÃë
- {
- if(count2<5000)
- {
- duty_r=light;
- duty_g=light;
- duty_b=light;
- }
- else
- {
- duty_r=0;
- duty_g=0;
- duty_b=0;
- }
- }
-
-
- if(mode==5)//ģʽ5 4ÃëÂýË«ÉÁ
- {
- if(count2<5000&&second==0)
- {
- duty_r=light;
- duty_g=light;
- duty_b=light;
- }
- else if(count2>5000&&second==0)
- {
- duty_r=0;
- duty_g=0;
- duty_b=0;
- }
- else if(count2<5000&&second==1)
- {
- duty_r=light;
- duty_g=light;
- duty_b=light;
- }
- else
- {
- duty_r=0;
- duty_g=0;
- duty_b=0;
- }
- }
-
- if(mode==6)//ģʽ6 4Ãë7É«ÅÜÂíµÆ
- {
- if(count2<5000&&second==0)
- {
- duty_r=light;
- duty_g=0;
- duty_b=0;
- }
-
- if(count2>5000&&second==0)
- {
- duty_r=light;
- duty_g=light;
- duty_b=0;
- }
-
- if(count2<5000&&second==1)
- {
- duty_r=0;
- duty_g=light;
- duty_b=0;
- }
-
- if(count2>5000&&second==1)
- {
- duty_r=0;
- duty_g=light;
- duty_b=light;
- }
-
- if(count2<5000&&second==2)
- {
- duty_r=0;
- duty_g=0;
- duty_b=light;
- }
-
- if(count2>5000&&second==2)
- {
- duty_r=light;
- duty_g=0;
- duty_b=light;
- }
-
- if(count2<5000&&second==3)
- {
- duty_r=light;
- duty_g=light;
- duty_b=light;
- }
-
- if(count2>5000&&second==3)
- {
- duty_r=0;
- duty_g=0;
- duty_b=0;
- }
-
- }
-
- if(mode==7)//ģʽ7 1ÃëºôÎüµÆ
- {
- if(count2<5000)
- {
- duty_r=count2/50;//0-100
- duty_g=count2/50;
- duty_b=count2/50;
- }
- else
- {
- duty_r=(10000-count2)/50;
- duty_g=(10000-count2)/50;
- duty_b=(10000-count2)/50;
- }
- }
- if(mode==8)//ģʽ8 4ÃëÂýºôÎü
- {
- if((count2+10000*second)<25000)
- {
- duty_r=(count2+10000*second)/250;
- duty_g=(count2+10000*second)/250;
- duty_b=(count2+10000*second)/250;
- }
- else
- {
- duty_r=(40000-(count2+10000*second))/150;
- duty_g=(40000-(count2+10000*second))/150;
- duty_b=(40000-(count2+10000*second))/150;
- }
- }
- if(mode==9)//ģʽ9 7²ÊºôÎü
- {
- if((count2+10000*second)<25000)
- {
- duty_r=(count2+10000*second)/250;
- }
- else
- {
- duty_r=(40000-(count2+10000*second))/150;
- }
-
- if((count3+10000*second)<25000)
- {
- duty_r=(count3+10000*second)/250;
- }
- else
- {
- duty_r=(40000-(count3+10000*second))/150;
- }
-
- if((count4+10000*second)<25000)
- {
- duty_r=(count4+10000*second)/250;
- }
- else
- {
- duty_r=(40000-(count4+10000*second))/150;
- }
- if(key2==0)
- {
- delay(10000);
- }
- }
- if(duty_r>count)//ºìÉ« pwm
- {
- led_r=1;
- }
- else
- {
- led_r=0;
- }
- if(duty_g>count)//ÂÌÉ« pwm
- {
- led_g=1;
- }
- else
- {
- led_g=0;
- }
- if(duty_b>count)//À¶É« pwm
- {
- led_b=1;
- }
- else
- {
- led_b=0;
- }
- }
- }
复制代码 |
| |