Help with printf()

This is a discussion on Help with printf() within the C Programming forums, part of the General Programming Boards category; Can someone help me with this code? Whenever I press a button I want the computer to print a statement. ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    26

    Help with printf()

    Can someone help me with this code? Whenever I press a button I want the computer to print a statement. I have an Atmel STK500 development board. But it doesn't work; here's the code:

    Code:
    #include <avr/io.h>     // avr header file for IO ports
    
    
    int main(void){
    
    
    	DDRA = 0x00;    // set PORTA for input
    	
    	if(PORTA == 0x00){
    		printf("Hello!");
    	}
    	return 1;
    	
    }
    Last edited by aosterminal; 09-21-2012 at 02:05 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    i do not know what DDRA or PORTA is,but i remember that main should return 0 in case of success

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    Does your AVR board have a console? Where do you expect to output anything?

    i do not know what DDRA or PORTA is,but i remember that main should return 0 in case of success
    This is a microcontroller project board , PORTA is a digital input/output port and DDRA I believe is the Port A Data Direction Register, which controls whether the port is an input or output port. Also since this is a non-hosted environment returning a value from main() is not required.

    Jim

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by jimblumberg View Post
    This is a microcontroller project board , PORTA is a digital input/output port and DDRA I believe is the Port A Data Direction Register, which controls whether the port is an input or output port. Also since this is a non-hosted environment returning a value from main() is not required.

    Jim
    Oh yeah,from the greek word Πόρτα !Now it makes sense !thanks

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    26
    Thank you both, and std I tried switching the values for return but nothing changed (I think Jim just explained that). Also to clarify, my code builds and compiles but when I press the button on the board nothing happens :/ Any help?

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2,282
    I'll echo jimblumberg's question in post #3:

    Does your AVR board have a console? Where do you expect to output anything?
    And which bit on PORTA is this button tied to?

    And what is the expected polarity (logic high or logic low) when the button is pressed?

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    26
    Quote Originally Posted by Matticus View Post
    I'll echo jimblumberg's question in post #3:



    And which bit on PORTA is this button tied to?

    And what is the expected polarity (logic high or logic low) when the button is pressed?
    Im sorry I didn't see that you asked that! I don't know if my board has a console :/ My teacher told me that I should be able to use printf() to output text to my computer monitor (with the board connected to my computer via RS232).

    What is a bit? :3 Next to the first button it says SW0. I have what I believe is a port labelled "SWITCHES" connected to PORTA. On "SWITCHES" there are pins labelled SW0-SW7 that the wire is connected to, and on PORTA there are pins labelled PA0-PA7.

    You can see for yourself on this picture if you zoom in, (I found this picture on the internet, same board though)

    http://obrazki.elektroda.net/11_1236666009.jpg

    Does that help? I just want to press a button and have the AVR output text on my computer monitor! Thank you

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    If your compiler has been setup to use the RS-232 port as the console then you should be able to use printf() to print to this port. However you must also be running some kind of terminal program, HyperTerminal for example, on your PC in order to receive this information. Also in order for this to work you must know the communication port parameters. Both the PC and your board must both be using the same settings.

    Please be aware that unless your compiler has been properly setup for your particular project board this communication will probably fail.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf hex
    By bazzano in forum C Programming
    Replies: 6
    Last Post: 01-04-2006, 01:54 PM
  2. about printf
    By cheesestick in forum C Programming
    Replies: 9
    Last Post: 03-25-2005, 12:32 PM
  3. make printf using printf?
    By germaneater in forum C Programming
    Replies: 9
    Last Post: 11-10-2004, 09:58 PM
  4. printf
    By xddxogm3 in forum C Programming
    Replies: 11
    Last Post: 03-16-2004, 05:58 PM
  5. Bad printf()!
    By kirby1024 in forum C Programming
    Replies: 2
    Last Post: 06-02-2002, 09:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21