Author Topic: Where to learn C for microcontrollers  (Read 25098 times)

0 Members and 1 Guest are viewing this topic.

Offline mrpsychotic

  • Contributor
  • Posts: 34
Where to learn C for microcontrollers
« on: January 26, 2011, 07:13:29 am »
I am basically sunk when it comes to microcontrollers because I don't know C. Here is my dilemma though:

When I search for C tutorials the results that come up are C tutorials for programming computer software programs, not for microcontrollers. When I look for tutorials for how to code microcontrollers with C, they assume you already know the general C used to code computer software.

My question is, is there a resource that teaches C microcontroller programming that assumes the reader is new to both C and microcontrollers. I dont want to go through the process of learning "general" C for computer programs and then learning the C used in microcontrollers.

I have 3 varieties of microcontroller lying around. I always think I will be able to finally use them but what ends up happening is that I can only use them to the extent that I can extrapolate and merge examples instead of making my own code.

Please help if you know of any resources or have any advice.
 

Offline stan001

  • Contributor
  • Posts: 28
    • Arduino for Beginners
Re: Where to learn C for microcontrollers
« Reply #1 on: January 26, 2011, 07:23:08 am »
I suggest you to Arduino :

http://arduino.cc/en/Tutorial/HomePage

It is a simple version of C adopted from Wiring ( http://wiring.org.co/learning/basics/index.html )

Just hv setup() and loop() only...


 

Offline the_raptor

  • Regular Contributor
  • *
  • Posts: 199
Re: Where to learn C for microcontrollers
« Reply #2 on: January 26, 2011, 08:47:01 am »
There is no such thing as "C for microcontrollers". Every micro is going to have different capability and different methods of accessing that through C.

What you need to learn is:
1) Functional programming language grammar (eg functions, loops, if statements, arithmetic etc).
2) C syntax (how you achieve the above in C).
3) Specific micro grammar and syntax.

Learning C for computer programming is directly applicable to points 1 and 2. The reason you get stuck with trying to code for your micros is because you aren't learning the foundational skills that apply to every functional programming language out there. Learning functional programming and C syntax will enable you, with a bit of familiarisation, to code in nearly every modern language out there.

If you don't want to bother learning how to program you are stuck cutting and pasting stuff into the Arduino IDE.

PS. The actual microcontroller programming is really the easiest bit. All it involves is knowing how to set-up the pins in the way you want, and how to do digital and analog I/O. However if you can't program you are going to have great difficult doing anything useful with those pins. It is like having a hammer, nails, and wood but not knowing how to put them together.
 

Offline Longhair

  • Regular Contributor
  • *
  • Posts: 80
Re: Where to learn C for microcontrollers
« Reply #3 on: January 26, 2011, 02:26:59 pm »
I am in the process of the same thing that you are facing - wanting to learn C for Microcontrollers.

I started reading "Beginner`s Guide to Embedded C Programming by Chuck Hellebuyck" and to follow the book, you will need "PICkit 2 Starter Kit".

I found out early on that having C programming is a good foundation so put the above book and starter kit on hold while I go through a beginners book on C programming. Sometimes you need to take a step back to go forward faster.

 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 9322
  • Country: my
  • reassessing directives...
Re: Where to learn C for microcontrollers
« Reply #4 on: January 26, 2011, 03:40:15 pm »
Sometimes you need to take a step back to go forward faster.
dont count on it. there is no faster way in learning. but moving a step back is a wise decision imho, if you think you cannot go faster.
if something can select, how cant it be intelligent? if something is intelligent, how cant it exist?
 

Offline Excavatoree

  • Frequent Contributor
  • **
  • Posts: 836
  • Country: us
Re: Where to learn C for microcontrollers
« Reply #5 on: January 26, 2011, 03:47:18 pm »
I'm glad someone asked this, because, I hate to admit this...I don't know C at all.

My first micro controller programming was a BASIC stamp, and my first REAL micro controller programming was PIC assembler, which I am still learning.

For my simple project, I believe PIC asm is the appropriate tool, but for more complex work, I believe I should learn C.

