beginners questions

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

Hello all

After much thought and deliberation i have decided to concentrate on learning .C programming using k&r's the C programming language 2nd edition book and avrstudio/winavr as the compiler.

I'am not used to requesting assistance and have mostly relied on a trial and error approach when homebrewing projects ,but through reading numerous tutorials and information available it would seem this is not the best approach for learning .C and hence this initial post which will most definately be one of many.

And so here's my questions

Is the software/enviroment and reading i have selected fitting for a first time user or would you advise another alternative.

Below is the opening beginners code extracted from k&r's book which on the first attempt would'nt compile without error.

#include

main()
{
printf("Hello, world!\n");
return 0;
}

Heres the modification's i made and it now compiles error free.

#include

int main()
{
printf("Hello, world!\n");
return 0;
}

My questions are

1) Is stdio.h a remmnant of older .C code/compiler's and hence the failure.

2) Or is it because it;s not specific to the AVR mcu and so then unuseable for AVR purposes ,will i need to replace this line in future examples.

3)When debugging winavr reports that the mcu selected ie at90s8515 is incorrect and should be atmeag128 ,i duly change mcu selection and debug run's ok but how did it know what mcu to use.

4)Why can i not replicate the original build fault by changing the code back ,whats changed.

5)The learning question is "where does the output go" am i correct in thinking it cycles within the mcu and would need some form of port statement adding in order to output a visual aid.

6)To add an lcd so i may view the output visually is this a case of linking the lcd.h file suitable for my display and writing in code to perform the output.

Thanks in advance
(be gentle :roll: )

stephen

ps It states in the bible "IF YOUR IN DOUBT ASK THE EXPERTS" i am in the right place then :wink: .

3A) Because Atmega128 is mcu selected when using winavr default setup.

Codevisionavr & Avrstudio 4.18
Easyavr5A-Jtagicemk1

Call me Pedantic, But not after 9.

if Milk_Brilliant
else Codevision_Avrs==Better

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

K&R is a good book, buut it's written from a large system perspective, like, with lots of memory, and a keyboard connected, and a display of some kind.... edit: and a real OS to be called from, return to, and generally hook into!

if K&R is the book you want to read and follow examples of, you shoud try and get a compiler for your computer, not the AVR (like, say, GCC) and follow the books examples in there.

From memory, all K&R examples require standard input/output in the form of a console. none of them are likely to do much without relatively major hacking to work with serial port input and serial port output, if you compile them for an AVR.

edit: I think that if you really do want to learn C for an AVR as opposed to just on your computer, you're really going to need smileymicros book and an Atmel Butterfly.

But I can't help but think if you're just learninf C for the sake of learning C, you should start programming for your computer, then start programming for AVR once you have the basic concepts down.

Last Edited: Wed. Dec 20, 2006 - 02:19 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't think that main() as a function should return anything. Unlike a big computer, the micro program can never terminate (sleep, but never really "end"). As a result, you will often see an endless loop inside main(). I also don't think that main() should contain a return statement, because that implies exiting the function, and you never want to do that with main() in a micro.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

Quote:
Heres the modification's i made and it now compiles error free.

#include

int main()
{
printf("Hello, world!\n");
return 0;
}


Well, not really. I should have given you a big fat warning. Lesson number 1 in C programming: never ignore warnings. In this case the warning was:
Quote:
implicate definition of function 'printf'

This is because the compiler is assuming that you have provided a definition for printf somewhere. You should still be including stdio.h. But even though at this point it will compile correctly, it won't do anything useful. As Julian said, some of the stdio (standard input and output) functions assume that there is a certain infrastructure for it to use. On the AVR, there is no default place to send the output of printf to. In order to use it, you need to provide it. There are instructions in the WinAVR documentation (as well as a thread on this forum) that explains what you need. So that should explain 1, 2 and 5. And 6 is one possible solution as to where to send the output, though you would need to also add either the lcd.c file or lcd.lib file to the project. Which you would need would depend on the implementation of the lcd code.

