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.

Compiler/MSP430G2553: Convert a decimal to hex number and use the hex number to set output pins

Part Number: MSP430G2553

Tool/software: TI C/C++ Compiler

I am taking an integer like 32, converting it to a hex number, and concatenating it with 0X to produce a char X= 0X1F. I then use X to set output pins on P1OUT. 
The below code does what I just explained. When I run this, I get the following error: a value of type "char *" cannot be assigned to an entity of type "unsigned char". 

Is there any better way to concatenate 0X with a Hex number to set P1.0?

char h[17] = "0123456789ABCDEF";
    static uint8_t num = 32; //decimal number to be converted to hex
    char hex[32];
    static uint8_t Quo; // Quotient
    static uint8_t Rem; //Remainder
    char *string = "0X";
    char *X;
    hex[0] = '\0';
    while (num !=0)
    {
    Quo = num/16;
    Rem = num % 16;
    memmove (&hex[1], hex, strlen(hex)+1);
    hex[0] = h[Rem];
    num = Quo;
        //concatinate 
    output= malloc(strlen(string)+strlen(hex)+1);
    strcpy(X, string );
    strcat(X, hex);
    P1OUT = output ;
    }

  • The variable output is not declared.  So I added a declaration "char *output;".

    Add --verbose_diagnostics to the compiler build options, and you see something similar to ...

    "file.c", line 29: warning: a value of type "char *" cannot be assigned to an
              entity of type "unsigned char"
          P1OUT = output ;
                ^

    Build with the option --gen_acp_raw.  This creates a compiler listing file.  It is in a file of the same name as the source file, with the extension changed to .rl.  Inspect that file and search for P1OUT.  You will discover the type of P1OUT is ...

    extern volatile unsigned char P1OUT;

    Thus you cannot write an entire string to P1OUT, only one character.  That's why you get the error message.

    I don't know the details of the HW underlying P1OUT.  

    Changing topics ... Creating a string representation of an integer value is typically done by calling the standard RTS function snprintf.  This function is a bit large.  In your case, you can reduce that code size a good amount by using the build option --printf_support=minimal.

    All of the compiler options mentioned in this post are described in the MSP430 compiler manual.

    Thanks and regards,

    -George

  • Sorry, that was my mistake. I meant to type P1OUT = X, where X is defined as a char right now.

    Thank you! I see where the problem is. I get that P1OUT must have a volatile unsigned char type written to it. I don't understand though how to concatenate 0X with a char hex [4] = 1F to get volatile unsigned char = 0X1F. Can you help me with that please.
  • Sahar Elyahoodayan said:
    I don't understand though how to concatenate 0X with a char hex [4] = 1F to get volatile unsigned char = 0X1F.

    Something similar to this would work ...

    #include <stdio.h>
    ...
       #define BUFLEN 10
       char buffer[BUFLEN];
       snprintf(buffer, BUFLEN, "0X%x", num);

    Thanks and regards,

    -George

  • Why are you trying to convert the integer to hex format? Why can't you just assign P1OUT = num?
  • Thank you George. Very helpful!
  • Archaeologist, I am generating a 5 bit random number which I then convert to a hex format to turn on P1.0-P1.4 at random.
  • There is no difference in the behavior of the following two statements:

    P1OUT = 32;
    P1OUT = 0x20;
    
  • That is great, I did not know that. Thank you! So I can just take the 5 bit random number, convert it to a decimal number and use it.
  • One more Question: So if I want to set P1OUT to a random decimal number (random) which changes on every clock, what format should random have? I have tried

    volatile uint8_t random;
    P1OUT = random;

     but the value of P1OUT is not updated at every clock, rather P1.0-P1.4 stay at a high DC voltage.

    Thank you,

  • You need to generate a random number.  Use the function rand() and call it from inside a loop.