Am I abusing printf function ?

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

Hi there

 

I'm a newbee in c and is using Joerg Wunsch's stdio library. (In AtmelStudio 6.1 with gcc)

 

I know how to send a readable string or char to the serial port. And sending escape codes with a "\" has also slipped into my mind.

 

But how to send a char = 0x01 without formating ?

I have hooked up a terminal program in hex mode and I want to receive "1" (not the ascii value=31)

 

I can't make out which formatting switch to use!

 

I tried this in my code and it worked. But I'm not sure it is a good idea ? 

 

FILE _io = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);

 

 uart_putchar(0x01,&_io) ;

 

regards

 

msskov

This topic has a solution.

Regards

 

msskov

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

Once you have 'attached' the <stdio> streams to your UART,   you should really stick to the regular <stdio.h> functions e.g.

    putchar(1);
    printf("\x1");
    printf("\1");
    printf("\001");
    printf("%c", 1);
    

If you are a masochist,   you could use the long form fprintf(stdout, ...)

 

David.

Last Edited: Thu. Oct 16, 2014 - 03:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I assume you mean you want to send a 'CTRL-A' character?  Then printf() or friends is not necessary.  As David suggests, stick with stdio.h functions.  Try putc().  Have a look at what else is available:

http://www.nongnu.org/avr-libc/u...

 

By the way, it's a bad idea to use variable names which begin with an underscore.  Those are reserved for use by the implementation and you might have a name collision with a library entity.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

Hi David

 

printf is fine for me.

I was just fumbling around to explore the posibilities. But I had to do some mistakes to learn :-)

Regards

 

msskov

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

Hi joeymorin

0x01 was just an example. It is just a single byte representation I tried to explain. And thanks for the underscore warning.

 

What do you mean by friends ?

 

/msskov

Regards

 

msskov

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

What do you mean by friends ?

He could be referring to all the ways you can set up printf().

 

Take a look HERE  and you will see what I mean.

 

I am no 'C' guru like David and Joey, but I have been steering away from the FILE _io = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW); and using PUT(c) and it's derivatives instead.  Don't know why, and not getting into it.

 

FWIW, you could always have a simple routine that tests the TXC flag and if the usart is not busy simply send 0x01 to the Usart directly with:

UDR = 0x01;

 

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

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

jgmdesign wrote:

What do you mean by friends ?

He could be referring to all the ways you can set up printf().

Actually I was referring to all of the variations:

  • vfprintf()
  • printf()
  • vprintf()
  • sprintf()
  • snprintf()
  • vsprintf()
  • vsnprintf()
  • fprintf()

And their PROGMEM variants.  The root of all of them is actually vfprintf().

 

Quote:
I am no 'C' guru like David and Joey,
Woah woah WOAH!  Let's put a stop to that rumour right now!  I certainly don't consider myself an expert, let alone a guru.  There are some real experts and bona fide gurus that hang around here.  Wouldn't be fair to them, their talents, or their investment of time and effort to lump me in with them.

 

But thanks for the sentiment... Let's go with 'motivated amateur' ;)

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

But thanks for the sentiment... Let's go with 'motivated amateur' ;)

You're welcome, and I like the moniker.

 

Actually I was referring to all of the variations:

After I posted I realized that I also meant the same as you and that the page I posted may not have been as clear.  Thanks for covering for me.....Motivator

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:

What do you mean by friends ?

He could be referring to all the ways you can set up printf().

 

Take a look HERE  and you will see what I mean.

 

I am no 'C' guru like David and Joey, but I have been steering away from the FILE _io = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW); and using PUT(c) and it's derivatives instead.  Don't know why, and not getting into it.

 

FWIW, you could always have a simple routine that tests the TXC flag and if the usart is not busy simply send 0x01 to the Usart directly with:

UDR = 0x01;

 

Yes,   printf() does have a whole selection of friends e.g. sprintf(),  snprintf(), ...

 

My point was that most apps will only ever want to sprintf() to a char array[] or printf() to <stdout>  e.g. UART.

So you are not going to need the full-fat versions.     But when you do,   Jim's link will show you how to use all the variants.

 

I strongly disagree with Jim's 'practice'.     He is asking for trouble if he tries to mix and match library and primitives.

IMHO,   if there is a suitable library function,    you should use it.     And if you want something bespoke,   you build it from library functions.      After all,   they are well proven and documented.     And you can test your 'special' on a regular PC compiler.

 

David.

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

Hi All and closing this tread for now

 

I thanks for all inputs and stick to the prinf or putchar function. It suits my needs for debugging.

Regards

 

msskov