Where to learn C for microcontrollers - Page 1
Forum section: electronics / beginners

author: mrpsychotic
author_rank: Contributor
timestamp: January 26, 2011, 07:13:29 am
content: 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.

author: stan001
author_rank: Contributor
timestamp: January 26, 2011, 07:23:08 am
content: I suggest you to Arduino :http://arduino.cc/en/Tutorial/HomePageIt is a simple version of C adopted from Wiring (http://wiring.org.co/learning/basics/index.html)Just hv setup() and loop() only...

author: the_raptor
author_rank: Regular Contributor
timestamp: January 26, 2011, 08:47:01 am
content: 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 isdirectly applicableto 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.

author: Longhair
author_rank: Regular Contributor
timestamp: January 26, 2011, 02:26:59 pm
content: 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.

author: Mechatrommer
author_rank: Super Contributor
timestamp: January 26, 2011, 03:40:15 pm
content: Quote from: Longhair on January 26, 2011, 02:26:59 pmdont 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.

author: Excavatoree
author_rank: Frequent Contributor
timestamp: January 26, 2011, 03:47:18 pm
content: 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.

author: the_raptor
author_rank: Regular Contributor
timestamp: January 26, 2011, 04:17:33 pm
content: Quote from: Excavatoree on January 26, 2011, 03:47:18 pmSo 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 superiorteachinglanguage 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).

author: Mechatrommer
author_rank: Super Contributor
timestamp: January 26, 2011, 04:39:54 pm
content: Quote from: the_raptor on January 26, 2011, 04:17:33 pmBullshit! Pascal is shittier than Basic IMHO. and what do you mean by "proper functional programming"?

author: Unknown
author_rank: Unknown
timestamp: January 26, 2011, 07:32:31 pm
content: Quote from: shafri on January 26, 2011, 04:39:54 pmI'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.

author: Zero999
author_rank: Super Contributor
timestamp: January 26, 2011, 07:40:59 pm
content: Learn assembler before learning C. *lights the fuse lol*

author: the_raptor
author_rank: Regular Contributor
timestamp: January 28, 2011, 01:54:43 am
content: Quote from: shafri on January 26, 2011, 04:39:54 pmI 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.

author: Mechatrommer
author_rank: Super Contributor
timestamp: January 28, 2011, 02:34:03 am
content: 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.

author: SuperMiguel
author_rank: Regular Contributor
timestamp: January 28, 2011, 04:09:15 am
content: that book any good?http://www.amazon.com/Beginners-Guide-Embedded-Programming-Microcontroller/dp/1438231598/ref=cm_cr_pr_product_top

author: DrGeoff
author_rank: Frequent Contributor
timestamp: January 28, 2011, 04:31:18 am
content: 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.

author: Unknown
author_rank: Unknown
timestamp: January 28, 2011, 04:41:36 am
content: Quote from: shafri on January 28, 2011, 02:34:03 amThe 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.Quote from: DrGeoff on January 28, 2011, 04:31:18 amAgreed. 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.

author: Mechatrommer
author_rank: Super Contributor
timestamp: January 28, 2011, 11:46:08 am
content: Quote from: alm on January 28, 2011, 04:41:36 amis 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.Quote from: alm on January 28, 2011, 04:41:36 amhave 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.Quote from: alm on January 28, 2011, 04:41:36 amAgreed. 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!"

author: Unknown
author_rank: Unknown
timestamp: January 28, 2011, 07:07:12 pm
content: Quote from: shafri on January 28, 2011, 11:46:08 amWhy 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.Quote from: shafri on January 28, 2011, 11:46:08 amI 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.

author: Mechatrommer
author_rank: Super Contributor
timestamp: January 28, 2011, 09:16:13 pm
content: i meant Win32 API, sorry. i thought its the same to "native" API. and i meant GUI app, not console app.

author: Longhair
author_rank: Regular Contributor
timestamp: January 28, 2011, 11:06:09 pm
content: Quote from: SuperMiguel on January 28, 2011, 04:09:15 amThat 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.

author: Slobodan
author_rank: Regular Contributor
timestamp: February 10, 2011, 09:08:12 am
content: Does anybody knows of any good PIC microcontroller programming video tutorial?If you do, please provide a link. Tanks.

author: adam1213
author_rank: Regular Contributor
timestamp: February 10, 2011, 10:39:30 am
content: Quote from: mrpsychotic on January 26, 2011, 07:13:29 amFocusing 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

author: osmosis321
author_rank: Regular Contributor
timestamp: February 11, 2011, 02:27:16 am
content: 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.

author: Slobodan
author_rank: Regular Contributor
timestamp: February 13, 2011, 12:19:06 pm
content: Well i am not sure that you were addressing to me, but thanks anyway. 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-cit is called "C language for PIC microcontrollers". This should be enuf, i hope.

author: Simon
author_rank: Unknown
timestamp: February 13, 2011, 01:00:44 pm
content: The mikroe.com website has a decent range of compilers that are fairly well documented

author: Bored@Work
author_rank: Super Contributor
timestamp: February 13, 2011, 01:09:48 pm
content: Quote from: Simon on February 13, 2011, 01:00:44 pmHowever, the quality of their compilers is arguable.

author: Simon
author_rank: Unknown
timestamp: February 13, 2011, 01:14:18 pm
content: Quote from: BoredAtWork on February 13, 2011, 01:09:48 pmoh ? maybe not worth me getting into their version then, I notice their english leaves a bit to be desired. Is there a better well explained compiler out there ?

author: Slobodan
author_rank: Regular Contributor
timestamp: February 13, 2011, 02:24:17 pm
content: Quote from: BoredAtWork on February 13, 2011, 01:09:48 pmI dont know what quality it is, but i downloaded it and it looks nice but a free version is limited to only 2KB of code.

author: Simon
author_rank: Unknown
timestamp: February 13, 2011, 03:31:13 pm
content: I use their basic compiler and it's ok but then I do real basic, basic (pun intended) I was told that it did have some issue but that their C compiler is better

author: Lance
author_rank: Frequent Contributor
timestamp: February 13, 2011, 05:56:59 pm
content: If you need a C compliler why not use gcc? Or cygwin? I've also heard great things about DevC++ for windows. For the stuff you'll be doing there's pretty much no difference between C and C++. C++ has a ton of stuff that C doesn't, but you probably won't need to learn those. I never did anything complicated in the course I took at school, and the only big differences I found were not being able to pass variables by reference, and having to use printf instead of cout.Of course "complicated' is a subjective term. I pretty much walked through that class playing games during the lectures and learning the stuff myself from books.