final project  1
uart.c
Go to the documentation of this file.
1 
10 #include "main.h"
11 
12 
16 void uart_init(void){
17 
18 
19  //enable clock to GPIO, R1 = port B
20  SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R1;
21  //enable clock to UART1, R1 = UART1. ***Must be done before setting Rx and Tx (See DataSheet)
22  SYSCTL_RCGCUART_R |= SYSCTL_RCGCUART_R1;
23  //enable alternate functions on port b pins 0 and 1
24  GPIO_PORTB_AFSEL_R |= (BIT0 | BIT1);
25  //enable Rx and Tx on port B on pins 0 and 1
26  GPIO_PORTB_PCTL_R |= 0x00000011;
27  //set pin 0 and 1 to digital
28  GPIO_PORTB_DEN_R |= (BIT0 | BIT1);
29  //set pin 0 to Rx or input
30  GPIO_PORTB_DIR_R &= ~BIT0;
31  //set pin 1 to Tx or output
32  GPIO_PORTB_DIR_R |= BIT1;
33  //calculate baudrate - 1152000, HSE should be 0, clkDiv = 16
34 
35  uint16_t iBRD = 0x8;
36  uint16_t fBRD = 0x2c;
37  //turn off uart1 while we set it up
38  UART1_CTL_R &= ~(UART_CTL_UARTEN);
39  //set baud rate
40  UART1_IBRD_R = iBRD;
41  UART1_FBRD_R = fBRD;
42  //set frame, 8 data bits, 1 stop bit, no parity, no FIFO
43  UART1_LCRH_R = UART_LCRH_WLEN_8;
44  //use system clock as source
45  UART1_CC_R = UART_CC_CS_SYSCLK;
46  //re-enable enable RX, TX, and uart1
47  UART1_CTL_R = (UART_CTL_RXE | UART_CTL_TXE | UART_CTL_UARTEN);
48 }
49 
54 void uart_sendChar(char data){
55  while(UART1_FR_R & 0x20) {} //wait to send
56  UART1_DR_R = data;
57 }
58 
63 int uart_receive(void){
64  char data = 0;
65  while(UART1_FR_R & UART_FR_RXFE) {} //wait 2 receive
66  data = (char)(UART1_DR_R & 0xFF); //mask and get data bits
67 
68  return data; //return data
69 }
70 
75 void uart_sendStr(const char *data) {
76  char *i = data;
77  while(*i != '\0') { //move through string, sending it char by char
78  uart_sendChar(*i);
79  i++;
80  }
81 }
void uart_init(void)
sets all necessary registers to enable the uart 1 module.
Definition: uart.c:16
int uart_receive(void)
polling receive an 8 bit character over uart 1 module.
Definition: uart.c:63
the main code for the robot to interact with the user
void uart_sendChar(char data)
Sends a single 8 bit character over the uart 1 module.
Definition: uart.c:54
void uart_sendStr(const char *data)
sends an entire string of character over uart 1 module
Definition: uart.c:75