-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
143 lines (128 loc) · 4.2 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
/*******************************************************************************
* MSP432 main file
*
* Description: start by initializing all the sensors and motor, followed by
* settings up the interrupt for switch S1,S2 and once
* connected to raspberry pi, it will receive commands
* and perform the actions
*
* MSP432P401
* ------------------
* /|\| |
* | | |
* --|RST P1.1 |<--Toggle Switch - change direction
* | |
* | P1.4 |<--Toggle Switch - stop car
* | |
* | |
* | |
*
*******************************************************************************/
/* DriverLib Includes */
#include <ti/devices/msp432p4xx/driverlib/driverlib.h>
#include "movement.h"
#include "serial.h"
#include <stdint.h>
#include <stdbool.h>
//define UART commands
#define FORWARD 'w'
#define LEFT 'a'
#define RIGHT 'd'
#define STOP 's'
#define ONRED '1'
#define OFFRED '2'
#define ONGREEN '3'
#define OFFGREEN '4'
#define ONBLUE '5'
#define OFFBLUE '6'
#define ONLED1 '7'
#define OFFLED1 '8'
int main(void)
{
/* Halting the watchdog */
MAP_WDT_A_holdTimer();
GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN0 | GPIO_PIN1 | GPIO_PIN2); // Configure P2 LED
GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0); // Configure P1 LED
//off all LEDs first
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0);
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0);
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN1);
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN2);
//initialization
initUltraSensors();
setWheelInterupt();
setMotorPorts();
setS1S2Interrupt();
initUART();
uPrintf("Going to Sleep\n\r");
//low power mode and wait for interrupt
while (1)
{
PCM_gotoLPM3InterruptSafe();
}
}
/* Port1 ISR */
void PORT1_IRQHandler(void)
{
uint32_t status = MAP_GPIO_getEnabledInterruptStatus(GPIO_PORT_P1);
GPIO_clearInterruptFlag(GPIO_PORT_P1, status);
if (status & GPIO_PIN1) //S1 interrupt progressively step up the duty cycle of the PWM on a button press
{
changeDirection();
}
else if (status & GPIO_PIN4) //S2 interrupt to stop car
{
zeroPWN();
}
}
//receive commands from raspberry pi through UART serial communication
void EUSCIA0_IRQHandler(void)
{
uint32_t status = MAP_UART_getEnabledInterruptStatus(EUSCI_A0_BASE);
MAP_UART_clearInterruptFlag(EUSCI_A0_BASE, status);
unsigned char msg = 0;
//receive data from raspberry pi
msg = UART_receiveData(EUSCI_A0_BASE);
//execute actions for the car
switch (msg)
{
case FORWARD: // move the car forward
startMoving();
break;
case LEFT: //turn the car left
rotateCarLeft();
break;
case RIGHT:// turn the car right
rotateCarRight();
break;
case STOP: // stop the car
zeroPWN();
break;
case ONRED: //turn on red for led 2
GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN0);
break;
case OFFRED: //off red for led 2
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0);
break;
case ONGREEN: //turn on green for led 2
GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN1);
break;
case OFFGREEN: //turn off green for led 2
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN1);
break;
case ONBLUE://turn on blue for led 2
GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN2);
break;
case OFFBLUE://turn off blue for led 2
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN2);
break;
case ONLED1: //turn on red for led 1
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN0);
break;
case OFFLED1://turn off red for led 1
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0);
break;
default: //pass data back to raspberry pi
UART_transmitData(EUSCI_A0_BASE, msg);
}
}