|
不知道为什么加个舵机的程序就不行
#include<reg52.h>
#include<math.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uint i=0,j,URTAReceivedCount=0,n=1,count=10;
uchar data Tempdatatable[2];//数据包
sbit led=P2^7;
void Delay_1ms(uint i)//1ms延时
{
uchar x,j;
for(j=0;j<i;j++)
for(x=0;x<=148;x++);
}
void Time0_Init() //定时器0初始化
{
TMOD=0x01;
IE=0x82;
TH0=0xfe;
TL0=0x33;
TR0=1;
}
void moto_forward()
{
P0=0x02;
Delay_1ms(200);
P0=0x00;
count--;
Tempdatatable[1]=0xff;
}
void Time0_Int() interrupt 1 //定时器0中断
{
TH0=0xfe;
TL0=0x33;
if(jishu<jd) //判断0.5ms次数是否小于角度标识
pwm=1; //确实小于,pwm输出高电平
else
pwm=0; //大于则输出低电平
jishu=(jishu+1);//0.5ms次数加1
jishu=jishu%40; //次数始终保持为40即保持周期为20ms
}
void Com_Int(void) interrupt 4
{
uchar temp;
ES=0; //关串口中断
RI=0; //软件清除接收中断
temp=SBUF;
if(temp==0XFF && URTAReceivedCount<3)
{
Tempdatatable[0]==0XFF; //包头
URTAReceivedCount++;
}
else
{
Tempdatatable[n]=temp;
n++;
}
if(URTAReceivedCount==2)//包尾
{
Tempdatatable[0]=0XFF;
Tempdatatable[2]=0XFF;
n=1;
URTAReceivedCount=0; //组包完毕
temp="";
}
ES=1;//开串口中断
}
void main() //程序入口
{
jishu=0;
jd=1;
Delay_1ms(200);
TMOD = 0x20;
PCON = 0x80;
SCON = 0x50;
TH1 = 0xfA; //设置波特率 9600
TL1 = 0xfA;
TR1 = 1; //启动定时器1
ET1 =0;
ES = 1; //开串口中断
PS=1; //设计串行口中断优先级
EA = 1; //开总中断
IT0=0;
EX0=1;
while(1)
{
if(Tempdatatable[0]==0XFF && Tempdatatable[2]==0XFF)
{
switch(Tempdatatable[1])
{
case 0X02:
moto_forward();
break;
case 0x03: //如果数据包是0x03 舵机角度+1
jd=jd++;
break;
default : TR0=0;TR2=0;
break;
}
}
}
}
|
|