C51单片机脉冲累加器(C语言程序) 200
2个回答
2016-10-20
展开全部
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit key =P2^5;
sbit key1=P2^4;
sbit key2=P2^3;
sbit key3=P2^2;
sbit pwm=P1^0;
uint pwm_value=1960;
uint count=0;
void timer0(void) interrupt 1 using 1
{
switch(count)
{
case 1:pwm=1;
TH0=-pwm_value/256;
TL0=-pwm_value%256;
break;
case 2: pwm=0;
TH0=-(2700-pwm_value)/256;
TL0=-(2700-pwm_value)%256;
break;
/*case 3:pwm1=1;
TH0=-pwm_value/256;
TL0=-pwm_value%256;
break;
case 4: pwm1=0;
TH0=-(2700-pwm_value)/256;
TL0=-(2700-pwm_value)%256;
break;
case 5:pwm2=1;
TH0=-pwm_value/256;
TL0=-pwm_value%256;
break;
case 6: pwm2=0;
TH0=-(2700-pwm_value)/256;
TL0=-(2700-pwm_value)%256;
break;
case 7:pwm3=1;
TH0=-pwm_value/256;
TL0=-pwm_value%256;
break;
case 8: pwm3=0;
TH0=-(2700-pwm_value)/256;
TL0=-(2700-pwm_value)%256;
break;
case 9:pwm4=1;
TH0=-pwm_value/256;
TL0=-pwm_value%256;
break;
case 10: pwm4=0;
TH0=-(2700-pwm_value)/256;
TL0=-(2700-pwm_value)%256;
break;
case 11:pwm5=1;
TH0=-pwm_value/256;
TL0=-pwm_value%256;
break;
case 12: pwm5=0;
TH0=-(2700-pwm_value)/256;
TL0=-(2700-pwm_value)%256;
break;*/
default: count=0;
}
count++;
}
void InitTimer0(void)
{
count=1;
TMOD=0x11;
TH0=-1500/256;
TL0=-1500%256;
EA=1;
ET0=1;
TR0=1;
}
void delay(void)
{
uint i=100;
while(i--);
}
void main(void)
{
InitTimer0();
while(1)
{
if(pwm_value<2200)
pwm_value+=2;
if(pwm_value>1000)
pwm_value-=2;
}
}
#define uchar unsigned char
#define uint unsigned int
sbit key =P2^5;
sbit key1=P2^4;
sbit key2=P2^3;
sbit key3=P2^2;
sbit pwm=P1^0;
uint pwm_value=1960;
uint count=0;
void timer0(void) interrupt 1 using 1
{
switch(count)
{
case 1:pwm=1;
TH0=-pwm_value/256;
TL0=-pwm_value%256;
break;
case 2: pwm=0;
TH0=-(2700-pwm_value)/256;
TL0=-(2700-pwm_value)%256;
break;
/*case 3:pwm1=1;
TH0=-pwm_value/256;
TL0=-pwm_value%256;
break;
case 4: pwm1=0;
TH0=-(2700-pwm_value)/256;
TL0=-(2700-pwm_value)%256;
break;
case 5:pwm2=1;
TH0=-pwm_value/256;
TL0=-pwm_value%256;
break;
case 6: pwm2=0;
TH0=-(2700-pwm_value)/256;
TL0=-(2700-pwm_value)%256;
break;
case 7:pwm3=1;
TH0=-pwm_value/256;
TL0=-pwm_value%256;
break;
case 8: pwm3=0;
TH0=-(2700-pwm_value)/256;
TL0=-(2700-pwm_value)%256;
break;
case 9:pwm4=1;
TH0=-pwm_value/256;
TL0=-pwm_value%256;
break;
case 10: pwm4=0;
TH0=-(2700-pwm_value)/256;
TL0=-(2700-pwm_value)%256;
break;
case 11:pwm5=1;
TH0=-pwm_value/256;
TL0=-pwm_value%256;
break;
case 12: pwm5=0;
TH0=-(2700-pwm_value)/256;
TL0=-(2700-pwm_value)%256;
break;*/
default: count=0;
}
count++;
}
void InitTimer0(void)
{
count=1;
TMOD=0x11;
TH0=-1500/256;
TL0=-1500%256;
EA=1;
ET0=1;
TR0=1;
}
void delay(void)
{
uint i=100;
while(i--);
}
void main(void)
{
InitTimer0();
while(1)
{
if(pwm_value<2200)
pwm_value+=2;
if(pwm_value>1000)
pwm_value-=2;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询