Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. //*****************************************************************************
  2. // UART 1
  3. //*****************************************************************************
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include "inc/hw_memmap.h"
  7. #include "driverlib/gpio.h"
  8. #include "driverlib/pin_map.h"
  9. #include "driverlib/sysctl.h"
  10. #include "driverlib/uart.h"
  11. #include <math.h>
  12.  
  13. #define GPIO_PINS_ALL GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
  14.  
  15. //*****************************************************************************
  16. // Configure the UART and perform reads and writes using polled I/O.
  17. //*****************************************************************************
  18.  
  19.  
  20. int
  21. main(void)
  22. {
  23.  
  24. char cThisChar;
  25. int p;
  26. int i=0;
  27. int j=0;
  28. // Set the clocking to run directly from the external crystal/oscillator.
  29. // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
  30. // crystal on your board.
  31. SysCtlClockSet (SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ);
  32.  
  33. // Enable the peripherals used by this example.
  34. // The UART itself needs to be enabled, as well as the GPIO port
  35. // containing the pins that will be used.
  36. SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
  37. SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
  38. SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
  39.  
  40.  
  41. GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PINS_ALL);
  42. GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PINS_ALL);
  43.  
  44. // Configure the GPIO pin muxing for the UART function.
  45. // This is only necessary if your part supports GPIO pin function muxing.
  46. // Study the data sheet to see which functions are allocated per pin.
  47. // TODO: change this to select the port/pin you are using
  48. //
  49. GPIOPinConfigure(GPIO_PA0_U0RX);
  50. GPIOPinConfigure(GPIO_PA1_U0TX);
  51.  
  52. // Since GPIO A0 and A1 are used for the UART function, they must be
  53. // configured for use as a peripheral function (instead of GPIO).
  54. // TODO: change this to match the port/pin you are using
  55. GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
  56.  
  57. // Configure the UART for 115,200, 8-N-1 operation.
  58. // This function uses SysCtlClockGet() to get the system clock
  59. // frequency. This could be also be a variable or hard coded value
  60. // instead of a function call.
  61. UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200,
  62. (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
  63. UART_CONFIG_PAR_NONE));
  64.  
  65. // Put a character to show start of example. This will display on the
  66. // terminal.
  67. UARTCharPut(UART0_BASE, '>');
  68.  
  69.  
  70. // Enter a loop to read characters from the UART, and write them back
  71. // (echo). When a line end is received, the loop terminates.
  72. //
  73. do
  74. {
  75. // Read a character using the blocking read function. This function
  76. // will not return until a character is available.
  77. cThisChar = UARTCharGet(UART0_BASE);
  78.  
  79. // Write the same character using the blocking write function. This
  80. // function will not return until there was space in the FIFO and
  81. // the character is written.
  82. if( (cThisChar != '\n') && (cThisChar != '\r') ){
  83.  
  84. if(cThisChar == "B")
  85. {
  86. cThisChar = UARTCharGet(UART0_BASE);
  87. for(i=0; i<=10; i++)
  88. {
  89. if(cThisChar == i)
  90. {
  91. cThisChar = UARTCharGet(UART0_BASE);
  92. if(cThisChar == "+") GPIOPinWrite(GPIO_PORTA_BASE, 0xFF, pow(2,i));
  93. else GPIOPinWrite(GPIO_PORTA_BASE, 0xFF, 0);
  94.  
  95. }
  96. }
  97. }
  98. else if(cThisChar == "C")
  99. {
  100. cThisChar = UARTCharGet(UART0_BASE);
  101. for(j=0; j<=10; j++)
  102. {
  103. if(cThisChar == j)
  104. {
  105. cThisChar = UARTCharGet(UART0_BASE);
  106. if (cThisChar == "+") GPIOPinWrite(GPIO_PORTA_BASE, 0xFF, pow(2,j));
  107. else GPIOPinWrite(GPIO_PORTA_BASE, 0xFF, 0);
  108.  
  109. }
  110. }
  111. }
  112.  
  113. // echo to terminal
  114. UARTCharPut(UART0_BASE, cThisChar);
  115. }else{
  116. // send a new line
  117. //
  118. UARTCharPut(UART0_BASE, '\n');
  119. UARTCharPut(UART0_BASE, '\r');
  120. UARTCharPut(UART0_BASE, '>');
  121. }
  122. }
  123. while(1);
  124.  
  125. return(0);
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement