-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
313 lines (289 loc) · 4.41 KB
/
main.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
//板1:人机交互板
//板2:地盘控制板
#include "reg51.h"
#include "package.h"
#include "motor.h"
/*
2016.11.08 程序框架
2016.11.15 完成协议的封装
2016.11.16 完成板1部分细节功能
2016.11.19 完成接收地盘速度信息功能
2016.11.20 完成矩阵键盘部分
2016.11.22 完成细节 LED及BUZZER
2016.11.23 完成速度设定功能
2016.11.25-2016.11.30 两板联调 PID调节
测速接INT0
按键接INT1
*/
#define uchar unsigned char
#define uint unsigned int
sbit buzzer=P2^5;
sbit red_led=P2^6;
sbit green_led=P2^7;
//对管接口
sbit Dec0=P1^4;
sbit Dec1=P1^5;
sbit Dec2=P1^6;
sbit Dec3=P1^7;
//电机使能口
sbit IN1=P0^0;
sbit IN2=P0^1;
sbit IN3=P0^2;
sbit IN4=P0^3;
sbit IN5=P0^4;
sbit IN6=P0^5;
sbit IN7=P0^6;
sbit IN8=P0^7;
uint dec_cnt=0;
uchar upFlag=0,downFlag=0,can_go=1;
uchar speed_tem,speed_read=0;
uchar now_speed[3]={0x00,0x30,0x30};
//滤波器
uchar SpeedFilter[5]={0,0,0,0,0};
void delay_1(int time)
{
int a,b;
for(a=time;a>0;a--)
{
for(b=125;b>0;b--);
}
}
uchar toggleBit(uchar dat)
{
if(dat==1)
{
return 0;
}else if(dat==0)
{
return 1;
}
}
void INT_init()
{
IT0=1;
IT1=1;
EX0=1;
EX1=1;
EA=1;
}
void TimerConfig()
{
TMOD = 0x21;
TH0 = (65536 - 50000)/256;
TL0 = (65536 - 50000)%256;
TR0=1;
ET0=1;
EA=1;
}
void UsartConfiguration()
{
SCON=0X50; //串口工作方式2
TMOD=0X21;
PCON=0X80;
TH1=0XF3; //baud 4800
TL1=0XF3;
ES=1;
EA=1;
TR1=1;
}
void main(void)
{
INT_init();
TimerConfig();
UsartConfiguration();
while(1)
{
if(receive_ok)
{
receive_ok=0;
if(check_data())
{
switch(receive_data[1])
{
case 0x01:{
ConfigSpeed=receive_data[4]*10+receive_data[5];
MotorOut=ConfigSpeed;
break;}
}
}
}
#if 1
if(speed_read)
{
speed_read=0;
now_speed[1]=now_speed[0]%100/10;
now_speed[2]=now_speed[0]%10;
now_speed[0]=now_speed[0]/100;
InPackage(now_speed,0x02);
}
#endif
#if 1
//--------------------------
if((upFlag==0)&&(downFlag==0))//主线
{
if((Dec0==1)||(Dec1==1))
{
go();
}else
{
left();
}
}
//装货区
if((Dec2==1)&&(can_go==1))//中线
{
stop();
upFlag=1;
}
//卸货区
if((Dec3==1)&&(can_go==0))//右线
{
stop();
downFlag=1;
}
#endif
//-------------------------------
}
}
//装货、卸货按键
void int1_SERVICE() interrupt 2
{
if((upFlag==1)||(downFlag==1))//只有在卸货区或者装货区才能触发,防止运货过程中误触
{
can_go=toggleBit(can_go);
upFlag=0;
downFlag=0;
}
}
//读取光电门脉冲
void int0_SERVICE() interrupt 0
{
speed_tem++;
}
void Timer0_SERVICE() interrupt 1 using 3
{
static uint motor_cnt,encoder_cnt,speed_read_cnt,FilterCnt=0,add=0,pwm_cnt;
uchar i;
TH0 = (65536 - 5000)/256; //50ms
TL0 = (65536 - 5000)%256;
motor_cnt++;
encoder_cnt++;
speed_read_cnt++;
//滑动滤波
//----------------------------------------------//
if(speed_read_cnt>100)
{
SpeedFilter[FilterCnt]=speed_tem;
speed_tem=0;
speed_read_cnt=0;
if(FilterCnt>=4)
{
FilterCnt=0;
}else
{
FilterCnt++;
}
}
//-----------------------------------------------//
if(encoder_cnt>100)
{
for(i=0;i<5;i++)
{
add+=SpeedFilter[i];
}
now_speed[0]=add*2/5;
encoder_cnt=0;
speed_read=1;
add=0;
MotorOut += speedControl(now_speed[0],ConfigSpeed);
if(MotorOut>=20)
{
MotorOut=20;
}
if(MotorOut<=0)
{
MotorOut=0;
}
}
//MotorOut=Amplitude(MotorOut,20);
/*
以下PWM程序
*/
if(pwm[0]==0)
{
IN1=0;
IN2=0;
IN3=0;
IN4=0;
IN5=0;
IN6=0;
IN7=0;
IN8=0;
}else
{
//1 2
if(motor_cnt>pwm[0])
{
IN1=0;
IN2=0;
IN5=0;
IN6=0;
}else
{
IN1=1;
IN2=0;
IN5=1;
IN6=0;
}
switch(pwm[1])//0 反转
{
case 0:{
if(motor_cnt>pwm[0])
{
IN3=0;
IN4=0;
IN7=0;
IN8=0;
}else
{
IN3=0;
IN4=1;
IN7=0;
IN8=1;
}
break;}
case 1:{
if(motor_cnt>pwm[0])
{
IN3=0;
IN4=0;
IN7=0;
IN8=0;
}else
{
IN3=1;
IN4=0;
IN7=1;
IN8=0;
}
break;}
}
}
// motor_cnt 清零
if(motor_cnt>=20)
{
motor_cnt=0;
}
}
void Usart_SERVICE() interrupt 4
{
if(RI)
{
RI = 0;
DePackage();
}else if (TI)
{
TI = 0;
send_flag=0;
}
}