To complete the computer programming requirement for my EE degree at the university, I took the computer science programming class, which was Pascal.  The EE offering was Fortran, which, I believe, isn't used much.

I used to write programs in asm for my Z80 computer, a long time ago, but the assembler used 8080 opcodes, and, in fact, the whole machine was hobbled by using software written for the 8080 - thus using only a portion of the Z80's capability.  But I digress.

Back in high school (secondary school for non US folks?)  the electronics department had more money than sense, and bought a heathkit microprocessor trainer, but didn't get the course to go with it.  So, it sat unused.  I wrote a program in ASM for the beginning students to practice Ohm's law calculations.  (this was back when computers were rare in classrooms - 1986 or so) I think it was a 6502, but I could be wrong.

Sorry for the long digression there.





« Last Edit: January 26, 2011, 03:59:13 pm by Excavatoree »
 

Offline the_raptor

  • Regular Contributor
  • *
  • Posts: 199
Re: Where to learn C for microcontrollers
« Reply #6 on: January 26, 2011, 04:17:33 pm »
I hate to admit this...I don't know C at all.

/snip

To complete the computer programming requirement for my EE degree at the university, I took the computer science programming class, which was Pascal. 

So you know all the actually important things about C (if you aren't planning on doing low level PC programming).

All you need to learn to "know C" is some minor syntax differences and the difference symbols.

My first functional programming language was Pascal and I consider it a far superior teaching language than C (BASIC is a horrid abomination that has to be unlearned before people can grasp proper functional programming).

You generally don't need to know the intricacies of the rarer and more bizarre C constructs to do hobbyist coding (C pointers are basically a whole hobby worth of information on their own and something I once knew far too much about).
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 9322
  • Country: my
  • reassessing directives...
Re: Where to learn C for microcontrollers
« Reply #7 on: January 26, 2011, 04:39:54 pm »
BASIC is a horrid abomination that has to be unlearned before people can grasp proper functional programming.
Bullshit! Pascal is shittier than Basic IMHO.  and what do you mean by "proper functional programming"?
if something can select, how cant it be intelligent? if something is intelligent, how cant it exist?
 

alm

  • Guest
Re: Where to learn C for microcontrollers
« Reply #8 on: January 26, 2011, 07:32:31 pm »
BASIC is a horrid abomination that has to be unlearned before people can grasp proper functional programming.
Bullshit! Pascal is shittier than Basic IMHO.  and what do you mean by "proper functional programming"?
I'm with the_raptor on this one, BASIC was a simplified version of FORTRAN so it could be interpreted by small microcomputers, almost all of the structured features were added later, and there are some bad design decisions (like the non-short-circuit behavior of boolean operators, which leads to the ugly AndAlso/OrElse in VB/derivatives, or the lack of differentiation between boolean and bitwise operators). There isn't much of a standard BASIC, and most common PC dialects lack for MCU critical features like bit shift operators, which means every MCU BASIC compiler builder has to invent their own.

Pascal also feels old, but at least it's fairly well designed and enforces more discipline. For teaching, I think of the currently-popular languages, Python is pretty good.

I'm also somewhat confused by 'proper functional programming', I don't think either BASIC or Pascal have functions as first-class citizen, so neither will allow functional programming. Something like Haskell/ML/LISP would be the obvious choice for that. I assume the_raptor meant something like structured programming.
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 13308
  • Country: gb
  • 0999
Re: Where to learn C for microcontrollers
« Reply #9 on: January 26, 2011, 07:40:59 pm »
Learn assembler before learning C. *lights the fuse lol*
 

Offline the_raptor

  • Regular Contributor
  • *
  • Posts: 199
Re: Where to learn C for microcontrollers
« Reply #10 on: January 28, 2011, 01:54:43 am »
BASIC is a horrid abomination that has to be unlearned before people can grasp proper functional programming.
Bullshit! Pascal is shittier than Basic IMHO.  and what do you mean by "proper functional programming"?

I meant structured programming. This is why I shouldn't post late at night when I get my terminology mixed up.

And why do you like BASIC over Pascal? I am talking about real BASIC not VB or later derivatives that attempt to strap structured programming features onto BASIC. BASIC was an attempt to make programming easy by removing structural constraints but in reality it in my opinion makes it harder to do real programming. I know I didn't really grasp programming until we moved from MS-BASIC (on the 386) to Pascal.

Pascal is very similar to C structurally but probably benefits from a bit more verbosity in its symbols which is IMO a good idea in a teaching language. I would also agree with alm that Python is probably a good teaching language in that it enforces layout as a syntactical element. One of my major peeves with C is when "clever" programmers format their code blocks in a way that makes it easy to confuse the scope (and for beginner programmers screwing up the scope is probably the number one gotcha). These days we don't need to play layout games with text files because of memory/screen limits.
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 9322
  • Country: my
  • reassessing directives...
Re: Where to learn C for microcontrollers
« Reply #11 on: January 28, 2011, 02:34:03 am »
now i got your point. maybe i was too young when the very basic BASIC was invented. I was talking newer VB version, so i think there is miscommunication. Why i said Pascal is shittier, because as you said, it resembles close to C languange, except there is a lot more to type, so make me thinking, whats the Pascal use for if there is C/C++ already? ie simpler to type, yet alot more powerful (in "proper functional" as you said). Basic (VB) on the other hand, is very human language friendly, and we can develop Windowed program in a matter of seconds. Which is not available in C. To summarize, IMHO:
1) Basic:  Pro = for easy learning easy development, con = not as powerful (proper functional)
2) C/C++: Pro = the most powerful, fast and "proper functional" high level language (closest to lower level) i've seen, con = need more work to get thing up and working, and not so human friendly.

