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.

Converted ADC results to be sent to UART

Other Parts Discussed in Thread: EK-TM4C1294XL

Hi I am doing a project where the converted ADC will be send to UARTBufferFlag where it checks the status of data received and it will be sent out to PuTTY for testing. But I do not know how to convert from binary to ASCII or hex format, help please? 

This is my main.c

-----------------------

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"
#include "driverlib/uart.h"
#include "driverlib/interrupt.h"
#include "utils/ustdlib.h"
#include "driverlib/timer.h"
#include "UART.h"
#include "ADC.h"
#include "Timer.h"

void delay(int count){
int i, j;
for (i=0;i<count;i++)
{ for (j=0;j<1000;j++);}
}

void UARTIntHandler(void)
{
uint32_t ui32Status;
ui32Status = UARTIntStatus(UART1_BASE, true); //get interrupt status
UARTIntClear(UART1_BASE, ui32Status); //clear the asserted interrupts

while(UARTCharsAvail(UART1_BASE)) //loop while there are chars
{
UARTCharPutNonBlocking(UART1_BASE, UARTCharGetNonBlocking(UART1_BASE)); //echo character
SysCtlDelay(SysCtlClockGet() / (1000 * 3)); //delay ~1 msec
}
}

int UARTBufferFlag = 0;
int main(void) {

SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

Timer_Init();
Timer0IntHandler();
ADC_Init();
UART_Init();

while (1)
{
if (UARTBufferFlag){
send_uart();
UARTBufferFlag= 0;
}
delay(100);
}

}

------------------------------- // end 

This is my UART.c 

------------------------

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "inc/hw_ints.h"
#include "driverlib/interrupt.h"
#include "UART.h"
#include "ADC.h"

void UART_Init (void) // initialization of UART
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
GPIOPinConfigure(GPIO_PB0_U1RX);
GPIOPinConfigure(GPIO_PB1_U1TX);
GPIOPinTypeUART(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);

UARTConfigSetExpClk(UART1_BASE, SysCtlClockGet(), 115200,
(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));
}

void send_uart (void)
{
UARTCharPut(UART1_BASE, ui32ADC0Avg);
UARTCharPut(UART1_BASE, ui32ADC0Avg);
UARTCharPut(UART1_BASE, ui32ADC0Avg);
UARTCharPut(UART1_BASE, ui32ADC0Avg);
}

--------------------------------------------------------------- //end 

  • Hello Domenic,

    You can use the uartstdio.c/.h to call in UARTprintf which will manage the conversion. Examples for the same can be found in TivaWare examples.

    Regards
    Amit
  • Thank you Amit.
  • Hello Domenic,

    Let the forum know how the reuse of the library goes in getting the output. Note that you are using UART1 which is not the ICDI COM Port. So you would need some sort of RS232 converter cable (Serial to USB Converter) to interface to a PC.

    Regards
    Amit
  • Hi Amit,

    I was using UART1 because I am using an external Bluetooth development kit to transmit my data from TIVA launchpad to my PC. I have already made necessary changes to UART0 to test my program. However, could you help me check if I have any errors in my coding because I could not get anything out on TeraTerm.

    I have another thread on the issue of testing my ADC :   

  • Hi Domenic,
    Assume you have EK-TM4C1294XL launch pad and moved straps JP4/5 redirecting targets UART2 for use by ICDI?
    That also assumes the use of Booster pack 2 pins for the Bluetooth device.
  • Hi,

    Could you help me check if my UART.c if it is correct?

    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "driverlib/gpio.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/uart.h"
    #include "inc/hw_ints.h"
    #include "driverlib/interrupt.h"
    #include "utils/ustdlib.h"
    #include "uartstdio.h"
    #include "UART.h"
    #include "ADC.h"

    void UART_Init (void) // initialization of UART
    {
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200,
    (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));
    IntEnable(INT_UART0); //enable the UART interrupt
    UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT); //only enable RX and TX interrupts
    }

    void send_uart (void) // sends converted ADC results out through UART
    {
    UARTprintf("Hello");
    UARTprintf ("%d", ui32ADC0Avg);
    }

  • Hi Domenic - I'm not an expert like Amit (he's helping me out in my own thread) but there are a couple things you might try with your initialization. Disclaimer: I'm using the TM4C123gxl, so some things may be different.

    • You might want to insert a delay after you init the clocks to UART0 and PORTA to give them time to start. The smart way to do it is write a code to test the clock has started and only move on afterwards, but I just put in a SysCtlDelay(10), which should be more than enough.
    • The results of writing to the UART_CTL register can be unreliable unless you make certain the UART is disabled first. I write UARTDisable(UARTx_BASE) before I call the UARTConfigSetExpClk function. 
    • I'm not sure if you're using the FIFO? It isn't enabled, so your Tx and Rx FIFO are only one byte deep right now. Call UARTFIFOEnable if you want to use it.
    • There shouldn't be anything wrong with using GPIOPinTypeUART, but just to be safe I didn't use it. After calling GPIOPinConfigure I "manually" set the Tx pin to an Output and the Rx pin to an Input using GPIOPinTypeGPIOOutput/Input. 
    • Important! At the end of your UART_Init, you enable the TX and RX interrupts. To the best of my knowledge, the Tx interrupt fires when the Transmit FIFO is empty. So you might be getting stuck immediately in your transmit ISR. I disable the Tx interrupt and only enable it after I've written to the uart data register.
    • Did you link an isr function the uART0 interrupt? I don't see a UARTIntRegister function anywhere.

    Good luck. You can view my uart init code in this thread: https://e2e.ti.com/support/microcontrollers/tiva_arm/f/908/t/437965

  • Hello Zero_PD,

    Instead of using UARTConfigSetExpClk did you try replacing it with UARTStdioConfig

    Regards
    Amit
  • It seems I have to eat my words! I was having my own problems with my uart code (not getting into the rx interrupt) until I added the line:

    GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_4 | GPIO_PIN_5);

    Lesson learned, use that function!
  • Hi Zero_PD,

    Thank you for the kind advice anyway.

    All the best for your program!
  • Hello Zero_PD,

    But for UART0 why would Port C Pins be required?

    Regards
    Amit
  • Hi Amit - sorry for the confusion. I took that line from my own code where I am using UART1.