Which route to go for coding embedded C?

Thread Starter

StrongPenguin

Joined Jun 9, 2018
287
I really wanna learn a programming language (for some odd reason..), but I am not that interested in software development (yet..?) However, I am interested in embedded coding, and here, C seems to be the language most people go to.

So I need (and want) to learn C, but for the purpose of fiddeling with embedded stuff (Microcontrollers, mainly..) I know very little about this stuff, so all help is more than welcome.

The question In my embedded journey, do I "just" learn C like any other self taught software developer would, by buying a book on C and start coding small programs? Or are there books on embedded programming for beginners? I actually found one, but I have no idea on the level. It had advanced in the title, so..
 

dl324

Joined Mar 30, 2015
12,681
I'd start by learning C because that's what "embedded C" is based on; with "extensions" to handle non-standard stuff.

I learned from "The C Programming Language" written by the people (K&R) who developed the language.
 
Last edited:

Thread Starter

StrongPenguin

Joined Jun 9, 2018
287
Ok, very interesting. But I am under the impression that that book is for intermediate folks, and not very beginner friendly at all.

I don't know any programming. At all. Is it still a good start?
 

dl324

Joined Mar 30, 2015
12,681
Ok, very interesting. But I am under the impression that that book is for intermediate folks, and not very beginner friendly at all.

I don't know any programming. At all. Is it still a good start?
Just about all programming language books assume you already know how to program in some other language.

Look for a programming for beginners book, preferably for the language you want to learn.
 

ebp

Joined Feb 8, 2018
2,332
C is useful, very popular for firmware development and annoyingly perverse in a number of ways. There are lots of nasty little hooks to snag the bigger and even experienced users - things that are wrong in terms of intended function but perfectly valid in terms of C syntax. Learning about some of these very early on can be a big time saver in debugging.

example of really common (at least for me) issue:
x = 3;
if x = 5 ...
The if statement will return true. This is because it evaluates x = 5 which is an assignment operation, which can be done so it returns tue

x = 3;
if 3 = x ...
This will generate an error at compile time because the assignment can't be done

x = 3;
if x == 5 ...
the if statement returns false - the double equal sign is the equality comparison operator

Until you get used to this sort of thing, it is easy to miss when you are staring at your code til your eyes bleed, trying to figure out why it doesn't work - or again, it is for me.

C compilers are rather variable in how good they are. I can't count the number of hours I've spent looking at an assembler listing from a C composter to figure out what it has actually done.

Learning to write assembler is often very useful. It can be very difficult to get clean compact code for some things in C and the ability to write some small routines in assembler can improve performance considerably. For example, I had a real time clock calendar chip that returned the bits for something in the opposite order of what any sensible thing would. C code to reverse the bits was simple and unbelievably inefficient. I wrote just a few lines of assembler that did the job vastly more efficiently in both time and code space.
 

WBahn

Joined Mar 31, 2012
26,398
I might suggest another alternative. Your main intentions seems to be more about programming microcontrollers rather than writing programs. So a much more profitable place for you to start might be working in assembly with very simple microcontrollers, like the more basic PIC lines from Microchip. It wasn't that long ago that nearly all embedded code was assembly because the available microcontrollers were too resource starved for much else and a LOT of embedded code is still done in assembly.

It's been a number of years since I worked with them so I don't know what the bare-bones families offer today, but the "entry level" stuff has a very limited instruction set and a fairly small number of peripherals, which makes learning how to use everything the chip has to offer pretty tractable. You will be amazed what you can do with such a chip and learning how to do so will teach you a LOT more about solving problems with a microcontroller than starting off with C and it will make it so that the C code you write later on will be much better because you will have a strong feel for what has to happen under the hood.
 

ebp

Joined Feb 8, 2018
2,332
I've done or had done or been associated with doing of embedded code for a long time and haven't seen assembler used for anything but small bits of code that needed to be optimized for more than 20 years. The last time I wrote a program entirely in assembler was for the Intel 4040. C compilers for micros generally produce code that is very good as long as the programmer knows what they are doing (HUGE caveat - lots of "self taught" programmers are terrible! Among other things, C does seem to encourage using floating point when there is zero merit to it.). I've used C for a number of products and projects using PIC12 family without any need to fudge things to fit.
 

WBahn

