-
Notifications
You must be signed in to change notification settings - Fork 12
/
SmartTracingCar.c
490 lines (459 loc) · 9.27 KB
/
SmartTracingCar.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
#include <STC12C5A60S2.h>
#include "intrins.h"
#define BLACK 0
#define WHITE 1
//---------------------------------ADC查询部分说明------------------------------------
#define FOSC 18432000L
#define BAUD 9600
#define ADC_POWER 0x80
#define ADC_FLAG 0x10
#define ADC_START 0x08
#define ADC_SPEEDLL 0x00
#define ADC_SPEEDL 0x20
#define ADC_SPEEDH 0x40
#define ADC_SPEEDHH 0x60
#define uint unsigned int
#define uchar unsigned char
//---------------------------------ADC查询部分说明------------------------------------
typedef unsigned char BYTE;
typedef unsigned int WORD;
sbit L298_IN1=P2^1;
sbit L298_IN2=P2^2;
sbit L298_IN3=P2^3;
sbit L298_IN4=P2^4;
sbit L29B_ENA=P2^5;
sbit L29B_ENB=P2^6;
char L298_flag=0;
sbit BSD=P3^7;
sfr ADC_LOW2 = 0xBE;
//-----------------------------------------传感器部分声明--------------------------------------------
#define uchar unsigned char
#define uint unsigned int
//char state ;
unsigned char disbuf[4];
unsigned int ADC_select=0; //5路通道AD选择
unsigned int ADC0_value; //0通道AD数据
unsigned int ADC1_value; //1通道AD数据
unsigned int ADC2_value; //2通道AD数据
unsigned int ADC3_value; //3通道AD数据
unsigned int ADC4_value; //4通道AD数据
sbit LED=P0^2;
sbit LY=P3^6;
sbit GD=P3^2;
sbit TEST=P3^7;
unsigned int OUTPUT_select=0; //5路通道AD输出选择
//声明
void _delay_ms(unsigned int ms);//延时
void InitUart();//初始化串口
void SendUart(unsigned char dat);//串口发送一字节
void ADC_Init(void);//初始化ADC
//-----------------------------------------传感器部分声明--------------------------------------------
int gray[10];
int flag=0;
int R;
int last_color;
int now_color;
/*************前进*************/
void L298_forward(char speed_M2,char speed_M1,char timer) //speed_M1、speed_M2的范围为0~20;数值越大速度越大
{
int i,j;
for(j=0;j<timer*10;j++)
{
for(i=0;i<100;i++)
{
if(L298_flag<speed_M1)
{
L29B_ENA=1;
L298_IN1=1;
L298_IN2=0;
}
else
{
L29B_ENA=0;
L298_IN1=0;
L298_IN2=0;
}
if(L298_flag<speed_M2)
{
L29B_ENB=1;
L298_IN3=1;
L298_IN4=0;
}
else
{
L29B_ENB=0;
L298_IN3=0;
L298_IN4=0;
}
L298_flag=++L298_flag%20;
}
}
L29B_ENA=0;
L298_IN1=0;
L298_IN2=0;
L29B_ENB=0;
L298_IN3=0;
L298_IN4=0;
}
/*************后退*************/
void L298_backward(char speed_M1,char speed_M2,char timer)
{
int i,j;
for(j=0;j<timer*10;j++)
{
for(i=0;i<100;i++)
{
if(L298_flag<speed_M1)
{
L29B_ENA=1;
L298_IN1=0;
L298_IN2=1;
}
else
{
L29B_ENA=0;
L298_IN1=0;
L298_IN2=0;
}
if(L298_flag<speed_M2)
{
L29B_ENB=1;
L298_IN3=0;
L298_IN4=1;
}
else
{
L29B_ENB=0;
L298_IN3=0;
L298_IN4=0;
}
L298_flag=++L298_flag%20;
}
}
L29B_ENA=0;
L29B_ENB=0;
}
/*************右转*************/
void L298_right(char speed_M1,char speed_M2,int timer)
{
int i,j;
for(j=0;j<timer*10;j++)
{
for(i=0;i<100;i++)
{
if(L298_flag<speed_M1)
{
L29B_ENA=1;
L298_IN1=1;
L298_IN2=0;
}
else
{
L29B_ENA=0;
L298_IN1=0;
L298_IN2=0;
}
if(L298_flag<speed_M2)
{
L29B_ENB=1;
L298_IN3=0;
L298_IN4=1;
}
else
{
L29B_ENB=0;
L298_IN3=0;
L298_IN4=0;
}
L298_flag=++L298_flag%20;
}
}
L29B_ENA=0;
L29B_ENB=0;
}
/************左转*************/
void L298_left(char speed_M1,char speed_M2,int timer)
{
int i,j;
for(j=0;j<timer*10;j++)
{
for(i=0;i<100;i++)
{
if(L298_flag<speed_M1)
{
L29B_ENA=1;
L298_IN1=0;
L298_IN2=1;
}
else
{
L29B_ENA=0;
L298_IN1=0;
L298_IN2=0;
}
if(L298_flag<speed_M2)
{
L29B_ENB=1;
L298_IN3=1;
L298_IN4=0;
}
else
{
L29B_ENB=0;
L298_IN3=0;
L298_IN4=0;
}
L298_flag=++L298_flag%20;
}
}
L29B_ENA=0;
L29B_ENB=0;
}
/*************后右走*************/
void backward_right(char speed,int timer)
{
int i,j;
for(j=0;j<timer*10;j++)
{
for(i=0;i<100;i++)
{
if(L298_flag<speed)
{
L29B_ENA=1;
L29B_ENB=1;
L298_IN1=0;
L298_IN2=1;
L298_IN3=1;
L298_IN4=0;
}
else
{
L29B_ENA=0;
L29B_ENB=0;
}
L298_flag=++L298_flag%20;
}
}
L29B_ENA=0;
L29B_ENB=0;
}
/************后左转*************/
void backward_left(char speed,int timer)
{
int i,j;
for(j=0;j<timer*10;j++)
{
for(i=0;i<100;i++)
{
if(L298_flag<speed)
{
L29B_ENA=1;
L29B_ENB=1;
L298_IN1=0;
L298_IN2=1; //you
L298_IN3=1;
L298_IN4=0; //zuo
}
else
{
L29B_ENA=0;
L29B_ENB=0;
}
L298_flag=++L298_flag%20;
}
}
L29B_ENA=0;
L29B_ENB=0;
}
//***********延时***************/
void _delay_ms(unsigned int ms)
{
unsigned int i;
while(ms--){
for(i=0;i<125*6;i++);}
}
void Sendstring(unsigned char *a)
{
while(*a)
{
SendUart(*a);
a++;
}
}
void ADC_OUTPUT(unsigned int temp)
{
disbuf[0]=temp%10000/1000;
disbuf[1]=temp%1000/100;
disbuf[2]=temp%100/10;
disbuf[3]=temp%10;
SendUart(disbuf[0]+48);
SendUart(disbuf[1]+48);
SendUart(disbuf[2]+48);
SendUart(disbuf[3]+48);
}
//-----------------------------------------ADC查询---------------------------------------------------------------
void InitUart();
void InitADC();
void SendData(BYTE dat);
BYTE GetADCResult(BYTE ch);
//void Delay(WORD n);
void ShowResult(BYTE ch);
void ShowResult(BYTE ch)
{
BYTE res;
//SendData(ch);
res = GetADCResult(ch);
//SendData(res);
if (res == 0)
gray[ch] = 0;
else
gray[ch] = 1;
//delta += gray[ch] * weight[ch];
}
BYTE GetADCResult(BYTE ch)
{
ADC_CONTR = ADC_POWER|ADC_SPEEDLL|ch|ADC_START;
_nop_();
_nop_();
_nop_();
_nop_();
while (!(ADC_CONTR & ADC_FLAG))
;
ADC_CONTR &= ~ADC_FLAG;
return ADC_RES;
}
void InitUart()
{
SCON = 0x5a;
TMOD = 0x20;
TH1 = TL1 = -(FOSC/12/32/BAUD);
TR1 = 1;
//分中断部
IT0=1;
EX0=1;
EA=1;
flag=0;
}
void exint0() interrupt 0
{
flag++;
}
void InitADC()
{
P1ASF = 0xff;
ADC_RES = 0;
ADC_CONTR = ADC_POWER|ADC_SPEEDLL;
//Delay(2);
}
void SendData(BYTE dat)
{
while (!TI);
TI=0;
SBUF = dat;
}
/************初始化*************/
void L298_init(void)
{
L29B_ENA=0;
L29B_ENB=0;
L298_IN1=0;
L298_IN2=0;
L298_IN3=0;
L298_IN4=0;
}
void f()
{
if(gray[4]==WHITE && gray[1]==BLACK && gray[0]==BLACK && gray[2]==BLACK && R==3)
{
//backward_right(15,60);
L298_forward(20,0,90);
return;
}
if(gray[4]==WHITE && gray[3]==WHITE && gray[1]==WHITE && gray[0]==WHITE)
{
L298_forward(20,20,10);
return;
}
if(gray[4]==BLACK && gray[3]==BLACK && gray[1]==BLACK && gray[0]==BLACK)
{
L298_forward(20,20,10);
return;
}
if(gray[4]==WHITE && gray[3]==BLACK && gray[1]==WHITE && gray[0]==WHITE)
{
L298_forward(15,20,10);
return;
}
if(gray[4]==WHITE && gray[3]==WHITE && gray[1]==BLACK && gray[0]==WHITE)
{
L298_forward(20,15,10);
return;
}
if(gray[4]==WHITE && gray[3]==WHITE && gray[1]==WHITE && gray[0]==BLACK)
{
L298_forward(20,0,10);
return;
}
if(gray[4]==BLACK && gray[3]==WHITE && gray[1]==WHITE && gray[0]==WHITE)
{
L298_forward(0,20,10);
return;
}
if(gray[4]==WHITE && gray[3]==BLACK && gray[1]==BLACK && gray[0]==BLACK && gray[2]==WHITE)
{
L298_forward(20,0,10);
return;
}
if(gray[4]==BLACK && gray[3]==BLACK && gray[1]==BLACK && gray[0]==WHITE)
{
L298_forward(0,20,10);
return;
}
if(gray[4]==WHITE && gray[3]==WHITE && gray[1]==BLACK && gray[0]==BLACK && gray[2]==WHITE)
{
L298_forward(20,0,10);
return;
}
if(gray[4]==WHITE && gray[3]==WHITE && gray[1]==BLACK && gray[0]==WHITE && gray[2]==BLACK)
{
L298_forward(20,0,10);
return;
}
if(gray[4]==BLACK && gray[3]==BLACK && gray[1]==WHITE && gray[0]==WHITE)
{
L298_forward(0,20,10);
return;
}
}
void main()
{
int i = 0;
LY=0;
BSD=0;
R=0;
last_color=WHITE;
now_color=WHITE;
L298_init();
InitUart();
InitADC();
while (1)
{
if(flag==1)
{
BSD=0;
LY=1;
}
if(flag==2)
{
LY=0;
BSD=1;
}
for (i = 0; i <= 4; ++i)
ShowResult(i);
last_color=now_color;
now_color=gray[2];
if(last_color==WHITE && now_color==BLACK)
++R;
f();
}
}