Puts

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hopefully this will be a quick thread

 

(famous last words)

 

I am working with a serial LCD display

 

http://elmicro.com/files/paralla...

 

I have been using this for a while now and it works welll, I have my code written that sets up the UART and I have no trouble

 

Its a case of finding the commands and simply printing to the port

 

If you look at the last page it gives the commands for some characters, I am trying to display the arrow symbol

 

0111 1110 =0x7E

 

If I use

 

putchar(0x7E)

it works, the arrow symbol is displayed as required

 

Now I want to print two of the arrow symbols next to each other (so that I can scroll)

 

So I thought OK I just use

 

puts(0x7E 0x7E)

And in my infinite noobness I expected two arrows separated by a space (I know your all laughing right now and I don't mind) but despite searching and reading I am left all confused as to how these things work

 

putchar sends a character, 0x7E is hex 7E which is 8 bits so it is the length of a char and it works

 

So if puts prints a string then I don't see what the difference is here

 

I have a working solution and that is

 

printf(%c %c,0x7E,0x7E)

which prints the two arrows separated by a space

 

This whole exercise has left me wondering and wanting a better understanding as I clearly havent got a clue how I should be using these functions

 

 

Thanks in advance

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So I thought OK I just use

You need to get a book on C.

 

putchar() takes a "char" as parameter. char's are things like 'A', 65, 0x7E and things.

 

puts() on the other hand takes a pointer to char array (that is a char *) so things like "hello", "\073\044\0x2F" and things like that. I imagine:

puts("\0x7E\0x7E");

should do what you require as it combines two 0x7E characters into a char array and passes the address of that array to puts(). To be honest, for something like this I think I'd be tempted to:

#define ARROW 0x7E
...
  putchar(ARROW);
  putchar(ARROW);

unless you are printing hundreds of the things this will be more documentary to the reader as to what the mysterious 0x7E is actually achieving.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Clawson, I will put my balls on the fence here! and say I probably have more books on C than what you have!

 

OK probably not as many as you have but I do have a lot, I have a lot of books on various things but I think I have more books on programming than any other subject which isn't a very good advert for me

 

I am at work at the minute most of my books are at home, this thread was a spur of the moment thing

 

puts() on the other hand takes a pointer to char array (that is a char *) so things like "hello", "\073\044\0x2F" and things like that. I imagine:

puts("\0x7E\0x7E");

 

Thanks Clawson, I tried this but it just doesn't print anything

 

I would ask what the \ does but I darent before I get to look what K&R say

 

 

I'd be tempted to:

#define ARROW 0x7E
...
  putchar(ARROW);
  putchar(ARROW);

 

 

Yeah I know what you are saying and it would be much better but this question is just for educational purposes, I have the thing working now with

 

printf(%c %c,0x7E,0x7E)

 

Its just for a temporary test never to be repeated, as long as the arrows are scrolling then its clear the process is still running your solution would make (to me) my code more complicated because I would need to shift the cursor after the first print

 

What I have is a count in the program n

 

and by using

putchar(188+n)
printf(%c %c,0x7E,0x7E)

As the count changes it shifts the cursor position to give me half assed text scrolling

 

I am wondering why your method doesn't print anything as I am sure its me again!

 

 

Thanks

 

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
printf(%c %c,0x7E,0x7E);

That syntax is not correct for using printf(). I presume you mean:

printf("%c %c",0x7E,0x7E);

But that's going to put a space between them. If you really want them adjacent then

printf("%c%c",0x7E,0x7E);

As for the \ thing. You may already be familiar in character strings with using \r and \n for return and new line? C actually supports a number of such "escapes". You can follow the \ with an octal number and it will be taken as the code for a character. You can also use \xNN... to give a hex number which is what my \x7E should do. 0x41 is the hex code for 'A's so equally you should find that \x41\x42\x43 prints "ABC" as in the following test I just ran:

$ cat test7.c
#include <stdio.h>

int main(void) {
    printf("\x41\x42\x43\n");
}

$ gcc test7.c -o test7
$ ./test7
ABC
$

On my PC you can see what I get with:

$ cat test7.c
#include <stdio.h>

int main(void) {
    printf("\x7D\x7E\x7F\n");
}

$ gcc test7.c -o test7
$ ./test7
}~
$ 

But that's clearly because 0x7E and 0x7F in my PC character set are different to 7E and 7F in the character set of the display device you are using. (in fact the 7F character was eaten by the editor here).

Last Edited: Tue. Sep 16, 2014 - 01:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

One can put a bunch of arrows in a string.

in ram:

unsigned char arrowstr[5]={ARROW,ARROW,ARROW,ARROW,\0};

in flash:

__flash unsigned char arrowstr[5]={ARROW,ARROW,ARROW,ARROW,\0};

puts should like these because they are null terminated strings of ascii chars.

 

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note that in avr-gcc puts() must be replaced by puts_P() if the string is in __flash.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In the imagecraft compiler, one uses printf and puts for chars in ram, cprintf and cputs for chars in flash. (c used to mean const). I guess puts_P is OK too.

Imagecraft compiler user

Last Edited: Tue. Sep 16, 2014 - 11:44 PM