哈工大嵌入式系统实验8源码(步进电机、电动机正反转等)
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
if(v1>=0.5)
angle=(int)((v1-0.5)/0.1);
else if(v1<=-0.5)
angle=(int)((v1+0.5)/0.1);
else
{
angle=0;
v1=v2=0;
}
}
else
{
v1=current;
if(-0.5<v1&&v1<0.5)
{
angle=0;
v1=v2=0;
char wordZ[32]={0x00,0x02,0x40,0x02,0x40,0x02,0x43,0xFE,0x40,0x02,0x40,0x02,0x40,0x02,0x7F,0xFE,
0x41,0x02,0x41,0x02,0x41,0x02,0x41,0x02,0x41,0x02,0x40,0x02,0x00,0x02,0x00,0x00};
{
printf("Fail to open leds.\n");
return 1;
}
/*-------------------------Open finish----------------------------*/
if(!fork())
{
v1=v2=0;
angle=0;
send_cmd(fdm,MOTOR_BJDJ);
static int my16to10(char num)
{
int end;
switch(num)
{
case '0':
end = 0;
break;
case '1':
end = 1;
break;
case '2':
end = 2;
break;
case '3':
end = 3;
break;
case '4':
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
loop=(++loop)%4;
sleep(1);
}
}
else if(angle<0)
{
angle=-angle;
for(i=0,loop=0;i<angle;i++)
{
write(fdm,&F[loop],1);
printf("fan zhuan,loop:%d,angle%d\n",loop,angle);
printf("Fail to open seg.\n");
return 1;
}
//OPEN AD
if((fdad=open("/dev/i2c-3",O_RDWR))<0)
{
printf("Fail to open i2c-3.\n");
return 1;
}
if (ioctl(fdad, I2C_SLAVE, 0x48) < 0)
case 'b':
end = 11;
break;
case 'c':
end = 12;
break;
caBaidu Nhomakorabeae 'd':
end = 13;
break;
case 'e':
end = 14;
break;
case 'f':
end = 15;
break;
default:
printf("error num:%d\n",num);
}
else
{
angle=(int)((v1-v2)/0.1);
v2=v1;
}
}
printf("v1:%lf,v2:%lf,angle:%d\n",v1,v2,angle);
if(angle>0)
{
for(i=0,loop=0;i<angle;i++)
{
write(fdm,&Z[loop],1);
printf("zheng zhuan,loop:%d,angle%d\n",loop,angle);
char wordF[32]={0x00,0x02,0x00,0x0C,0x3F,0xF1,0x24,0x01,0x24,0x02,0x27,0x02,0x24,0xC4,0x24,0x28,
0x44,0x10,0x44,0x28,0x44,0x44,0xC5,0x82,0x46,0x02,0x00,0x01,0x00,0x01,0x00,0x00};
while(1)
{
//////////////ad working//////////////////
read (fdad, buffer, 2 *sizeof(char));
printf ("ad value = %x%x;\n",buffer[0], buffer[1]);
sprintf(temp,"%02x%02x",buffer[0],buffer[1]);
//////////////ZLDJ and led working//////////////////
if(num == 0x01)
{
printf("ZLDJ Z\n");
firstaddr= 0x02;
write(fdm,&firstaddr,1);
write(fdl,wordZ,32);
}
else if(num == 0x02)
}
sleep(1);
}
}
else
{
wait(NULL);
wait(NULL);
}
send_cmd(fdm, 0x00);
close(fd);
close(fdad);
close(fdm);
close(fdw);
close(fdl);
return 0;
}
if( write(fd, (unsigned int *)&V, sizeof(V)) < 0)
{
printf("fail to write\n");
return -1;
}
//////////////BJDJ working//////////////////
if(0==v2)
{
v1=v2=current;
}
void send_cmd(int fd, int type)
{
int sbuf[3] = {'@', 0x00, '@'};
int i = 5;
sbuf[1] = '0'+type;
while(i--)
{
write(fd, sbuf, 3);
usleep(1000 * 30);
}
}
int main(int argc,char** argv)
end = 4;
break;
case '5':
end = 5;
break;
case '6':
end = 6;
break;
case '7':
end = 7;
break;
case '8':
end = 8;
break;
case '9':
end = 9;
break;
case 'a':
end = 10;
break;
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#define MOTOR_ZLDJ0x01
#define MOTOR_BJDJ0x02
char Z[4] = {0x45, 0xc4, 0xc1, 0x85};
char F[4] = {0x85, 0xc1, 0xc4, 0x45};
{
printf("ioctl error");
return 1;
}
//OPEN SWITCH
if((fdw=open("/dev/switch",O_RDWR))<0)
{
printf("Fail to open switch.\n");
return 1;
}
//OPEN LED
if((fdl=open("/dev/leds",O_RDWR))<0)
exit( -1 );
break;
}
return end;
}
static int value( char *buffer)
{
int sum = 0;
char * p = buffer;
for( ; *p !='\0';p++)
{
sum = sum*16 + my16to10( *p );
}
return sum;
loop=(++loop)%4;
sleep(1);
}
}
angle=0;
sleep(1);
}
}else if(!fork())
{
//////////////working//////////////////
while( 1 )
{
char firstaddr;
unsigned int num;
send_cmd(fdm,MOTOR_ZLDJ);
//////////////switch working//////////////////
if(read(fdw,&num,sizeof(num))<0)
{
perror("read error ");
exit(-1);
}
num = num & 0x03;
printf("switch :%#010x\n",num);
{
int fd,fdad,fdm,fdw,fdl;
double v1,v2;
int angle,i,loop;
char temp[8],buffer[8];
//OPEN SEG
fd=open("/dev/seg",O_RDWR);
if((fd=open("/dev/seg",O_RDWR))<0)
{
{
printf("ioctl error.\n");
return 1;
}
//OPEN MOTOR
if((fdm=open("/dev/i2c-3",O_RDWR))<0)
{
printf("Fail to open i2c-3.\n");
return 1;
}
if (ioctl(fdm, I2C_SLAVE_FORCE,0x20) < 0)
printf("v+ - v- : %f V\n\n",(( short ) value( temp ) * 2.048 /32768));
double current=(( short ) value( temp ) * 2.048 /32768);
int V=current*100+500;
//////////////seg working//////////////////
char wordS[32]={0x01,0x00,0x06,0x00,0x1F,0xFE,0xE0,0x00,0x00,0xC0,0x20,0x80,0x2E,0xA0,0x2A,0xA2,
0xAA,0xA1,0x6A,0xBE,0x2A,0xA0,0x2A,0xA0,0x2E,0xA0,0x20,0x80,0x00,0xC0,0x00,0x00};
{
printf("ZLDJ F\n");
firstaddr=0x10;
write(fdm,&firstaddr,1);
write(fdl,wordF,32);
}
else
{
printf("ZLDJ S\n");
firstaddr=0x00;
write(fdm,&firstaddr,1);
write(fdl,wordS,32);
angle=(int)((v1-0.5)/0.1);
else if(v1<=-0.5)
angle=(int)((v1+0.5)/0.1);
else
{
angle=0;
v1=v2=0;
}
}
else
{
v1=current;
if(-0.5<v1&&v1<0.5)
{
angle=0;
v1=v2=0;
char wordZ[32]={0x00,0x02,0x40,0x02,0x40,0x02,0x43,0xFE,0x40,0x02,0x40,0x02,0x40,0x02,0x7F,0xFE,
0x41,0x02,0x41,0x02,0x41,0x02,0x41,0x02,0x41,0x02,0x40,0x02,0x00,0x02,0x00,0x00};
{
printf("Fail to open leds.\n");
return 1;
}
/*-------------------------Open finish----------------------------*/
if(!fork())
{
v1=v2=0;
angle=0;
send_cmd(fdm,MOTOR_BJDJ);
static int my16to10(char num)
{
int end;
switch(num)
{
case '0':
end = 0;
break;
case '1':
end = 1;
break;
case '2':
end = 2;
break;
case '3':
end = 3;
break;
case '4':
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
loop=(++loop)%4;
sleep(1);
}
}
else if(angle<0)
{
angle=-angle;
for(i=0,loop=0;i<angle;i++)
{
write(fdm,&F[loop],1);
printf("fan zhuan,loop:%d,angle%d\n",loop,angle);
printf("Fail to open seg.\n");
return 1;
}
//OPEN AD
if((fdad=open("/dev/i2c-3",O_RDWR))<0)
{
printf("Fail to open i2c-3.\n");
return 1;
}
if (ioctl(fdad, I2C_SLAVE, 0x48) < 0)
case 'b':
end = 11;
break;
case 'c':
end = 12;
break;
caBaidu Nhomakorabeae 'd':
end = 13;
break;
case 'e':
end = 14;
break;
case 'f':
end = 15;
break;
default:
printf("error num:%d\n",num);
}
else
{
angle=(int)((v1-v2)/0.1);
v2=v1;
}
}
printf("v1:%lf,v2:%lf,angle:%d\n",v1,v2,angle);
if(angle>0)
{
for(i=0,loop=0;i<angle;i++)
{
write(fdm,&Z[loop],1);
printf("zheng zhuan,loop:%d,angle%d\n",loop,angle);
char wordF[32]={0x00,0x02,0x00,0x0C,0x3F,0xF1,0x24,0x01,0x24,0x02,0x27,0x02,0x24,0xC4,0x24,0x28,
0x44,0x10,0x44,0x28,0x44,0x44,0xC5,0x82,0x46,0x02,0x00,0x01,0x00,0x01,0x00,0x00};
while(1)
{
//////////////ad working//////////////////
read (fdad, buffer, 2 *sizeof(char));
printf ("ad value = %x%x;\n",buffer[0], buffer[1]);
sprintf(temp,"%02x%02x",buffer[0],buffer[1]);
//////////////ZLDJ and led working//////////////////
if(num == 0x01)
{
printf("ZLDJ Z\n");
firstaddr= 0x02;
write(fdm,&firstaddr,1);
write(fdl,wordZ,32);
}
else if(num == 0x02)
}
sleep(1);
}
}
else
{
wait(NULL);
wait(NULL);
}
send_cmd(fdm, 0x00);
close(fd);
close(fdad);
close(fdm);
close(fdw);
close(fdl);
return 0;
}
if( write(fd, (unsigned int *)&V, sizeof(V)) < 0)
{
printf("fail to write\n");
return -1;
}
//////////////BJDJ working//////////////////
if(0==v2)
{
v1=v2=current;
}
void send_cmd(int fd, int type)
{
int sbuf[3] = {'@', 0x00, '@'};
int i = 5;
sbuf[1] = '0'+type;
while(i--)
{
write(fd, sbuf, 3);
usleep(1000 * 30);
}
}
int main(int argc,char** argv)
end = 4;
break;
case '5':
end = 5;
break;
case '6':
end = 6;
break;
case '7':
end = 7;
break;
case '8':
end = 8;
break;
case '9':
end = 9;
break;
case 'a':
end = 10;
break;
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#define MOTOR_ZLDJ0x01
#define MOTOR_BJDJ0x02
char Z[4] = {0x45, 0xc4, 0xc1, 0x85};
char F[4] = {0x85, 0xc1, 0xc4, 0x45};
{
printf("ioctl error");
return 1;
}
//OPEN SWITCH
if((fdw=open("/dev/switch",O_RDWR))<0)
{
printf("Fail to open switch.\n");
return 1;
}
//OPEN LED
if((fdl=open("/dev/leds",O_RDWR))<0)
exit( -1 );
break;
}
return end;
}
static int value( char *buffer)
{
int sum = 0;
char * p = buffer;
for( ; *p !='\0';p++)
{
sum = sum*16 + my16to10( *p );
}
return sum;
loop=(++loop)%4;
sleep(1);
}
}
angle=0;
sleep(1);
}
}else if(!fork())
{
//////////////working//////////////////
while( 1 )
{
char firstaddr;
unsigned int num;
send_cmd(fdm,MOTOR_ZLDJ);
//////////////switch working//////////////////
if(read(fdw,&num,sizeof(num))<0)
{
perror("read error ");
exit(-1);
}
num = num & 0x03;
printf("switch :%#010x\n",num);
{
int fd,fdad,fdm,fdw,fdl;
double v1,v2;
int angle,i,loop;
char temp[8],buffer[8];
//OPEN SEG
fd=open("/dev/seg",O_RDWR);
if((fd=open("/dev/seg",O_RDWR))<0)
{
{
printf("ioctl error.\n");
return 1;
}
//OPEN MOTOR
if((fdm=open("/dev/i2c-3",O_RDWR))<0)
{
printf("Fail to open i2c-3.\n");
return 1;
}
if (ioctl(fdm, I2C_SLAVE_FORCE,0x20) < 0)
printf("v+ - v- : %f V\n\n",(( short ) value( temp ) * 2.048 /32768));
double current=(( short ) value( temp ) * 2.048 /32768);
int V=current*100+500;
//////////////seg working//////////////////
char wordS[32]={0x01,0x00,0x06,0x00,0x1F,0xFE,0xE0,0x00,0x00,0xC0,0x20,0x80,0x2E,0xA0,0x2A,0xA2,
0xAA,0xA1,0x6A,0xBE,0x2A,0xA0,0x2A,0xA0,0x2E,0xA0,0x20,0x80,0x00,0xC0,0x00,0x00};
{
printf("ZLDJ F\n");
firstaddr=0x10;
write(fdm,&firstaddr,1);
write(fdl,wordF,32);
}
else
{
printf("ZLDJ S\n");
firstaddr=0x00;
write(fdm,&firstaddr,1);
write(fdl,wordS,32);