自己(新手)仿照教程编了个PWM控制流水灯亮度的程序,但却不能控制其亮度,不知道错在什么地方,请高手指教,多谢!
void delay(unsigned char t); void delayms(unsigned char ms); void beep(); sbit K1 =P1^4 ; //PWM值增加键 sbit K2 =P1^5 ; //PWM值减少键 sbit BEEP =P3^3 ; //蜂鸣器 unsigned char PWM=0x7f ; //赋初值
main() { P1=0xff; TMOD=0x12; TH0=0xff; TL0=0xff; TH1=0x00; TL1=PWM; EA=1; ET0=1; ET1=1; //开中断 TR0=1;TR1=0; while(1) { if(K1==0) //调亮 { if(PWM==0xff) beep(); else {PWM++; delayms(10);} }
if(K2==0) //调亮 { if(PWM==0x00) beep(); else {PWM--; delayms(10);} } } }
void time0() interrupt 1 { TR1=0; TL1=PWM; TR1=1; P0=0x00; //开灯 }
void time1() interrupt 2 { TR1=0; P0=0xff; //关灯 }
void delay(unsigned char t) { while(t--) ; }
void delayms(unsigned char ms) { unsigned char i; while(ms--) { for(i=0;i<120;i++); } }
|