Programming Microcontrollers - C vs Machine Code

In summary: C is actually less efficient than assembly. However, for more complex microcontrollers or those with more specialized features, assembly can be more efficient.
  • #1
mrmjp
9
0
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.
 
Engineering news on Phys.org
  • #2
mrmjp said:
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.

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
Yes, assembly code is what I ment.
 
  • #4
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.
 
  • Like
Likes 1 person
  • #5
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
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
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
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
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
Any web sites, recommended books, ect for microcontrollers or assembly language would also be appreciated.
 
  • #11
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.
 

1. What is the difference between programming microcontrollers in C and machine code?

Programming microcontrollers in C involves using a high-level programming language to write code that is then compiled into machine code, which is the low-level, binary instructions that the microcontroller can understand and execute. On the other hand, programming in machine code involves directly writing the binary instructions that the microcontroller can understand, without the use of a high-level language.

2. Which method is more efficient in terms of time and memory usage?

In general, programming microcontrollers in machine code is more efficient in terms of time and memory usage, as there is no need for the additional step of compiling the code. However, programming in C may be more efficient for larger and more complex projects, as it allows for easier organization and readability of code.

3. Is there a significant difference in performance between programming in C and machine code?

In most cases, programming in machine code may result in slightly better performance, as the code is directly translated into the microcontroller's native language. However, the difference in performance may not be significant and may vary depending on the specific project and the skill of the programmer.

4. Can microcontrollers be programmed in other languages besides C and machine code?

Yes, microcontrollers can also be programmed in other high-level languages such as BASIC, Python, and Java. However, these languages may require the use of additional libraries or compilers to convert the code into machine code that the microcontroller can understand.

5. Which method is more suitable for beginners learning to program microcontrollers?

For beginners, it may be easier to start with programming in C, as it allows for a more structured and organized approach to coding. However, learning the basics of machine code can also be beneficial in understanding the inner workings of a microcontroller and can lead to a deeper understanding of programming concepts.

Similar threads

  • Electrical Engineering
Replies
10
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
Replies
9
Views
1K
  • Programming and Computer Science
4
Replies
107
Views
5K
  • Programming and Computer Science
Replies
29
Views
2K
  • Electrical Engineering
Replies
17
Views
23K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
963
  • Programming and Computer Science
Replies
8
Views
875
  • STEM Academic Advising
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
847
Back
Top