just a 1.9 cnts.
if something can select, how cant it be intelligent? if something is intelligent, how cant it exist?
 

Offline SuperMiguel

  • Regular Contributor
  • *
  • Posts: 205
 

Offline DrGeoff

  • Frequent Contributor
  • **
  • Posts: 709
  • Country: au
    • AXT Systems
Re: Where to learn C for microcontrollers
« Reply #13 on: January 28, 2011, 04:31:18 am »
Learn to program in C first.
Use ANSI C and get a good understanding of the language on a working platform, programming simple modules or going through some online tutorials. When you have a good understanding of how the language works and how to use it, pick your microcontroller and its compiler and read about the extensions that are added to support the microcontroller, if any. Programming in C on a microcontroller is really the same as programming on any platform. You can even design, write and test a lot of code on a PC or Linux box before compiling for your microcontroller.
Was it really supposed to do that?
 

alm

  • Guest
Re: Where to learn C for microcontrollers
« Reply #14 on: January 28, 2011, 04:41:36 am »
Why i said Pascal is shittier, because as you said, it resembles close to C languange, except there is a lot more to type, so make me thinking, whats the Pascal use for if there is C/C++ already? ie simpler to type, yet alot more powerful (in "proper functional" as you said). Basic (VB) on the other hand, is very human language friendly, and we can develop Windowed program in a matter of seconds.
The funny thing about this is that VB6 has been discontinued for years, and your argument against Pascal is even more true for VB.NET: it's much closer to C# than VB was to C++.

Not sure why developing Windows programs with C would be an issue, it's not like there's any shortage of compilers for that platform, but it wouldn't be my first choice for developing a GUI app, if that's what you mean.

Learn to program in C first.
Use ANSI C and get a good understanding of the language on a working platform, programming simple modules or going through some online tutorials. [...]
Agreed. Not much point in learning the specifics of GUI frameworks or IDEs (eg. Visual C++), just focus on console apps and standard libraries. Embedded compilers are usually more limited in supported standards and features than their PC equivalents.
« Last Edit: January 28, 2011, 04:46:10 am by alm »
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 9322
  • Country: my
  • reassessing directives...
Re: Where to learn C for microcontrollers
« Reply #15 on: January 28, 2011, 11:46:08 am »
The funny thing about this is that VB6 has been discontinued for years, and your argument against Pascal is even more true for VB.NET: it's much closer to C# than VB was to C++.
is it? i'm not entirely sure as i never got into depth with VB.NET and C#. but i kinda agree with you, since last time, i managed to try and install VB.NET. its (the coding style) just look like shit to me, so i stay loyal to VB6.

