Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] To draw a circle in CNC using AT89S52

Status
Not open for further replies.

MD_SHAHRUKH

Advanced Member level 4
Joined
Jun 5, 2017
Messages
103
Helped
4
Reputation
8
Reaction score
5
Trophy points
18
Location
Bangalore, India
Activity points
1,107
Hello everyone,
I have made a small CNC machine, in which I have made a square shape. I want to draw a circle with this method.
here is my code.

Code ASM - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;NAME: MD SHAHRUKH
;PROGRAM: TO MAKE MULTIPLE POSIBLE MOVEMENT                     
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
;DIRECTIVES
 
SP1_1           BIT             P0.0
SP1_2           BIT             P0.1
SP1_3           BIT             P0.2
SP1_4           BIT             P0.3
SP2_1           BIT             P0.4
SP2_2           BIT             P0.5
SP2_3           BIT             P0.6
SP2_4           BIT             P0.7
SP12            EQU             P0
        
R8                      EQU             30H
R9                      EQU             31H
R10                     EQU             32H
R11                     EQU             33H
R12                     EQU             34H
R13                     EQU             35H
R14                     EQU             36H
R15                     EQU             37H
R16                     EQU             38H
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;PROGRAM STARTS HERE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                                ORG                     00H
                                MOV                     R3,#02
                                MOV                     R4,#02
                                MOV                     R7,#50H
                                MOV                     R6,#50H
                                MOV                     R15,#50H
                                MOV                     R14,#50H
                                MOV                     R16,#50H
                                MOV                     R8,#02
                                MOV                     R9,#02
                                MOV                     R10,#02
                                MOV                     R11,#02
                                MOV                     R12,#02
                                MOV                     R13,#02
                                
START:                  JB                      P1.0,START
 
                                MOV                     R5,#20
LEVEL1:                 ACALL           MOVE_MOTOR1_FWD
                                DJNZ            R5,LEVEL1
                                
                                MOV                     R5,#20
LEVEL2:                 ACALL           MOVE_MOTOR2_FWD
                                DJNZ            R5,LEVEL2
 
                                MOV                     R5,#20
LEVEL3:                 ACALL           MOVE_MOTOR1_BWD
                                DJNZ            R5,LEVEL3
                                
                                MOV                     R5,#20
LEVEL4:                 ACALL           MOVE_MOTOR2_BWD
                                DJNZ            R5,LEVEL4
                                
                                MOV                     SP12,#0FFH
                                SJMP            START
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
MOVE_MOTOR1_FWD:MOV                     R0,#25
M3:                             CJNE            R7,#50H,M1
                                MOV                     R7,#30H
                                ACALL           STEP_1_FWD_2
                                SJMP            M2
                                
M1:                             MOV                     R7,#50H
                                ACALL           STEP_1_FWD_1
 
M2:                             DJNZ            R0,M3
                                RET
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
MOVE_MOTOR2_FWD:MOV                     R0,#25
M33:                    CJNE            R6,#50H,M11
                                MOV                     R6,#30H
                                ACALL           STEP_2_FWD_2
                                SJMP            M22
                                
M11:                    MOV                     R6,#50H
                                ACALL           STEP_2_FWD_1
 
M22:                    DJNZ            R0,M33
                                RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
MOVE_MOTOR1_BWD:MOV                     R0,#25
                                CLR                     A
                                MOV                     A,R15
M6:                             CJNE            A,#50H,M4
                                CLR                     A
                                MOV                     A,#30H
                                ACALL           STEP_1_BWD_1
                                SJMP            M5
                                
M4:                             CLR                     A
                                MOV                     A,#50H
                                ACALL           STEP_1_BWD_2
 
M5:                             DJNZ            R0,M6
                                MOV                     R15,A
                                CLR                     A
                                RET
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
MOVE_MOTOR2_BWD:MOV                     R0,#25
                                CLR                     A
                                MOV                     A,R14