Joined Mar 31, 2012
26,398
I've done or had done or been associated with doing of embedded code for a long time and haven't seen assembler used for anything but small bits of code that needed to be optimized for more than 20 years.
Upon reflection, I just realized that "not that long ago", for me, is 25 years. Sigh.:(
 

Thread Starter

StrongPenguin

Joined Jun 9, 2018
287
@dl324 And now I get why I usually have the `Am I missing something..’ feeling when I look through programming books. C Language book will have to wait a bit.

@MrChips I’ve always thought the tutorials online to be very shallow, but that one seems pretty good.

@shteii01 Interesting, really makes me wanna play more with my Arduino, although I always thought it was purely C++..?

@ebp Oh, thanks for the heads up :)

@WBahn I will just stick with the latest knowlege :p
 

shteii01

Joined Feb 19, 2010
4,644
@dl324 And now I get why I usually have the `Am I missing something..’ feeling when I look through programming books. C Language book will have to wait a bit.

@MrChips I’ve always thought the tutorials online to be very shallow, but that one seems pretty good.

@shteii01 Interesting, really makes me wanna play more with my Arduino, although I always thought it was purely C++..?

@ebp Oh, thanks for the heads up :)

@WBahn I will just stick with the latest knowlege :p
It is C-like and it has elements of C++. Or you can turn it the other way around, it is C++ with elements of C. I just use it as C, and if I ever run into stuff that I don't know, then I use internetz to find where someone else has done what I want and then modify it for my needs.
In the past I have found Adafruit tutorials to be of great use, and Arduino Playground website usually has just about everything you can possibly use.
 

dl324

Joined Mar 30, 2015
12,681
Interesting, really makes me wanna play more with my Arduino, although I always thought it was purely C++..?
C came first, the object orientation of C++ was added around it.

If you're not doing something that requires a real time operating system, I'd consider something like RPi. For years I had been thinking about getting into microcontrollers. They were all proprietary and I couldn't decide which one I wanted to learn. By the time I actually enough discretionary time to do anything, RPi and other Linux based "computers" were available; so I went that route. I like being able to use the skills I spent a career learning for my hobby.

I built a single digit (so far) LED matrix clock and I'm using a single board computer called C.H.I.P. that runs Debian. It has a single core 1 GHz ARM processor, 512MB memory, 8 GB FLASH, WiFi, Bluetooth, battery charger, and enough I/O's to do a lot of experimenting; and it only cost $9 (plus shipping).

Since I can't stop the OS from interrupting my clock program, it freezes occasionally. That doesn't bother me because since it's a Linux computer running on the network, it can also update the time when it boots; for me, the benefit of running Linux and being able to use the software skills I spent decades acquiring offsets any downside.
 

Thread Starter

StrongPenguin

Joined Jun 9, 2018
287
@shteii01 Ok, I didn't know they were so related. I bought an Uno some time ago, but newer got around to play with it. I bought a whole box of goodies, like a bit kit with various sensors, step motor and switches. Going through all the exercises, one is bound to learn something.

I'm just under the impression that Arduino is a lot of Plug N Play, since the makers brag about kids being able to work with it. I am not dissing Arduino, since I know of a lot of cool projects that are controlled with Arduino. It's just that the way I'm wired, I need to know what's going on under the hood, not necessarily be an under the hood expert, but at least a decent grasp of it. I'm going to start a thread on a project I have in mind, which I think will have a lot of Uno in it.

@dl324 I don't think I fully understand what they have to do with each other, the CHIP and the clock. What is the purpose of those computers? Can you Google stuff with it, and use it as a "regular" laptop?

EDIT: Just read that you can do all that stuff. Pretty cool. I'm still not sure when I would see myself needing one. I find this interesting, but I don't know if it's for the right reason.
 

dl324

Joined Mar 30, 2015
12,681
I don't think I fully understand what they have to do with each other, the CHIP and the clock. What is the purpose of those computers? Can you Google stuff with it, and use it as a "regular" laptop?

EDIT: Just read that you can do all that stuff. Pretty cool. I'm still not sure when I would see myself needing one. I find this interesting, but I don't know if it's for the right reason.
If you get a Linux based SBC, you can program in multiple languages. C, C++, Pascal, Fortran, PERL, Python, Ruby, ... The advantage of an SBC over a laptop is that they're easier to interface to circuits.

They can do many of the same things as a "regular" computer (browsing, word processing, image editing), but the one I'm using is under powered compared to any "real" computer. But as a software development platform, they're fine for what I'm doing.
 

shteii01

Joined Feb 19, 2010
4,644
@shteii01 Ok, I didn't know they were so related. I bought an Uno some time ago, but newer got around to play with it. I bought a whole box of goodies, like a bit kit with various sensors, step motor and switches. Going through all the exercises, one is bound to learn something.

