Monday, January 31, 2011

Getting Used to AVR


Getting used to AVR

Datasheet. A datasheet is a document provided by the manufacturer of an IC that gives information about various Pins of those IC’s and their function.
To learn AVR Microcontrollers, it is necessary to have that particular Microcontroller’s Datasheet with you. The datasheets are available for free on the Internet.
Our programming platform shall be WinAVR, which is also a free software available on the internet.
The best way to learn using a microcontroller, for a newbie, is using a development board. You could either make one on your own or buy them online. It’s best to buy one, it reduces the errors that might arise out of incorrect hardware connections. So, if you are looking to buy a development board, you can buy them from robokits.co.in, thinklabs.in...there are many other websites too. There are different types of programmers available. Please make sure that you know which programmer is used to program your board. It may be different for different boards.
The 1st thing that you need to be familiar with is a microcontroller’s Inputs and Outputs.
We will be considering Atmega16 throughout the course of this series. The PIN DIAGRAM of Atmega16 and Atmega32 are identical.



(click on the photo to enlarge it)
As can be seen the pins for both the IC’s are identical. The only major difference is that ATmega16 has 16K Bytes of In-System programmable flash memory while an ATmega32 has 32K Bytes of flash memory.
ATmega16 (and ATmega32) has a total of 40 pins, of which 32 can be used as Input or output. These pins are divided into 7 ports

PORTA-  8 pins ( PA0 to PA7)

PORTB-  8 pins ( PB0 to PB7)

PORTC-  8 pins ( PC0 to PC7)

PORTD-  8 pins ( PD0 to PD7)

Each of these ports is associated with 3 registers: DDRx, PORTx, PINx, where ‘x’ stands for the port name (A,B,C or D). e.g DDRA, PORTA, PINA etc.
NOTE: While programming, DDRx,PORTx,PINx are always written in capitals.
DDR stands for Data Direction Register. It decides whether a particular pin will act as input or output.
(please note that PIN refers to the register and pin refers to some pin of the microcontroller)
Since a Port has 8 pins, all the registers, DDR,PORT,PIN are 8 bit register. Each bit corresponds to a pin of that particular port. Consider the example of port A


(click on the photo to enlarge it)
MSB stands for Most Significant Bit. LSB stands for Least Significant Bit.

Embedded Systems


Embedded System. The term itself brings to mind some chip ‘embedded’ on a surface with complex wiring connections further connecting it to some even more complex ‘embedded’ systems.
Well, they are actually a lot simpler.
In simple words and Embedded System is any electronic system that uses a ‘chip’.
Microcontrollers and Microprocessors are two of the most common and most used embedded systems.
A microprocessor is a general purpose chip used in applications which require huge amount arithmetic, logical etc. operations. Our laptops use a microprocessor chip.
A microcontroller on the other hand is designed for specific tasks. Unlike a microprocessor it cannot be designed for controlling several processes. A microcontroller is preferred in applications that do not require too many tasks to be performed. If you guys ever open up your washing machines, you might find a microcontroller chip inside it. All those functions that a washing machine performs, toggle, spin, dry.. are controlled by a microcontroller.
A microcontroller is much cheaper than a microprocessor. It requires much less power. For this reason, they are the favoured option for most robotic applications.
Microcontrollers are, for us robo-tists, a godsend. They showed us how we can make interesting projects in the seemingly dull course structure. They give us the first glimpse of what it is like to be an engineer.
Microcontrollers are easily available in the market. They belong to different families aka companies.
  1. 8051:  the ‘8051 core’ was the de-facto standard in 8-bit (and 4-bit) microcontrollers. Developed by Intel in the 1980’s, it still seems to be the instruction set they love to teach you in college. They are based on VERY OLD, but field proven instruction set. Very old techs, in my humble opinion, but these IC’s have been significantly improved.
  2. PIC: This is the classic microcontroller from Microchip. Very simple, very proven, but it lacks some features that other manufacturer’s are building into their chips.
  3. AVR:  This is a direct competitor of PIC. Designed and named AVR for god knows what reason by Atmel, it is a direct competitor of PIC. It does everything a PIC does, only faster and cheaper. The AVR series of Microcontrollers are readily available in the market. Also, their platform for programming is not licensed, but freely available on the web.
  4. MSP: These microcontrollers by Texas Instruments are pretty good, but, they truly excel at low power applications. Imagine running a complete system on one AA battery for 5 years. This is the realm of nano-amp  current consumption.
  5. ARM: Dunno what ‘ARM’ stands for. But, these are VERY powerful, extremely low power consumption and very low cost. But, for a beginner they can get really intimidating if you haven’t played around with a Microcontroller before.
The most popular microcontroller among robot-enthusiasts, for now, is Atmel’s AVR. In the coming weeks we shall be covering various topics on ‘How to Program a Microcontroller’.
If you are interested in learning how to program and use a microcontroller, it’s best to buy a development board. Because, you see, it reduces the number of errors possible in the system. If you are using a development board, the only errors that will be present will be the ones you make in programming, which can be easily rectified. But, once you have mastered the skill of programming a microcontroller, go for designing the hardware of your Microcontroller system ( not the microcontroller, you can use the chip in your system :P ) on your own.



Voltage Regulator IC 7805


Voltage Regulator IC 7805




Let's say you want to play with your Microcontroller today. 

With any IC, you need to power the thing. 
There are two power connections on basic micros : VCC and GND. What the heck is VCC? This is the label for the positive voltage. Don't worry, after a few days of this, seeing 'VCC' will become very normal. GND is short for ground. All electrical current needs a way to flow back to ground. This can be called 'common' but is often just labeled GND.
There are thousands of different micros out there, but 5V (five volts) is the typical VCC. 3.3V is also typical but you'll also see 2.8V and 1.8V VCCs on more exotic micros. For now, just worry about 5V and GND.

Where do I find this 5V?

You need to hook up 5V and GND to your micro. 

Quick note: If you reverse the connection on your micro - bad things happen. Always make sure your 5V power supply is connected to the VCC pins and GND to GND. If you reverse this and connect 5V to GND on the micro and GND to VCC on the micro, things won't explode, probably no smoke, but things will probably heat up like crazy, and you'll probably damage your 250 bucks micro. You probably will. I did. Many times. Try not to do it.
Ok! You need 5V. Time to build a simple voltage regulator circuit!

The most common regulator is called the LM7805. Why? I dunno. I've never actually touched a component with LM7805 stamped on the outside. There's always other letters stamped on the outside like 'LM78X05' or 'LV78X05' or some such crazyiness. Just know that there are many many manufacturers out there and they are all producing the same basic part, with small tweaks to each one. What you need is one of these generic parts that is designated as a '5V linear regulator'.

LM7805

Here you can see the 'pin-out' of the LM7805. Say 'IGO' in your head and commit this to memory (input, ground, output). You'll probably hook up a lot of these. When in doubt, always check the datasheet before hooking up a new part - or else be close to the on/off switch! Input is the input voltage of anything greater than about 7V. GND is ground. Output is the 5V output pin. All grounds need to be connected together for current to flow across the system. One more time - connect all grounds. This is the main reason why a novii can't get a system to work. 

Schematic

Schematic note: The two ground pins are not shown connected. We assume that nets (the green wires) of the same name are connected together. Schematics can get big and complex, so you won't see all the wires together, but in your breadboard you need to connect all the GND pins together. In this case it's the GND wire from your 9V battery (power source) connected to the GND pin on the regulator.

Now, just wire it all up on a breadboard.
Congratulations! You've built up your very first voltage regulator! Now leave this 5V power supply wired in your breadboard! You are going to use it many times...