M66:                    CJNE            A,#50H,M44
                                CLR                     A
                                MOV                     A,#30H
                                ACALL           STEP_2_BWD_1
                                SJMP            M55
                                
M44:                    CLR                     A
                                MOV                     A,#50H
                                ACALL           STEP_2_BWD_2
 
M55:                    DJNZ            R0,M66
                                MOV                     R14,A
                                CLR                     A
                                RET
                                
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;BASIC STEP MOVEMENT OF BOTH STEPPER MOTOR
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
STEP_1_FWD_1:   DJNZ            R3,LL1  
                                CLR                     SP1_1
                                ACALL           DELAY1
                                SETB            SP1_1
                                MOV                     R3,#02H
                                RET
LL1:                    CLR                     SP1_2
                                ACALL           DELAY1
                                SETB            SP1_2
                                RET
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
STEP_1_FWD_2:   DJNZ            R4,LL2  
                                CLR                     SP1_3
                                ACALL           DELAY1
                                SETB            SP1_3
                                MOV                     R4,#02H
                                RET
LL2:                    CLR                     SP1_4
                                ACALL           DELAY1
                                SETB            SP1_4
                                RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
STEP_2_FWD_1:   DJNZ            R8,L1   
                                CLR                     SP2_1
                                ACALL           DELAY1
                                SETB            SP2_1
                                MOV                     R8,#02H
                                RET
L1:                             CLR                     SP2_2
                                ACALL           DELAY1
                                SETB            SP2_2
                                RET
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
STEP_2_FWD_2:   DJNZ            R9,L2   
                                CLR                     SP2_3
                                ACALL           DELAY1
                                SETB            SP2_3
                                MOV                     R9,#02H
                                RET
L2:                             CLR                     SP2_4
                                ACALL           DELAY1
                                SETB            SP2_4
                                RET
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
STEP_1_BWD_1:   DJNZ            R10,B1  
                                CLR                     SP1_2
                                ACALL           DELAY1
                                SETB            SP1_2
                                MOV                     R10,#02H
                                RET
B1:                             CLR                     SP1_1
                                ACALL           DELAY1
                                SETB            SP1_1
                                RET
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
STEP_1_BWD_2:   DJNZ            R11,B2  
                                CLR                     SP1_4
                                ACALL           DELAY1
                                SETB            SP1_4
                                MOV                     R11,#02H
                                RET
B2:                             CLR                     SP1_3
                                ACALL           DELAY1
                                SETB            SP1_3
                                RET
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
STEP_2_BWD_1:   DJNZ            R12,B3  
                                CLR                     SP2_2
                                ACALL           DELAY1
                                SETB            SP2_2
                                MOV                     R12,#02H
                                RET
B3:                             CLR                     SP2_1
                                ACALL           DELAY1
                                SETB            SP2_1
                                RET
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
STEP_2_BWD_2:   DJNZ            R13,B4  
                                CLR                     SP2_4
                                ACALL           DELAY1
                                SETB            SP2_4
                                MOV                     R13,#02H
                                RET
B4:                             CLR                     SP2_3
                                ACALL           DELAY1
                                SETB            SP2_3
                                RET
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
STEP_DIAG_FWD_1:DJNZ            R8,D1
                                CLR                     SP1_1
                                CLR                     SP2_1
                                ACALL           DELAY1
                                SETB            SP2_1
                                SETB            SP1_1
                                MOV                     R8,#02H
                                RET
D1:                             CLR                     SP2_2
                                CLR                     SP1_2
                                ACALL           DELAY1
                                SETB            SP2_2
                                SETB            SP1_2
                                RET
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
STEP_DIAG_FWD_2:DJNZ            R9,D2   
                                CLR                     SP2_3
                                CLR                     SP1_3
                                ACALL           DELAY1
                                SETB            SP2_3
                                SETB            SP1_3
                                MOV                     R9,#02H
                                RET
