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 » printf problems

December 15, 2012
by jmuthe
jmuthe's Avatar

I notice that anytime I write a program that uses printf it doesn't work. When I say that it doesn't work, I mean that the program compiles correctly and the other parts of the program work fine. However, I just don't see anything on the screen when I use the Nerdkit. Here is a program with the printf statement:

#include <stdio.h>
int main()
{
    int a, b, c;
    a = 5;
    b = 7;
    c = a + b;
    while (1) 
{   
printf("%d + %d = %d\n", a, b, c);
} 
return 0; 
}

The program compiles but nothing appears on the screen. I then created another program where I included the same printf statement but I added a part to make a LED blink at PC0. Here it is:

     #include <stdio.h>
    #define F_CPU 14745600
    #include <avr/io.h>
    #include "../libnerdkits/delay.h"
    int main()
    {
        DDRC = (1<<PC0); 
        int a, b, c;
        a = 5;
        b = 7;
        c = a + b;

        while (1) 
    {   
    printf("%d + %d = %d\n", a, b, c);
    PORTC=1;
    delay_ms(100);
    PORTC = 0;
    delay_ms(100);

    }

    return 0;
    }

The LED blinked like it should but I don't see the printf message on the screen. How do I fix this. I am not sure what information you need in order to help me but I use Windows 7.
December 15, 2012
by Noter
Noter's Avatar

Do you have the lib's included in LINKFLAGS in the makefile?

LINKFLAGS=-Wl,-u,vfprintf -lprintf_flt -Wl,-u,vfscanf -lscanf_flt -lm
December 16, 2012
by Ralphxyz
Ralphxyz's Avatar

Hi jmuthe, when you say "screen" do you mean the LCD?

If LCD where is your

lcd_init();

?

Can you run initialload?

Ralph

December 16, 2012
by Rick_S
Rick_S's Avatar

Might need the

#include "../libnerdkits/lcd.h"

as well...

Rick

December 16, 2012
by jmuthe
jmuthe's Avatar

No, when I say screen, I don't mean the LCD. I am able to print things out on the LCD just fine. I mean my computer screen. Whenever you execute a printf statement in C or a cout statement in C++, a window with a black screen and white letters should pop up. It looks a lot like the command prompt window and it should display what you wrote in your program. Here is an example of this: http://www.youtube.com/watch?v=b00HsZvg-V0. In this video, a man writes a simple program in C that would display the message "Hello World." At the 8 minute mark he executes the program and we see a window with a black screen that displays the message "Hello World". You could just skip to the 8 minute mark if you don't want to watch the whole video. My question is why doesn't that window show up when I execute the program?

When I wrote my Makefile, I just copied and pasted it from the sample files. Here it is:

GCCFLAGS=-g -Os -Wall -mmcu=atmega168 LINKFLAGS=-Wl,-u,vfprintf -lprintf_flt -Wl,-u,vfscanf -lscanf_flt -lm AVRDUDEFLAGS=-c avr109 -p m168 -b 115200 -P COM8 LINKOBJECTS=../libnerdkits/delay.o ../libnerdkits/lcd.o ../libnerdkits/uart.o

all: led_blink-upload

led_blink.hex: led_blink.c make -C ../libnerdkits avr-gcc ${GCCFLAGS} ${LINKFLAGS} -o led_blink.o led_blink.c ${LINKOBJECTS} avr-objcopy -j .text -O ihex led_blink.o led_blink.hex

led_blink.ass: led_blink.hex avr-objdump -S -d led_blink.o > led_blink.ass

led_blink-upload: led_blink.hex avrdude ${AVRDUDEFLAGS} -U flash:w:led_blink.hex:a

December 16, 2012
by Noter
Noter's Avatar

A program on the atmega microsontroller cannot pop open a window on the PC. The PC and the atmega are different and separate machines.

December 16, 2012
by pcbolt
pcbolt's Avatar

jmuthe -

If you look at the third to the last line in you makefile post, the command "avr-gcc" tells the computer to compile using the "avr-gcc" compiler NOT "gcc" which is used to compile programs for the PC. The "gcc" compiler is great for learning C and testing code syntax etc, but it won't work when you want to directly test programs written for the microchip. You could simulate the MCU for test purposes, but built-in functions like "printf()" need to be written differently in each case.

December 16, 2012
by jmuthe
jmuthe's Avatar

Okay, I was a little confused by pcbolt's last comment. It seems that you are saying that if I change the makefile or program slightly by making it work off of gcc instead of avr-gcc then the printf function may work. If so then what do I have to change in my program and what do I have to change in my makefile?

December 16, 2012
by pcbolt
pcbolt's Avatar

jmuthe -

The first program of your first post in this thread (13 lines in total) should compile with "gcc" and display on your PC screen. If you put these lines in a text file called "test.c", and at the command line type...

gcc test.c

you should get a program "test.exe" (or sometimes "a.exe") that will display on the command line when you type the name of the .exe file. That is assuming you have the gcc compiler and have all the PATH settings set up for you computer. BTW, I used a different compiler similar to "gcc" and got no errors, but when I ran it, the "while(1)" loop kept executing over and over and I had to kill the program. I'd take that out for testing purposes.

Post a Reply

Please log in to post a reply.

Did you know that using an alternating excitation voltage can reduce the noise in many sensor measurements? Learn more...