Printing Integer Value to LCD in ASCII Format

This is a discussion on Printing Integer Value to LCD in ASCII Format within the C Programming forums, part of the General Programming Boards category; Hey again, I've had a lot of help from the board in sorting out a Pedometer circuit I'm currently working ...

  1. #1
    Deleted Account
    Join Date
    Nov 2010
    Posts
    16

    Printing Integer Value to LCD in ASCII Format

    Hey again,

    I've had a lot of help from the board in sorting out a Pedometer circuit I'm currently working on as part of a group project.

    Unfortunately, we've been using the sprintf function to write the value of an integer 'noofsteps' into a character string, which was then written to the screen. Now that we've come to try and compile the completed code, it's too long to fit onto the chip (PIC16F684).

    As suggested by members of this forum, I've looked at trying to accomplish what I'm trying to do without using sprintf (as I don't need anywhere near all of the functionality that sprintf allows).

    Currently I'm trying to do the follow:

    I have an integer called 'noofsteps', which will have a value between 0 and 20,000. I want to take the value of 'noofsteps' and display it on my LCD screen (Hitachi 44780) without using sprintf as it's too memory intensive.

    I'm currently trying to implement this snippet I found elsewhere:

    Code:
    value = 12345;	// number you want to display
    length = 5;              // five digits	
    div = 10000;	// divisor
    
    
    while( length )
    {
     lcd_write( (value / div) + 48 );	// write highest digit first
     value = value % div;
     length--;
     div /= 10;
    }
    But as a complete beginner, I'm having trouble getting my head around how this could be integrated into the rest of the program.

    I understand how this strips the integer number apart into its component numbers and then adds 48 so that each decimal number aligns to its ASCII equivalent.

    However, what I don't understand is how I would write each number to my screen in sequence without removing the digit that was there before it. I.e. in this case, how to output "1", then shift the cursor on the screen to the right, then output "2", then shift the cursor to the right... etc. I'm guessing this information would be in the Hitachi 44780 Datasheet but I've got no clue how to find it, or even really what I'm looking for. For information, I am using the Hitachi 44780 in 4 BIT OPERATION.

    Secondly, I'm confused as to what
    Code:
    while(length)
    does? Does it simply loop the code below until 'length' = 0?

    Would anybody be able to offer some pointers? Unfortunately I'm away from the programming equipment until 4th Jan so I won't be able to test anything until this, but it would be good to know that I am moving along the right lines.

    Thanks in advance for any help that people may be able to give, and please remember that I am a total beginner and will be asking stupid questions in response to your answers!

    Thanks,

    JT

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by JTerry View Post
    Hey again,

    I've had a lot of help from the board in sorting out a Pedometer circuit I'm currently working on as part of a group project.

    Unfortunately, we've been using the sprintf function to write the value of an integer 'noofsteps' into a character string, which was then written to the screen. Now that we've come to try and compile the completed code, it's too long to fit onto the chip (PIC16F684).

    As suggested by members of this forum, I've looked at trying to accomplish what I'm trying to do without using sprintf (as I don't need anywhere near all of the functionality that sprintf allows).

    Currently I'm trying to do the follow:

    I have an integer called 'noofsteps', which will have a value between 0 and 20,000. I want to take the value of 'noofsteps' and display it on my LCD screen (Hitachi 44780) without using sprintf as it's too memory intensive.

    I'm currently trying to implement this snippet I found elsewhere:

    Code:
    value = 12345;	// number you want to display
    length = 5;              // five digits	
    div = 10000;	// divisor
    
    
    while( length )
    {
     lcd_write( (value / div) + 48 );	// write highest digit first
     value = value % div;
     length--;
     div /= 10;
    }
    But as a complete beginner, I'm having trouble getting my head around how this could be integrated into the rest of the program.

    I understand how this strips the integer number apart into its component numbers and then adds 48 so that each decimal number aligns to its ASCII equivalent.

    However, what I don't understand is how I would write each number to my screen in sequence without removing the digit that was there before it. I.e. in this case, how to output "1", then shift the cursor on the screen to the right, then output "2", then shift the cursor to the right... etc. I'm guessing this information would be in the Hitachi 44780 Datasheet but I've got no clue how to find it, or even really what I'm looking for. For information, I am using the Hitachi 44780 in 4 BIT OPERATION.
    I've not used LCDs much, but isn't that sort of The Complete Opposite to how they usually work? (I've searched Hitachi 44780 enough to verify it's dot matrix and not seven-segment.) Once you've got them into accepting data mode, I seem to remember you just start sending bytes down the pipe and they show up. That is to say, you doing "send (12345)" is you sending five bytes to the LCD, and you doing "send(1) send(2) send(3) send(4) send(5)" is you sending five bytes to the LCD. The LCD has no way of knowing, and doesn't care, which one it is, right?

    Quote Originally Posted by JTerry View Post
    Secondly, I'm confused as to what
    Code:
    while(length)
    does? Does it simply loop the code below until 'length' = 0?
    That's what while does. It loops until the thing in the parentheses is false (false means zero).

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    I suggest going back and looking though your previous post.
    Quote Originally Posted by JTerry View Post
    As suggested by members of this forum, I've looked at trying to accomplish what I'm trying to do without using sprintf (as I don't need anywhere near all of the functionality that sprintf allows).

    Currently I'm trying to do the follow:

    I have an integer called 'noofsteps', which will have a value between 0 and 20,000. I want to take the value of 'noofsteps' and display it on my LCD screen (Hitachi 44780) without using sprintf as it's too memory intensive.

    I'm currently trying to implement this snippet I found elsewhere:

    Code:
    value = 12345;	// number you want to display
    length = 5;              // five digits	
    div = 10000;	// divisor
    
    
    while( length )
    {
     lcd_write( (value / div) + 48 );	// write highest digit first
     value = value % div;
     length--;
     div /= 10;
    }
    But as a complete beginner, I'm having trouble getting my head around how this could be integrated into the rest of the program.

    I understand how this strips the integer number apart into its component numbers and then adds 48 so that each decimal number aligns to its ASCII equivalent.
    Salem wisely suggests using itoa if it's available. If not:
    Based on your last post's code, I would put the chars directly in TopMessage or BotMessage instead of calling lcd_write on each one. I also would do my loop like so:
    Code:
    void num_to_str(int num, char *str)
    {
        int div = 10000; // this is big enough for 20000 steps
        int flag = 0;  // track if we've started printing yet -- this is for "trapped" zeros like 2001
        int i = 0;
        char c;
        while (div) {
            if (flag || num > div) {
                c = num / div + '0';
                flag = 1;
            }
            else {
                c = ' ';  // put a space char since we haven't started printing the number yet
            }
            str[i++] = c;
            num %= div;    // get rid of the first digit
            div /= 10;   // shrink the divisor
        }
    }
    ...
    num_to_str(12345, &TopMessage[6]);  // puts "12345" into TopMessage starting at the 7th character
    However, what I don't understand is how I would write each number to my screen in sequence without removing the digit that was there before it. I.e. in this case, how to output "1", then shift the cursor on the screen to the right, then output "2", then shift the cursor to the right... etc.
    Not a problem. You wrote the following instruction in the code from your previous post:
    Quote Originally Posted by JTerry View Post
    Code:
        LCDWrite(0b00000110, 0);    //  Move Cursor After Each Character
    So just put the number directly into TopMessage or BotMessage, then call LCDOutput().

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,773
    Did you try the non standard itoa from <stdlib.h>

    It exists in Hi-Tech C

    extern char * itoa(char * buf, int val, int base);

    Tim S.

  5. #5
    Deleted Account
    Join Date
    Nov 2010
    Posts
    16

    Thanks again guys

    Hey guys, thanks again for the help.

    Is itoa much smaller than sprintf? That seemed to be the largest part of the previous code that was written, and the reason that we were filling the chip memory.

    If not, I will look to try your code Anduril, but unfortunately I'm away from the kit at the moment due to various holidays etc.

    Thanks a bunch for everyone taking to time to post a response, this is a great resource and I appreciate the patient help.

    JT

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Yes, itoa is much smaller. It's probably a few dozen lines at the most, where as sprintf will run into the hundreds since it has to handle so many different types of data (ints, floats, chars) and the numbers can have a wide variety of formats. The code I gave you is basically itoa, so if Hi-Tech C has it, as stahta01 states, use it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing an integer stored in an array
    By Freez3L in forum C Programming
    Replies: 4
    Last Post: 11-18-2002, 02:11 AM
  2. printing ASCII
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-10-2002, 10:13 PM
  3. Trouble Printing ASCII Character
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2002, 09:04 PM
  4. Printing extended ASCII characters
    By Jonny M in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 10:12 AM
  5. Printing characters not in the ASCII table
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 01-21-2002, 01:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21