/
carcontrol.c
199 lines (180 loc) · 5.16 KB
/
carcontrol.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
#include "includes.h"
#define CAR_PORT GPIOE
#define CAR_FORWARD_MASK GPIO_Pin_2
#define CAR_BACKWARDS_MASK GPIO_Pin_3
#define CAR_RIGHT_MASK GPIO_Pin_4
#define CAR_LEFT_MASK GPIO_Pin_5
void Car_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = JS_RIGHT_MASK;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(JS_RIGHT_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = JS_UP_MASK;
GPIO_Init(JS_UP_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = JS_LEFT_MASK;
GPIO_Init(JS_LEFT_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = JS_DOWN_MASK;
GPIO_Init(JS_DOWN_PORT, &GPIO_InitStructure);
//PE2 init
GPIO_InitStructure.GPIO_Pin = CAR_FORWARD_MASK;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(CAR_PORT, &GPIO_InitStructure);
//PE3 init
GPIO_InitStructure.GPIO_Pin = CAR_BACKWARDS_MASK;
GPIO_Init(CAR_PORT, &GPIO_InitStructure);
//PE4 init
GPIO_InitStructure.GPIO_Pin = CAR_RIGHT_MASK;
GPIO_Init(CAR_PORT, &GPIO_InitStructure);
//PE5 init
GPIO_InitStructure.GPIO_Pin = CAR_LEFT_MASK;
GPIO_Init(CAR_PORT, &GPIO_InitStructure);
GPIO_SetBits(CAR_PORT, CAR_FORWARD_MASK); //Reset bit for not going forward
GPIO_SetBits(CAR_PORT, CAR_BACKWARDS_MASK); //Set bit for not going backwards
GPIO_SetBits(CAR_PORT, CAR_RIGHT_MASK); //Reset bit for not going forward
GPIO_SetBits(CAR_PORT, CAR_LEFT_MASK); //Set bit for not going backwards
}
/*************************************************************************
* Function Name: Forward
* Parameters: none
*
* Return: none
*
* Description: Reset the bit for going backwards and set the one going
*
*************************************************************************/
void Forward(void)
{
GPIO_SetBits(CAR_PORT, CAR_BACKWARDS_MASK); //Reset bit for not going backwards
GPIO_ResetBits(CAR_PORT, CAR_FORWARD_MASK); //Set bit for going forward
}
/*************************************************************************
* Function Name: Backwards
* Parameters: none
*
* Return: none
*
* Description: Reset the bit for going forwards and set the one going backwards
*
*************************************************************************/
void Backwards(void)
{
GPIO_SetBits(CAR_PORT, CAR_FORWARD_MASK); //Reset bit for not going forward
GPIO_ResetBits(CAR_PORT, CAR_BACKWARDS_MASK); //Set bit for going backwards
}
/*************************************************************************
* Function Name: Stop
* Parameters: none
*
* Return: none
*
* Description: Resets bits for forwards and backwards
*
*************************************************************************/
void Stop(void)
{
GPIO_SetBits(CAR_PORT, CAR_FORWARD_MASK); //Reset bit for not going forward
GPIO_SetBits(CAR_PORT, CAR_BACKWARDS_MASK); //Set bit for not going backwards
}
/*************************************************************************
* Function Name: Right
* Parameters: none
*
* Return: none
*
* Description: Resets bits for forwards and backwards
*
*************************************************************************/
void Right(void)
{
GPIO_ResetBits(CAR_PORT, CAR_LEFT_MASK); //Reset bit for not going forward
GPIO_SetBits(CAR_PORT, CAR_RIGHT_MASK); //Set bit for not going backwards
}
/*************************************************************************
* Function Name: Left
* Parameters: none
*
* Return: none
*
* Description: Resets bits for forwards and backwards
*
*************************************************************************/
void Left(void)
{
GPIO_ResetBits(CAR_PORT, CAR_RIGHT_MASK); //Reset bit for not going forward
GPIO_SetBits(CAR_PORT, CAR_LEFT_MASK); //Set bit for not going backwards
}
/*************************************************************************
* Function Name: StopTurn
* Parameters: none
*
* Return: none
*
* Description: Resets bits for forwards and backwards
*
*************************************************************************/
void StopTurn(void)
{
GPIO_SetBits(CAR_PORT, CAR_LEFT_MASK); //Reset bit for not going forward
GPIO_SetBits(CAR_PORT, CAR_RIGHT_MASK); //Set bit for not going backwards
}
void GoCars(car_state command)
{
if (command == 0){
StopTurn();
Forward();
}
else if (command == 1){
Right();
Forward();
}
else if (command == 2){
Left();
Forward();
}
else if (command == 6){
StopTurn();
Backwards();
}
else if (command == 4){
Right();
Backwards();
}
else if (command == 5){
Left();
Backwards();
} else {
StopTurn();
Stop();
}
}
void GoCar(int Test, int TestTurn)
{ //Control speed
if (Test==1)
{
Forward();
}
else if(Test==2)
{
Backwards();
}
else if(Test==0)
{
Stop();
}
//Control direction
if (TestTurn==1)
{
Right();
}
else if(TestTurn==2)
{
Left();
}
else if(TestTurn==0)
{
StopTurn();
}
}