You are on page 1of 6

W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\PWMService.

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

Page 1

/****************************************************************************
Module
PWMService.c
Revision
1.0

Description
Author
Anne Alter, Derrick Contreras
Date
2/14/16

****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
//headers for framework
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "TemplateService.h"
#include "termio.h"
//headers to access the pwm and nvic libraries
#include "inc/hw_timer.h"
#include "inc/hw_nvic.h"
#include "inc/hw_pwm.h"
//headers to access the GPIO subsystem
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_ssi.h"

//headers to access the TivaWare Library


#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"

//headers for other modules in the program


#include "PWMService.h"
#include "PoundDefines.h"
/*----------------------------- Module Defines ----------------------------*/
/*---------------------------- Module Functions ---------------------------*/
void InitPWMWheelMotors(void);
/*---------------------------- Module Variables ---------------------------*/
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
InitAllPWM
Parameters
None
Returns
None

Description
Initializes PWM service

Author
Anne Alter, Derrick Contreras
****************************************************************************/
void InitAllPWM(void)

W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\PWMService.c
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

Page 2

{
}

InitPWMWheelMotors();

/****************************************************************************
Function
InitPWMWheelMotors
Parameters
None
Returns
None

Description
Initializes PWM service

Author
Anne Alter, Derrick Contreras
****************************************************************************/
void InitPWMWheelMotors(void)
{
//initialize Dummy variable using volatile to avoid over-optimization
volatile uint32_t Dummy;
//enable the clock to the PWM module (PWM0)
HWREG(SYSCTL_RCGCPWM) |= SYSCTL_RCGCPWM_R0;

//enable the clock to port B


HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R1;

//select the PWM clock as system clock/32


HWREG(SYSCTL_RCC) = (HWREG(SYSCTL_RCC) & ~SYSCTL_RCC_PWMDIV_M) | (SYSCTL_RCC_USEPWMDIV
|SYSCTL_RCC_PWMDIV_32);
//make sure the PWM clock has gotten going
while ((HWREG(SYSCTL_PRPWM) & SYSCTL_PRPWM_R0) != SYSCTL_PRPWM_R0);
// FOR WHEEL MOTORS
//disable PWM while initializing
HWREG(PWM0_BASE+PWM_O_0_CTL) = 0;

//program generator A to go to 1 at rising compare A, 0 at falling compare A


HWREG(PWM0_BASE+PWM_O_0_GENA) = (PWM_0_GENA_ACTCMPAU_ONE | PWM_0_GENA_ACTCMPAD_ZERO);
//program generator B to go to 1 at rising compare B, 0 at falling compare B
HWREG(PWM0_BASE+PWM_O_0_GENB) = (PWM_0_GENB_ACTCMPBU_ONE | PWM_0_GENB_ACTCMPBD_ZERO);

//set the PWM period, since counting up and down, initialize the load register to half the desired
total period
HWREG(PWM0_BASE+PWM_O_0_LOAD) = ((PeriodInMS * PWMTicksPerMS)-1)>>1;
//set the initial duty cycle on A (50%)
//HWREG(PWM0_BASE+PWM_O_0_CMPA) = HWREG(PWM0_BASE+PWM_O_0_LOAD)>>1;

//set the initial duty cycle on B (75%)


//HWREG(PWM0_BASE+PWM_O_0_CMPB) = (HWREG(PWM0_BASE+PWM_O_0_LOAD))-(((PeriodInMS *
PWMTicksPerMS)-1)>>2);
// END OF WHEEL MOTORS
// FOR FLYWHEEL MOTOR AND SERVO
//disable PWM while initializing
HWREG(PWM0_BASE+PWM_O_1_CTL) = 0;

//program generator A to go to 1 at rising compare A, 0 at falling compare A


HWREG(PWM0_BASE+PWM_O_1_GENA) = (PWM_1_GENA_ACTCMPAU_ONE | PWM_1_GENA_ACTCMPAD_ZERO);
//program generator B to go to 1 at rising compare B, 0 at falling compare B
HWREG(PWM0_BASE+PWM_O_1_GENB) = (PWM_1_GENB_ACTCMPBU_ONE | PWM_1_GENB_ACTCMPBD_ZERO);

//set the PWM period for servo (ok to be teh same for flywheel), since counting up and down,
initialize the load register to half the desired total period

W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\PWMService.c
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

Page 3

HWREG(PWM0_BASE+PWM_O_1_LOAD) = ((ServoPeriodInMs * PWMTicksPerMS)-1)>>1;


// END OF FLYWHEEL MOTOR AND SERVO

