51单片机程序控制舵机,求解释
程序如下,求解释:#include<reg52.h>typedefunsignedintuint;typedefunsignedcharuchar;sbitpwm=P0^...
程序如下,求解释:
#include<reg52.h>
typedef unsigned int uint;
typedef unsigned char uchar;
sbit pwm=P0^3;
uint pwm_value=1500;
uint value[]={1500,1000,1500,2000,1500};
void delay_ms(uint x)
{
uint i;
while(x--)for(i=0;i<125;i++);
}
void timer0(void) interrupt 1
{
pwm=1;
TH0=-20000/256;
TL0=-20000%256;
TR1=1;
}
void timer1(void) interrupt 3
{
pwm=0;
TH1=-pwm_value/256;
TL1=-pwm_value%256;
TR1=0;
}
void main(void)
{
while(1)
{
uchar i;
TMOD=0x11;
TH0=-20000/256;
TL0=-20000%256;
TH1=-1500/256;
TL1=-1500%256;
EA=1;
TR0=1;
ET0=1;
TR1=1;
ET1=1;
pwm_value=1500;
for(i=0;i<5;i++)
{
pwm_value=value[i];
delay_ms(1000);
}
}
}
这个程序用到了2个定时器,定时器0,定时器1,他们具体是怎样配合来工作产生脉冲的,就是这个搞不明白 展开
#include<reg52.h>
typedef unsigned int uint;
typedef unsigned char uchar;
sbit pwm=P0^3;
uint pwm_value=1500;
uint value[]={1500,1000,1500,2000,1500};
void delay_ms(uint x)
{
uint i;
while(x--)for(i=0;i<125;i++);
}
void timer0(void) interrupt 1
{
pwm=1;
TH0=-20000/256;
TL0=-20000%256;
TR1=1;
}
void timer1(void) interrupt 3
{
pwm=0;
TH1=-pwm_value/256;
TL1=-pwm_value%256;
TR1=0;
}
void main(void)
{
while(1)
{
uchar i;
TMOD=0x11;
TH0=-20000/256;
TL0=-20000%256;
TH1=-1500/256;
TL1=-1500%256;
EA=1;
TR0=1;
ET0=1;
TR1=1;
ET1=1;
pwm_value=1500;
for(i=0;i<5;i++)
{
pwm_value=value[i];
delay_ms(1000);
}
}
}
这个程序用到了2个定时器,定时器0,定时器1,他们具体是怎样配合来工作产生脉冲的,就是这个搞不明白 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询