How Can I Write a Print Function for a Microcontroller?

  • Thread starter Thread starter Lancelot59
  • Start date Start date
  • Tags Tags
    Function Writing
AI Thread Summary
The discussion centers around creating a custom print function for a microcontroller-controlled LCD display without a built-in controller. The user seeks to implement functionality similar to the standard printf function in C, allowing for string input and variable argument handling. Key points include the need to manually define characters for display output and the challenge of working with character arrays instead of strings, as C does not support strings as a type. The conversation highlights the importance of using pointers to manage character arrays effectively, even if the strings are not dynamically allocated. Additionally, the concept of variable arguments (varargs) is emphasized as essential for mimicking printf's functionality, enabling the function to handle different types and numbers of inputs. The user also expresses uncertainty about formatting floating-point numbers and the necessity of pre-defining strings, which is clarified as unnecessary if the correct pointers are used. Overall, the focus is on developing a flexible and efficient way to display text on the LCD using C programming principles.
Lancelot59
Messages
640
Reaction score
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

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:
printf("my string");
However I can't just write the function as:
Code:
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:
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:
Technology news on Phys.org
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.
 
chiro said:
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.

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:
Lancelot59 said:
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.

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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top