unsigned
int
a=0
,b=10
void
main()
{
tmod=0x01
th0=(65536-50000)/256
tl0=(65536-50000)%256
ea=1
et0=1
tr0=1
}
void
pwm()
interrupt
1
{
th0=(65536-50000)/256
tl0=(65536-50000)%256
a++
p1^1=0
if(a=20)
{
a=0
}
if(a=b)
{
p1^1=1
}
}
调节a
,b的值即可调节占空比,程序框架就是这样,具体自己改吧
//程序说明:使用内部内部时钟fosc/12产生周期为256us8406占空比为25%的方波#include<STC12C5616AD.h>#define uchar unsigned char #define uint unsigned intsbit d1=P1^0;sbit d2=P1^1;void PWM_init(){CMOD=0x00;//PCA计数脉冲选择内部时钟fosc/02(0x02:fosc/2)CL=0x00;//PCA赋初值CH=0x00;}void PWM0(uchar ZKB){CCAP0L=CCAP0H=ZKB;//255*(100-ZKB)/100;//占空比设置//=;//255*(100-ZKB)/100;//CL由ff-00溢出时vmosCCAP0H的值装入CCAP0LCCAPM0=0x42;// 8位PWM模式CR=1;//开始计数}void PWM1(uchar ZKB)//高电平占得比例占空比为(Gao/255){CCAP1H = CCAP1L = ZKB;//256-(int)(256/100)*Gao;CCAPM1=0x42;// 8位PWM模式CR=1;}void PWM2(uchar ZKB)//高电平占得比例占空比为(Gao/255){CCAP2H = CCAP2L = ZKB;//256-(int)(256/100)*Gao;CCAPM2=0x42;// 8位PWM模式CR=1;}void PWM3(uchar ZKB)//高电平占得比例占空比为(Gao/255){CCAP3H = CCAP3L = ZKB;//256-(int)(256/100)*Gao;CCAPM3=0x42;// 8位PWM模式CR=1;}void delay(uint z) {uint xy;for(x=z;x>0;x--)for(y=110;y>0;y--);}void main(){PWM_init();while(1){if(d1!=0){PWM0(130);PWM1(130);PWM2(255);PWM3(255);}if(d2==1){PWM0(60);PWM1(255);PWM2(20);PWM3(255);delay(300);}i}}若不是新手加上按键应该会的...余下全文>>欢迎分享,转载请注明来源:内存溢出
评论列表(0条)