How Can I Write a Print Function for a Microcontroller?

In summary: 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.
  • #1
Lancelot59
646
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
  • #2
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.
 
  • #3
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:
  • #4
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.
 
  • #5
I would appreciate any advice or suggestions you can provide.I can understand your frustration with trying to write a print function for your microcontroller. It can be challenging to work with limited resources and programming languages like C. However, I would like to offer some suggestions that may help you in your task.

Firstly, it is important to understand the limitations of your microcontroller and the display you are working with. The fact that there is no controller for the LCD means that you will have to manually control the segments. This can be time-consuming and requires precise code to ensure that the display shows the desired output. Additionally, the display may have limited space for displaying characters, so it is important to check the length of the string before attempting to print it.

Based on your description, it seems like you have already made some progress in creating functions for displaying numbers and letters on the display. This is a good starting point and can be used to build upon for creating a print function. One approach could be to create a function that takes in a string as a parameter and then breaks it down into individual characters. You can then use your existing functions to display each character on the LCD.

Another approach could be to use a character array to store the string and then pass the array as a parameter to your print function. This would allow you to use the printf-like syntax that you are looking for. You can also use a pointer to the character array to access and display each character.

It is also worth considering using libraries or existing code that may already have functions for printing to LCD displays. This can save you time and effort in writing your own print function from scratch.

In conclusion, I would suggest breaking down your problem into smaller steps and using the resources available to you, such as libraries and existing code, to help you in your task. With patience and persistence, I am sure you will be able to write a successful print function for your microcontroller. Best of luck!
 

Related to How Can I Write a Print Function for a Microcontroller?

1. What is a print function?

A print function is a programming command that displays output on a computer screen or other output device. It allows the user to see the results of their code or program in a readable format.

2. How do I write a print function?

To write a print function, you need to use the print() command followed by the text or variable you want to display. For example, print("Hello, world!") will display the text "Hello, world!" on the screen.

3. Can I print multiple items using a print function?

Yes, you can print multiple items using a print function by separating them with a comma. For example, print("Hello", "world") will display "Hello world" on the screen.

4. How do I format the output of a print function?

To format the output of a print function, you can use string formatting techniques such as concatenation or string interpolation. You can also use built-in functions like format() to specify the format of the output.

5. What are some common errors when using a print function?

Some common errors when using a print function include forgetting to include parentheses, using incorrect syntax, or trying to print a variable that has not been defined. It is also important to make sure your code is properly indented to avoid errors.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
26
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
2
Replies
43
Views
3K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
20
Views
2K
Back
Top