-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
215 lines (176 loc) · 3.4 KB
/
test.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
/**
*
*@file
*
*@brief Automatic Infrared Tracking Car \n
*
*@author <邝晨阳>
*
*@date <2018/11/9>
*
*@version 1.0
*
*@end
*/
#include "reg52.h"
#include "math.h"
typedef unsigned char u8;
typedef unsigned int u32;
u8 count = 0;
u8 channel, pct;
u8 time;
/**
* Motor drive IO port and ENA, ENB enable terminal definition;
* IN1&IN2 control left motor; IN3&IN4 control right motor;
*/
sbit IN1 = P2^3;
sbit IN2 = P2^4;
sbit ENB = P2^5;
sbit IN3 = P2^1;
sbit IN4 = P2^2;
sbit ENA = P2^0;
/**
* Infrared tracking module definition
*/
sbit left = P3^1;
sbit middle = P3^2; //externel interruption reserved
sbit right = P3^3;
/**
*@brief Delay func. definition
*/
void delay_us(u8 a) //Millisecond
{
while (a--);
}
void delay(u8 z) //Microseconds
{
u8 x, y;
for (x = z; x > 0; x--)
for (y = 110; y > 0; y--);
}
void init()
{
TMOD = 0x01; //Timer 0 working mode 1
TH0 = 0xff; //(65536-10)/256; //Initial value timing
TL0 = 0xf7; //(65536-10)%256; //0.01ms
EA = 1; //Open total interruption
ET0 = 1; //Open timer 0 interrupt
TR0 = 1; //Start timer 0
}
/**
*@defgroup
*
*@brief Speed control using Timer0;
*@{
* Value: -255 ~ 255
* Mode: backward ~ forward
*@}
*@end
*/
void spdctrl(char ch, int spdvalue)
{
if (spdvalue == 0)
{
ENA = 0;
ENB = 0;
}
else if (ch == "ENA")
{
if (spdvalue > 0)
{
IN1 = 1;
IN2 = 0;
}
else
{
IN1 = 0;
IN2 = 1;
}
}
else if (ch == "ENB")
{
if (spdvalue > 0)
{
IN3 = 1;
IN4 = 0;
}
else
{
IN3 = 0;
IN4 = 1;
}
}
}
/**
*
*@brief Mode definition, associated with function 'spdctrl()';
*
*@end
*/
void workMode(int value_A, int value_B)
{
init();
spdctrl("ENA", value_A);
spdctrl("ENB", value_B);
}
void track()
{
if (left == 0) //Returns 0 while checking out black lines
workMode(80, 20);
if (right == 0)
workMode(20, 80);
}
void main()
{
int i;
EA = 1; //Total interruption opened
EX1 = 1; //Open INT1
while (count <= 18)
{
track();
if (middle == 0)
IT1 = 1; //Set the falling edge trigger
switch (count)
{
case 1: workMode(80, 20);
break;
case 2:
case 5:
case 8: workMode(80, 80);
break;
case 3: workMode(80, 80);
workMode(100, -80);
break;
case 4: workMode(20, 80);
break;
case 7: workMode(80, 20);
break;
case 12: workMode(80, 80);
workMode(-80, 100);
break;
case 13: workMode(20, 80);
break;
case 14: workMode(0, 0);
break;
}
}
}
void Timer0() interrupt 1 //Timer 0 interrupt routine //12MHz
{
TR0 = 0; //赋初值时,关闭定时器
TH0 = 0xff; //(65536-10)/256; //赋初值定时
TL0 = 0xf7; //(65536-10)%256; //0.01ms
TR0 = 1; //打开定时器
time++;
if (time >= 100) //Guaranteed frequency is 1kHZ //freq = 1s/(0.01ms * 100)
time = 0;
if (time <= pct) //Duty cycle at pct% and can be changed
channel = 1;
else channel = 0;
}
void lineCounter() interrupt 2 //External interrupt for counting
{
while (!middle)
tracking();
count++;
}