This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/TM4C123GH6PM: Problem- Sending analog value via UART Serial Communication

Part Number: TM4C123GH6PM

Tool/software: Code Composer Studio

// hello_launchpad.c

#include "TM4C123.h"                    // Device header
#include <stdint.h>
#include <stdlib.h>


#define UART_FR_TXFF            0x00000020  // UART Transmit FIFO Full
#define UART_FR_RXFE            0x00000010  // UART Receive FIFO Empty
#define UART_LCRH_WLEN_8        0x00000060  // 8 bit word length
#define UART_LCRH_FEN           0x00000010  // UART Enable FIFOs
#define UART_CTL_UARTEN         0x201  // UART Enable


void GPIO_InIt(void);
void ADC1_InIt(void);
void ADC1Seq3_Handler(void);
void UART0_initialize_115200bps_16MHz(void);
unsigned char UART0_RxChar(void);
void UART0_TxChar(unsigned char data);
unsigned long UART_InUDec(void);


int Result; 
int a,b,c,d,e,f;

int main()	
	
{
     SYSCTL->RCGCGPIO |=0x10; // Enable clock for Port E
	   SYSCTL->RCGCADC  |=0x01; // Enable Clock for ADC Module 0 
     GPIO_InIt(); // Initialization of GPIO
     ADC1_InIt(); // Initialization of ADC
	   UART0_initialize_115200bps_16MHz(); // Initialization of UART
	   UART_InUDec(); // Conversion and ADC performed
}


void GPIO_InIt(void)
{   
   // PE3 int
	   GPIOE->AFSEL |=8;  // Enable alternative function
     GPIOE->DEN   &=~8; // Enable Digital Function
     GPIOE->AMSEL |=8; //  Enable Analog Function
}

void ADC1_InIt(void)
{   
	   ADC0->ACTSS &=~8; // Disable sample Sequencer 3(SS3)
	   ADC0->EMUX &=~0xF000; // Software Trigger Conversion
	   ADC0->SSMUX3=0; //  Get inout from channel 0
	   ADC0->SSCTL3 |=6; // Take one sample at a time set flat at 1st sample
	   ADC0->ACTSS|=8; //  Enable SS3
	   ADC0->PP|=1;
}

void ADC1Seq3_Handler(void)
{
      ADC0->PSSI|=8; //  Start Conversion at SS3
		  while((ADC0->RIS &8)==0){} // Wait for conversion complete
			Result =ADC0->SSFIFO3; //
			ADC0->ISC=8;
}

//------------UART_Init------------
// Initialize the UART for 115,200 baud rate (assuming 16 MHz bus clock),
// 8 bit word length, no parity bits, one stop bit, FIFOs enabled
// Input: none
// Output: none
void UART0_initialize_115200bps_16MHz(void){
	
	SYSCTL->RCGCUART |= 0x01;            // activate UART0
	SYSCTL->RCGCGPIO |= 0x01;            // activate port A
	UART0->CTL &= ~UART_CTL_UARTEN;      // disable UART
	

  UART0->IBRD= 8;                     // IBRD = int(16,000,000 / (16 * 115,200)) = int(8.680)
	UART0->FBRD= 44;                    // FBRD = round(0.5104 * 64 ) = 33
	UART0->LCRH=(UART_LCRH_WLEN_8|UART_LCRH_FEN);   // 8 bit word length (no parity bits, one stop bit, FIFOs) //0X70
	
	UART0->CTL |= UART_CTL_UARTEN;       // enable UART
	GPIOA->AFSEL |= 0x03;           // enable alt funct on PA1-0
	GPIOA->DEN |= 0x03;             // enable digital I/O on PA1-0
                                      // configure PA1-0 as UART
	GPIOA->PCTL= (GPIOA->PCTL&0xFFFFFF00)+0x00000011;
  GPIOA->AMSEL&= ~0x03;          // disable analog functionality on PA  
}
//------------UART_InChar------------
// Wait for new serial port input
// Input: none
// Output: ASCII code for key typed

   unsigned char UART0_RxChar(void){
   while((UART0->FR&UART_FR_RXFE)!= 0);
   return((unsigned char)(UART0->DR&0xFF));
}
//------------UART_OutChar------------
// Output 8-bit to serial port
// Input: letter is an 8-bit ASCII character to be transferred
// Output: none

   void UART0_TxChar(unsigned char data){
   while((UART0->FR&UART_FR_TXFF) != 0);
   UART0->DR = data;
}

// Accept ASCII input in unsigned decimal format, up to 4294967295
// if n>4294967295, it will turncate without format reporting the error 
   unsigned long UART_InUDec(void){
  // unsigned long n=0;    // this will be the return value
	// char character;      //  this is the input ASCII typed
   while(1){
	  
	 ADC1Seq3_Handler();
	// character=UART0_RxChar();  // accept input
	 a=Result/1000;
   b=Result%1000;
   c=b/100;
   d=b%100;
   e=d/10;
   f=d%10;
		 
 	 UART0_TxChar(a+48);   // out this character
	 UART0_TxChar(c+48);   // out this character
	 UART0_TxChar(e+48);   // out this character
	 UART0_TxChar(f+48);   // out this character
	 UART0_TxChar(32);     // out this character
		 
		 // if((character<'0')|| (character>'9')){ // check for non-number
			//  return n;    // quit if not a number
		// }
		// n= 10*n + (character-0x30);   // overflow if above 4294967295
	 }
 }