I'm just under the impression that Arduino is a lot of Plug N Play, since the makers brag about kids being able to work with it. I am not dissing Arduino, since I know of a lot of cool projects that are controlled with Arduino. It's just that the way I'm wired, I need to know what's going on under the hood, not necessarily be an under the hood expert, but at least a decent grasp of it. I'm going to start a thread on a project I have in mind, which I think will have a lot of Uno in it.
I understand. You are not wrong.
However, during my college education I had classes:
- in assembly language
- in C
- in C++

I want to make sure you understand that I AM NOT a programmer, never been one, never will be. Those languages were just part of my curriculum. I took the class, I got passing grade, I moved on. So. With that in mind... I have never done anything in C++ on my own. I use C extensively when doing Arduino stuff. The real nuts and bolts are done with assembly, I have done a little bit, but my first choice is to do stuff in C and then, once everything is running, rewrite it in assembly for optimization.

If I have to draw parallels. C is like buying a sub at a store, it is ready, all you have to do is pick the toppings, carry it to the register, pay for it and eat it. Assembly is like buying all the individual parts of the sub, cutting and slicing the meats and cheeses in your kitchen, applying the souses, putting the sub together at the kitchen table, then sitting down and eating it. Notice... I did not even mention C++.
 

WBahn

Joined Mar 31, 2012
26,398
It's just that the way I'm wired, I need to know what's going on under the hood, not necessarily be an under the hood expert, but at least a decent grasp of it.
It sounds like a fun and valuable project for you to undertake would be the Nand-2-Tetris project. It will give you a very good handle on what goes on under the hood from the gate level right up to the operating system level -- though all of it at a pretty narrow focus to make it manageable. But with that background I think you will much better appreciate how most high-level embedded code works and the kind of processing overhead that is involved in supporting it.
 

MrChips

Joined Oct 2, 2009
23,081
If you wish to get under the hood I would recommend the TI MSP-EXP430G2 Launchpad. I would also recommend starting with ASM.

I have created an MSP430 blog but the examples are written for C.
I could create a similar blog with the focus on ASM for you.
 

Thread Starter

StrongPenguin

Joined Jun 9, 2018
287
@WBahn I am also in the middle of building an 8 bit SAP-1 computer, which also helps me a lot with understanding (and appreciating!) computers better. But you are the second person to suggest Nand2Tetris, so I will definitely give it a look.

@MrChips What I meant by under the hood is I want to understand why the Arduino (or others like that) is so much easier to operate, than just buying a regular microcontrol chip and a programmer. Whats under the abstraction blanket of the various new types of controllers, that make them so relatively easy to work with? That's what I'm interested in, not particularity the ASM language (unless ASM is what is under the blanket..)

However, I would like a link to the blog, so I can do some reading about the TI controller.
 
Last edited:

WBahn

Joined Mar 31, 2012
26,398
@WBahn I am also in the middle of building an 8 bit SAP-1 computer, which also helps me a lot with understanding (and appreciating!) computers better. But you are the second person to suggest Nand2Tetris, so I will definitely give it a look.

@MrChips What I meant by under the hood is I want to understand why the Arduino (or others like that) is so much easier to operate, than just buying a regular microcontrol chip and a programmer. Whats under the abstraction blanket of the various new types of controllers, that make them so relatively easy to work with? That's what I'm interested in, not particularity the ASM language (unless ASM is what is under the blanket..)
The Nand2Tetris project will REALLY pull away that abstraction blanket for you!

You will see how you can write a program in a high-level, object-oriented language and use a bunch of high level functions to do string and array operations, keyboard input, screen text output, screen graphics, basic math operations such as multiplication, division, and square root, dynamic memory allocation, recursion, and a bunch of other stuff that we take for granted all on a processor that has just 18 instructions and whose interaction with the user is via a single memory-mapped input address for the keyboard that simply contains the code of the presently pressed key, and a memory-mapped block of memory for the screen. You'll know exactly how all of this works because YOU will have designed and implemented the entire computer using only 2-input NAND gates and D-type flip flops, YOU will have written the assembler, YOU will have written the stack-oriented virtual machine, YOU will have written the compiler, and YOU will have written the operating system libraries that make it all happen. It really is an amazing and extremely well-scoped and well-crafted project (actually a series of twelve projects that walk you from one end to the other).

You might see if you can translate the project to your SAP-1 -- that might be a lot of fun for you.
 
Top