-
Notifications
You must be signed in to change notification settings - Fork 0
/
Motors.c
116 lines (103 loc) · 3.96 KB
/
Motors.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
/**
* @file Motors.c
* @author Michelle Tran & Grecia Francisco
* @brief DC motor controls implementations
*/
#include "tm4c123gh6pm.h"
#include "Motors.h"
// Initialize motors to use Hardware PWM
// Right Wheel - PB6 - M0PWM0
// Left Wheel - PD1 - M1PWM1
// The signal goes Low on the LOAD value & High on the COMPx value
// Use PLL clock (16 MHz)
void Motors_Init(void){
if((SYSCTL_RCGC2_R & SYSCTL_RCGC2_GPIOB) == 0)
{ // initialize Port B clk
SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOB;
while((SYSCTL_RCGC2_R & SYSCTL_RCGC2_GPIOB) == 0){};
}
if((SYSCTL_RCGC2_R & SYSCTL_RCGC2_GPIOD) == 0)
{// activate port D clk
SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOD;
while((SYSCTL_PRGPIO_R & SYSCTL_RCGC2_GPIOD) == 0){};
}
// PB6 M0PWM0
GPIO_PORTB_AFSEL_R |= PB6; // enable PB6 alt. func.
GPIO_PORTB_PCTL_R &= ~GPIO_PCTL_PB6_M; // clear M0PWM0 slot
GPIO_PORTB_PCTL_R |= GPIO_PCTL_PB6_M0PWM0; // enable M0PWM0
GPIO_PORTB_DR8R_R |= PB6; // enable 8mA drive
GPIO_PORTB_DEN_R |= PB6;
// Initialize PWM settings
SYSCTL_RCGCPWM_R |= 0x03; // PWM Module 0 & Module 1
SYSCTL_RCC_R = (SYSCTL_RCC_R & (~0x001E0000))|SYSCTL_RCC_USEPWMDIV;
PWM0_0_CTL_R = 0; // re-loading down-counting mode
PWM0_0_GENA_R |= 0xC8; // low on LOAD, high on CMPA down => DUTY = COMPA
PWM0_0_LOAD_R = PERIOD - 1;
PWM0_0_CMPA_R = 0; // count value when output rises
PWM0_0_CTL_R |= PWM_0_CTL_ENABLE; // start the timers in PWM generator
PWM0_ENABLE_R &= ~PWM_ENABLE_PWM0EN; // disable to NOT produce PWM signals on initialization
// Using PD1 M1PWM1
GPIO_PORTD_AFSEL_R |= 0x02; // enable alt funct on PD1
GPIO_PORTD_PCTL_R &= ~0x000000F0; // configure PD0 as PWM0
GPIO_PORTD_PCTL_R |= 0x00000050;
GPIO_PORTD_DEN_R |= 0x02; // enable digital I/O on PD1
GPIO_PORTD_DR8R_R |= 0x02; // 8mA
PWM1_0_CTL_R = 0; // re-loading down-counting mode
PWM1_0_GENB_R |= 0xC08; // low on LOAD, high on CMPA down
PWM1_0_LOAD_R = PERIOD - 1; // cycles needed to count down to 0
PWM1_0_CMPB_R = 0; // count value when output rises
PWM1_0_CTL_R |= PWM_0_CTL_ENABLE; // set PWM0
PWM1_ENABLE_R &= ~PWM_ENABLE_PWM1EN; // disable PD1/M1PWM1
}
// Description: Initializes PB5432 for use with L298N motor driver direction
void Car_Dir_Init(void){
if ((SYSCTL_RCGC2_R&SYSCTL_RCGC2_GPIOB)==0) {
SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOB; // Activate B clocks
while ((SYSCTL_RCGC2_R&SYSCTL_RCGC2_GPIOB)==0){};
}
GPIO_PORTB_AMSEL_R &= ~0x3C; // disable analog function
GPIO_PORTB_AFSEL_R &= ~0x3C; // no alternate function
GPIO_PORTB_PCTL_R &= ~0x00FFFF00; // GPIO clear bit PCTL
GPIO_PORTB_DIR_R |= 0x3C; // output on pin(s)
GPIO_PORTB_DEN_R |= 0x3C; // enable digital I/O on pin(s)
}
// Dependency: PWM_Init()
// Inputs:
// duty_L is the value corresponding to the duty cycle of the left wheel
// duty_R is the value corresponding to the duty cycle of the right wheel
// Outputs: None
// Description: Changes the duty cycles of by changing the CMP registers
void PWM_PB6PD1_Duty(unsigned long duty_L, unsigned long duty_R){
PWM0_0_CMPA_R = duty_L - 1; // PB6 count value when output rises
PWM1_0_CMPB_R = duty_R - 1; // PD1 count value when output rises
}
// turn right by slowing down the right motor
void turnRight(void)
{
WHEEL_DIR = FORWARD;
PWM_PB6PD1_Duty(PERIOD * 0.3, 0);
}
// turn left by slowing down the left motor
void turnLeft(void)
{
WHEEL_DIR = FORWARD;
PWM_PB6PD1_Duty(0, PERIOD * 0.3);
}
// drive the motors at a certain duty cycle
void moveStraight(void)
{
PWM_PB6PD1_Duty(PERIOD * 0.3, PERIOD * 0.3);
}
// enable both motors
void start(void)
{
WHEEL_DIR = FORWARD;
PWM0_ENABLE_R |= PWM_ENABLE_PWM0EN;
PWM1_ENABLE_R |= PWM_ENABLE_PWM1EN;
}
// disable both motors
void stop(void)
{
PWM0_ENABLE_R &= ~PWM_ENABLE_PWM0EN;
PWM1_ENABLE_R &= ~PWM_ENABLE_PWM1EN;
}