占空比和频率都可调的两对互补的pwm波,其中由四个按钮来分别控制占空比和频率的加减
- #include<reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- uint shiwan,wan,qian,bai,shi,ge;
- //uint D[]={shiwan,wan,qian,bai,shi,ge};
- uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x80}; //共阴极数码管段码表
- //uchar code wei[]={0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
- uint a, b,f=500;
- sbit P1_0=P1^0;
- sbit P1_1=P1^1;
- sbit SegSel=P2^6; //段选
- sbit BitSel=P2^7;
- sbit P3_6=P3^6;
- sbit P3_7=P3^7;
- void delay_ms(uint x)
- {
- uint i,j;
- for(i=x;i>0;i--)
- for(j=240;j>0;j--)
- ;
- }
- /*void show()
- {
- uint i;
- for(i=0;i<6;i++)
- {
- BitSel=1;P0=wei[i];BitSel=0;SegSel=1;P0=table[D[i]];SegSel=0;delay_ms(1);
- }
- } */
- void display(uint a,uint b,uint c,uint d,uint e,uint f) //数码管显示函数
- {
- P2=0xfe;
- P0=table[f];
- delay_ms(2);
- P2=0xfd;
- P0=table[e];
- delay_ms(2);
- P2=0xfb;
- P0=table[d];
- delay_ms(2);
- P2=0xf7;
- P0=table[c];
- delay_ms(2);
- P2=0xef;
- P0=table[b];
- delay_ms(2);
- P2=0xdf;
- P0=table[a];
- delay_ms(2);
- }
- //------------------------------------------
- void main()
- {
- TMOD = 0x01; //T0定时方式1
- TH0 = (65536-100000/f) / 256; //50ms@12MHz
- TL0 = (65536-100000/f) % 256;
- TR0 = 1;
- ET0 = 1;
- EX0 = 1; EX1 = 1;
- IT0 = 1; IT1 = 1;
- EA = 1;
- a = 0;
- b = 5;
- if(P3_6==1)
- f=f-500;
-
- if(P3_7==1)
- f=f+500;
- if(f<500)
- f=500;
- if(f>9500)
- f=9500;
- shiwan=b/10;
- wan=table[9];
- qian=b%10;
- bai=f/1000;
- shi=table[9];
- ge=f%1000/100;
- display(ge,shi,bai,qian,wan,shiwan);
- while(1);
- }
- //------------------------------------------
- void time0() interrupt 1
- {
- TL0 = (65536-100000/f) % 256;
- TH0 = (65536-100000/f) / 256; //50ms@12MHz
- a++;
- if(a == 10) a = 0; //在这里调整周期.
- if(a < b)
- {P1_0 = 0xff; //在这里调整占空比.
- P1_1 =~P1_0;
- }
- else {
- P1_0 = 0x00;
- P1_1 =~P1_0;}
- }
- //------------------------------------------
- void X0_INT() interrupt 0
- {
- b++;
- if(b > 10) b = 10; //占空比等级最大为19
- }
- //------------------------------------------
- void X1_INT() interrupt 2
- {
- b--;
- if(b < 1) b = 1; //占空比等级最小为1.
- }
复制代码
|