-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbasic_example_UART.c
More file actions
148 lines (112 loc) · 5.32 KB
/
Copy pathbasic_example_UART.c
File metadata and controls
148 lines (112 loc) · 5.32 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
/*
* This is a simple program to introduce UART.
* Pushing S2 on the BoosterPack toggles the LED1 on the Launchpad at any time.
* The program gets characters sent from your Laptop and decides whether it is Number, Letter or Other. Then,
* it sends back N, L or O to the laptop in response.
* Sending lower case c from laptop changes the baudrate. Currenlty, the only other baudrate implemented is 19200.
* The system clock for this project is 3MHz and all the UART calcuations are based on this number.
*/
#include <ti/devices/msp432p4xx/driverlib/driverlib.h>
typedef enum {baud9600, baud19200, baud38400, baud57600} UARTBaudRate_t;
void TurnOn_Launchpad_LED1();
void TurnOff_Launchpad_LED1();
char S1isPressed();
void initializeGPIO();
int main(void) {
WDT_A_hold(WDT_A_BASE);
initializeGPIO();
// make sure Tx and Rx pins of EUSCI_A0 work for UART and not a regular GPIO pin
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);
eUSCI_UART_ConfigV1 uartConfig;
// Related to baudrate generation and sampling
uartConfig.selectClockSource = EUSCI_A_UART_CLOCKSOURCE_SMCLK;
uartConfig.clockPrescalar = 19; // UCBR = 19
uartConfig.firstModReg = 8; // UCBRF = 8
uartConfig.secondModReg = 0x55; // UCBRS = 0x55
uartConfig.overSampling = EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION; // Oversampling
// Related to other configurations
uartConfig.parity = EUSCI_A_UART_NO_PARITY; // No praity
uartConfig.msborLsbFirst = EUSCI_A_UART_LSB_FIRST; // LSB First
uartConfig.numberofStopBits = EUSCI_A_UART_ONE_STOP_BIT; // One stop bit
uartConfig.dataLength = EUSCI_A_UART_8_BIT_LEN; // Data length is 8 bits
// Beyond this course's knowledge base
uartConfig.uartMode = EUSCI_A_UART_MODE; // UART mode -- the most typical usage
// initialize and enable EUSCI_A0
UART_initModule(EUSCI_A0_BASE, &uartConfig);
UART_enableModule(EUSCI_A0_BASE);
UARTBaudRate_t baudRate = baud9600;
char rChar, tChar;
while (1)
{
// To avoid blocking, we check to see if there is a character to process. In that case, we proceed to receiving it.
if (UART_getInterruptStatus (EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG))
{ //beginning of "got new char"
// calling this function without the above check results in a blocking code.
// this function waits until it receives a character from UART
rChar = UART_receiveData(EUSCI_A0_BASE);
// Depending on if the received char is a Number, a Letter, or Otherwise, the transmit char is N, L or O
if (('0'<=rChar) && (rChar <= '9'))
tChar = 'N';
else if ((('a'<=rChar) && (rChar <= 'z')) ||
(('A'<=rChar) && (rChar <= 'Z')))
tChar = 'L';
else
tChar = 'O';
if (UART_getInterruptStatus (EUSCI_A0_BASE, EUSCI_A_UART_TRANSMIT_INTERRUPT_FLAG))
UART_transmitData(EUSCI_A0_BASE, tChar);
// If the character is 'c', it also means to change the baudrarte
if (rChar == 'c')
{
switch(baudRate)
{
case baud9600:
uartConfig.clockPrescalar = 9;
uartConfig.firstModReg = 12;
uartConfig.secondModReg = 0x22;
UART_initModule(EUSCI_A0_BASE, &uartConfig);
UART_enableModule(EUSCI_A0_BASE);
baudRate = baud19200;
break;
case baud19200:
// to complete
break;
case baud38400:
// to complete
break;
} // end of switch-case
} // end of if (rChar == 'c')
} // end "got new char"
// The non-blocking test. This part of code should always work smoothly if other parts of code are not blocking it.
// If the button is not pressed, keep the LED off
if (!S1isPressed())
TurnOff_Launchpad_LED1();
// otherwise, turn the LED on
else
TurnOn_Launchpad_LED1();
} // end of while(1)
}
//// Simple GPIO HAL for non-blocking test ///////////////////////////////////
// According to the schematics on page 37 of the Launchpad user guide,
// When a button is pressed, it is grounded (logic 0)
#define PRESSED 0
// The HAL itself is written using Driverlib, so it is much easier to implement.
void initializeGPIO()
{
// Initializing LED1, which is on Pin 0 of Port P1 (from page 37 of the Launchpad User Guide)
GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0);
// Initializing S1 (switch 1 or button 1),
// which is on Pin1 of Port 1 (from page 37 of the Launchpad User Guide)
GPIO_setAsInputPinWithPullUpResistor (GPIO_PORT_P1, GPIO_PIN1);}
void TurnOn_Launchpad_LED1()
{
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN0);
}
void TurnOff_Launchpad_LED1()
{
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0);
}
char S1isPressed()
{
return ((GPIO_getInputPinValue(GPIO_PORT_P1, GPIO_PIN1) == PRESSED));
}