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.

TM4C123GH6PM:how can i use usb_dev_serial sample code to replace UART

Part Number: TM4C123GH6PM
Other Parts Discussed in Thread: EK-TM4C123GXL

HI,I want to use the usb_dev_serial sample code to replace uart on my adc code,i hope the result can printf on the tera term,this is my code

#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_uart.h"
#include "inc/hw_sysctl.h"
#include "driverlib/adc.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "driverlib/usb.h"
#include "driverlib/rom.h"
#include "usblib/usblib.h"
#include "usblib/usbcdc.h"
#include "usblib/usb-ids.h"
#include "usblib/device/usbdevice.h"
#include "usblib/device/usbdcdc.h"
#include "utils/uartstdio.h"
#include "utils/ustdlib.h"
#include "usb_serial_structs.h"
#define resolution (3.3/4096)
#define countmax 1000 //sampling times



void
InitConsole(void)
{

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);


    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);


    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);


    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);

    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);


    UARTStdioConfig(0, 115200, 16000000);
}

int
main(void)
{



    uint32_t pui32ADC0Value[1];
//    char p[3];
    uint32_t MAX[1]={0};
    uint32_t MIN[1]={4095};
    uint32_t AVG[1]={0};
    uint32_t data;
    float vol;
    char volt_Display[4];
    uint32_t data1;
    float vol1;
    char volt_Display1[4];
    uint32_t data2;
    float vol2;
    char volt_Display2[4];
    uint32_t sum[1];
    uint32_t count=0;
    uint32_t MAX2[1]={0};
    uint32_t MIN2[1]={4095};
    uint32_t data3;
    float vol3;
    char volt_Display3[4];
    uint32_t data4;
    float vol4;
    char volt_Display4[4];
    SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_16MHZ);


    InitConsole();


    UARTprintf("ADC ->\n");
    UARTprintf("  Type: Single Ended\n");
    UARTprintf("  Samples: One\n");
    UARTprintf("  Update Rate: 0.5s\n");
    UARTprintf("  Input Pin: AIN0/PE3\n\n");

    //
    // The ADC0 peripheral must be enabled for use.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);


    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);


    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3);


    ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);


    ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH0 | ADC_CTL_IE |
                             ADC_CTL_END);


    //
    // Since sample sequence 3 is now configured, it must be enabled.
    //
    ADCSequenceEnable(ADC0_BASE, 3);

    //
    // Clear the interrupt status flag.  This is done to make sure the
    // interrupt flag is cleared before we sample.
    //
    ADCIntClear(ADC0_BASE, 3);


    // Sample AIN0 forever.  Display the value on the console.
    //
    while(1)
    {
        //
        // Trigger the ADC conversion.


        for (count = 0; count < countmax; count++)
        {
           MAX2[0]=0;//when  max2[] get the maximun  value everytime, return to 0
           MIN2[0]=4095;//when  min2[] get the minimun  value everytime, return to 0
          ADCIntClear(ADC0_BASE, 3);
          ADCProcessorTrigger(ADC0_BASE, 3);
          while(!ADCIntStatus(ADC0_BASE, 3, false));
          ADCSequenceDataGet(ADC0_BASE, 3, pui32ADC0Value);

          sum[0] += pui32ADC0Value[0];
          if(pui32ADC0Value[0]>MAX2[0])

           {
             MAX2[0] = pui32ADC0Value[0];
           }
         if(pui32ADC0Value[0]<MIN2[0])
           {
             MIN2[0] = pui32ADC0Value[0];
           }

        }

        sum[0] /= countmax;
        AVG[0] = sum[0];
//          sum[0] /= countmax;
//          AVG[0] = sum[0];
         vol = AVG[0] * (resolution) * 10000; //UARTprintf can not ptintf %f
         data = vol;
         volt_Display[0] = (data / 10000);
         volt_Display[1] = ((data % 10000) / 1000);
         volt_Display[2] = ((data % 1000) / 100);
         volt_Display[3] = ((data % 100) / 10);



//         UARTprintf("digital values = %d\r",pui32ADC0Value[0]);
//         UARTprintf("VOLT = %d.%d%dV\r",volt_Display[0],volt_Display[1],volt_Display[2]);

//       UARTprintf("VOLT = %d.%d%dV\r",p[0],p[1],p[2]);
         if(AVG[0]>MAX[0])

          {
            MAX[0] = AVG[0];
          }
         if(AVG[0]<MIN[0])
          {
            MIN[0] = AVG[0];
          }

        vol1 = MAX[0]*(resolution)*10000;
        data1 = vol1;
        volt_Display1[0] = (data1/10000);
        volt_Display1[1] = ((data1%10000)/1000);
        volt_Display1[2] = ((data1%1000)/100);
        volt_Display1[3] = ((data1%100)/10);
        vol2 = MIN[0]*(resolution)*10000;
        data2 = vol2;
        volt_Display2[0] = (data2/10000);
        volt_Display2[1] = ((data2%10000)/1000);
        volt_Display2[2] = ((data2%1000)/100);
        volt_Display2[3] = ((data2%100)/10);
        vol3 = MAX2[0]*(resolution)*10000;
        data3 = vol3;
        volt_Display3[0] = (data3/10000);
        volt_Display3[1] = ((data3%10000)/1000);
        volt_Display3[2] = ((data3%1000)/100);
        volt_Display3[3] = ((data3%100)/10);
        vol4 = MIN2[0]*(resolution)*10000;
        data4 = vol4;
        volt_Display4[0] = (data4/10000);
        volt_Display4[1] = ((data4%10000)/1000);
        volt_Display4[2] = ((data4%1000)/100);
        volt_Display4[3] = ((data4%100)/10);
//        UARTprintf("VMAX = %d.%d%dV\r",volt_Display1[0],volt_Display1[1],volt_Display1[2]);
//        UARTprintf("VMIN = %d.%d%dV\r",volt_Display2[0],volt_Display2[1],volt_Display2[2]);
        UARTprintf("digital values = %d----VAVG = %d.%d%d%dV---VMAX = %d.%d%d%dV---VMIN = %d.%d%d%dV---VMAX1000= %d.%d%d%dV---VIN1000= %d.%d%d%dV\r"
                ,pui32ADC0Value[0],volt_Display[0],volt_Display[1],volt_Display[2],volt_Display[3]
                ,volt_Display1[0],volt_Display1[1],volt_Display1[2],volt_Display1[3]
                ,volt_Display2[0],volt_Display2[1],volt_Display2[2],volt_Display2[3]
                ,volt_Display3[0],volt_Display3[1],volt_Display3[2],volt_Display3[3]
                ,volt_Display4[0],volt_Display4[1],volt_Display4[2],volt_Display4[3]);





        SysCtlDelay(SysCtlClockGet()/ 12);

    }


}

  • Hi,

      You can find the example in C:\ti\TivaWare_C_Series-2.2.0.295\examples\boards\ek-tm4c123gxl\usb_dev_serial. I will suggest you run the example as is. This example turns the MCU into a virtual serial port when connected to the USB host system. I will suggest you first run the example on a LaunchPad to get a feel for how it works. Once the example is running, the USB Device on the LaunchPad will enumerate as a serial port. Note there are two USB ports on the LaunchPad, one on the top side is the USB debug port and the other on the left side is the USB Device port. Anything you type on a terminal that is connected to USB Device port will be echoed back to the terminal. Once the example is working, you can modify the code to output your ADC output to the terminal.