Electronic – arduino – Write an AVR Program

arduinoavrprogramming

So I saw a post talking about the commercial feasibility of an Arduino (http://electronics.stackexchange.com/questions/11333/commercial-use-of-arduino-is-it-suitable) . From this it said a great idea would be writing your own AVR Program i believe.

What does it mean to:

1) Write an AVR Program? What is an AVR Program?
2) Program your own microcontroller?

Is it possible to make your own microcontroller and how would you do this? Would you still use something such as an ATMEGA? What would this change and how would you "upload" an AVR to this?

Edit (after Comment #1): My questions is would i still use the Arduino or would it be better for me to buy … a different? microcontroller and program that? To refine my other question, how would you program … a microcontroller? what interface would be used to upload code?

Best Answer

It's a broad question. I don't think the previous question was meant to imply that an Arduino isn't a 'real' or 'professional' development platform. It has its niche. Every microcontroller has its niche - you have to choose one that helps you do what you're trying to do. Does that sound vague? Yes. Because it all depends on what you're trying to do.

So let's back up - what IS the Arduino compared to everything else? I'll split it up into hardware and software.

As far as hardware goes the Arduino is a development board. There are lots of development boards out there - tons of them. They each are built around one or several microcontrollers. They are, to a certain extent, meant to be somewhat generic in function - you need to be able to use them to develop for just about any application. But they can also have a focus - you might see development boards with HDMI and Ethernet controllers - these are obviously meant to help people develop applications for networking or visual applications. The Arduino is a solid development board, but it has very few neat features. The approach behind Arduino is a wide-open sandbox - that's why there's so many shields available.

So development boards are built around microcontrollers. The Arduino is built around the AVR ATMega328. This is a specific microcontroller made by Atmel. It is one of Atmel's 8-bit microcontrollers. It is aimed at general purpose embedded applications that don't require a whole lot of bells and whistles or lots of I/O (it's rather limited in the number of pins it has). To put this in perspective, Atmel also offers 32-bit ARM-based offerings - this is a type of microcontroller you might find in a router that runs Linux. There are, of course, many different microcontroller manufacturers - all offering differnt microcontrollers focused on certain applications - automotive, networking, DSP, etc.

Now there's the question of the software - what does it mean to write an AVR program? Well, everything you write for an Arduino is an AVR program - it's just hard to tell. Arduino comes with a framework that basically hides a lot of the complexity of microcontroller programming from you. Typically, writing any microcontroller code (including AVR programs) involves lots of playing with bits in registers, keeping track of timing, etc. It's very low-level and there's a lot to keep track of.

In a broader sense, microcontrollers are typically programmed in a variant of C. I say a variant because every microcontroller needs a special C compiler with its own rules. So every different chip has its own variant of C. There aren't earth-shattering differences between each of them, but you can get tripped up on the differences if you're not careful.

How do you program them? There's lots of ways, but generally nowadays chips tend to program themselves. That's no joke - most programmers just implement a special kind of serial communication that lets them be programmed by one of several devices. This is called In-System Programming. You program microcontrollers with a variety of devices - they can be $5 USB dongles, parallel port devices or $1200 JTAG adapters. Typically you need a specific programmer for a specific line of chips. That is to say, you can buy a JTAG that will program most to all ARM chips, and you can find programmers that will program most if not all AVR chips.

So programming your own microcontroller basically means you 1) Select a microcontroller you want to use, 2) find a good development board for it, 3) find some sort of programmer - either an In-System Programmer, or JTAG or something else, 4) figure out what compiler you need, what code editor to use, etc. (Arduino of course, provides all of this, so it's easy). Then, you read the chip's data sheet and figure out where to start.