So I watched a tutorial on setting up a UART connection where I could print things and see them in a putty secession however I cannot get the code to work correctly. It will compile and seems to initialize correctly when I check the memory during debug but it is getting stuck in the printChar function and I cannot figure what is wrong with it. Can someone explain were my error is either in concept or in implementation? Thanks in adance.
#include <stdlib.h>
#include <stdio.h>
#include "lm4f120h5qr.h"
char readChar(void);
void printChar(char c);
void printString(char* string);
/**
* main.c
*/
int main(void)
{
SYSCTL_RCGCGPIO_R |= (1<<5);
GPIO_PORTF_DIR_R = (1<<1) | (1<<2) | (1<<3);
GPIO_PORTF_DEN_R = (1<<1) | (1<<2) | (1<<3);
GPIO_PORTF_DATA_R &= ~((1<<1) | (1<<2) | (1<<3)); // INITIALLY TURN THE LEDS OFF
char c;
//1. Enable the UART module using the RCGCUART register (see page 344).
SYSCTL_RCGCUART_R |=(1<<0);
// 2. Enable the clock to the appropriate GPIO module via the RCGCGPIO register (see page 340).
SYSCTL_RCGCGPIO_R |= (1<<0);
// To find out which GPIO port to enable, refer to Table 23-5 on page 1351.
// 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.
GPIO_PORTA_AFSEL_R = (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).
GPIO_PORTA_PCTL_R = (1<<0) | (1<<1);
GPIO_PORTA_DEN_R = (1<<0) | (1<<1);
// UARTFBRD[DIVFRAC] = integer(0.8507 * 64 + 0.5) = 54
// With the BRD values in hand, the UART configuration is written to the module in the following order:
// 1. Disable the UART by clearing the UARTEN bit in the UARTCTL register.
UART0_CTL_R &= ~(1<<0);
// 2. Write the integer portion of the BRD to the UARTIBRD register.
UART0_IBRD_R = 104;
// 3. Write the fractional portion of the BRD to the UARTFBRD register.
UART0_FBRD_R = 11;
// 4. Write the desired serial parameters to the UARTLCRH register (in this case, a value of
// 0x0000.0060).
UART0_LCRH_R = (0x03<<5); //BIT 5 SETS THE DATA LENGT IN THIS CASE 8 BITS
// 5. Configure the UART clock source by writing to the UARTCC register.
UART0_CC_R = (0x0); // 0X0 IS THE SYSTEM CLOCK
// 6. Optionally, configure the µDMA channel (see “Micro Direct Memory Access (µDMA)” on page 585)
// and enable the DMA option(s) in the UARTDMACTL register.
// 7. Enable the UART by setting the UARTEN bit in the UARTCTL register.
UART0_CTL_R |= (1<<0) | (1<<8) | (1<<9); //BIT 0 ENABLES UART BIT 8 ENABLES RX AND BIT 9 ENABLES TX
int x = 5;
printf("%d", x);
//UARTprintf("please work corretly");
while(1){
printString("enter r g b: \n\r"); // prompt the user for an input
c = readChar();// read the users input
printChar(c); // print the users input back to them
printString("\n\r");// start a new line to keep things clean
switch (c){
case 'r': GPIO_PORTF_DATA_R = (1<<1); // READ VALUE OF R SETS THE RED LED TO ON
break;
case 'g': GPIO_PORTF_DATA_R = (1<<2);// READ VALUE OF G SETS THE LED TO GREEN
break;
case 'b': GPIO_PORTF_DATA_R = (1<<3);
break;
}
}
return 0;
}
char readChar(void){
char c ; // declare a character
while((UART0_FR_R & (1<<4)) != 0);// wait until the flag register tells us there is data to receive
c = UART0_DR_R; // read the data in and assign it to c
return c;
}
void printChar(char c){
while((UART0_FR_R & (1<<5)) != 0); //make sure the transmit line is empty
UART0_DR_R = c; // write the data assigned to c onto the bus
}
void printString(char* string){
while(*string){
printChar(*(string++));
}
}