# Programming Microcontrollers - C vs Machine Code

1. Apr 1, 2014

### mrmjp

What is the most commonly used programming language for microcontrollers?

I have done programming in machine code and it is a pain. I will get better at it if I have to, but would rather use a high level language like C.

I hear C is much easier to write programs with than machine code, but C is less efficient, or is it all minimal in the end.

Wondering what people who actually program microcontrollers for a living use code wise, or does it just depend on what the situation calls for.

2. Apr 1, 2014

### Staff: Mentor

By "machine code", I assume you mean assembly code, right?

I write in both C and assembly. As you say, it depends on the situation. Sometimes I write in a mix -- maybe use assembly for my ISRs and C for the rest. Assembly is hard to beat for the fastest, most deterministic execution of what you want to do. But as you say, it is harder to write and maintain, so I'll do as much in C as possible.

3. Apr 1, 2014

### mrmjp

Yes, assembly code is what I ment.

4. Apr 1, 2014

### mjhilger

I have done software in both as well. I personally like C/C++. I mainly use Microchip embedded controllers and most of the libraries they provide are intended to use C/C++. Microchip claims that their paid versions of their compilers optimize almost as well as well written assembly code. I have been to their Master Conference and all the classes that demonstrate or utilize their code is using C/C++.
While I have not done so yet, ARM software that I have seen is mainly in C/C++ also. The main advantage when RISC processors were introduced was that the compilers would be able to do a better job of allowing the developer to write in a higher level language like C/C++ and produce well optimized code. With pipeline and multi-million transistor CPU's RISC does not really show much advantage these days. And the $2 embedded parts provide good speed and execution per clock. Most developers I know write mainly in C/C++ and if it is working too slowly or needs critical timing or such, drop that routine into assembly. All that being said; I highly recommend that you learn assembly and write at least a small program, preferably using interrupts also, to gain a good working knowledge of what is taking place and understanding the limits of the hardware. It makes it so much easier to debug problems if you understand small details before they are hit. The compilers usually warn in straight line code, but things like multiple interrupts hitting at the same time can create unanticipated issues the compiler cannot see. Also a form of being able to step through your code to debug like an ICE unit or on chip debugger is extremely helpful. 5. Apr 1, 2014 ### meBigGuy I agree with everything said so far. It is commonplace to use as much C as possible. It is worth it to learn C for many reasons. (portability, maintainability, speed of development, etc). I think the advantages far outweigh the inefficiency in most cases. Sometimes the inefficiency can be significant. But, I expect you will be happy with using C. That said, if you can write assembly language for the processor you are using, it provides several advantages. 1. You can write assembly when you need the performance or want to save space. 2. By understanding the machine model, you can write better C or at least understand when you cross certain resource boundaries. 3. You can understand the code produced by the compiler, and perhaps recode your C for efficiency 4. Helps debugging if you know assembly. 6. Apr 2, 2014 ### analogdesign I agree strongly with meBigGuy here. While I haven't written a line of production assembly code in over 10 years, the fact that I have experience with it means I can understand what the microcontroller is doing far better than if I never had assembly experience. Since the OP already has some assembly language experience writing in C should be fine going forward. 7. Apr 2, 2014 ### AlephZero Microcontrollers tend to have fairly simple instruction sets and architecture, so there is no reason why a good C compiler should generate worse code than you can write yourself in assembler. In fact for more complex CPUs (like modern PCs) compilers often produce faster code than you would yourself, because the compiler writing team knows more about the CPU than you do, unless you spend months studying it in detail. I agrree with the other answers that it's worth doing some assembler programming, to find out how interrupts etc really work at the hardware level. Standard C doesn't have any language features that map onto the concept of interrupts, so microcontroller versions of C add some non-standard features to the language. If you don't fullly understand how the C code relates to the real-world hardware, you are living dangerously IMO. 8. Apr 2, 2014 ### mrmjp Thanks for the responses y'all. I recently took an intro to microcontrollers for my EE degree that was all assembly language based. It might just be the learning curve of assembly that I was struggling with, but assembly programming was kind of a turn off even though I really loved the MC itself and the possibilities with it. Then one day my professor mentions that the programming could be done in C. This makes me not want to give up on MCs as a whole and learn C just so I can continue to work with MCs. 9. Apr 2, 2014 ### mjhilger I learned assembly on the Apple II 6502 @ around 4MHz. Really painful compared to the microcontrollers of today. Hang in there and the assembly knowledge will pay off big in the end. It is a huge benefit to understanding many of the problems that can occur and helps if you have to trace the ICE output of a problem down deep. Never hurts to understand your target well. C certainly makes many of those tasks easier, but unless you have a good feel for the underlying hardware and how to set bits and such from the assembly level some of the settings will confuse you. One control word might have bits that control DMA & A/D & interrupt all together, a good understanding of the assembly moving those bits into the register and the electrical/digital hardware understanding is crucial to see how those all fit to provide the services you need and get your tight routines working properly. 10. Apr 2, 2014 ### mrmjp Any web sites, recommended books, ect for microcontrollers or assembly language would also be appreciated. 11. Apr 2, 2014 ### mjhilger www.microchip.com www.sparkfun.com Microchip is the manufacturer of PIC microcontrollers. Range from under a dollar in quantity to maybe$15 for their high end 32 bit stuff in quantity. Numerous app notes and an IDE (integrated development environment) that will allow you to write assembly and C all free for the download.

Sparkfun is a sales outlet for numerous development and project boards with the parts already soldered on and ready to be used. They have a number of links depending on which board you are looking at. This site is great in that they offer high quality boards at reasonable prices ready to have wires connected via breadboard connections (under \$30 on up).

For raw microcontrollers I really like Microchip's offerings. They have many many app notes and libraries that provide solutions for numerous applications.
If you lean toward FPGA, many of those have ARM processors that can be embedded as part of their set and you can choose the surrounding peripherals to be included for your intended hardware application. The ARM processors are mainly programmed in C but I think you can look at the assembly. Most ARM compilers are pretty efficient and most of the devices have device drivers if you run a version of Linux on the ARM instead of raw stand alone code.