For 3, if you are using AVR Studio as your IDE, you need to select the correct AVR for the compiler and the simulator separately. If you use the Project Wizard, then you choose the AVR on the second screen (which sets both). If you don't do it that way, then you set the AVR for the compiler under "Project/Configuration Options", and set it for the simulator under "Debug/AVR Simulator Options". If you are using programmers notepad as your IDE, you need to set the AVR in the makefile, and of course you still need to set the proper AVR for the simulator.

As for 4, I am assuming that your first attempt had something else different since it should have been just the warning both times.

Regards,
Steve A.

The Board helps those that help themselves.

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

Stephen,

For a complete beginner I'd have said www.smileymicros.com was a "gentle" way into AVR programming using avr-gcc

Joe Pardue based his beginners guide around the use of the $20 AVR Butterfly. This has several advantages for the beginner

1) it's bloody cheap!

2) there's lot of "toys" on board already

3) it's easy to program (no ISP programmer required)

4) it has an LCD and there are LCD routines in GCC for it which means you *can* get to the printf("Hello World") stage quite quickly

5) it has a level converter for the RS232 so interface to a PC is easy and hence Hello World can go up the cable and appear on Hyperterminal very easily too

6) It's based around the Mega169 which is one of the CPUs supported by the AVR Dragon so you have the potential to cheaply ($50) and easily JTAG debug it with a device you'll then be able to re-use to debug future AVRs. The Dragon is also an easy solution to the ISP programming thing which will be useful for future AVRs too (and even the Butterfly should the need ever arise to ISP program it)

Cliff

PS While all C programmers MUST have a copy of K&R, it is NOT a good beginners guide to C programming. It's more of a reference work.

PPS Couldn't help noticing avr\io.h above - don't fall into the trap of "thinking Windows". In the GNU world path separators are '/' and not '\' so that should have been avr/io.h

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

ka7ehk wrote:
I don't think that main() as a function should return anything. Unlike a big computer, the micro program can never terminate (sleep, but never really "end"). As a result, you will often see an endless loop inside main(). I also don't think that main() should contain a return statement, because that implies exiting the function, and you never want to do that with main() in a micro.

Jim

The return is still mandated by GCC - otherwise you get a nice warning along the lines of "return from function expecting non-void value". You should put in the return 0; to your main routine - but you should ensure that it is never called.

AVR-GCC is patched I believe (WinAVR is, at any rate) to enter an infinite loop once main returns (ignoring the return value). However in a proper AVR program, the main function should not return at all. Despite that, you still need the return statement to keep the compiler happy.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

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

Many thanks
For the constructive advice and all your point's are duly noted ,i already own a butterfly and the smiley adaptor board+ebook so this is where i'll begin my .C learning.

Thank's again
stephen

Codevisionavr & Avrstudio 4.18
Easyavr5A-Jtagicemk1

Call me Pedantic, But not after 9.

if Milk_Brilliant
else Codevision_Avrs==Better

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

abcminiuser wrote:
The return is still mandated by GCC - otherwise you get a nice warning along the lines of "return from function expecting non-void value".

I don't believe the "return" is mandated- I never put it in any of my code, and GCC doesn't complain. I do, however, still declare my main as "int main(void)".

I don't know if this is because my makefile is different than what you normally use, or something else.

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

rjbishop wrote:
I don't believe the "return" is mandated- I never put it in any of my code, and GCC doesn't complain. I do, however, still declare my main as "int main(void)".

I don't know if this is because my makefile is different than what you normally use, or something else.

Well I'll be darned. I just tried it out again and you are correct. I'm positive it used to complain, so perhaps it was patched in the last few WinAVR releases.

I'd still add in the "return 0" to the end, if only for personal preference. However, I suppose it is entirely up to the programmer now.

I do remember some noise recently regarding patching AVR-GCC to accept a void return type for main. I wonder if anything ever came from that.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!