//enable the PWM outputs (0 and 1 are wheel motors, 2 and 3 are flywheel and servo)
HWREG(PWM0_BASE+PWM_O_ENABLE) |= (PWM_ENABLE_PWM3EN | PWM_ENABLE_PWM2EN | PWM_ENABLE_PWM1EN |
PWM_ENABLE_PWM0EN);
//configure port B pins to be PWM outputs
HWREG(GPIO_PORTB_BASE+GPIO_O_AFSEL) |= (BIT4HI | BIT5HI | BIT6HI | BIT7HI);

//choose to map PWM to those pins


HWREG(GPIO_PORTB_BASE+GPIO_O_PCTL) = (HWREG(GPIO_PORTB_BASE+GPIO_O_PCTL) & 0x0000fff) +
(4<<(7*BitsPerNibble)) + (4<<(6*BitsPerNibble))+(4<<(5*BitsPerNibble)) + (4<<(4*BitsPerNibble));
//enable pins 4&5 & 6&7 on port B for digital I/O
HWREG(GPIO_PORTB_BASE+GPIO_O_DEN) |= (BIT4HI | BIT5HI | BIT6HI | BIT7HI);
//enable pins 4&5 & 6&7 as outputs
HWREG(GPIO_PORTB_BASE+GPIO_O_DIR) |= (BIT4HI | BIT5HI | BIT6HI | BIT7HI);

//set up/down count mode for wheel motors


HWREG(PWM0_BASE+PWM_O_0_CTL) = (PWM_0_CTL_MODE | PWM_0_CTL_ENABLE | PWM_0_CTL_GENAUPD_LS |
PWM_0_CTL_GENBUPD_LS);
//set up/down count mode for servo and flywheel
HWREG(PWM0_BASE+PWM_O_1_CTL) = (PWM_1_CTL_MODE | PWM_1_CTL_ENABLE | PWM_1_CTL_GENAUPD_LS |
PWM_1_CTL_GENBUPD_LS);
// Ensure Motors are off
SetDirection(LEFT_MOTOR, FORWARD);
SetDirection(RIGHT_MOTOR, FORWARD);
SetDuty(LEFT_MOTOR, OFF_DUTY);
SetDuty(RIGHT_MOTOR, OFF_DUTY);
// Ensure Flywheel is off
SetFlywheelDuty(1);
}

//ensure servo is at reset angle


SetServoAngle(RESET_ANGLE);

/****************************************************************************
Function
SetDuty
Parameters
Value for duty cycle to be set
Returns
Nothing

Description
Notes

Author
Anne Alter, Derrick Contreras
****************************************************************************/
void SetDuty(uint8_t WHICH_MOTOR, uint32_t SET_DUTY)
{
if (WHICH_MOTOR == LEFT_MOTOR) {
//set the DC to 0% by programming the compare value to the load value
if (SET_DUTY == 0)
{
//since the CmpADn action (set to zero) wins, the output stays low (per/2-0=per/2)
HWREG(PWM0_BASE+PWM_O_0_CMPA) = HWREG(PWM0_BASE+PWM_O_0_LOAD);
}
//set DC to 100% is more complex
else if (SET_DUTY == 100)
{
//set the load value to 0 since the desired hi time is Per/2 (per/2-per/2=0)

W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\PWMService.c
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

Page 4

HWREG(PWM0_BASE+PWM_O_0_CMPA) = 0;

//since the CmpADn action (was set to zero) wins, change generator events to always force hi
//HWREG(PWM0_BASE+PWM_O_0_GENB)=(PWM_0_GENB_ACTCMPBU_ONE | PWM_0_GENB_ACTCMPBD_ONE);
//turn off enable
HWREG(PWM0_BASE+PWM_O_ENABLE) &= ~(PWM_ENABLE_PWM0EN);
//

}
//else set desired duty cycle
else
{
//HWREG(PWM0_BASE+PWM_O_0_CMPA) = (HWREG(PWM0_BASE+PWM_O_0_LOAD) (HWREG(PWM0_BASE+PWM_O_0_LOAD)*(100-SET_DUTY))/100);
HWREG(PWM0_BASE+PWM_O_0_CMPA) = ((PeriodInMS * PWMTicksPerMS)-1)*(100-SET_DUTY)/200;
//printf("%u\t %u\t \r\n", WHICH_MOTOR, SET_DUTY);
}
} else if (WHICH_MOTOR == RIGHT_MOTOR) {
//set the DC to 0% by programming the compare value to the load value
if (SET_DUTY == 0)
{
//since the CmpADn action (set to zero) wins, the output stays low (per/2-0=per/2)
HWREG(PWM0_BASE+PWM_O_0_CMPB) = HWREG(PWM0_BASE+PWM_O_0_LOAD);
}
//set DC to 100% is more complex
else if (SET_DUTY == 100)
{
//set the load value to 0 since the desired hi time is Per/2 (per/2-per/2=0)
HWREG(PWM0_BASE+PWM_O_0_CMPB) = 0;

//since the CmpBDn action (was set to zero) wins, change generator events to always force hi
//HWREG(PWM0_BASE+PWM_O_0_GENB)=(PWM_0_GENB_ACTCMPBU_ONE | PWM_0_GENB_ACTCMPBD_ONE);
//turn off enable
HWREG(PWM0_BASE+PWM_O_ENABLE) &= ~(PWM_ENABLE_PWM1EN);

}
//else set desired duty cycle
else
{
//HWREG(PWM0_BASE+PWM_O_0_CMPB) = (HWREG(PWM0_BASE+PWM_O_0_LOAD) (HWREG(PWM0_BASE+PWM_O_0_LOAD)*(100-SET_DUTY))/100);
HWREG(PWM0_BASE+PWM_O_0_CMPB) = ((PeriodInMS * PWMTicksPerMS)-1)*(100-SET_DUTY)/200;
//printf("%u\t %u\t \r\n", WHICH_MOTOR, SET_DUTY);
}
}
}
/****************************************************************************
Function
SetDirection
Parameters
int to indidcate which motor to change (1 or 2)
bool to indicate direction (true = forward, false = reverse)
Returns
Nothing

Description
Notes

Author
M. Metlitz, 02/02/16
****************************************************************************/
void SetDirection(uint8_t WHICH_MOTOR, bool DIRECTION)
{
// Motor one selected
if (WHICH_MOTOR == LEFT_MOTOR)
{

W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\PWMService.c
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

Page 5

// Want to go forward
if (DIRECTION == FORWARD)
{
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA + ALL_BITS)) &= ~(LEFT_MOTOR_DIRECTION);
}
// Want to go backward
else
{
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA + ALL_BITS)) |= (LEFT_MOTOR_DIRECTION);
}

}
// Motor two selected
else
{
// Want to go forward
if (DIRECTION == FORWARD)
{
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA + ALL_BITS)) &= ~(RIGHT_MOTOR_DIRECTION);
}
// Want to go backward
else
{
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA + ALL_BITS)) |= (RIGHT_MOTOR_DIRECTION);
}
}

