Circuit Negma

C++, C, VB.NET, PCB, Electronics, Circuit Design

How to Display Hex number on a LCD Screen

Posted by Circuit Negma on April 15, 2009


Created By: Hussein Nosair

Technorati Tags: ,,,,

The following routine will aid in displaying a hex value to LCD character display.

   1: /********************************************************************
   2: *       Function Name:  HextoASCII                                  *
   3: *       Return Value:   None                                        *
   4: *       Parameters:     unsigned hex number                         *
   5: *       Description:    This routine will display a hex number on   *
   6: *                       LCD by converting the number to ASCII       *
   7: ********************************************************************/
   8: void HextoASCII(unsigned char *hex)
   9: {
  10:     unsigned char temp_L;           // dummy variable that will hold LSBs of Hex number
  11:     unsigned char temp_H;           // dummy variable that will hold MSBs of Hex number
  12:     
  13:     temp_H = *hex & 0xF0;           // Obtain the upper 4 bits (MSBs) of hex number
  14:     temp_H = temp_H >> 4;           // 
  15:     if(temp_H >9)                   // Check if the number is a letter
  16:         temp_H += 0x37;             // Convert the number to a letter in ASCII
  17:     else
  18:         temp_H += 0x30;             // Convert the number to ASCII number
  19:  
  20:     LCDWriteData(temp_H);           // Display the number
  21:             
  22:     temp_L = *hex & 0x0F;           // Obtain the lower 4 bits (LSBs) of hex number
  23:     if(temp_L >9)                   // Check if the the number is a letter
  24:         temp_L += 0x37;             // Convert the number to a letter in ASCII
  25:     else
  26:         temp_L += 0x30;             // Convert the number to ASCII number
  27:                         
  28:     LCDWriteData(temp_L);           // Display the number
  29: }   

Ex:

Hex number = 0x3F

1. If try to send 3F to the LCD directly, the LCD will display ? on screen, due to the fact that the LCD uC works with ASCII characters and thus the equivalent character for 3F is ?. CHECK THIS VALUE WITH ASCII TABLE.

2. If try to convert the hex to string/ASCII using itoa(), then the LCD will display the equivalent decimal value of 3F = 63.

3. Using the  above routine :: HextoASCII(0x3F), the LCD will display the 3F on screen.

3 Responses to “How to Display Hex number on a LCD Screen”

  1. Yossarian said

    A very helpful article, Thank you very much for posting this – saved me a lot of time. One thing though, I used this in AVR studio to program an ATMega16 and had to turn ‘*hex’ to ‘hex’, as the compiler would post an error and the routine wouldn’t work. Otherwise everything is perfect. Best regards from Poland!

    • *hes is a pointer where as hex without the * is just the variable. What I am doing is I am passing a pointer (memory location of the stored value) to the function rather than passing the variable itself to the function.

      you are welcome

  2. Mandar said

    void LCD_X(unsigned int x)
    {
    LCD_send_data((x/16>10)?55+x/16:48+x/16);
    LCD_send_data((x%16>10)?55+x%16:48+x%16);

    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: