NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Printing strings on a pc console instead of lcd

February 11, 2013
by Q
Q's Avatar

I'm new to C and microcontroller programming, and so far all of the programs I have put on my NK have not involved using the printf function. Before wiring the lcd up though, I thought I would try mastering the printf function by simply printing some strings onto whatever console winAVR would use in Windows 7. So I wrote the program and loaded it onto the nerdkit just fine. I made sure chip wasn't in programming mode and then turned on the power supply and nothing happened. I'm completely in the dark about how to work this and would appreciate any advice.

February 11, 2013
by Ralphxyz
Ralphxyz's Avatar

Q, what is your intended output? Are you using the LCD or are you expecting output to your terminal?

If you did/do the tempsensor project you use the printf:

// start up the serial port
uart_init();
FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
stdin = stdout = &uart_stream;

// write message to serial port
printf_P(PSTR("%.2f degrees F\r\n"), temp_avg);

Lots of the Nerdkits Tutorials and Projects use printf.

Ralph

February 14, 2013
by Q
Q's Avatar

My intended purpose was to print the message onto a Windows 7 command prompt screen. And I actually wasn't aware of that section of code in the tempsensor project, all I had written was the printf_P line without the three lines that start the serial port. Anyway, with that new knowledge, I wrote this short program and loaded it onto the chip with no syntax errors or anything. (Sorry it doesn't look nice here; I'm posting off of a school computer that can't have a code editor on it, and I had a problem with posting a screenshot I had copied from my "internet-less" computer that does have an editor)

#define F_CPU 14745600 #include <stdio.h> #include <math.h> #include <avr/io.h> #include <avr/interrupt.h> #include <avr/pgmspace.h> #include <inttypes.h> #include "../libnerdkits/delay.h" #include "../libnerdkits/lcd.h" #include "..libnerdkits/uart.h"

int main(){ uart_init(); FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW); stdin = stdout = &uart_stream; printf_P(PSTR("Hello World!")); DDRC |= (1<<PC4); while (1){ PORTC |= (1<<PC4); } return 0}

As you can see, I made it so the LED would turn on after the message was supposed to have been printed on my command prompt screen. I watched very closely when I connected the chip to power, but no message came up. The LED turned on just fine though. Do I need to do anything else to enable the printf function?

February 14, 2013
by Noter
Noter's Avatar

You can't print directly from the microcontroller to your computer screen. You can write characters out the serial port on the microcontroller but there must be a program running on the PC that will read the characters coming into it's serial port and then print them on the screen.

February 15, 2013
by Q
Q's Avatar

Alright, I'll look into that. Thanks.

Post a Reply

Please log in to post a reply.

Did you know that first-order systems have a exponentially decaying response to step inputs? Learn more...