Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Writing A Print Function

  1. Jul 23, 2011 #1
    This is something that has had me stumped for quite a while. I'm working with a microchip microcontroller to control this LCD: http://www.varitronix.com/Product/LCD/VIM-332-DP(R0).pdf [Broken]

    As you can see it has no controller. I'm manually controlling the segments. Now the issue is that I need to write a function to print to it. From some research I did I learned that I can't just modify the "putch" function because I need to manually define characters (after I learned what putch does). Now I have made functions which will create numbers and letters on the display where I tell them to, but that's only good for manually defining outputs.

    What I want is to create a function like printf, where I can pass it a string and then it passes that string further down to some processing where I check to make sure the string will fit on the display, and then if it passes writes the string to the display. In the examples here "print" is when I'm referring to the function I want to write.

    So I want the input to look like the printf function in stdio.h:
    Code (Text):
    printf("my string");
    However I can't just write the function as:
    Code (Text):
    print(String INPUT);
    because C doesn't have strings as a type, you use a character array.

    My question is, how can I write the function such that I can call it like the normal printf.
    Code (Text):

    print(%f,float_variable);
    print("PIC");
     
    Everyone keeps telling me to use a pointer, however the string isn't originating outside the function, therefore I think a pointer would be useless. Unless I have another function above which reads a variable, and parses it in as a string. However this leads to the issue where I wouldn't be able to print short messages on the screen unless I defined functions for each word I would want to make, which actually wouldn't be too terrible if it really came down to it.

    I haven't had luck finding example code for it that I can understand, because all the stuff I've found is written in assembly. I'm at the point with assembly where I can follow individual steps, but I can't see what the code is doing overall.

    I've been stuck on this problem for a while and I'm out of ideas at the moment.
     
    Last edited by a moderator: May 5, 2017
  2. jcsd
  3. Jul 23, 2011 #2

    chiro

    User Avatar
    Science Advisor

    Yeah you need to use a pointer.

    For data that is explicitly declared like "PIC", it will be stored in the data segment of your executable and loaded into memory just like the executable code is.

    If you intend to overload the printf function, you need to work with variable arguments (I'm not sure if its vararg or not, so I can't help you there).

    Just remember that just because something isn't created dynamically, it doesn't mean it doesn't have an address.

    Use a standard character array pointer (char*) and you should be fine. If you are using some custom string structure where the string is not null terminated, then you write your code to reflect the changes in the data structure.
     
  4. Jul 23, 2011 #3
    But all the strings would have to be pre-defined? C doesn't support function overloading.

    Now to print out a number like "1.234", or "12.23" I'm not sure how I would set that up. I currently have functions that I can use to manually determine what sections show what. I'm trying to figure out how I can write a function that parses floating point numbers.
     
    Last edited: Jul 23, 2011
  5. Jul 23, 2011 #4

    chiro

    User Avatar
    Science Advisor

    No the strings don't have to be, you just have to pass it the right pointer: if it's pre-declared the pointer will be in the data segment of your loaded exe, and if its dynamic it will just be some pointer on the heap (heap is just the area of memory for dynamic stuff).

    The reason I mentioned the "varargs" is because that is what things like printf use: you basically put all your arguments on the stack and the function figures out how to restore the stack back to its original value. That's basically how you are able to put different amounts of arguments to something like printf without it crashing.

    Again if you want to do what printf does, look at variable argument lists.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook