Microcontrollers will often allow an optimal solution, combining complex functionality with reduced part count.
The AVR microcontroller advantages include:
Lots of bang for the buck. These chips pack lots of power (1 MIPS/MHz, clocks up to 16MHz) and space (up to 128K of flash program memory and 4K of EEPROM and SRAM) at low prices.
HLL Support. Programming in high level languages, like C, helps increase reuse and reduce turn-around/debug time/headaches.
In-System Programmable flash--can easily program chips, even while in-circuit. Many peripherals. A whole bunch of internal and external interrupt sources and peripherals are available on a wide range of devices (timers, UARTs, ADC, watchdog, etc.).
32 registers. The 32 working registers (all directly usable by the ALU) help keep performance snappy, reducing the use of time-consuming RAM access. Internal RC oscillators can be used on many chips to reduce part count further. Flexible interrupt module with multiple internal/external interrupt sources.
Multiple power saving modes.
AVR Microcontroller Primer
This mini tutorial focuses on the AVR microcontroller architecture. It is meant as an introduction to the AVR platform in particular but will also (very briefly) introduce a few microcontroller-related topics--be warned that it may be a little abrupt for the absolute microcontroller newbie, but it should be a good intro for everyone nonetheless.
The AVRs are 8 bit RISC platforms with a Harvard architecture (this means that program and data memory are separate). They are microcontrollers: basically, complete computers on one integrated circuit. This means that they combine, on a single chip, a number of the elements you might find separately within a microcomputer. These include:
a microprocessor (CPU) core memory program memory long-term memory (EEPROM) volatile memory (SRAM) I/O interface for input and output numerous peripherals CPU
The processor core includes elements to read the program memory, decoding and executing the instructions within. The CPU can also fetch and store data to and from the EEPROM, SRAM and the 32 registers. The registers act as extremely efficient storage for 8 bit values (1 byte) and the ALU (arithmetic/logic unit) can operate on each of the 32 registers directly.
The AVR processor features a real life stack and its instruction set was designed and optimized for use with high level languages--it is easy to program these chips using C. Note that certain implementations don't provide any SRAM, so the stack is actually hardware based (limiting the stack depth to three).
It is interesting to note that most instructions only take a single clock cycle to execute and there is no internal clock division. Also, the CPU will fetch and decode the next instruction as it is executing the current instruction (i.e. in parallel). All this means that AVRs will reach performances of nearly 1 MIPS (Million Instructions Per Second) per MHz--they are fast and efficient. Another advantage of reaching comparable performance at reduced clock rates (e.g. an AVR at 4 MHz will execute about as many instructions as a PIC microcontroller running at 16MHz) is reduced power consumption and lower electro-magnetic noise from high frequency signals.
Memory
The program memory is a contiguous block of flash memory. It may be programmed and reprogrammed numerous times easily. In fact, using the In-System Programming, you can design your system to allow chip firmware upgrades in-place. Program memory is 16 bits wide and can usually be erased/re-written at least 1000 times.
All AVRs have some EEPROM and most have SRAM available. Both are 8 bits wide. The EEPROM is programmable during execution (to retain data even without power) or directly during programming (which is very useful for things like production line calibration), see our AVR Programmer HOWTO for details. Using EEPROM is slower than storing data in SRAM but, unlike the volatile RAM, once written the data will remain available indefinitely (even when the power source has been removed). The EEPROM is said to withstand 100,000 erase/write cycles.
I/O and Peripherals
Peripherals are where the members of the AVR family distinguish themselves. They all have the same core and support a basically uniform instruction set, which means you can easily develop for all of them. However each has its own particular blend of peripherals and extras.
All AVRs, from the tiny 8 pin DIPs to the 44 pin Megas, have at least one data port. Data ports allow for input or output of logic level (binary, HIGH or LOW) data. The AVR ports are bi-directional, allowing you to set them for input or output on a pin-by-pin basis. The AT90S8535 depicted here has four 8-bit ports available. Often, the external pins which make up a port will serve dual (or triple!) purposes, and how the pins are used will depend on how you've configured the controller. For instance, you can see from the diagram that all pins for port A (upper left) are also used as separate channels to the ADC (analog to digital converter) while certain pins of port D (lower right) are used for communication through the UART.
Other peripherals which communicate with the outside world are, as discussed above, the ADC and UART. The Analog to Digital Converter included with some AVRs is normally multiplexed; this means that you can monitor multiple analog values, through different pins, one at a time. The UARTs allow asynchronous serial communication, directly with other chips or with computers systems using an RS-232 interface. Analog comparators and other peripherals are available on certain chips, check the relevant datasheets for details.
Internal peripherals include timers and counters (8 and 16 bit, with optional overflow interrupts), a watchdog timer (to automatically reset the chip after a preset delay of 16ms to 2s), RC oscillators and more.
Programming
AVR microcontrollers may be programmed using assembly or a higher level language, such as C.
Learning to code assembly is a good idea, as it gives you in depth understanding of the process under the hood. With this type of "bare metal" programming, you manipulate the CPU registers directly and can optimize their use according to your specific needs. The process can, however, become tiresome--at least for programmers accustomed to letting a compiler handle all the dirty work.
Using a high level language can allow you to code much more efficiently. Here is a very simplistic program in C:
CODE
void main (void) { uint8_t i; uint16_t j=0; for (i=0; i<100; i++) { j += i; } }
All it does is add all the integers between 0 and 99 and store the result in the variable j. Here is part of the same program, as transformed into AVR assembly by the AVR-GCC compiler (corresponding C code has been inserted):
line number in hex, e.g. 80: machine code instruction, e.g. ef cf assembly language version, e.g. rjmp .-34 generated comment (line number or hex value translation)
Whichever language you choose to use, the next step is to write and build your program (compiling, linking and translating it to a suitable format) and finally transfer it to the microcontroller using a suitable programmer.
- author unknown
Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.