Society of Robots - Robot Forum

Software => Software => Topic started by: ed1380 on January 04, 2008, 02:58:45 AM

Title: leaning C
Post by: ed1380 on January 04, 2008, 02:58:45 AM
as a new year resolution i want to learn C and build a robot from scratch by the end of the year.  :D

a member from the forum said they learned by reading "The C programming language"

i started reading it and it says that it is written for people with basic knowledge in variables, functions.
words like kernel, registry scare me.  :'(

I also found this site. http://www.imada.sdu.dk/~svalle/courses/dm14-2005/mirror/c/

1990's is that too old?

searching the forum for "program c" and "learn c" didnt help much

I was wondering if anyone knew if there was any interactive tutorials/courses online that might be better, or is the book/website the best?



and where do i program the code to for practice? do i open ms-dos or something?


thanks
Title: Re: leaning C
Post by: Rebelgium on January 04, 2008, 05:02:59 AM
search google for C tutorials, and download a C developers program, I recommend "eclipse".
Title: Re: leaning C
Post by: GUmeR on January 04, 2008, 07:45:22 AM
1990 isn't to old. C language is old. Very old.

You can take one of two approaches:

1. Learn C first. Get compiler (Eclipse will be very good). Start writing code. Read book or two, check if book tells the truth :). Don't learn advanced C. You will most probably don't need it just right now. Go to #2 then.

2. Don't learn C. Buy microcontroler (ATmega8?), programmer (STK200/300?), build very simple circuit, install AVRStudio, paste some ready code, compile and upload it to microcontroller, see if works. Then start modifying code with help of some good book. Useful links for this approach:
- How to build circuit: http://www.sparkfun.com/commerce/present.php?p=BEE-1-PowerSupply
- AVR forum, lots of tutorials: http://www.avrfreaks.net/

Of course you don't have to choose AVR, there are many good microcontrollers out there.
Title: Re: leaning C
Post by: ed1380 on January 04, 2008, 04:06:13 PM
lol I'd be doing 2

I have 3 atmega8. 1 serial dongle programmer. and 1 parralel port programmer i made

I already have a development board ($50 robot)

but i was never able to get the danng thing programmed



i have an avr butteryfly so i'll try with that.

how much do you recommend doing #2?
IMO it doesnt seam like I'd actually learn how to code
Title: Re: leaning C
Post by: hazzer123 on January 04, 2008, 04:30:22 PM
Was the book you mentioned  "The C Programming Language" by Kernighan and Ritchie? - LINK (http://www.amazon.co.uk/C-Programming-Language-2nd/dp/0131103628)
Its widely thought of as the best book out there, although i read it with a little prior experience, so i didn't have any trouble with it.

My very first programming book was SAMs teach yourself C in 21 days. Here is a LINK (http://lib.daemon.am/Books/C/) to that book, you might find it a bit more friendly to a complete beginner.

I briefly checked out that book and it seems to define a function very well -
Quote
A function is an independent section of program code that performs a certain task and has been assigned a name. By referencing a function's name, your program can execute the code in the function. The program also can send information, called arguments, to the function, and the function can return information to the main part of the program.

I really do recommend using these books to learn C. You will progress really quickly, learn great habits, and experience good coding.
I read the first couple of chapters of that SAMs teach yourself book, missing out on the delights of pointers and structures, and went off programming little bits in C for PICs. About a month ago, i worked through "the c programming language" and was astonished at how much more efficient i could make my programs.

Hazzer


Title: Re: leaning C
Post by: GUmeR on January 04, 2008, 05:03:22 PM
In my opinion You will learn new things by example, practice and theory. Example from good tutorial. Theory from book. Coding is difficult. There is no easy way.

If you don't know what variable, operator, method, main, bit, byte, bit operation, method parameter is. Well... read the book. But check every word of it. Book says  '+' adds thing. Check it. If '+' doesn't add thing. You made a mistake. Check again. In Eclipse you can write output to console and see 3+4 = 7. In microcontroller it's not so easy. You don’t have screen to show output :P You have to use diodes :P (working code example shows how). That’s it.

Try both #1 and #2 at the same time. Make step in Eclipe. Repeat it on real life microcontroller.
Title: Re: leaning C
Post by: ed1380 on January 04, 2008, 05:36:45 PM
when programming mcu's you need to define teh pins and stuff for the MCU, right? (for example admin's makefile)

where would i learn about that? I'm guessing it wont be taught in a C book
Title: Re: leaning C
Post by: hazzer123 on January 04, 2008, 06:05:29 PM
The makefile on the $50 robot tutorial is either a standard makefile, that you can just copy into your project directory before compiling, or a computer generated one made by his AVR Studio.

When coding for an MCU you generally 'include' a header file. This file is effectively pasted to the top of your code. It contains all of the pin and register definitions that you need.

For example in Admins 'Photovore_v1.c' he  wrote the line
Code: [Select]
#include "SoR_Utils.h"This means the contents of SoR_Utils.h are placed in the top of Photovore_v1.c when compiling.

The 'SoR_Utils.h' file itself has header files included in it -
Code: [Select]
#include <avr/io.h>     // include I/O definitions (port names, pin names, etc)
This is where all the pins and registers are named and defined, you don't have to worry about it. Its allready been done for you.
Title: Re: leaning C
Post by: Admin on January 04, 2008, 06:17:06 PM
Quote
In microcontroller it's not so easy. You don’t have screen to show output
True, unless you use the UART with your PC:
http://www.societyofrobots.com/microcontroller_uart_50_robot.shtml
http://www.societyofrobots.com/microcontroller_uart.shtml

This lets you output text, such as sensor values, error checking stuff, etc.

Highly recommended everyone do this if they make a robot!!! Makes debugging a LOT easier . . .
Title: Re: leaning C
Post by: ed1380 on January 04, 2008, 06:51:25 PM
thanks for all the help and great links guys   :D


just a curious Q. since the avr butterfly comes with a lcd screen, can you do uart to the lcd screen.

or would that be called something else? but still show various sensor data
Title: Re: leaning C
Post by: hazzer123 on January 04, 2008, 07:07:29 PM
UART stands for Universal Asynchronous Receiver Transmitter. Its something thats inside a MCU that allows the MCU to communicated with other electronics, be it your computers serial port or another MCU.

What Admin mean by use the UART with your PC, is transmit ASCII characters to your computer using the UART on the MCU you have, via a serial port in the computer. This text would be displayed in the Hyperterminal program on windows. Its a way of seeing what the MCU is up to.

The LCD screen on the AVR butterfly doesn't communicate to the MCU through the UART. I'm guessing it uses a parallelmethod of communication since wikipedia says
Quote
The LCD interface consumes many of the I/O pins.
and UART communication only uses 2 pins.

Hazzer
Title: Re: leaning C
Post by: ed1380 on January 04, 2008, 08:07:30 PM
ahhh. thanks.


the programming gods are really hating me. i cant even do a hello world .exe given a tutorial and a source code

here are some problems.

no compile button. so i used build all
no toolkits supported, so i can only do makefiles.
if i try to bypass that, it'll give me a bunch of errors (pic 3)

so none of the toolkits work on my system. now what? reinstal xp?
Title: Re: leaning C
Post by: hazzer123 on January 04, 2008, 08:12:25 PM
sorry mate i don't know what to make of that, I've always used visual c to code for windows, or MPLAB to code for MCUs, so I'm not familiar with what your seeing.

Whats the link for the tutorial you are using?

hazzer
Title: Re: leaning C
Post by: ed1380 on January 04, 2008, 08:24:35 PM
visual c. is that the same as visual c++

cause i might try that instead

heres teh link. i think you gave it to me http://lib.daemon.am/Books/C/ i used it for the genereal stuff and the source code although eclipse comes with its own hello world tutorial in C, but again no toolkits to use it
Title: Re: leaning C
Post by: Admin on January 04, 2008, 10:39:27 PM
Quote
The LCD screen on the AVR butterfly doesn't communicate to the MCU through the UART. I'm guessing it uses a parallelmethod of communication since wikipedia says
Quote
The LCD interface consumes many of the I/O pins.
and UART communication only uses 2 pins.
Most LCDs today use the UART, so yeap you can normally use it. However, it appears the avr butterfly does not use the uart for the LCD . . .


As for programming in C . . . try this compiler:
http://www.bloodshed.net/download.html

And my code to do the wavefront algorithm simulation (http://www.societyofrobots.com/programming_wavefront.shtml) using that compiler:
http://www.societyofrobots.com/downloads/wave_front_simulation_software.zip

It should make sense if you stare at it for awhile . . . its well commented code . . .

First just try compiling the code without changing anything. Then make changes here and there to see how the output changes.
Title: Re: leaning C
Post by: paulstreats on January 04, 2008, 11:20:18 PM
I know its quite a late reply, but my thoughts are to just learn the basics of C.

The reason is that any advanced levels of C work for computers but not necessarily from mcu compilers. In the same vein it is likely that you will learn a lot from the BASIC languages and then use this knowledge for C development. A lot of C type things in books specialise in graphics and sound and computer i/0 - you dont actually need these and they are dependant on the computer platform that you are using. For MCU's it is only the very basic structures of the language.

If you want a recommendation, I think you should look at existing codes for your chosen mcu and try to understand it and reverse engineer it to an extent. Any problems come to the forum and i'm sure we'll all help


PS it is worth learning a computer language like C fully because it will give you an all round knowledge. By just working off of other people examples it may take longer to learn the basics - its a funny circle to get into (because computers are at such a high level and mcu's are working on such a low level)
Title: Re: leaning C
Post by: ed1380 on January 05, 2008, 09:21:46 AM
Quote
The LCD screen on the AVR butterfly doesn't communicate to the MCU through the UART. I'm guessing it uses a parallelmethod of communication since wikipedia says
Quote
The LCD interface consumes many of the I/O pins.
and UART communication only uses 2 pins.
Most LCDs today use the UART, so yeap you can normally use it. However, it appears the avr butterfly does not use the uart for the LCD . . .


As for programming in C . . . try this compiler:
http://www.bloodshed.net/download.html

And my code to do the wavefront algorithm simulation (http://www.societyofrobots.com/programming_wavefront.shtml) using that compiler:
http://www.societyofrobots.com/downloads/wave_front_simulation_software.zip

It should make sense if you stare at it for awhile . . . its well commented code . . .

First just try compiling the code without changing anything. Then make changes here and there to see how the output changes.
o wow. i was gonna download that compiler, but had to go to sleep  :D

 admin thats a pretty cool code. but I'm gona stick with hello world for now

im gonna try to find out a way to program my $50 robot and play around with it. maybe even someday put the wavefront code in it  ;D
Title: Re: leaning C
Post by: Admin on January 05, 2008, 10:30:12 AM
Quote
admin thats a pretty cool code. but I'm gona stick with hello world for now
seriously, just try and compile/run it.

then add printf("Hello World"); in it ;)
Title: Re: leaning C
Post by: ed1380 on January 05, 2008, 11:31:46 PM
i will once i get teh $50 robot up and running


with dev c++ i was able to compile and link the hello world, but couldnt execute it in xp nor vista nor cmd  :'( it just didnt do nothing

since I'm gonna be useing this mainly for robots, wouldnt it be better getting avr studio?
Title: Re: leaning C
Post by: Admin on January 06, 2008, 09:50:45 AM
Quote
since I'm gonna be useing this mainly for robots, wouldnt it be better getting avr studio?
dev c++ can't compile for microcontrollers (at least to my knowledge).

you must use gcc, which is what AVR Studio uses.
Title: Re: leaning C
Post by: ed1380 on January 06, 2008, 10:01:37 AM
Quote
since I'm gonna be useing this mainly for robots, wouldnt it be better getting avr studio?
dev c++ can't compile for microcontrollers (at least to my knowledge).

you must use gcc, which is what AVR Studio uses.
a. thanks. guess I'll get that too.

do you have any idea why the hello.exe aint working? anybody else?
Title: Re: leaning C
Post by: Admin on January 06, 2008, 10:09:05 AM
what error do you get?

does a command prompt open and close really quick?

code? ;)
Title: Re: leaning C
Post by: Dr_Worm on January 06, 2008, 06:20:19 PM
If you're using that sams book then I think your using this code right?

#include <stdio.h>

main()
{
        printf("This is a C program\n");
}

what I think you need is

#include <stdio.h>

int main (void)
{
printf ("hello world\n");

return 0;
}
Title: Re: leaning C
Post by: ed1380 on January 06, 2008, 09:08:37 PM
hmm doctor.

at first your code didnt compile due to some makefile error. then i restarted teh ide and both codes compiled and using cmd i got both to work

i guess i can move on to day 2  ;D

thanks everyone
Title: Re: leaning C
Post by: jmain on January 07, 2008, 02:14:20 AM
A Sorry for the late reply and 1st post and all that.

 "The C programming language" by K&R is a good book but is at
quite a high level and although you can learn a huge amount
it's not related to microcontrollers at a low level.

as paulstreats said

Quote
The reason is that any advanced levels of C work for computers but not necessarily from mcu compilers. In the same vein it is likely that you will learn a lot from the BASIC languages and then use this knowledge for C development. A lot of C type things in books specialise in graphics and sound and computer i/0 - you dont actually need these and they are dependant on the computer platform that you are using. For MCU's it is only the very basic structures of the language.

Absolutely right.

There are many compilers with free trial options such

as Sourceboost, MikroC plus about 5 others

one I am looking at now is SDCC (Small Device C Compiler) on
sourceforge which is a free open source compiler for microcontrollers.

This is the best way to start with microcontroller C as they have example programs
built into the install (not SDCC) - you'll have enough problems getting each hardware
component setup correctly anyway:

correct i.e. PC Compiler/ Hex programmer software / Programming hardware / Dev board

John Main
Title: Re: leaning C
Post by: Bubbles on January 16, 2008, 04:46:33 AM
i will once i get teh $50 robot up and running


with dev c++ i was able to compile and link the hello world, but couldnt execute it in xp nor vista nor cmd  :'( it just didnt do nothing

if by didn't do nothing you mean that you couldn't see anything come up i would suggest adding a segment of code just before 'return 0;' which says: 'system("pause");' (this may not be exact as i am taking this from my c++ days however i am pretty sure it will)
Title: Re: leaning C
Post by: ed1380 on January 16, 2008, 06:35:18 AM
if you open it in cmd you see it. when i posted that it wouldnt open at all. now i know you can do "new>project>console application"
before i was doing window application
Title: Re: leaning C
Post by: Bubbles on January 16, 2008, 06:01:50 PM
lol  ;D that would definitely account for some weird stuff goin' on