D2:                             CLR                     SP2_4
                                CLR                     SP1_4
                                ACALL           DELAY1
                                SETB            SP2_4
                                SETB            SP1_4
                                RET
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
STEP_DIAG_BWD_1:DJNZ            R12,D3
                                CLR                     SP2_2
                                CLR                     SP1_2
                                ACALL           DELAY1
                                SETB            SP2_2
                                SETB            SP1_2
                                MOV                     R12,#02H
                                RET
D3:                             CLR                     SP1_1
                                CLR                     SP2_1
                                ACALL           DELAY1
                                SETB            SP1_1
                                SETB            SP2_1
                                RET
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
STEP_DIAG_BWD_2:DJNZ            R13,D4  
                                CLR                     SP1_4
                                CLR                     SP2_4
                                ACALL           DELAY1
                                SETB            SP1_4
                                SETB            SP2_4
                                MOV                     R13,#02H
                                RET
D4:                             CLR                     SP1_3
                                CLR                     SP2_3
                                ACALL           DELAY1
                                SETB            SP1_3
                                SETB            SP2_3
                                RET
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
STEP_ODIAG_FWD_1:DJNZ           R8,OD1
                                CLR                     SP1_2
                                CLR                     SP2_1
                                ACALL           DELAY1
                                SETB            SP2_1
                                SETB            SP1_2
                                MOV                     R8,#02H
                                RET
OD1:                    CLR                     SP2_2
                                CLR                     SP1_1
                                ACALL           DELAY1
                                SETB            SP2_2
                                SETB            SP1_1
                                RET
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
STEP_ODIAG_FWD_2:DJNZ           R9,OD2  
                                CLR                     SP2_4
                                CLR                     SP1_3
                                ACALL           DELAY1
                                SETB            SP2_4
                                SETB            SP1_3
                                MOV                     R9,#02H
                                RET
OD2:                    CLR                     SP2_3
                                CLR                     SP1_4
                                ACALL           DELAY1
                                SETB            SP2_3
                                SETB            SP1_4
                                RET
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
STEP_ODIAG_BWD_1:DJNZ           R12,OD3
                                CLR                     SP2_1
                                CLR                     SP1_2
                                ACALL           DELAY1
                                SETB            SP2_1
                                SETB            SP1_2
                                MOV                     R12,#02H
                                RET
OD3:                    CLR                     SP1_2
                                CLR                     SP2_1
                                ACALL           DELAY1
                                SETB            SP1_2
                                SETB            SP2_1
                                RET
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
STEP_ODIAG_BWD_2:DJNZ           R13,OD4 
                                CLR                     SP1_3
                                CLR                     SP2_4
                                ACALL           DELAY1
                                SETB            SP1_3
                                SETB            SP2_4
                                MOV                     R13,#02H
                                RET
OD4:                    CLR                     SP1_4
                                CLR                     SP2_3
                                ACALL           DELAY1
                                SETB            SP1_4
                                SETB            SP2_3
                                RET
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
;DELAY FOR STEPPER MOTOR STEPS                                  
DELAY1:                 MOV                     R2,#30
X1:                             MOV                     R1,#20
X2:                             DJNZ            R1,X2
                                DJNZ            R2,X1
                                RET
                                
                                END
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



I also want to convert the g code to make it more usable but i am confused with the coordinates. please help me here.

thank you.

regards
 

Attachments

  • cncn.txt
    6.8 KB · Views: 77

I also want to convert the g code to make it more usable but i am confused with the coordinates. please help me here.

There is no reference in your code to the SBUF register of the UART peripheral. Does this mean that you are about to do this hardcoded? Another detail is that in assembly language it would be expected little support from the others, since in C it would be necessary only the straightfoward application of the formula of the conversion from Cartesian to the Polar form, namely:

Code:
< x , y > = < r.cosθ , r.sinθ >
 
