# How to program Micro-controllers for beginners [duplicate]

I have never worked with micro-controllers in my life, and have a small experience in programming. I know a bit of java and TI-basic, but haven't ever made any useful programs. I have random micro-controllers sitting around (i have a few PIC's and an ATMEGA168.) First, I am wondering what hard-ware I need to get to connect it up to USB. (I don't think it can just be a direct connection), second what software should I get (mac or windows, or even linux it doesn't matter), and last, for someone horrible in programming, what is a good way to learn, and what language does it require. How do computers work to communicate with these devices?

My recommendation is use the ATMega168 (and save that datasheet to your computer, you will need it often). The WinAVR package is open source, Atmel Studio is free from their website, and Sparkfun has an excellent set of tutorials on putting an AVR micro on a breadboard. Instead of building your own in circuit serial programmer, I would buy one. That will allow you to use the GUI programming facilities in Atmel Studio, instead of needing to worry about AVRDude, which is not something beginners should be doing in my opinion.

Microcontrollers are commonly programmed in C/C++ or assembly. Start off working in C. There is sample code for anything you can imagine, and it's more beginner friendly than working in assembly. What you can do, is look at the disassembly after compilation. If you want to do that, turn the optimization off, or you might get confused by what you see. Once you understand the processor architecture, you can write your really high performance stuff in assembly if you need it.

I wouldn't normally advocate it, but another option is buy an Arduino board, probably an Uno to get started. It has an ATMega328 on it, which is just a 168 with more memory. I develop my stuff in Atmel Studio, then use my ICSP and Arduino Uno to program a fresh AtMega168 or 328, then drop the programmed chip onto the PCB. It's fast and easy to reprogram.

I personally Like to work with Atmel micro-controllers as the development hardware is relatively cheap. You can get the programmer (ATAVRISP2) for around $38 and in-circuit emulator/programmer ((ATJTAGICE2) for around$400 . You can download AVR Studio from the Atmel web site to program/emulate your devices. If you want to do serious programming, then I would recommend the in-circuit emulator as it allows you single step through your program and let's you monitor internal device registers. The AVR Studio supports both assembly language and C.

This question is really open ended. I'll try and give some feedback on the programming aspects of the question.

There is a very little difference between programming for a microcontroller and a PC. If you have little experience programming, learn how to program on a PC rather than on a microcontroller. Microcontrollers are more difficult to use (cross-compilation, slow download times, hardware initialization, bootloaders, more complex debug, ...) so first learn to write programs for your PC. Learn C as this is the most widely used programming language for microcontrollers. There are tons of websites and books for new users.

I would suggest downloading and using Eclipse to write, compile and debug your programs. Experience with Eclipse is valuable as many microcontrollers use this as their programming environment.

Once you have a firm grasp on programming, and have a project in mind which requires a microcontroller, buy a development kit that meets your hardware requirements and more importantly most closely resembles your PC and your skillset. Buying a Raspberry Pi might be overboard for a small project, but being able to interact with it as if it were a PC will ease your transition into microcontroller development.

• Yes, the development process for PCs and microcontrollers is similar, and that's a good way to learn the language. However, software engineers that think they can just jump into embedded programming generally suck at it. Sloppiness that you can get away with on a PC, is not acceptable on a limited resource machine. The kind of bit twiddling that is necessary for embedded systems, is foreign to PC users. At the end of the day, it's an apples to banana comparison, with the language being the common denominator. – Matt Young Apr 9 '13 at 18:33
• +1 Still, learning some programming first and then taking on microcontrollers is not a bad path. There are other routes (start with Arduino for instance). – Wouter van Ooijen Apr 9 '13 at 20:22
• I beg to differ on a specific aspect of "very little difference between programming for a microcontroller and a PC": Many PC software developers work in the very sanitized environment of platforms like Java, or on languages like Python or Ruby on Rails - Bringing those programming habits, which may be good in their own place, to the embedded world, is a recipe for disaster. – Anindo Ghosh Apr 10 '13 at 6:20
• The OP admits to being new to programming. My suggestion is to learn basic programming constructs (for loops, even bit manipulation) on the PC where the toolchain is simpler and where the knowledge gained is 100% transferable to embedded programming. – spearson Apr 10 '13 at 23:17
• @AnindoGhosh - I'd word it even stronger, I don't see that there is any difference between the two. There are always limitations of both architecture and language, good programmers knows how to work within these limitations whether on a PC or a micro. Embedded systems are changing and OSes are capable of running Java, Python, and RoR (though likely not ideal). The danger, for me, is that if embedded programming is different then we might not need inline documentation, code review, unit tests, source control, continuous integration, and other valuable typically PC programming habits. – spearson Apr 10 '13 at 23:49