51单片机实现PWM波占空比可调平台:STC89C52名称:本程序为用单片机制作的占空比均匀间隔可调PWM发生器。占空比可调间隔为0.1。即可取得占空比为0%,10%,20%……90%到100%的PWM波。该波的频率固定为1KH(周期1000微秒)。SY-1学习板上的操作键:S2----占空比加S3----占空比减晶振:11.0592MHZ程序代码:#includereg52.h#defineuintunsignedintuintpp;charnum=2,dis;sbitpwm=P1^0;sbits2=P3^4;sbits3=P3^5;sbitdula=P2^6;sbitwela=P2^7;unsignedcharcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};voidkeyscan(){if(s2==0){while(!s2);num++;if(num==11)num=10;dis=num;}if(s3==0){while(!s3);num--;if(num==-1)num=0;dis=num;}}voiddisplay(a){dula=0;P0=table[a];dula=1;dula=0;wela=0;P0=0xfe;wela=1;wela=0;}voidmain(){TMOD=0x01;//模式设置,00000001,可见采用的是定时器0,工作与模式1(M1=0,M0=1)。TR0=1;//打开定时器TH0=0Xff;//定时器设置,每隔100微秒发起一次中断。TL0=0Xa4;ET0=1;//开定时器0中断EA=1;//开总中断while(1){keyscan();if((num!=0)&&(num!=10))//对于占空比为0和100%这两种极端情况,最好分离出,单独考虑{if(pp=num)pwm=1;elsepwm=0;}elseif(num==0)//当占空比为0pwm=0;elsepwm=1;if(pp==10)//当占空比为100%pp=0;display(num);//显示num当前值,占空比为num/10。}}voidtime0()interrupt1{TH0=0Xff;TL0=0Xa4;pp++;}