This is the source program for drawing two stepper motors, which is compiled by keilc51 and implemented by single chip microcomputer.
# Include <reg52.h>
# Include <math. h>
Sbit cp1 = P1 ^ 0;
Sbit cw1 = P1 ^ 1;
Sbit cp2 = P1 ^ 2;
Sbit cw2 = P1 ^ 3;
Float step (float x1, float y1, float x2, float y2)
{
Float buj;
Float chd1;
Chd1 = sqrt (x1) * (x1) + y1 * y1)-sqrt (x2) * (x2) + y2 * y2 );
Buj = chd1/0.01;
Return buj;
}
Void delay (int I)
{
While (I --);
}
Void buj (float buj1, float buj2)
{
Int I, j, y, B;
If (buj1 <0)
{
CW1 = 1;
Buj1 =-buj1;
}
Else
CW1 = 0;
If (buj2 <0)
{
Cw2 = 1;
Buj2 =-buj2;
}
Else
Cw2 = 0;
If (buj1> buj2 ){
B = buj1/buj2;
Y = buj1-B * buj2;
For (I = 1; I <= buj2; I ++ ){
For (j = 1; j <= B; j ++ ){
Cp1 = 0;
Delay (100 );
Cp1 = 1;
Delay (100 );
}
Cp2 = 0;
Delay (100 );
Cp2 = 1;
Delay (100 );
If (y! = 0 ){
Cp1 = 0;
Delay (100 );
Cp1 = 1;
Delay (100 );
Y --;
}
}
}
Else {
B = buj2/buj1;
Y = buj2-buj1 * B;
For (I = 1; I <= buj1; I ++ ){
For (j = 1; j <= B; j ++ ){
Cp2 = 0;
Delay (100 );
Cp2 = 1;
Delay (100 );
}
Cp1 = 0;
Delay (100 );
Cp1 = 1;
Delay (100 );
If (y! = 0 ){
Cp2 = 0;
Delay (100 );
Cp2 = 1;
Delay (100 );
Y --;
}
}
}
}
Void delay10ms ()
{
Unsigned int I, J;
For (j = 10; J! = 0; j --)
For (I = 125; I! = 0; I --);
}
Void San () // rose Wire
{
R = 20 * sin (3*(-1.5707963 ));
X = 40 + R * Cos (-1.5707963 );
Y = 50 + R * sin (-1.5707963 );
For (I =-1.5707963; I <= 1.5707963; I = I + 3.1415926/180 ){
R = 20 * sin (3 * (I ));
W = 40 + R * Cos (I );
V = 50 + R * sin (I );
Buj1 = step (x + 15,115-y, W + 15,115-V );
Buj2 = step (95-X, 115-y, 95-W, 115-V );
Buj (buj1, buj2 );
X = W;
Y = V;
}
}
Void yuan () {// circle
R = 20;
X = 40;
Y = 30;
For (I =-1.5707963; I <= 4.85; I = I + 3.1415926/180 ){
W = 40 + R * Cos (I );
V = 50 + R * sin (I );
Buj1 = step (x + 15,115-y, w + 15,115-v );
Buj2 = step (95-x, 115-y, 95-w, 115-v );
Buj (buj1, buj2 );
X = w;
Y = v;
}
}
Void star () // star Line
{
Float a1, b1;
R = 20;
A1 = 60;
B1 = 30;
X = a1 + r * cos (1.7 );
Y = b1 + r * sin (1.7 );
For (I = 1.7; I <= 3.1415926; I = I + 3.1415926/180)
{
W = a1 + r * cos (I );
V = b1 + r * sin (I );
Buj1 = step (x + 15,115-y, w + 15,115-v );
Buj2 = step (95-x, 115-y, 95-w, 115-v );
Buj (buj1, buj2 );
X = w;
Y = v;
}
A1 = 20;
B1 = 30;
For (I = 0; I <= 1.5707963; I = I + 3.1415926/180)
{
W = a1 + r * cos (I );
V = b1 + r * sin (I );
Buj1 = step (x + 15,115-y, w + 15,115-v );
Buj2 = step (95-x, 115-y, 95-w, 115-v );
Buj (buj1, buj2 );
X = w;
Y = v;
}
A1 = 20;
B1 = 70;
For (I =-1.5707963; I <= 0; I = I + 3.1415926/180)
{
W = a1 + r * cos (I );
V = b1 + r * sin (I );
Buj1 = step (x + 15,115-y, w + 15,115-v );
Buj2 = step (95-x, 115-y, 95-w, 115-v );
Buj (buj1, buj2 );
X = w;
Y = v;
}
A1 = 60;
B1 = 70;
For (I = 3.1415926; I <= 4.65; I = I + 3.1415926/180)
{
W = a1 + r * cos (I );
V = b1 + r * sin (I );
Buj1 = step (x + 15,115-y, w + 15,115-v );
Buj2 = step (95-x, 115-y, 95-w, 115-v );
Buj (buj1, buj2 );
X = w;
Y = v;
}
}
Void mysin () // y = zhengfu * sin (pinlv * (x-xiangwei) + pianzhi // sine line
{
X = 20;
Y = 30;
V = 20 + 1/0.13*6.6;
R = 1/0.13*6.2831852/360;
For (I = 20; I <= v; I = I + r)
{
W = 15 * sin (0.13 * (I-20) + 30;
Buj1 = step (x + 15,115-y, I + 15,115-w );
Buj2 = step (95-x, 115-y, 95-I, 115-w );
Buj (buj1, buj2 );
X = I;
Y = w;
}
}
Void list () // straight line
{
Buj1 = step (54 + 15,115-50, 40 + 15,115-30 );
Buj2 = step (95-54,115-50, 95-40,115-30 );
Buj (buj1, buj2 );
}
Void main ()
{
Bit Flag;
While (1)
{
Do {
While (key );
Delay10ms ();
If (Key = 0)
{
Flag = 0;
Mysin ();
}
Else
Flag = 1;
} While (flag );
Do {
While (key );
Delay10ms ();
If (key = 0)
{
Flag = 0;
Star ();
}
Else
Flag = 1;
} While (flag );
Do {
While (key );
Delay10ms ();
If (key = 0)
{
Flag = 0;
List ();
}
Else
Flag = 1;
} While (flag );
Do {
While (key );
Delay10ms ();
If (key = 0)
{
Flag = 0;
Yuan ();
}
Else
Flag = 1;
} While (FLAG );
Do {
While (key );
Delay10ms ();
If (Key = 0)
{
Flag = 0;
San ();
}
Else
Flag = 1;
} While (FLAG );
}
}