Sir, I have not done the serial communication, but the code is running fine to make a square of 20mm. With the technique i have used here, I want to draw a circle, which i think will be very lenghty, I want to know the method through which I can draw a circle. I am not giving any command externally, i want to feed information directly to the microcontroller to draw a circle. So whenever I power on my controller it will only draw one circle.

thank you.
 

Drawing a square is remarkably easy, just keep one axle stopped and move the other axle at a constant speed. Drawing a circle in other hand involves trigonometric calculus not being a look up table a feasible solution since the resolution is a factor to consider, otherwise the shape of the line would be something corrugated. In that case you would have to deal with assembly sine calculation, as mentioned earlier, I recall the recommendation to consider programming in C.
 
Drawing circle without trigonometric:
Code:
void DrawCircle(int x, int y, int r, unsigned int color) 
{
  int x1,y1,yk = 0;
  int sigma,delta,f;
  x1 = 0;
  y1 = r;
  delta = 2*(1-r);
  do {
    LCD_SetPoint(x+x1,y+y1,color);
    LCD_SetPoint(x-x1,y+y1,color);
    LCD_SetPoint(x+x1,y-y1,color);
    LCD_SetPoint(x-x1,y-y1,color);
    f = 0;
    if(y1 < yk) break;
    if(delta < 0) {
      sigma = 2*(delta+y1)-1;
      if(sigma <= 0) {
        x1++;
        delta += 2*x1+1;
        f = 1;
      }
    }
    else if(delta > 0) {
      sigma = 2*(delta-x1)-1;
      if(sigma > 0) {
        y1--;
        delta += 1-2*y1;
        f = 1;
      }
    }
    if(!f) {
      x1++;
      y1--;
      delta += 2*(x1-y1-1);
    }
  } while(1);
}
 
Drawing circle without trigonometric:
Code:
void DrawCircle(int x, int y, int r, unsigned int color) 
{
  int x1,y1,yk = 0;
  int sigma,delta,f;
  x1 = 0;
  y1 = r;
  delta = 2*(1-r);
  do {
    LCD_SetPoint(x+x1,y+y1,color);
    LCD_SetPoint(x-x1,y+y1,color);
    LCD_SetPoint(x+x1,y-y1,color);
    LCD_SetPoint(x-x1,y-y1,color);
    f = 0;
    if(y1 < yk) break;
    if(delta < 0) {
      sigma = 2*(delta+y1)-1;
      if(sigma <= 0) {
        x1++;
        delta += 2*x1+1;
        f = 1;
      }
    }
    else if(delta > 0) {
      sigma = 2*(delta-x1)-1;
      if(sigma > 0) {
        y1--;
        delta += 1-2*y1;
        f = 1;
      }
    }
    if(!f) {
      x1++;
      y1--;
      delta += 2*(x1-y1-1);
    }
  } while(1);
}

I have tried this code, but the values are not giving me sequence so that I can directly call the stepper motor to rotate, I have not fed this code to the microcontroller, but in another compiler to get the outputs. the outputs are the following. and in the code section i have just print each values.
Code:
void DrawCircle(int,int,int);
#include"stdio.h"

void main()
{	
		DrawCircle(40,20,10);
}
void DrawCircle(int x, int y, int r) 
{
          int x1,y1,yk = 0;
          int sigma,delta,f,z1,z2,z3,z4,z5,z6,z7,z8;
          x1 = 0;
          y1 = r;
          delta = 2*(1-r);
          do {
           // rotate(x+x1,y+y1);
						z1=x+x1;
						z2=y+y1;
		   // rotate(x-x1,y+y1);
						z3=x-x1;
						z4=y-y1;
           
            //rotate(x+x1,y-y1);
            			z5=x+x1;
            			z6=y-y1;
           // rotate(x-x1,y-y1);
           				z7=x-x1;
           				z8=y-y1;
           printf("z1=%d,z2=%d,z3=%d,z4=%d,z5=%d,z6=%d,z7=%d,z8=%d\n",z1,z2,z3,z4,z5,z6,z7,z8);
            f = 0;
            if(y1 < yk) break;
            if(delta < 0) {
              sigma = 2*(delta+y1)-1;
              if(sigma <= 0) {
                x1++;
                delta += 2*x1+1;
                f = 1;
              }
            }
            else if(delta > 0) {
              sigma = 2*(delta-x1)-1;
              if(sigma > 0) {
                y1--;
                delta += 1-2*y1;
                f = 1;
              }
            }
            if(!f) {
              x1++;
              y1--;
              delta += 2*(x1-y1-1);
            }
          } while(1);
}

