• Join
  • Sign In with my.TI Login
Texas Instruments
  • Products
  • Applications
  • Tools & Software
  • Support & Community
  • Sample & Buy
  • About TI
Sample & Purchase Cart Sample & Purchase Cart
  • Search
  • Advanced
TI E2E™ Community
  • Support Forums
  • Blogs
  • Groups
  • Videos
  • 简体中文
  • More ...
TI Home » TI E2E Community » Support Forums » Microcontrollers » MSP430™ Microcontrollers » MSP430 Ultra-Low Power 16-bit Microcontroller Forum » hexadecimal to ASCII
Share
MSP430™ Microcontrollers
  • Forum
  • Announcements
  • E2E Wiki
Options
  • Subscribe via RSS
MSP430 Resources
  • MSP430 Product Folder
  • MSP-EXP430G2 - MSP430 LaunchPad Value Line Development kit
  • MSP430 Getting Started Guide
  • MSP430 Microcontroller Projects
  • More Resources >
  • Forums

    hexadecimal to ASCII

    This question is not answered
    Preston Lortie
    Posted by Preston Lortie
    on Apr 06 2012 17:38 PM
    Prodigy80 points

    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;
    }
    }

    ADC12 ascii hexadecimal
    Report Abuse
    • Reply
    You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    All Replies
    • old_cow_yellow
      Posted by old_cow_yellow
      on Apr 07 2012 01:23 AM
      Guru25715 points

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



      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Andy Neil
      Posted by Andy Neil
      on Apr 07 2012 03:35 AM
      Guru31975 points

      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!

       

      ascii patterns binary to ASCII hex to ASCII
      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Andy Neil
      Posted by Andy Neil
      on Apr 07 2012 03:44 AM
      Guru31975 points

      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...

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Roberto Romano
      Posted by Roberto Romano
      on Apr 07 2012 11:31 AM
      Mastermind6840 points

      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.

       

       Regards

       Roberto


       Please login & click    Verify Answer    if this post answered your question.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Andy Neil
      Posted by Andy Neil
      on Apr 07 2012 12:32 PM
      Guru31975 points

      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!

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Andy Neil
      Posted by Andy Neil
      on Apr 07 2012 12:36 PM
      Guru31975 points

      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...

      lookup table
      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Roberto Romano
      Posted by Roberto Romano
      on Apr 07 2012 14:22 PM
      Mastermind6840 points

      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?

       Regards

       Roberto


       Please login & click    Verify Answer    if this post answered your question.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Preston Lortie
      Posted by Preston Lortie
      on Apr 07 2012 14:26 PM
      Prodigy80 points

      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.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Andy Neil
      Posted by Andy Neil
      on Apr 07 2012 14:36 PM
      Guru31975 points

      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).

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Roberto Romano
      Posted by Roberto Romano
      on Apr 07 2012 14:36 PM
      Mastermind6840 points

      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.

       Regards

       Roberto


       Please login & click    Verify Answer    if this post answered your question.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Roberto Romano
      Posted by Roberto Romano
      on Apr 07 2012 14:47 PM
      Mastermind6840 points

      Preston Lortie

      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

       Hi Preston, here is where you and Andy are in a disagree point.

       I cannot say you have reason but this is your fault: values sent to serial are BINARY values and has nothing to do with decimal hex or octal values, they are binary and hiperterminal interpret as ascii codes.

       Value 65 decimal is interpreted as 0100 0001 in binary as 'A' in ascii value 65 as decimal 0x41 in hexadecimal and o101 in octal so if you wish to print it you have to send equivalent ascii stream to serial line.

       I know about this mistake by my student fault too.

       Andy, this can clarify what is about my idea?

       Regards

       Roberto


       Please login & click    Verify Answer    if this post answered your question.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Andy Neil
      Posted by Andy Neil
      on Apr 07 2012 15:12 PM
      Guru31975 points

      Roberto Romano
      Transmit from MSP as HEX

      No!

      You need to transmit ASCII-coded text strings!

      Hyperterminal is perfectly adequate to receive this.

       

      Roberto Romano
      add a cr lf comma or tab to separate one word from the other.

      Yes - good point!

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Roberto Romano
      Posted by Roberto Romano
      on Apr 07 2012 15:28 PM
      Mastermind6840 points

      Andy Neil

      Roberto Romano
      Transmit from MSP as HEX

      No!

      You need to transmit ASCII-coded text strings!

      Hyperterminal is perfectly adequate to receive this.

       Andy, sometimes you are more worst than a nerd ;)

       To all community my apologizes about HEX used instead as HEX ascii coded value representing binary value of variable, I was thinking transmitting as HEX signify transmitting a string coded from  {0..,a..f} char set from character encoding ascii or whichever is used to transmit data.

       Regards

       Roberto


       Please login & click    Verify Answer    if this post answered your question.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Andy Neil
      Posted by Andy Neil
      on Apr 07 2012 15:58 PM
      Guru31975 points

      The thing is, when it comes to computer programming, attention to details is absolutely key!!

      If one doesn't clearly understand the difference between sending a pure biary number, and sending its text representation - then one will come unstuck!

      However, I do realise that it can be hard to express such nuances when you're having to write in a foreign language. So, please, take such clarifications just as clarifications - not as criticisms!

       

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Preston Lortie
      Posted by Preston Lortie
      on Apr 08 2012 00:29 AM
      Prodigy80 points

      Alright I tried using the printf command and it would not print serially to PUTTY. I tried to do a much simpler example code using printf, and this would not work either. I am using CCS5.1. Just to be clear here is the simple printf code I am running that will not work.

      #include <stdio.h>
      void  main( void )
      {
          printf("Hello World! %i\n", 123 );
      }

      This does not output anything via serial. Is this the version of CCS I am using or do I have to do anything special to get the printf command to work?

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    12
    TI E2E™ Community
    • Support Forums
    • Blogs
    • Videos
    • Groups
    • Site Support & Feedback
    • Settings
    TI E2E™ Community Groups
    • TI University Program
    • Make the Switch
    • Microcontroller Projects
    • Motor Drive & Control
    Other Communities
    • Deyisupport
    • Designsomething.org
    • beagleboard.org
    • TI on Element 14
    • TI on TechXchangeSM
    Other Technical & Support Resources
    • WEBENCH® Design Center
    • Product Information Centers
    • Technical Documents
    • TI Design Network
    • TI Technical Articles
    • TI Training

    All content and materials on this site are provided "as is". TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with regard to these materials, including but not limited to all implied warranties and conditions of merchantability, fitness for a particular purpose, title and non-infringement of any third party intellectual property right. TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with respect to these materials. No license, either express or implied, by estoppel or otherwise, is granted by TI. Use of the information on this site may require a license from a third party, or a license from TI.

    Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms of Use of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms of Use of this site. TI, its suppliers and providers of content reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

    Follow Us Texas Instruments on Facebook Texas Instruments on Twitter Texas Instruments on LinkedIn Texas Instruments on Google+
    TI Worldwide | Contact Us | my.TI Login | Site Map | Corporate Citizenship | mobile m.ti.com (Mobile Version)

    TI is a global semiconductor design and manufacturing company. Innovate with 100,000+ analog ICs and
    embedded processors, along with software, tools and the industry’s largest sales/support staff.

    © Copyright 1995-2013 Texas Instruments Incorporated. All rights reserved.
    Trademarks | Privacy Policy | Terms of Use