51单片机制作简单机械手
时间:10-02
整理:3721RD
点击:
前一段时间用51和舵机做了一个小机械手,感觉这个小玩意很适合新手锻炼技术,给大家分享一下。下面是我的程序。供大家参考共用了5个舵机控制五个关节,用串口通信控制
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sfr T2MOD=0xC9;
sbit k1=P2^0;
sbit k2=P2^1;
sbit k3=P2^2;
sbit k4=P2^3;
sbit k5=P2^4;
sbit n=P1^0;
uchar i,m;
uint t[7]={1000,1000,1000,1000,1000,0,0}; 舵机转角初值
uint d[7];
uint z[7];
uint r[7];
void init()
{
TMOD=0x21;
T2MOD=0x00;
T2CON=0x00;
TH0=(65536-2300)/256;
TL0=(65536-2300)%256;
TH1=0xfd;
TL1=0xfd;
TH2=(65536-18432)/256;
TL2=(65536-18432)%256;
RCAP2H=(65536-18432)/256;
RCAP2L=(65536-18432)%256;
EA=1;
PS=1;
ET0=1;
ET2=1;
TR0=1;
TR1=1;
TR2=1;
SCON=0X50;
ES=1;
}
void main()
{
init();
i=0;
m=0;
while(1);
}
void T0_time() interrupt 1
{
d[i]=256-t[i]/256;
z[i]=(2048-t[i])%256;
TH0=d[i];
TL0=z[i];
i++;
switch(i)
{
case 1:
k1=1;
break;
case 2:
k1=0;
k2=1;
break;
case 3:
k2=0;
k3=1;
break;
case 4:
k3=0;
k4=1;
break;
case 5:
k4=0;
k5=1;
break;
case 6:
k5=0;
i=0;
ET0=0;
break;
}
}
void ser() interrupt 4
{
RI=0;
r[m]=SBUF;
t[m]=700+12*r[m];
m=m+1;
if(m>=7)
{
m=0;
}
}
void time2() interrupt 5
{
TF2=0;
i=0;
ET0=1;
TH0=(65536-2300)/256;
TL0=(65536-2300)%256;
n=~n;
}
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sfr T2MOD=0xC9;
sbit k1=P2^0;
sbit k2=P2^1;
sbit k3=P2^2;
sbit k4=P2^3;
sbit k5=P2^4;
sbit n=P1^0;
uchar i,m;
uint t[7]={1000,1000,1000,1000,1000,0,0}; 舵机转角初值
uint d[7];
uint z[7];
uint r[7];
void init()
{
TMOD=0x21;
T2MOD=0x00;
T2CON=0x00;
TH0=(65536-2300)/256;
TL0=(65536-2300)%256;
TH1=0xfd;
TL1=0xfd;
TH2=(65536-18432)/256;
TL2=(65536-18432)%256;
RCAP2H=(65536-18432)/256;
RCAP2L=(65536-18432)%256;
EA=1;
PS=1;
ET0=1;
ET2=1;
TR0=1;
TR1=1;
TR2=1;
SCON=0X50;
ES=1;
}
void main()
{
init();
i=0;
m=0;
while(1);
}
void T0_time() interrupt 1
{
d[i]=256-t[i]/256;
z[i]=(2048-t[i])%256;
TH0=d[i];
TL0=z[i];
i++;
switch(i)
{
case 1:
k1=1;
break;
case 2:
k1=0;
k2=1;
break;
case 3:
k2=0;
k3=1;
break;
case 4:
k3=0;
k4=1;
break;
case 5:
k4=0;
k5=1;
break;
case 6:
k5=0;
i=0;
ET0=0;
break;
}
}
void ser() interrupt 4
{
RI=0;
r[m]=SBUF;
t[m]=700+12*r[m];
m=m+1;
if(m>=7)
{
m=0;
}
}
void time2() interrupt 5
{
TF2=0;
i=0;
ET0=1;
TH0=(65536-2300)/256;
TL0=(65536-2300)%256;
n=~n;
}
感谢小编的分享。
谢谢小编分享好资料,先收藏了,方便学习交流,我正在收集这方面资料。
1111111111111111111111111