51单片机来做的pwm来无级调光程序通过光耦可控硅来控制可控硅的导通角,从而实现白炽灯亮度的变化.
#i nclude "reg51.h" #i nclude "user.h" #define uchar unsigned char
unsigned int value=100; unsigned int del=0; uchar value_temp=4; uchar temp=0; uchar code tab[11]={0x88,0xbe,0xc4,0x94,0xb2,0x91,0x81,0xbc,0x80,0x90,0xf7}; sbit pwm_port=p3^1; sbit key=p3^7; bit lamp_enable=0;
void display(uchar dat) { p1=tab[dat]; }
void timer_init() { tmod=0x02; th0=0xff; tl0=0xff; px1=1; it1=1;
et0=1; ex1=1; ea=1; display(10); }
void int_t0() interrupt 1 { temp++; if(temp<value) { pwm_port=1; } else { pwm_port=0; } }
void int_x1() interrupt 2 { lamp_enable=1; del=0; }
void main() { timer_init(); while(1) { key=1; if(key==0) { value=value+25; value_temp++; if(value==250) { value=0; value_temp=0; } display(value_temp); delay(20); } if(lamp_enable==0) { pwm_port=0; display(10); tr0=0; } else { tr0=1; display(value_temp); } if(++del==40000) { lamp_enable=0; del=0; } } }
|
阅读(5794) | 评论(0) | 转发(0) |