#include <reg51.h>
#include <math.h>
#include <stdlib.h>
#define uchar unsigned char
#define uint unsigned int
#define Port P2
#define pai 3.14159
uchar B1,B2; int i;
sbit a=P1^0; sbit b=P1^1; sbit c=P1^2; sbit d=P1^3;
sbit e=P1^4; sbit f=P1^5; sbit g=P1^6; sbit h=P1^7;
#define step_l 0.026
#define step_r 0.026
uchar Position;
uchar model;
uchar keycode,scancode;
uchar X11,X12,Y11,Y12;
uchar X21,X22,Y21,Y22;
uchar line_input_time=0;
float line_point[2][2];
uchar circle_input_time=0;
float circle_point[1][2];
float circle_r;
uchar moreline=0;
uchar more_line_time=0;
float px[10],py[10];
uchar recieve_point[21];
bit forl=0;
bit ctrl_edit=0;
sbit RS = P3^3;
sbit RW = P3^4;
sbit E = P3^5;
void delay(unsigned time);
unsigned char ReadState_LCD(void);
unsigned char IsBusy_LCD(void);
void WriteData_LCD(unsigned char Data);
void WriteCommand_LCD(unsigned char Code);
void Init_LCD(void);
void PutStr_LCD(unsigned char *str);
void PutStrXY_LCD(unsigned char x, unsigned char y, unsigned char *str);
void PutIntXY_LCD(unsigned char x, unsigned char y, unsigned Num);
void PutNumXY_LCD(unsigned char x, unsigned char y, unsigned Num, unsigned char Len);
void SetPos_LCD(unsigned char x, unsigned char y);
void Delay5Ms(void)
{uchar j; uint i;
for(j=0;j<10;j++) for(i=0;i<500;i++) ;
}
void delay(unsigned time)
{
while(time--);
}
unsigned char ReadState_LCD(void)
{
Port = 0xff;
RS = 0;
RW = 1;
E = 1;
return Port;
}
unsigned char IsBusy_LCD(void)
{
Port = 0xff;
RS = 0;
RW = 1;
E = 1;
return (Port >> 7) & 0x01;
}
void WriteData_LCD(unsigned char Data)
{
while ( IsBusy_LCD() );
RS = 1;
RW = 0;
Port = Data;
E = 0;
E = 1;
E = 0;
}
void WriteCommand_LCD(unsigned char Code)
{
while ( IsBusy_LCD() );
RS = 0;
RW = 0;
Port = Code;
E = 0;
E = 1;
E = 0;
}
void Init_LCD(void)
{
delay(15000);
WriteCommand_LCD(0x38);
delay(5000);
WriteCommand_LCD(0x38);
delay(5000);
WriteCommand_LCD(0x38);
delay(5000);
WriteCommand_LCD(0x38);
WriteCommand_LCD(0x0f);
WriteCommand_LCD(0x08);
WriteCommand_LCD(0x01);
WriteCommand_LCD(0x06);
WriteCommand_LCD(0x0c);
WriteCommand_LCD(0x80);
WriteCommand_LCD(0x0f);
}
void PutStr_LCD(unsigned char *str)
{
while(*str)
{
WriteData_LCD(*str++);
}
}
void PutStrXY_LCD(unsigned char x, unsigned char y, unsigned char *str)
{
SetPos_LCD(x, y);
PutStr_LCD(str);
}
void PutIntXY_LCD(unsigned char x, unsigned char y, unsigned Num)
{
unsigned char i = 0;
unsigned char Len = 1;
if(Num >= 10) Len++;
if(Num >= 100) Len++;
if(Num >= 1000) Len++;
if(Num >= 10000) Len++;
for(i = 0; i < Len; i++)
SetPos_LCD(x + Len - i - 1, y);
WriteData_LCD(Num % 10 + 48);
Num /= 10;
}
void PutNumXY_LCD(unsigned char x, unsigned char y, unsigned Num, unsigned char Len)
{
unsigned char i = 0;
unsigned char Add = ReadState_LCD() & 0x7f;
for(i = 0; i < Len; i++)
{
SetPos_LCD(x + Len - i - 1, y);
WriteData_LCD(Num % 10 + 48);
Num /= 10;
}
}
void SetPos_LCD(unsigned char x, unsigned char y)
{
unsigned char i = y * 0x40 + x;
WriteCommand_LCD(0x80 + i);
}
float utof(uchar c11,uchar c12,uchar c21,uchar c22)
{
float f11=(float)c11,f12=(float)c12,f21=(float)c21,f22=(float)c22;
float n=f11*10+f12+f21/10+f22/100;
return n;
}
void motor_r_frap()
{B2--; if(B2==0) B2=4;
switch (B2)
{case 1:a=1;b=0;c=1;d=0;delay(1000);break;
case 2:a=0;b=1;c=1;d=0;delay(1000);break;
case 3:a=0;b=1;c=0;d=1;delay(1000);break;
case 4:a=1;b=0;c=0;d=1;delay(1000);break;
}
}
void motor_r_loosen()
{B2++; if(B2==5) B2=1;
switch (B2)
{case 1:a=1;b=0;c=1;d=0;delay(1000);break;
case 2:a=0;b=1;c=1;d=0;delay(1000);break;
case 3:a=0;b=1;c=0;d=1;delay(1000);break;
case 4:a=1;b=0;c=0;d=1;delay(1000);break;
}
}
/*****左马达松开线**************************/
void motor_l_loosen()
{B1--; if(B1==0) B1=4;
switch (B1)
{case 1: e=1;f=0;g=1;h=0;delay(1000);break;
case 2: e=0;f=1;g=1;h=0;delay(1000);break;
case 3: e=0;f=1;g=0;h=1;delay(1000);break;
case 4: e=1;f=0;g=0;h=1;delay(1000);break;
}
}
void motor_l_frap()
{B1++; if(B1==5) B1=1;
switch (B1)
{case 1: e=1;f=0;g=1;h=0;delay(1000);break;
case 2: e=0;f=1;g=1;h=0;delay(1000);break;
case 3: e=0;f=1;g=0;h=1;delay(1000);break;
case 4: e=1;f=0;g=0;h=1;delay(1000);break;
}
}
void drive_motor(int c_l,int c_r)
{int j;
for(j=0;j<abs(c_l)||j<abs(c_r);j++)
{
if(j<abs(c_l))
{if(c_l>0) motor_l_loosen();
if(c_l<0) motor_l_frap() ;
}
if(j<abs(c_r))
{if(c_r>0) motor_r_loosen();
if(c_r<0) motor_r_frap() ;
}
}
}
void draw_line(float x0,float y0,float x1,float y1)
{float ll,inc_x,inc_y,xx,yy,L_l0,L_r0,L_l,L_r,inc_Ll,inc_Lr ; (yy=kk*xx+cc)
int count_ll,count_l,count_r,i ;
ll=sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0)) ;
count_ll=ll/0.5+0.5 ;
inc_x=(x1-x0)/count_ll ;
inc_y=(y1-y0)/count_ll ;
L_l0=sqrt((x0+15)*(x0+15)+(115-y0)*(115-y0)) ;
L_r0=sqrt((95-x0)*(95-x0)+(115-y0)*(115-y0)) ;
for(i=1; i<=count_ll; i++)
{xx=inc_x*i+x0 ;// xxx=xx ;
yy=inc_y*i+y0 ;
L_l=sqrt((xx+15)*(xx+15)+(115-yy)*(115-yy)) ;
L_r=sqrt((95-xx)*(95-xx)+(115-yy)*(115-yy)) ;
inc_Ll=L_l-L_l0 ;
inc_Lr=L_r-L_r0 ;
count_l=inc_Ll/step_l+(inc_Ll>=0 ?0.5:-0.5) ;
count_r=inc_Lr/step_r+(inc_Lr>=0 ?0.5:-0.5) ;
drive_motor(count_l,count_r) ;
delay(50) ;
L_l0=L_l ;
L_r0=L_r ;
}
}
void draw_circularity(float rr,float x0,float y0)
{float L_l,L_r,L_l0,L_r0,inc_Ll,inc_Lr,xx,yy,xxx ;
int count_l,count_r,i ;
L_l0=sqrt((x0+rr+15)*(x0+rr+15)+(115-y0)*(115-y0)) ;
L_r0=sqrt((95-x0-rr)*(95-x0-rr)+(115-y0)*(115-y0)) ;
for(i=1;i<=360;i++)
{xx=rr*cos(pai*i/180)+x0 ;
xxx=xx;
yy=rr*sin(pai*i/180)+y0 ;
L_l=sqrt((xx+15)*(xx+15)+(115-yy)*(115-yy)) ;
L_r=sqrt((95-xx)*(95-xx)+(115-yy)*(115-yy)) ;
inc_Ll=L_l-L_l0 ;
inc_Lr=L_r-L_r0 ;
count_l=inc_Ll/step_l+(inc_Ll>=0 ?0.5:-0.5) ;
count_r=inc_Lr/step_r+(inc_Lr>=0 ?0.5:-0.5) ;
drive_motor(count_l,count_r) ;
delay(50) ;
L_l0=L_l ;
L_r0=L_r ;
}
}
void draw_trangle(float tranX1,float tranY1,float tranX2,float tranY2,float tranX3,float tranY3)
{
draw_line(0,0,tranX1,tranY1);
draw_line(tranX1,tranY1,tranX2,tranY2);
draw_line(tranX1,tranY1,tranX2,tranY2);
}
*/
void key_pass()
{Delay5Ms();
Delay5Ms();
switch (keycode)
{
case 0x77:{forl=!forl;PutNumXY_LCD(15,1,forl,1);SetPos_LCD(Position,0);break;}
case 0x7b:{ if(forl==1){motor_l_frap();motor_l_frap();motor_l_frap();motor_l_frap();
motor_l_frap();motor_l_frap();motor_l_frap();motor_l_frap();
motor_l_frap();motor_l_frap();motor_l_frap();motor_l_frap();
motor_l_frap();motor_l_frap();motor_l_frap();motor_l_frap();
motor_l_frap();motor_l_frap();motor_l_frap();motor_l_frap();
}
else{
motor_l_loosen(); motor_l_loosen(); motor_l_loosen(); motor_l_loosen();
motor_l_loosen(); motor_l_loosen(); motor_l_loosen(); motor_l_loosen();
motor_l_loosen(); motor_l_loosen(); motor_l_loosen(); motor_l_loosen();
motor_l_loosen(); motor_l_loosen(); motor_l_loosen(); motor_l_loosen();
motor_l_loosen(); motor_l_loosen(); motor_l_loosen(); motor_l_loosen();
}
break;
}
case 0x7d:{
if(forl==1){
motor_r_frap();motor_r_frap();motor_r_frap();motor_r_frap();
motor_r_frap();motor_r_frap();motor_r_frap();motor_r_frap();
motor_r_frap();motor_r_frap();motor_r_frap();motor_r_frap();
motor_r_frap();motor_r_frap();motor_r_frap();motor_r_frap();
motor_r_frap();motor_r_frap();motor_r_frap();motor_r_frap();
}
else{
motor_r_loosen();motor_r_loosen();motor_r_loosen();motor_r_loosen();
motor_r_loosen();motor_r_loosen();motor_r_loosen();motor_r_loosen();
motor_r_loosen();motor_r_loosen();motor_r_loosen();motor_r_loosen();
motor_r_loosen();motor_r_loosen();motor_r_loosen();motor_r_loosen();
motor_r_loosen();motor_r_loosen();motor_r_loosen();motor_r_loosen();
}
break;
}
case 0x7e:{
if(ctrl_edit==0){
model++;
if(model==7)model=0;
PutNumXY_LCD(0,0,model,1);
}
break;
}
case 0xb7:{
if(ctrl_edit==1){
if(forl==1){
Position++;
if(Position==16)Position=0;
SetPos_LCD(Position, 0);
}
else{
Position--;
if(Position==-1)Position=15;
SetPos_LCD(Position, 0);
}
}
break;
}
case 0xbb:{ if(ctrl_edit==0)}
ctrl_edit=!ctrl_edit;
switch (model)
}
case 0:}
int i=0;
X11=0;X12=0;Y11=0;Y12=0; X21=0;X22=0;Y21=0;Y22=0;
RI=0;
while(!RI);
recieve_point[0]=SBUF;
recieve_point[0]=recieve_point[0]-48;
//PutNumXY_LCD(2,0,X11,1);
if(recieve_point[0]==1) {
RI=0;
while(!RI);
recieve_point[1]=SBUF;
recieve_point[1]=recieve_point[1]-48;
RI=0;
while(!RI);
recieve_point[2]=SBUF;
recieve_point[2]=recieve_point[2]-48;
RI=0;
while(!RI);
recieve_point[3]=SBUF;
recieve_point[3]=recieve_point[3]-48;
RI=0;
while(!RI);
recieve_point[4]=SBUF;
recieve_point[4]=recieve_point[4]-48;
RI=0;
while(!RI);
recieve_point[5]=SBUF;
recieve_point[5]=recieve_point[5]-48;
RI=0;
while(!RI);
recieve_point[6]=SBUF;
ecieve_point[6]=recieve_point[6]-48;
RI=0;
while(!RI);
recieve_point[7]=SBUF;
recieve_point[7]=recieve_point[7]-48;
RI=0;
while(!RI);
recieve_point[8]=SBUF;
recieve_point[8]=recieve_point[8]-48;
X11=recieve_point[1]; X12=recieve_point[2];
X21=0;
X22=0;
Y11=recieve_point[3];
Y12=recieve_point[4];
Y21=0;
Y22=0;
PutStrXY_LCD(4,0,".");
PutStrXY_LCD(10,0,".");
PutNumXY_LCD(2,0,X11,1);
PutNumXY_LCD(3,0,X12,1);
PutNumXY_LCD(5,0,X21,1);
PutNumXY_LCD(6,0,X22,1);
PutNumXY_LCD(8,0,Y11,1);
PutNumXY_LCD(9,0,Y12,1);
PutNumXY_LCD(11,0,Y21,1);
PutNumXY_LCD(12,0,Y22,1);
line_point[0][0]=utof(X11,X12,X21,X22);
line_point[0][1]=utof(Y11,Y12,Y21,Y22);
for(i=0;i<100;i++)
{
Delay5Ms();
}
X11=recieve_point[5];//将接收来的数
X12=recieve_point[6];
X21=0;
X22=0;
Y11=recieve_point[7];
Y12=recieve_point[8];
Y21=0;
Y22=0;
PutStrXY_LCD(4,0,".");
PutStrXY_LCD(10,0,".");
PutNumXY_LCD(2,0,X11,1);
PutNumXY_LCD(3,0,X12,1);
PutNumXY_LCD(5,0,X21,1);
PutNumXY_LCD(6,0,X22,1);
PutNumXY_LCD(8,0,Y11,1);
PutNumXY_LCD(9,0,Y12,1);
PutNumXY_LCD(11,0,Y21,1);
PutNumXY_LCD(12,0,Y22,1);
line_point[1][0]=utof(X11,X12,X21,X22);
line_point[1][1]=utof(Y11,Y12,Y21,Y22);
for(i=0;i<100;i++)
{
Delay5Ms();
}
PutStrXY_LCD(2,0," ");
SBUF =recieve_point[0]+48; while(!TI);
TI = 0;
SBUF =recieve_point[1]+48; while(!TI);
TI = 0;
SBUF =recieve_point[2]+48; while(!TI);
TI = 0;
SBUF =recieve_point[3]+48; while(!TI);
TI = 0;
SBUF =recieve_point[4]+48; while(!TI);
TI = 0;
SBUF =recieve_point[5]+48;
while(!TI);
TI = 0;
SBUF =recieve_point[6]+48;
while(!TI);
TI = 0;
SBUF =recieve_point[7]+48;
while(!TI);
TI = 0;
SBUF =recieve_point[8]+48;
while(!TI);
TI = 0;
|