yet another UART question with printf

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

Sorry - my review of https://www.avrfreaks.net/forum/how-use-printf-uart didn't yield the result I desired.

 

AtmelStudio 7.  Target device ATmega168A.

 

Baudrate or 19200 allows me to see single characters sent to my C# app.  I read those fine using...

 

</p>
<p>UDR0 = 0x53;// ByteToSend; // Send out the byte value in the variable "ByteToSend"</p>
<p>while ((UCSR0A & (1 << TXC0)) == 0) {}; // Do nothing until transmission complete flag set</p>
<p>

 

However, reading the how-to above and reviewing the stdio.h file didn't get me to a place where I could use printf().  Any help or pointer, please?

 

Peter

(and I'm busy looking through the forum help to see how to properly 'quote' code...)

Last Edited: Mon. May 29, 2017 - 12:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Looks like fdev_setup_stream() of fdevopen() is involved? :-?

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

Look at post #4 in the link you provided in post #1.

 

If you have installed Atmel Studio on your computer, you will find the file: group__stdiodemo.html

On my computer is can be found here: file:

  C:\Program Files (x86)\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.1061\avr8-gnu-toolchain\doc\avr-libc\avr-libc-user-manual\group__stdiodemo.html

 

This file documents the demo program in the file stdiodemo.c, which is also on your computer.

 

stdiodemo.c includes the lines:

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

 stdout = stdin = &uart_str;

 

where uart_putchar and uart_getchar can be found in uart.h and uart.c.

 

 

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

To post code do not use [ code ] tags as they don't work properly. Instead on the toolbar above the message editor press the icon that looks like "<>". If you access from a small screen device (phone/tablet) you may not see the toolbar, maybe try ticking "request desktop site"? 

 

Oh and don't block on TXC unless doing RS485. Normally you block on UDRE and do that before the write to UDR. 

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

clawson wrote:
To post code ...  on the toolbar above the message editor, press the icon that looks like "<>".

Full, illustrated instructions here: https://www.avrfreaks.net/comment... (also, for completeness, how to post images)

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

(awneil, you sure that link is correct?  It takes me to a thread about an I2C problem)

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

OK.  I'm struggling with this.

 

Chuck99 wrote:

stdiodemo.c includes the lines:

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

 stdout = stdin = &uart_str;

 

where uart_putchar and uart_getchar can be found in uart.h and uart.c.

 

I've looked at the demo (in fairness, I only cursorily looked at lcd or hd44780 c and h files) I can't seem to include things to get a successful build.  I have copied the uart.c, h (with no modifications) files to my project folder and have added the 'include section from the stop of stdiodemo.c at the top of my projects main file.

 

However, I get build errors that point to UBBRL, USCRB, TXEN, (and a few others) as being undeclared.  It seems the includes have things defined in the correct order but alas, no build.

 

 

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

engrpetero2 wrote:
(awneil, you sure that link is correct?

Yes - it links to a specific post (#4) in that thread:

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sun. Jun 4, 2017 - 09:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Figured out the problem.  Adjusted the demo code and have things working.  Thanks for the pointer to the demo and the explicit instruction, Chuck99.

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

For the benefit of future readers who may have the same problem, please describe what you figured out - and the required fix.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Target device ATmega168A.

I get build errors that point to UBBRL, USCRB, TXEN, (and a few others) as being undeclared.

 Atmega168 doesn't have TXEN/etc, it has TXEN0/etc.

Presumably the code you were copying was from an older chip (Atmega8, perhaps) which has the symbols without the "0" qualifier.

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

westfw - that was exactly the case (for all register names in uart.h). 

 

And it was not helpful for me to post problem solved without posting what I did - sorry about that.

 

For the benefit of any who have an STK600 and want a simple app to play with using the uart (and a heartbeat LED and a switch 1 mask), I figured I'd upload my simple demo AtmelStudio demo project.  However, I can't find a way to do so using the toolbar buttons.  Back to searching the help... :-)

Last Edited: Tue. Jun 6, 2017 - 06:47 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simply zip it and "Add attachments" below.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly