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 with sprint() function

Part Number: TM4C123GH6PM


Tool/software: Code Composer Studio

Good morning,

I need help with the sprint() function, I am using the ADC from the tiva c microcontroller and the value es saved in an integer variable ("dato"). Then I tried to show that value using the UART port, but the program  always get stuck in the sprintf() function, I tried everything, but I still have the same problem. I would appreciate so much if someone can help me to solve the problem.

My program:

#include "tm4c123gh6pm.h"
#include <stdint.h>
#include <stdio.h>

void Config_UART(void){
unsigned long temp;
uint16_t i = 0;

SYSCTL_RCGC1_R |= 0x01; //Activates the clock signal module of UART0 (PA0 and PA1)
while(!(SYSCTL_PRUART_R & 0x01)); //Wait until the clock of the UART module is activated
SYSCTL_RCGC2_R |= 0x01; //Activates the clock signal module of Port A.
temp =SYSCTL_RCGC1_R; //Wait for 2 clock cycles.

GPIO_PORTA_AMSEL_R &= ~0x03; //Deactivates the analogical mode of PA0 and PA1.
GPIO_PORTA_DEN_R |= 0x03; //Enable pins A0 and A1 for digital signals
GPIO_PORTA_AFSEL_R |= 0x03; //Activate the alternative functions.
GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R & 0xFFFFFF00)|0x00000011; //Connect the pins A0 A1 to the UART module.

UART0_CTL_R &=~0x01; //Disable the UART module.
UART0_IBRD_R = (UART0_IBRD_R & 0xFFFF0000)|104; //Set the speed of the data (Integer).
UART0_FBRD_R = (UART0_FBRD_R & 0xFFFFFFC0)|11; //Set the speed of the data (Fraction).
UART0_LCRH_R = (UART0_LCRH_R & 0xFFFFFF00)|0x76; //Configure the framing, parity and the buffers.
UART0_CTL_R |= 0x01; //Enable the UART module.

for(i=0;i<10000;i++); //Delay for first use.

}

void Transmite_Byte(unsigned char datoTx){


while(UART0_FR_R & UART_FR_TXFF); //Wait until the buffer is free.
UART0_DR_R = datoTx;
}

void Transmite_Cadena(const char Cadena[]){

int i = 0;

while(Cadena[i] != '\0'){

Transmite_Byte(Cadena[i]);
i++;
}
}

void Config_ADC(void){


SYSCTL_RCGC2_R |= 0x10; //PORT E
SYSCTL_RCGC0_R |= 0x10000; //Activates signal clock of ADC0
SYSCTL_RCGC0_R &= ~0x300; //Sample speed 125 k samples/second

//Pin PE1
GPIO_PORTE_DIR_R &=~0x02;
GPIO_PORTE_AFSEL_R |= 0x02;
GPIO_PORTE_AMSEL_R |= 0x02;
GPIO_PORTE_DEN_R &= ~0x02;

ADC0_SSPRI_R = 0x0123; //Sequencer priority (ss3 the highest)
ADC0_ACTSS_R &= ~0x08;
ADC0_EMUX_R &= ~0xF000;
ADC0_SSMUX3_R &= ~0x0F;
ADC0_SSMUX3_R |= 0x02; // AIN 2
ADC0_SSCTL3_R |= 0x06;
ADC0_ACTSS_R |= 0x08;

}

uint16_t CaptureADC(void){


unsigned int result = 0;
ADC0_PSSI_R |= 0x8;
while(!(ADC0_RIS_R & ADC_RIS_INR3));
result = ADC0_SSFIFO3_R & 0xFFF;
ADC0_ISC_R |= 0x8;
return result;


}


/**
* main.c
*/


int main(void){


int data = 0;       

char msg[20];

Config_UART();
Config_ADC();
Transmite_Byte(12);

while(1){
data = CaptureADC();    //The data has 12 bits (works fine)

sprintf(msg,"%d",data);  //THE PROBLEM IS HERE (It gets stuck inside this function, like an infinite loop)

Transmite_Cadena(msg);

Transmite_Byte(10);

Transmite_Byte(13);

}

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

It gets stuck in this part of the sprintf() function