Not sure why developing Windows programs with C would be an issue, it's not like there's any shortage of compilers for that platform, but it wouldn't be my first choice for developing a GUI app, if that's what you mean.
have you tried? with lines of native Windows API, or even the fancy but confusing MFC? its just alot of hassle. with VB(6), all we do is just new project->compile. and walla! a windowed app. I have a custom made "Windows Library" for my VC++ development which took years to develop eventhough very basic. with VB you dont have to type an alphabet, you just concentrate with your code. But as raptor said, i partly aggree that VB is less functional compared to the mighty C++, but its good enough to make it functional according to our/my need.

Learn to program in C first.
Use ANSI C and get a good understanding of the language on a working platform, programming simple modules or going through some online tutorials. [...]
Agreed. Not much point in learning the specifics of GUI frameworks or IDEs (eg. Visual C++), just focus on console apps and standard libraries. Embedded compilers are usually more limited in supported standards and features than their PC equivalents.
Agreed. once you are firm with what is subroutines, classes, templates for C++, what is standard/non standard/external libraries etc, then you can comfortably upgrade to a more UI specific, or even the a lot of fruits and varieties of mcu's development.

ps: just as every possibly examples/tutorials i've ever seen, everything will start with the "Hello World!"
« Last Edit: January 28, 2011, 11:54:14 am by shafri »
if something can select, how cant it be intelligent? if something is intelligent, how cant it exist?
 

alm

  • Guest
Re: Where to learn C for microcontrollers
« Reply #16 on: January 28, 2011, 07:07:12 pm »
Not sure why developing Windows programs with C would be an issue, it's not like there's any shortage of compilers for that platform, but it wouldn't be my first choice for developing a GUI app, if that's what you mean.
have you tried? with lines of native Windows API, or even the fancy but confusing MFC? its just alot of hassle. with VB(6), all we do is just new project->compile. and walla! a windowed app. I have a custom made "Windows Library" for my VC++ development which took years to develop eventhough very basic. with VB you dont have to type an alphabet, you just concentrate with your code. But as raptor said, i partly aggree that VB is less functional compared to the mighty C++, but its good enough to make it functional according to our/my need.
Why would you need access to the native API, which is (mostly) undocumented and not intended for application programmers? At least use the Win32 API. But you don't really need either to write console apps, which is why I don't understand your issue with writing C programs for Windows. The standard 'Hello World' program with stdio.h will work just fine on Windows. Yes, a GUI app is more hassle, which is why I stated C wouldn't be my first choice for that. But remember that almost all of the software included with Windows is written in C (they may have added some .NET applications in recent versions), many of them have a GUI.

Agreed. once you are firm with what is subroutines, classes, templates for C++, what is standard/non standard/external libraries etc, then you can comfortably upgrade to a more UI specific, or even the a lot of fruits and varieties of mcu's development.
I would probably skip C++ entirely, especially for 8-bit MCU's, and focus just on C. The extra cost in terms of memory and execution speed of OOP is not worth it for small programs, IMHO, and embedded C++ compilers tend to be less developed and rarer.
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 9322
  • Country: my
  • reassessing directives...
Re: Where to learn C for microcontrollers
« Reply #17 on: January 28, 2011, 09:16:13 pm »
i meant Win32 API, sorry. i thought its the same to "native" API. and i meant GUI app, not console app.
if something can select, how cant it be intelligent? if something is intelligent, how cant it exist?
 

Offline Longhair

  • Regular Contributor
  • *
  • Posts: 80
Re: Where to learn C for microcontrollers
« Reply #18 on: January 28, 2011, 11:06:09 pm »
that book any good? http://www.amazon.com/Beginners-Guide-Embedded-Programming-Microcontroller/dp/1438231598/ref=cm_cr_pr_product_top