and the outputs are
Code:
z1=40,z2=30,z3=40,z4=10,z5=40,z6=10,z7=40,z8=10
z1=41,z2=30,z3=39,z4=10,z5=41,z6=10,z7=39,z8=10
z1=42,z2=30,z3=38,z4=10,z5=42,z6=10,z7=38,z8=10
z1=43,z2=30,z3=37,z4=10,z5=43,z6=10,z7=37,z8=10
z1=44,z2=29,z3=36,z4=11,z5=44,z6=11,z7=36,z8=11
z1=45,z2=29,z3=35,z4=11,z5=45,z6=11,z7=35,z8=11
z1=46,z2=28,z3=34,z4=12,z5=46,z6=12,z7=34,z8=12
z1=47,z2=28,z3=33,z4=12,z5=47,z6=12,z7=33,z8=12
z1=48,z2=27,z3=32,z4=13,z5=48,z6=13,z7=32,z8=13
z1=49,z2=26,z3=31,z4=14,z5=49,z6=14,z7=31,z8=14
z1=50,z2=25,z3=30,z4=15,z5=50,z6=15,z7=30,z8=15
z1=50,z2=24,z3=30,z4=16,z5=50,z6=16,z7=30,z8=16
z1=51,z2=23,z3=29,z4=17,z5=51,z6=17,z7=29,z8=17
z1=51,z2=22,z3=29,z4=18,z5=51,z6=18,z7=29,z8=18
z1=51,z2=21,z3=29,z4=19,z5=51,z6=19,z7=29,z8=19
z1=51,z2=20,z3=29,z4=20,z5=51,z6=20,z7=29,z8=20
z1=51,z2=19,z3=29,z4=21,z5=51,z6=21,z7=29,z8=21
how can I get the values sequencially?
 

You could make experiments with spreadsheet chart tools to see that the results with the above values would not look like a circle at all; Taking one of the four quadrants of the circle, the outline seems to have a rather poor resolution:

quarter.png

For stepping motor control, you would have to adjust the above function to output the difference between the value of < Xi , Yi > and < Xi+1 , Yi+1 > or more exactly < ΔX , ΔY > along the whole circle.

In other words, instead of drawing the cicle in cartesian slices, perhaps by constant angular increments could be easiest to deal, since the plot would be made straight in line, therefore surelly fastest than plotting a pair of quadrants at each pass.

Keep in mind that the interpolation of the <X,Y> points of the plotter are in general made with 0,45,90 degrees, which means that either one mottor, other mottor, or both mottors would walk together, therefore the lines need to have a resolution as thin as possible.
 

Easiest was is to borrow the code from 3d printer sources. They are available for free.
 

Hi,

If yiu don't want trigonometric function, then I recommend to use a polygon table.
Let's say even for large circles an 60 vertices is a good enough.
Then a table with 15 (+1) values for 0..90° is sufficient.
If you optimize even more, then you may reduce it to 8 table entries, because every 45° just the X and Y are switched/ mirrored

The benefit with 60 is that it may get integer divided by, 2, 3, 4, 5, 6, 12....
With smaller circles you may reduce the edges...

But you are free to increase edges.
For a 1° resolution 46 table entries are sufficient.
I recommend to store them as two 16 bit integer values for X and Y.

I recommend to do some excel tests with different circle diameters and different degree resolution to get the deviation (error) to an ideal circle.

Klaus
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top