-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
293 lines (258 loc) · 8.33 KB
/
main.cpp
File metadata and controls
293 lines (258 loc) · 8.33 KB
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
/****************************************************************
** OrangeBot Project
*****************************************************************
** /
** /
** /
** ______ \
** \
** \
*****************************************************************
** Project
*****************************************************************
** Brief
****************************************************************/
/****************************************************************
** DESCRIPTION
****************************************************************
** Test Four VNH7040
** Give a ramp each
****************************************************************/
/****************************************************************
** HISTORY VERSION
****************************************************************
**
****************************************************************/
/****************************************************************
** USED PINS
****************************************************************
** VNH7040
** | DRV0 | DRV1 | DRV2 | DRV3 | VNH7040
** -------------------------------------------------------------------------
** uC_SEN | PF0 | PF0 | PF0 | PF0 | SENSE ENABLE
** uC_DIAG | PF1 | PF1 | PF1 | PF1 | SEL1
** uC_PWM | PA2,B20 | PA3,B21 | PB4,B22 | PB5,B23 | PWM
** uC_CTRLA | PA4 | PA6 | PB2 | PD6 | INA, SEL0
** uC_CTRLB | PA5 | PA7 | PB3 | PD7 | INB
****************************************************************/
/****************************************************************
** KNOWN BUGS
****************************************************************
**
****************************************************************/
/****************************************************************
** DEFINES
****************************************************************/
#define EVER (;;)
/****************************************************************
** INCLUDES
****************************************************************/
//Pin Configuration
//PF5: Embedded LED0
#include "global.h"
/****************************************************************
** FUNCTION PROTOTYPES
****************************************************************/
//Set direction and speed setting of the VNH7040 controlled motor
extern void set_vnh7040_speed( uint8_t index, bool f_dir, uint8_t speed );
/****************************************************************
** GLOBAL VARIABLES
****************************************************************/
volatile Isr_flags g_isr_flags;
/****************************************************************************
** Function
** main |
****************************************************************************/
//! @return bool |
//! @brief dummy method to copy the code
//! @details test the declaration of a lambda method
/***************************************************************************/
int main(void)
{
//----------------------------------------------------------------
// VARS
//----------------------------------------------------------------
//system tick prescaler
uint8_t pre = 0;
//! Motor Test
//index of the motor under test
uint8_t motor_index = 0;
//Speed ramp. false = accelerate | true = decelerate
bool f_ramp = false;
//Direction. false = clockwise | true = counterclockwise
bool f_dir = false;
//Speed counter
uint8_t speed = 0;
//----------------------------------------------------------------
// INIT
//----------------------------------------------------------------
//! Initialize AT4809 internal peripherals
init();
//! Initialize VNH7040
//Enable sense output
SET_BIT_VALUE( PORTF.OUT, 0, true );
//Diagnostic mode OFF
SET_BIT_VALUE( PORTF.OUT, 1, false );
//Set direction stop
SET_BIT_VALUE( PORTA.OUT, 4, false ); //CTRLA
SET_BIT_VALUE( PORTA.OUT, 5, false ); //CTRLB
//----------------------------------------------------------------
// BODY
//----------------------------------------------------------------
//Main loop
for EVER
{
//If: System Tick 500Hz
if (g_isr_flags.system_tick == 1)
{
//Clear system tick
g_isr_flags.system_tick = 0;
//----------------------------------------------------------------
// LED BLINK
//----------------------------------------------------------------
//If prescaler has reset
if (pre == 0)
{
//Toggle PF5.
SET_BIT( PORTF.OUTTGL, 5 );
}
//Increment prescaler and reset if it exceeds the TOP. 2Hz
pre = AT_TOP_INC( pre, 249);
//----------------------------------------------------------------
// MOTOR CONTROL
//----------------------------------------------------------------
//If increase speed
if (f_ramp == false)
{
//increase speed
speed++;
//if: speed cap
if (speed >= (uint8_t)0xff)
{
//now decrease speed
f_ramp = true;
}
}
//If decrease speed
else
{
//decrease speed
speed--;
//If: speed cap
if (speed <= (uint8_t)0x00)
{
//Now increase speed
f_ramp = false;
//Invert motor direction
f_dir = !f_dir;
//If I'm going forward again
if (f_dir == false)
{
//change motor under test
motor_index = AT_TOP_INC( motor_index, 3);
}
}
}
//Set direction and speed setting of the VNH7040 controlled motor
set_vnh7040_speed( motor_index, f_dir, speed );
} //End If: System Tick
} //End: Main loop
//----------------------------------------------------------------
// RETURN
//----------------------------------------------------------------
return 0;
} //end: main
/****************************************************************************
** Function
** set_vnh7040_speed
****************************************************************************/
//! @return void |
//! @param index | index of the motor to be controlled. 0 to 3
//! @param f_dir | direction of rotation of the motor
//! @param speed | Speed of the motor
//! @brief Set direction and speed setting of the VNH7040 controlled motor
//! @details
//!
/***************************************************************************/
void set_vnh7040_speed( uint8_t index, bool f_dir, uint8_t speed )
{
//----------------------------------------------------------------
// VARS
//----------------------------------------------------------------
//----------------------------------------------------------------
// INIT
//----------------------------------------------------------------
//----------------------------------------------------------------
// BODY
//----------------------------------------------------------------
//Driver 0
if (index == 0)
{
//Set INA, INB and SEL0 to select the direction of rotation of the motor
if (f_dir == true)
{
SET_MASKED_BIT( PORTA.OUT, 0x30, 0x10);
}
else
{
SET_MASKED_BIT( PORTA.OUT, 0x30, 0x20);
}
//Set the PWM value of the right PWM channel of TCA0
TCB0.CCMPH = speed;
}
//Driver 1
else if (index == 1)
{
//Set INA, INB and SEL0 to select the direction of rotation of the motor
if (f_dir == true)
{
SET_MASKED_BIT( PORTA.OUT, 0xc0, 0x40);
}
else
{
SET_MASKED_BIT( PORTA.OUT, 0xc0, 0x80);
}
//Set the PWM value of the right PWM channel of TCA0
TCB1.CCMPH = speed;
}
//Driver 2
else if (index == 2)
{
//Set INA, INB and SEL0 to select the direction of rotation of the motor
if (f_dir == true)
{
SET_MASKED_BIT( PORTB.OUT, 0x0c, 0x04);
}
else
{
SET_MASKED_BIT( PORTB.OUT, 0x0c, 0x08);
}
//Set the PWM value of the right PWM channel of TCA0
TCB2.CCMPH = speed;
}
//Driver 3
else if (index == 3)
{
//Set INA, INB and SEL0 to select the direction of rotation of the motor
if (f_dir == true)
{
SET_MASKED_BIT( PORTD.OUT, 0xc0, 0x40);
}
else
{
SET_MASKED_BIT( PORTD.OUT, 0xc0, 0x80);
}
//Set the PWM value of the right PWM channel of TCA0
TCB3.CCMPH = speed;
}
//Default case
else
{
//Driver index not installed.
//Do nothing
}
//----------------------------------------------------------------
// RETURN
//----------------------------------------------------------------
return;
} //End: