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: problem with convertion to dec to hexa

Other Parts Discussed in Thread: TM4C123GH6PM

Tool/software: Code Composer Studio

i need help in this program

i have to show on display the value but hexa

how to do this?

this is my code

#include <stdint.h>
#include <stdbool.h>
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/adc.h"
#include "driverlib/debug.h"


uint8_t milu;
uint8_t uni;
uint8_t dec;
uint8_t cent;
uint8_t catodo;
uint32_t ui32ADC0Value[1];
volatile uint32_t pot;
int main(void)
{
    uint32_t ui32Period;
    SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);                                       // Enable Timer 0
    TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);                                    // Configure Timer 0 for periodic signal
    ui32Period = (SysCtlClockGet() / 500);                                           // Calcular frecuencia en base a la frecuencia de tiempo del sistema y se divide entre 2 para asegurar una interrupcion cada mitad de ciclo
    TimerLoadSet(TIMER0_BASE, TIMER_A, ui32Period -1);                                  // se ingresa el valor de la frecuencia al timer A
    IntEnable(INT_TIMER0A);                                                             // Se habilita la interrupcion del timer 0A
    TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);                                    // Se configura la interrupcion del timer
    IntMasterEnable();                                                                  //Se habilitann las interrupciones
    TimerEnable(TIMER0_BASE, TIMER_A);                                                  // Se habilita el timer 0 con base de tiempo
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    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);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
    GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
    GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
    ADCSequenceEnable(ADC0_BASE, 3);
    while(1)
    {
        ADCIntClear(ADC0_BASE, 3);
        ADCProcessorTrigger(ADC0_BASE, 3);

        while(!ADCIntStatus(ADC0_BASE, 3, false))
          {
          }


          ADCSequenceDataGet(ADC0_BASE, 3, ui32ADC0Value); // Watch on ui32ADC0Value
          pot=ui32ADC0Value[0];

          milu=pot/1000;
          cent=(pot-(milu*1000))/100;
          dec=(pot-((milu*1000)+(cent*100)))/10;
          uni=pot%10;




        if (catodo>3){catodo=0;
        }

        switch(catodo)
        {
        case 0: visual(uni);
                GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,0x80);
                break;
        case 1: visual(dec);
                GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,0x40);
                break;
        case 2: visual(cent);
                GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,0x20);
                break;
        case 3: visual(milu);
                GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,0x10);
                break;
        }
    }
}
void Timer0IntHandler(void)
{
    TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);                         // Clear the timer interrupt
    catodo=catodo+1;
}

void visual(int digito)
{
    switch (digito)
            {
            case 0:
                GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,126);
                break;
            case 1:
                GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,12);

                break;

            case 2:
                GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,182);

                break;

            case 3:
                GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,158);
                break;

            case 4:
                GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,204);

                break;

            case 5:
                GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,218);


                break;

            case 6:
                GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,250);


                break;

            case 7:
                GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,14);


                break;

            case 8:
                GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,254);
                break;

            case 9:
                GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,206);


                break;
            case 10:
                            GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,238);


                            break;
            case 11:
                            GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,248);


                            break;
            case 12:
                            GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,114);


                            break;
            case 13:
                            GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,188);


                            break;
            case 14:
                            GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,242);


                            break;
            case 15:
                            GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,226);


                            break;
            default:
                GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,0xFF);


                break;
            }
    }

  • Your question is unclear to me. What do you have and how do you want to display it?

    Remember, the computer stores things in binary. So it is already in hex:

    uint8_t a = 11; // a now contains 0b00001011 or 0x0b
  • this program show the value of adc, but in decimal in my display, i need to convert this value and show in hex form
  • There is nothing to convert. pot is already in binary. just break it up into hex digits (each 4 bits) and display them.
  • Hi,

    >>but in decimal in my display,
    Judging by your code and this assertive, it seems you are writing to a 7 segment display, aren't you? Given you are already separating each digit inside your while loop, the same logic applies to hex - hint: the variable "pot" already contains the representation in powers of two, as Keith mentioned above.

    This is a coding question and not a CCS or TM4C question.

    --Cheers
  • sorry i am new in this forum, i can fix the problem

    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/tm4c123gh6pm.h"
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/gpio.h"
    #include "driverlib/timer.h"
    #include "driverlib/adc.h"
    #include "driverlib/debug.h"

    volatile uint32_t quotient;
    long remainder;
    int i=1;
    int j=0;
    char hexadecimalnum[100];
    int hexa;
    uint8_t milu;
    uint8_t uni;
    uint8_t dec;
    uint8_t cent;
    uint8_t catodo;
    uint32_t ui32ADC0Value[1];
    volatile uint32_t pot;
    int main(void)
    {
        uint32_t ui32Period;
        SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);                                       // Enable Timer 0
        TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);                                    // Configure Timer 0 for periodic signal
        ui32Period = (SysCtlClockGet() / 500);                                           // Calcular frecuencia en base a la frecuencia de tiempo del sistema y se divide entre 2 para asegurar una interrupcion cada mitad de ciclo
        TimerLoadSet(TIMER0_BASE, TIMER_A, ui32Period -1);                                  // se ingresa el valor de la frecuencia al timer A
        IntEnable(INT_TIMER0A);                                                             // Se habilita la interrupcion del timer 0A
        TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);                                    // Se configura la interrupcion del timer
        IntMasterEnable();                                                                  //Se habilitann las interrupciones
        TimerEnable(TIMER0_BASE, TIMER_A);                                                  // Se habilita el timer 0 con base de tiempo
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
        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);

        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
        GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
        GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
        ADCSequenceEnable(ADC0_BASE, 3);
        while(1)
        {
            ADCIntClear(ADC0_BASE, 3);
            ADCProcessorTrigger(ADC0_BASE, 3);

            while(!ADCIntStatus(ADC0_BASE, 3, false))
              {
              }


              ADCSequenceDataGet(ADC0_BASE, 3, ui32ADC0Value); // Watch on ui32ADC0Value
              pot=ui32ADC0Value[0];



              cent=pot/0x100;
              dec=pot/0x10;
              uni=pot/0x01;




            if (catodo>3){catodo=0;
            }

            switch(catodo)
            {
            case 0: visual(uni);
                    GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,0x80);
                    break;
            case 1: visual(dec);
                    GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,0x40);
                    break;
            case 2: visual(cent);
                    GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,0x20);
                    break;
            case 3: visual(milu);
                    GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,0x10);
                    break;
            }
        }
    }
    void Timer0IntHandler(void)
    {
        TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);                         // Clear the timer interrupt
        catodo=catodo+1;
    }

    void visual(int digito)
    {
        switch (digito)
                {
                case 0x00:
                    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,126);
                    break;
                case 0x01:
                    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,12);

                    break;

                case 0x02:
                    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,182);

                    break;

                case 0x03:
                    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,158);
                    break;

                case 0x04:
                    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,204);

                    break;

                case 0x05:
                    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,218);


                    break;

                case 0x06:
                    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,250);


                    break;

                case 0x07:
                    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,14);


                    break;

                case 0x08:
                    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,254);
                    break;

                case 0x09:
                    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,206);


                    break;
                case 0x0A:
                                GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,238);


                                break;
                case 0x0B:
                                GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,248);


                                break;
                case 0x0C:
                                GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,114);


                                break;
                case 0x0D:
                                GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,188);


                                break;
                case 0x0E:
                                GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,242);


                                break;
                case 0x0F:
                                GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,226);


                                break;
                default:
                    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,0xFF);


                    break;
                }
        }

    sorry for the inconvenience, i am new in tm4c123 and code composer 

    :D
  • sorry this is my solution

    milu=pot/4096;
    cent=(pot-(milu*4096))/256;
    dec=(pot-((milu*4096)+(cent*256)))/16;
    uni=pot%10;


    but i have a question, in my display y can not see the units why?
  • For hex all you need are shifts and masks

    units = pot & 0xF;
    cent = (pot>>4) & 0xF;

    ...