That is the book I am using now and it starts off alright - I have no programming experience at all - then it gets over my head very fast. What I don't like is how the author keeps making comparisons to BASIC, something that I feel should have been left out completely because that only gets confusing if you have no programming experience at all.

Today I picked up the book and thought I would follow along with the projects even though I don't fully understand them. I will probably go back through the book a couple more times while learning C so a few things will make more sense in the why it is being done instead of the just follow along routine that I am doing now.
 

Offline Slobodan

  • Regular Contributor
  • *
  • Banned!
  • Posts: 159
  • Country: cs
Re: Where to learn C for microcontrollers
« Reply #19 on: February 10, 2011, 09:08:12 am »
Does anybody knows of any good PIC microcontroller programming video tutorial?
If you do, please provide a link. Tanks.
 

Offline adam1213

  • Regular Contributor
  • *
  • Posts: 119
  • Country: au
Re: Where to learn C for microcontrollers
« Reply #20 on: February 10, 2011, 10:39:30 am »
My question is, is there a resource that teaches C microcontroller programming that assumes the reader is new to both C and microcontrollers. I dont want to go through the process of learning "general" C for computer programs and then learning the C used in microcontrollers.

I have 3 varieties of microcontroller lying around. I always think I will be able to finally use them but what ends up happening is that I can only use them to the extent that I can extrapolate and merge examples instead of making my own code.

Focusing on learning c only for microcontrollers will likely involve you modifying code as you stated, eventually learning the syntax. It is possible to learn to write basic programs this way. However there are a lot of things you will probably not learn which are very useful for doing more complex things and writing better and simpler programs.


With microcontrollers it can often be more difficult to debug your program, especially when you start learning to program. With a program on the computer its easier to print things out.
- if you ever get to learning pointers this way (depending on the microcontroller input/outputs might not be interfaced with using pointers) then you are very unlikely to have an OS to teach you what a segmentation fault is.


can you give some examples of programs that you would like to write for the microcontrollers
« Last Edit: February 10, 2011, 10:43:15 am by adam1213 »
 

Offline osmosis321

  • Regular Contributor
  • *
  • Posts: 64
Re: Where to learn C for microcontrollers
« Reply #21 on: February 11, 2011, 02:27:16 am »
There's a book out there called The C Programming Language which is pure C.  If you could get ahold of that or something similar, the rest is just a matter of getting your hands dirty.  Get a PicKit and copy a Hello World program in their MPLAB compiler.
 

Offline Slobodan

  • Regular Contributor
  • *
  • Banned!
  • Posts: 159
  • Country: cs
Re: Where to learn C for microcontrollers
« Reply #22 on: February 13, 2011, 12:19:06 pm »
Well i am not sure that you were addressing to me, but thanks anyway  ;D. I will buy some books from domestic (Serbian) authors. I have already ordered this one: http://www.mikroe.com/eng/products/view/11/book-pic-microcontrollers/ and i plan to order this one: http://www.infoelektronika.net/knjiga/n=pic-c it is called "C language for PIC microcontrollers". This should be enuf, i hope.
« Last Edit: February 13, 2011, 02:25:22 pm by Slobodan »
 

Offline Simon

  • Global Moderator
  • *****
  • Posts: 14038
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Where to learn C for microcontrollers
« Reply #23 on: February 13, 2011, 01:00:44 pm »
The mikroe.com website has a decent range of compilers that are fairly well documented
https://www.simonselectronics.co.uk/shop
Varied stock of test instruments and components including EEVblog gear and Wurth Elektronik Books.
Also, if you want to get ripped off: https://www.ebay.co.uk/usr/simons_electronics?_trksid=p2047675.l2559
 

Offline Bored@Work

  • Super Contributor
  • ***
  • Posts: 3932
  • Country: 00
Re: Where to learn C for microcontrollers
« Reply #24 on: February 13, 2011, 01:09:48 pm »
The mikroe.com website has a decent range of compilers that are fairly well documented
However, the quality of their compilers is arguable.
I delete PMs unread. If you have something to say, say it in public.
For all else: Profile->[Modify Profile]Buddies/Ignore List->Edit Ignore List
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf