• Not Answered

hexadecimal to ASCII

I am doing an ADC12 conversion and sending this value to ADC12MEM0. Once the ADC12 is complete, I am sending the ADC12MEM0 values upper and lower 8 bits to the hyperterminal.  I understand that it is simply sending the hex values in the register over as ascii, but I would like to display these values as hex on the hyperterminal. I would like to copy the register exactly as it appears to the hyperterminal (0x0FFF on ADC12MEM0 would make hyperterminal display 0x0FFF... etc.). I have looked at hex to ascii tables and I understand how this works, but I have no idea how to write this as code in code composer studio. Is there some advice you could give me or an example code I can look at to better understand. Below I have provided my code. Thanks!

# include <msp430xG46x.h>

volatile unsigned int temp;
volatile unsigned int i;


void main(void)
{
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog
// Initialization of ADC12//

P6SEL |= 0x01; // Enable A/D channel A0
ADC12CTL0 &= ~ENC;
ADC12CTL0 = ADC12ON + SHT0_2 + REFON + REF2_5V+MSC; // turn on ADC12, set samp time
ADC12CTL1 = SHP+CONSEQ_2; // Use sampling timer
ADC12MCTL0 = SREF_1 + INCH_0; // Vr+=VeREF+ (external)
ADC12CTL0 |= ENC; // Enable conversions
// Initialization of Rs-232//
FLL_CTL0 |= XCAP14PF; // Configure load caps

do
{
IFG1 &= ~OFIFG; // Clear OSCFault flag
//_delay_cycles(50000); // Time for flag to set
}
while ((IFG1 & OFIFG)); // OSCFault flag still set?
P2SEL |= 0x30; // P2.4,5 = USCI_A0 RXD/TXD

UCA0CTL1 |= UCSSEL_1; // CLK = ACLK
UCA0BR0 = 0x03; // 32k/9600 - 13.65
UCA0BR1 = 0x00; //
UCA0MCTL = 0x06; // Modulation
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0TXIE + UCA0RXIE; // enable RXD and TXD interrupt;
while (1)
{
// ADC loop //
ADC12CTL0 |= ADC12SC; // Start conversions
//while (!(ADC12IFG & 0x0001)); // Conversion done?
temp = ADC12MEM0; // Move result
__no_operation(); // SET BREAKPOINT HERE
// RS-232 loop //
UCA0TXBUF = temp; // send lower part
__no_operation();
//while(!(IFG2 & UCA0TXIFG))
//{
//_delay_cycles(1000); // wait for first transmit
//}
UCA0TXBUF = temp >> 8;// send upper part
__no_operation();
// _delay_cycles(1000);
//UCA0TXBUF = temp;
}
}

22 Replies

  • printf( ... ); will do that (and a lot more than that).



  • Preston Lortie
    I am sending the ADC12MEM0 values upper and lower 8 bits to the hyperterminal.  I understand that it is simply sending the hex values in the register over as ascii

    No, you have a slight misunderstanding there!

    You are sending pure binary (or "hex") data, and hyperterminal is interpreting it as ASCII.

    That is, you send the hex value 0x0D, and hyperterminal thinks, "what character has ASCII code value 0x0D?", and displays that character - in this case, a Carriage Return (CR).

    0x0FFF on ADC12MEM0 would make hyperterminal display 0x0FFF

    Remember that the "0x" prefix is merely a 'C' notation to indicate that what follows is to be interpreted as a hexadecimal number - the "0x" does not exist as part of the ADC value! Therefore, if you want to see "0x" on hyperterminal, your code will have to generate those two characters.

    I have looked at hex to ascii tables and I understand how this works, but I have no idea how to write this as code in code composer studio

    'C' code is 'C' code - irrespective of what tool you use to write it!

    Is there some advice you could give me

    Write out all 16 possible values of a hex digit, 0..F, in a column.
    Then write out the ASCII codes of the 16 characters '0' to 'F' in another column alongside.
    Then look at the two columns, and see what pattern you can see between the hex digit values and the ASCII codes...

    Hint: there are actually two patterns: one for 0..9, and another for A..F

    A great deal of computer programming is about spotting patterns - so this is a very good learning exercise!

     

  • In reply to old_cow_yellow:

    old_cow_yellow
    printf( ... ); will do that

    Indeed it will!

    As will  sprintf(), and a number of other functions from the standard 'C' library...

    However, the ability to represent a hex digit value as a "displayable" character is so fundamental that it is probably worth the OP learning (and understanding) now how to do it.

     (and a lot more than that).

    Indeed. But therein lies a potential problem for small embedded systems: because printf() does do such a lot more, it can significantly "bloat" the size of your code...

  • In reply to Andy Neil:

    Andy Neil

     quote(and a lot more than that)./quote

    Indeed. But therein lies a potential problem for small embedded systems: because printf() does do such a lot more, it can significantly "bloat" the size of your code...

     HI Andy, you never was in the nerd status?

     So why not answer in an helpful manner?

     Why bother with an hidden hint and not show how hex and ascii tables are related to?

     Why also at OCW answer you replyed in a so bad look? On 46x that is plenty of flash I also use (s)printf also if I have my library to sending out hex on a one line based code.

     Sometimes guru level has to be settled down to deal with person too and understand what they want. This forum as I see is not a high level one specialized but an intermix of beginner and advanced so I hope some hint to TI are to be send to focus on what level question are to be answered.

     

  • In reply to Roberto Romano:

    Roberto Romano
    So why not answer in an helpful manner?

    There is an old saying:

    Give a man a fish, and you have fed him for one meal;

    Teach a man to fish, and you have fed him for life.

    I think that it is far more "helpful" to get the OP to think for himself than to just give him the answer "on a plate".

    Roberto Romano
    at OCW answer you replyed in a so bad look?

    Why do you think it's a "bad look"?

    If I had a dollar for every forum post saying, "My code is so simple (sic), but overflows the memory space - what could be wrong?"  - where the answer is the use of printf() - then I would be very rich indeed!

  • In reply to Andy Neil:

    In this case, the set of values to be converted is so small (only 16), that it can easily & economically be done with just a lookup table...

  • In reply to Andy Neil:

    Andy sorry but you forgot the more important part: we have to build something better, so TI probably has to think split forum to beginner/student/hobbyst/professional, this way we prepare teaching material about and we can concentrate on real colleague problem. In my point of view some library can be loaded to forum and left here with instruction on how to use, yes I agree with you our work cannot be wasted away, I decided to dedicate some free time to this formum and I do when I can.

    example:

    char bin2hex(int a)

    {

    a&=0xf;

    if(a>9)

    return('A'-1+a);

    return('0'+a);

    }

    or lookup table (tricky)

    declaration

    char BIN2HEX[]="0123456789ABCDEF";

    usage

     hexchar=BIN2HEX[a&0xf];

     I am sure these codes alone create troubles to our friend, why not release with some explanations? I agree tons of example exist but why not collect here to MSP area?

     Hint about?

  • Thanks for the replies guys, but honestly you haven't answered my question. As I have said I understand that the values being sent through serial are hex values, but are being treated as ASCII characters (and I understand the table of hex to ASCII). I am having my problem with converting these hex values to ASCII, since my program will repeatedly do an ADC conversion and send this to the hyperterminal. So my question is how do I CONTINUALLY convert the hex value to ASCII characters that will reflect the ADC12MEM0 register (and yes i understand I must put a 0x0030 and 0x0078 to get the "0x" prefix I want). My problem is mainly coding, since I know there is a way to keep reading and converting these values to ASCII without going through all 4095 different possibilities.

  • In reply to Preston Lortie:

    Preston Lortie
    my question is how do I CONTINUALLY convert the hex value to ASCII

    If you know how to do one hex value, then doing it "continually" is just a matter of doing the same thing each time you get a new value!

    ie, in pseudo-code:

    repeat:
       read a value;
       convert it to ASCII text;
       send the ASCII text

    Preston Lortie
    converting these values to ASCII without going through all 4095 different possibilities.

    You convert each digit in turn, to build the full string.

    As already noted, printf() will do this for you (though best not called in an ISR).

  • In reply to Preston Lortie:

    Preston Lortie

    My problem is mainly coding, since I know there is a way to keep reading and converting these values to ASCII without going through all 4095 different possibilities.

     Hi Preston, sorry to see Andy has full reason in some writing, what do you think to have a correct transfer of byte values without a supporting protocol still ascii or binary? Your code transmitted bynary codes and hyperterminal interpret it, you can store them but I think you lose some codes due to ascii control codes interpreteation. Hyperterminal is not the best program to do this and your coding style still is wrong due impossibility of select where byte start and where finish. A better approach is to use 7 bit encoding and define a start symbol grouping some bytes on a frame.

     Again what do you want to do? Hyperterminal is not good to dump binary, choose a better program or write your code.

     Transmit from MSP as HEX then add a cr lf comma or tab to separate one word from the other.