当前位置:文档之家› 单片机六自由度机械手控制程序

单片机六自由度机械手控制程序

单片机六自由度机械手控制程序
#include<reg51.h>
#include<absacc.h>
#include<stdio.h>
#define uint unsigned int #define uchar unsigned char #define COM1 XBYTE[0x5800] #define C01 XBYTE[0x4000] #define C11 XBYTE[0x4800]
#define C21 XBYTE[0x5000] #define COM2 XBYTE[0x3800] #define C02 XBYTE[0x2000] #define C12 XBYTE[0x2800] #define C22 XBYTE[0x3000] sbit k1=P3^2;//电机复位按钮
sbit k2=P3^3;//电机选择按钮
sbit k3=P3^4;//电机正转
sbit k4=P3^5;//电机反转
sbit rs=P2^0;
sbit rw=P2^1;
sbit en=P2^2;
uint m=0,i=0;
void reservo();
void lcd(uint i);
void timer(uint n);
void delay(uint n);
void lcd_init();
void lcd_wcom(uchar com); void lcd_wdat(uchar dat); void
lcd_wndat(uint dat); void delay(uint n);
void init(void);
void EXT1_INT(void)
{ EX1=1;
IT1=1;
EA=1;
}
void EXT0_INT()
{ EX0=1;
IT0=1;
EA=1;
}
void EXT1_INT_SRV() interrupt 2 {
i++;
}
//主程序
void main() {
while(1)
{if(k1==0)
{reservo();//电机复位程序break;}
}
EXT1_INT();//中断初始化
if(i!=0&&i%6==0)
i=6;
else
i=i%6;
lcd(i);
timer(i);
}
//电机复位程序
void reservo() {
EXT0_INT();
delay(200); }
void EXT0_INT_SRV() interrupt 0 {
TMOD=0x01;
TH0=0xB1;
TL0=0xE0;
ET0=1;
EA=1;
TR0=1;
COM1=0x30;
C01=0xdc; C01=0x05;
COM1=0x70;
C11=0xdc; C11=0x05;
COM1=0xB0;
C21=0xdc; C21=0X05;
COM2=0x30;
C02=0xdc; C02=0x05;
COM2=0x70;
C12=0xdc; C12=0x05;
COM2=0xB0;
C22=0xdc; C22=0X05; }
//显示屏程序
void lcd(uint i)
{ uint n;
uchar code table[]="servo"; lcd_init();
lcd_wcom(0x80);
for(n=0;n<16;n++)
{
lcd_wdat(table[m]);
delay(200);
}
lcd_wcom(0x80+0x07); switch(i) {
case 1: lcd_wndat(1);
delay(200);
break;
case 2: lcd_wndat(2);
delay(200);
break;
case 3: lcd_wndat(3);
delay(200);
break;
case 4: lcd_wndat(4);
delay(200);
break;
case 5: lcd_wndat(5);
delay(200);
break;
case 6: lcd_wndat(6);
delay(200);
}
}
void delay(uint n)
{
uint x,y;
for(x=n;x>0;x--)
for(y=110;y>0;y--);
}
void lcd_wcom(uchar com)//1602写命令程序 { rs=0;
rw=0;
P0=com;
delay(5);
en=1;
en=0;
}
void lcd_wdat(uchar dat)//1602写数据程序 { rs=1;
rw=0;
P0=dat;
delay(5);
en=1;
en=0;
}
void lcd_wndat(uint dat)//1602写数据程序 { rs=1;
rw=0;
P0=dat;
delay(5);
en=1;
en=0;
}
void lcd_init() {
lcd_wcom(0x38);
lcd_wcom(0x0c);
lcd_wcom(0x06);
lcd_wcom(0x01); } //舵机程序
void timer(uint n) {
init();
uint n;
for(n=1;n<=10;n++) {if(k3==0) servozheng();
else
break;
}
n=1;
for(;n<=10;n++) {if(k4==0)
void servofan();} }
void init(void) {
TMOD=0x01;
TH0=0xB1;
TL0=0xE0;
ET0=1;
EA=1;
TR0=1;
}
void servozheng() interrupt 1 {
TH0=0xB1;
TL0=0xE0;
switch(i)
{
case1: com1=0x30;
c01=0x(dc+64*n); c01=0x05; break;
case2: com1=0x70;
c11=0x(dc+64*n); c11=0x05; break;
case3: com1=0xB0;
c21=0x(dc+64*n); c21=0x05; break;
case4: com1=0x30;
c02=0x(dc+64*n); c02=0x05; break;
case5: com1=0x70;
c12=0x(dc+64*n); c12=0x05; break;
case6: com1=0xB0;
c22=0x(dc+64*n); c22=0x05; }
}
void servofan() interrupt 1 {
TH0=0xB1;
TL0=0xE0;
switch(i)
{
case1: com1=0x30;
c01=0x(dc-64*n); c01=0x05; break;
case2: com1=0x70;
c11=0x(dc-64*n); c11=0x05; break;
case3: com1=0xB0;
c21=0x(dc-64*n); c21=0x05; break;
case4: com1=0x30;
c02=0x(dc-64*n); c02=0x05; break;
case5: com1=0x70;
c12=0x(dc-64*n); c12=0x05; break;
case6: com1=0xB0;
c22=0x(dc-64*n); c22=0x05; }
}。

相关主题