0

I am fairly new to the Tiva C series launchpad and the microcontroller arena, in general. I am using TM4C123GXL launchpad, and keil uvision. Have been fairly successful in implementing basic examples like Blinking etc. That means there's nothing wrong with my configuration of the software. I followed a video lecture which used the serial port connected to the USB to change the LED colours as per the input by the user. The serial monitor I used is Hercules and the baud rate is 9600. I checked with a previous program, the system clock is running at 50 MHz. The program is upload without any issue, but when I open the serial port, nothing comes up.

I was going through the debugger, and for some reason it refuses to move on from the line while((UART0->FR & (1<<5)) != 0);

I am attaching the code below

#include <TM4C123.h>
#include <stdlib.h>
#include <string.h>
char readChar(void);
void printChar(char c);
void printString(char *string);

int main(void) {
  // char c;
  SYSCTL->RCGCUART |=
      (1 << 0); // Enabling the UART module with RCGCUART register

  // 2. Enable the clock to the appropriate GPIO module via the RCGCGPIO
  // register (see page 340).
  // To find out which GPIO port to enable, refer to Table 23-5 on page 1351.

  SYSCTL->RCGCGPIO |= (1 << 0);

  // 3.    Set the GPIO AFSEL bits for the appropriate pins (see page 671). To
  // determine which GPIOs to configure, see Table 23-4 on page 1344.

  GPIOA->AFSEL = (1 << 1) | (1 << 0);
  // 4. Configure the GPIO current level and/or slew rate as specified for the
  // mode selected (see page 673 and page 681).

  // 5. Configure the PMCn fields in the GPIOPCTL register to assign the UART
  // signals to the appropriate
  // pins (see page 688 and Table 23-5 on page 1351).

  GPIOA->PCTL = (1 << 0) | (1 << 4);
  GPIOA->DEN = (1 << 0) | (1 << 1);

  // Find the Baud Rate  Divisor
  // for 50 MHz, we have 27.12673611

  UART0->CTL &= ~(1 << 0);
  // write the interger portion
  UART0->IBRD = 325;
  UART0->FBRD = 33;

  // write the desired serial parameter

  UART0->LCRH = (0x3 << 5);
  // select the uart config clock
  UART0->CC = 0x5;

  // reenable UART
  UART0->CTL &= (1 << 0) | (1 << 8) | (1 << 9);

  // config LED pins

  SYSCTL->RCGCGPIO |= (1 << 5);                     // PORT F
  GPIOF->DIR = (1 << 1) | (1 << 2) | (1 << 3);      // LEDs as output
  GPIOF->DEN = (1 << 1) | (1 << 2) | (1 << 3);      // LEDs as Digital
  GPIOF->DATA &= ~((1 << 1) | (1 << 2) | (1 << 3)); // Turn off LEDs

  // delay(5);
  while (1) {
    printString("Enter \"r\",\"g\",or \"b\":\n\r");

    char c = readChar();
    printChar(c);
    printString("\n\r");
    switch (c) {
    case 'r':
      GPIOF->DATA = (1 << 1);
      break;
    case 'b':
      GPIOF->DATA = (1 << 2);
      break;
    case 'g':
      GPIOF->DATA = (1 << 3);
      break;
    default:
      GPIOF->DATA &= ~((1 << 1) | (1 << 2) | (1 << 3));
      break;
    }
  }
}
char readChar(void) {
  char c;
  while ((UART0->FR & (1 << 4)) != 0)
    ;
  c = UART0->DR;
  return c;
}

void printChar(char c) {
  while ((UART0->FR & (1 << 5)) != 0)
    ;
  UART0->DR = c;
}

void printString(char *string) {
  while (*string) {
    printChar(*(string++));
  }
}
Marcus Müller
  • 88,280
  • 5
  • 131
  • 237
  • may I ask where your `readChar` and `printChar` code ide comes from? It's important here that `UART0->FR` be `volatile`, or else your C compiler can optimize the `while` loops away – or make them infinite. Generally, spinning on checking a status flag is not how you code for modern microcontrollers. There's usually an interrupt-based method that would be more appropriate here. – Marcus Müller Feb 25 '22 at 15:49
  • 1
    Have you paused the code while it is checking `UART0->FR` to see what value is in this register? Is bit 5 high? If so then there is nothing wrong with the execution of your loop, so the problem is elsewhere in your code. – Elliot Alderson Feb 25 '22 at 15:52
  • @ElliotAlderson, yeah I did. It is zero. But I will explain what I did, I put the address UART0_base + offset, i.e 0x4000C018, and the value of 5th bit was 0. – Ashutosh Singh Feb 26 '22 at 04:53
  • @MarcusMüller, I am sorry, i am new and dont know how to use interrupt based method to check for the end of string file. I was following a tutorial. – Ashutosh Singh Feb 26 '22 at 04:54
  • maybe find a different tutorial. You're using the CMSIS hardware abstraction layer, and it would surprise me if TI didn't implement functionality that should give you both a function for "send these N bytes from this buffer using DMA and don't pester the CPU about it " as well as "send this byte and call me when you're done". I guess you'd want to use the first method. But Elliot is right, if the thing never comes back done, then you might have set up something incorrectly. – Marcus Müller Feb 26 '22 at 10:02

0 Answers0