How Can I Write a Print Function for a Microcontroller?

  • Thread starter Thread starter Lancelot59
  • Start date Start date
  • Tags Tags
    Function Writing
Click For Summary

Discussion Overview

The discussion revolves around the challenge of writing a print function for a microcontroller to control an LCD display without a built-in controller. Participants explore how to create a function similar to printf in C, addressing issues related to string handling, pointers, and variable arguments.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses difficulty in writing a function to print to an LCD, noting the need to manually define characters and the limitations of C regarding string types.
  • Another participant suggests using a pointer to handle string data, explaining that even explicitly declared strings have addresses in memory.
  • There is a discussion about the need to work with variable arguments if the printf function is to be overloaded, with uncertainty about the specifics of implementing varargs.
  • Concerns are raised about the requirement for strings to be pre-defined in C, with a participant questioning how to handle floating-point numbers in the print function.
  • Further clarification is provided that strings do not need to be pre-defined if the correct pointer is passed, and the concept of variable argument lists is reiterated as essential for mimicking printf functionality.

Areas of Agreement / Disagreement

Participants generally agree on the necessity of using pointers and variable arguments, but there remains uncertainty about the implementation details, particularly regarding floating-point number parsing and the handling of string data.

Contextual Notes

Limitations include the lack of clarity on how to parse floating-point numbers and the specifics of variable argument handling in the context of the microcontroller's capabilities.

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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 26 ·
Replies
26
Views
3K
Replies
55
Views
7K
Replies
1
Views
7K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
5
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K