/****************************************************************************
Function
SetFlywheelDuty
Parameters
Value for duty cycle to be set
Returns
Nothing

Description
Notes
***Need to account for both motors
Author
M. Metlitz, 02/24/16
****************************************************************************/
void SetFlywheelDuty(uint32_t SET_FLYWHEEL_DUTY)
{
//set the DC to 0% by programming the compare value to the load value
if (SET_FLYWHEEL_DUTY == 0)
{
//since the CmpADn action (set to zero) wins, the output stays low (per/2-0=per/2)
HWREG(PWM0_BASE+PWM_O_1_CMPA) = HWREG(PWM0_BASE+PWM_O_1_LOAD);
}
//set DC to 100% is more complex
else if (SET_FLYWHEEL_DUTY == 100)
{
//set the load value to 0 since the desired hi time is Per/2 (per/2-per/2=0)
HWREG(PWM0_BASE+PWM_O_1_CMPA) = 0;

//since the CmpADn action (was set to zero) wins, change generator events to always force hi
//HWREG(PWM0_BASE+PWM_O_1_GENA)=(PWM_0_GENA_ACTCMPAU_ONE | PWM_0_GENA_ACTCMPBA
//turn off enable
HWREG(PWM0_BASE+PWM_O_ENABLE) &= ~(PWM_ENABLE_PWM2EN);

}
//else set desired duty cycle
else
{
HWREG(PWM0_BASE+PWM_O_1_CMPA) = ((ServoPeriodInMs * PWMTicksPerMS)-1)*(100 - SET_FLYWHEEL_DUTY)/200;
}

W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\PWMService.c
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

Page 6

/****************************************************************************
Function
SetServoAngle
Parameters
Value for servo angle to go to
Returns
Nothing

Description
Notes
Don't give this values of 0 or 180 degrees
Author
M. Metlitz, 02/25/16
****************************************************************************/
void SetServoAngle(uint32_t WHICH_ANGLE)
{
// Adjust duty cycle between 1ms and 2ms based on value of angle
//HWREG(PWM0_BASE+PWM_O_1_CMPB) = (((ServoPeriodInMs * PWMTicksPerMS)-1)*(WHICH_ANGLE)/1800+.1)/2;
HWREG(PWM0_BASE+PWM_O_1_CMPB) = (((ServoPeriodInMs * PWMTicksPerMS)-1)*(1000-WHICH_ANGLE)/1000)/2;
}

You might also like