-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcmotor.c
538 lines (489 loc) · 9.32 KB
/
dcmotor.c
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*------------------------
DC Motor Speed Control
Close loop P control
With UART
Author: Xinyou Han, PMC, Wuhan University
Wuhan, Hubei Province, China
------------------------*/
/*------------------------
This module contains a DC motor, an optocoupler to measure speed
Timers should be used in PWM output, timing, pulse counting, and UART
However, 89C52 has just 3 timers
As PWM cycle is fixed, it can be also used in timing
At first it's designed to communicate with KingView
Somehow the communicate cannot be established (namely failed LOL)
Though communicate with PC via UART succeeded (with some soft in PC instead of KingView)
It's not completed yet
The actual speed is up to about 160n/s
P control is excellent
The motor driving ic is L9110
------------------------*/
//Crystal Frequency 12MHz
//TIMER0: PWM output and timing
//TIMER1: Serial port control
//TIMER2: Pulse count
//A phase of the motor connects to VCC, when B phase to PWM out(P1.2)
#include <reg52.h>
#include <math.h>
#define UINT unsigned int
#define UCHAR unsigned char
//------------------------
sbit SEG0 = P2^2;
sbit SEG1 = P2^3;
sbit SEG2 = P2^4;
UCHAR code table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//------------------------
#define N 4 //coefficient for duty cycle
#define DZ 5 //deadzone
#define START 0x40
#define END 0x0d
#define ADDR 0x01
#define READ 0x00
#define WRITE 0x01
//limits of duty cycle, 1% and 99%
#define Tmax 9900
#define Tmin 100
sbit PWM = P1^2;
sfr T2MOD = 0xc9;
int N_d; //actual speed
int N_s; //expected speed
UCHAR FLAG; //PWM duty cycle modulate, FLAG=0 keep, =1 decrease(speed up), =2 increase(speed down)
//UCHAR LIM; //duty cycle limitation, LIM=0 normal, =1 minimun(fatest), =2 maximun(lowest)
//PWM cycle, unit:us, initial duty cycle is 50%, cycle:10ms
UINT T_H = 5000;
UINT T_L = 5000;
//define flag bits
bit OFW; //counter overflow
bit TIMER; //time up
bit RCV; //MCU receive data success
//communication
UCHAR CNT = 0;
UCHAR ACK[10] = {0x40,0x30,0x31,0x30,0x31,0x31,0x31,0x30,0x32,0x0d};
UCHAR ACK_WR[8] = {0x40,0x30,0x31,0x23,0x23,0x30,0x31,0x0d};
UCHAR BUFF[20] = {0}; //receive buff
//UCHAR CRC = 0;
UCHAR DAT_ADDR = 0; //data addr
UCHAR RCV_FLAG = 0; //receive 1st byte flag
/*------------------------
FUNC: delay
DESCR: time delay
IN: delaying time(ms)
OUT: N/A
------------------------*/
void delay(UCHAR t)
{
UCHAR i,j;
for(i=t;i>0;i--)
for(j=106;j>0;j--);
}
/*------------------------
FUNC: TH0_cfg
DESCR: set reg value of TIMER0, TH0
IN: time
OUT: TH0 value
------------------------*/
UCHAR TH0_cfg(UINT t)
{
return (65536-t)/256;
}
/*------------------------
FUNC: TL0_cfg
DESCR: set reg value of TIMER0, TL0
IN: time
OUT: TL0 value
------------------------*/
UCHAR TL0_cfg(UINT t)
{
return (65536-t)%256;
}
/*------------------------
FUNC: asc2hex
DESCR: transfer 2 ascii to hex
IN: ascii high and low
OUT: hex
------------------------*/
UCHAR asc2hex(UCHAR asc)
{
UCHAR hex;
if((asc>=0x30)&&(asc<=0x39))
hex = asc-0x30;
else if((asc>=0x41)&&(asc<=0x46))
hex = asc-0x37;
else
hex = 0;
return hex;
}
/*------------------------
FUNC: hex2asc
DESCR: transfer hex to ascii
IN: hex
OUT: ascii
------------------------*/
UCHAR hex2asc(UCHAR hex)
{
UCHAR asc;
if((hex>=0x00)&&(hex<=0x09))
asc = hex+0x30;
else if((hex>=0x0a)&&(hex<=0x0f))
asc = hex+0x37;
else
asc = 0;
return asc;
}
/*------------------------
FUNC: UART_init
DESCR: UART initialize, baud rate: 4800
IN: N/A
OUT: N/A
------------------------*/
void UART_init()
{
TMOD |= 0x20;
SCON = 0x50;
PCON = 0x80;
TH1 = 0xf3;
TL1 = 0xf3;
TR1 = 1;
EA = 1;
ES = 1;
PS = 1;
}
/*------------------------
FUNC: init
DESCR: main initialize
IN: N/A
OUT: N/A
------------------------*/
void init()
{
TMOD |= 0x01; //TIMER0:mode 1 timer
//TIMER2: 16-bit auto-reload counter
RCLK = 0;
TCLK = 0;
EXEN2 = 0;
TR2 = 1;
C_T2 = 1;
CP_RL2 = 0;
T2MOD = 0x00;
//set initial value for timers
TH0 = TH0_cfg(T_L);
TL0 = TL0_cfg(T_L);
TH2 = 0x00;
TL2 = 0x00;
RCAP2H = 0x00;
RCAP2L = 0x00;
//enable all interrupts, and timers on, timers interrupts on
EA = 1;
ET0 = 1;
TR0 = 1;
ET2 = 1;
EX0 = 0; //enable external interrupt 1,disble EXIT0
//EX1 = 1;
//global variable initialize
N_d = 0;
N_s = 0;
FLAG = 0;
//LIM = 0;
//flag bit initialize
OFW = 0;
TIMER = 0;
RCV = 0;
PWM = 0; //PWM initial low
}
/*------------------------
FUNC: display
DESCR: digital tube display, for test
IN: expected speed, actual speed
OUT: N/A
------------------------*/
void display(int n, int s)
{
int n1,n2,n3; //n1: actual speed's ones, n2: tens, n3: hundreds
int s1,s2,s3; //s1: expected speed's ones, s2: tens, s3: hundreds
n1 = n%10;
n2 = n/10%10;
n3 = n/100;
s1 = s%10;
s2 = s/10%10;
s3 = s/100;
P0 = 0x00;
P2 = 0x00;
SEG0 = 0;
SEG1 = 0;
SEG2 = 0;
P0 = table[n1];
delay(5);
SEG0 = 1;
SEG1 = 0;
SEG2 = 0;
P0 = table[n2];
delay(5);
SEG0 = 0;
SEG1 = 1;
SEG2 = 0;
P0 = table[n3];
delay(5);
SEG0 = 0;
SEG1 = 0;
SEG2 = 1;
P0 = table[s1];
delay(5);
SEG0 = 1;
SEG1 = 0;
SEG2 = 1;
P0 = table[s2];
delay(5);
SEG0 = 0;
SEG1 = 1;
SEG2 = 1;
P0 = table[s3];
delay(5);
//SEG0 = 1;
//SEG1 = 1;
//SEG2 = 1;
//P0 = table[LIM];
//delay(5);
}
/*------------------------
FUNC: UART_send
DESCR: send data to host
IN: data, length
OUT: N/A
------------------------*/
void UART_send(UCHAR *dat,UCHAR length)
{
UCHAR i;
ES = 0;
for(i=0;i<length;i++)
{
SBUF = *(dat+i);
while(!TI);
TI = 0;
}
ES = 1;
}
/*------------------------
FUNC: dat_ack
DESCR: process data from host, and acknowledge
IN: N/A
OUT: N/A
------------------------*/
void dat_ack()
{
UCHAR temp[2] = {0};
temp[0] = BUFF[11];
temp[1] = BUFF[12];
N_s = asc2hex(temp[0])*16+asc2hex(temp[1]);
if((BUFF[7]==0x30)&&(BUFF[8]==0x31))
UART_send(ACK_WR,8);
}
/*------------------------
FUNC: dat_wr
DESCR: send actual speed to host
IN: N/A
OUT: N/A
------------------------*/
void dat_wr()
{
UCHAR dat_xor,i,tmp0,tmp1,wr_buff;
dat_xor = 0;
ACK[5] = hex2asc(N_d/16);
ACK[6] = hex2asc(N_d%16);
for(i=1;i<7;i++)
dat_xor ^= ACK[i];
wr_buff = dat_xor;
tmp0 = (wr_buff>>4)&0x0f;
ACK[7] = hex2asc(tmp0);
tmp1 = wr_buff&0x0f;
ACK[8] = hex2asc(tmp1);
UART_send(ACK,10);
}
/*------------------------
FUNC: main
DESCR: main func
IN: N/A
OUT: N/A
------------------------*/
void main()
{
// UCHAR i;
UART_init();
init();
while(1)
{
if(OFW)
{
//overflow, invalid data, N_d = 0
OFW = 0;
N_d = 0;
}
if(TIMER)
{
TIMER = 0;
N_d = TL2+TH2*256;
N_d = N_d*10;
N_d = N_d/4;
TH2 = 0x00;
TL2 = 0x00;
TR0 = 1;
TR2 = 1;
dat_wr();
}
if((N_s-N_d)>DZ) //expected > actual, speed up
FLAG = 1;
else if((N_s-N_d)<(0-DZ)) //expected < actual, speed down
FLAG = 2;
else
FLAG = 0;
display(N_d,N_s);
if(RCV) //receive data from host
{
dat_ack();
RCV = 0; //reset receive flag
}
// UART_send();
}
}
/*------------------------
FUNC: T2_int
DESCR: interrupt service of TIMER2
IN: N/A
OUT: N/A
------------------------*/
void T2_int() interrupt 5
{
OFW = 1;
TH2 = 0x00;
TL2 = 0x00;
}
/*------------------------
FUNC: UART_get
DESCR: read data from host via UART
IN: N/A
OUT: N/A
------------------------*/
void UART_get() interrupt 4
{
UCHAR temp;
temp = SBUF;
if(RCV_FLAG==0)
{
if(temp==START)
{
RCV_FLAG = 1;
BUFF[CNT] = temp;
CNT++;
}
}
else
{
BUFF[CNT] = temp;
CNT++;
if(temp==END)
{
RCV = 1;
CNT = 0;
RCV_FLAG = 0;
}
}
RI = 0;
}
/*------------------------
FUNC: PWM_out
DESCR: PWM output, as well as timing
IN: N/A
OUT: N/A
------------------------*/
void PWM_out() interrupt 1
{
static UCHAR i;
UCHAR delta; //delta=|N_s-N_d|/4
i++; //i+1 when PWM reverses
TR0 = 0;
if(i==20) //if PWM reverses for 20 times, it's 10 cycle, namely 0.1s
{
TR0 = 0;
TR2 = 0;
TIMER = 1;
i = 0;
}
switch(FLAG)
{
case 0: //speed keep, PWM keep
if(PWM) //currently PWM is high, then next time is low, hereinafter
{
TH0 = TH0_cfg(T_L);
TL0 = TL0_cfg(T_L);
TR0 = 1;
PWM = 0;
}
else //currently PWM is low, then next time is high, hereinafter
{
TH0 = TH0_cfg(T_H);
TL0 = TL0_cfg(T_H);
TR0 = 1;
PWM = 1;
}
break;
case 1: //speed up, duty cycle decreases
delta = abs(N_s-N_d)/N;
if(T_H>(Tmin+delta))
{
T_H -= delta;
T_L += delta;
//LIM = 0;
}
else //duty cycle minimum
{
T_H = Tmin;
T_L = Tmax;
//LIM = 1;
}
if(PWM)
{
TH0 = TH0_cfg(T_L);
TL0 = TL0_cfg(T_L);
TR0 = 1;
PWM = 0;
}
else
{
TH0 = TH0_cfg(T_H);
TL0 = TL0_cfg(T_L);
TR0 = 1;
PWM = 1;
}
break;
case 2: //speed down, duty cycle increases
delta = abs(N_s-N_d)/N;
if(T_H<(Tmax-delta))
{
T_H += delta;
T_L -= delta;
//LIM = 0;
}
else //duty cycle maximum
{
T_H = Tmax;
T_L = Tmin;
//LIM = 2;
}
if(PWM)
{
TH0 = TH0_cfg(T_L);
TL0 = TL0_cfg(T_L);
TR0 = 1;
PWM = 0;
}
else
{
TH0 = TH0_cfg(T_H);
TL0 = TL0_cfg(T_H);
TR0 = 1;
PWM = 1;
}
break;
default:
break;
}
}