Saturday, July 21, 2012

Introduction to MCU [MicroController]- Atmega-16 & Atmega-8


There are many MCUs available in market.
Some famous are AVR, 8051, pic,ARM, etc
We'll be using atmega series (atmega-8, atmega-16,atmega-32,etc).

In atmega-16 there are 40 pins, 4 ports, PORTA, PORTB, PORTC, PORTD.Each port has 8 pins. So you got a total of 32 input/output pins. The rest 8 pins  are 2-VCC, 2-GND,2-XTAL (crystals) 1-AREF(reference),1-RESET. 

Each PORT has 3-registers, for e.g. port-A has PORTA, PINA, DDRA. 
Each register has 8 BITS, as you can see in the following image. By default all 8 BITS are zero.
Lets talk about port-A, same will be for others also.
NOTE: port-A & PORTA are different terms. Dont confuse between them.

The PORTA & DDRA registers are writable that means we can change there value by PORTA=8; or like any value between 0-255. But PINA register is not writable, we can only read from it.
Read from it? Wait a min.... I didn't got it.
We'll see it in a couple of minutes.
First you need to know that what is the role of DDRA register. Well you can see it as a switch, if any particular bit's value is one then the respected bit's of PORTA & PINA are shorted. It means the value of PORTA gets copied to PINA if DDRA is 1. 








If you don't know how PORTD=12 make bit 2 and bit 3 as 1 and rest 0 then you need to read this before going further.
Its an article on "How to use decimal and binary numbers to change values of ports of a Micro-Controller".

Link Broken? Post a comment, i'll renew it.



Now when you have read the above article then u may know whats the meaning of DDRB=69, so lets see how DDR register works.




Lets suppose we want to glow a LED.
To do that you have to connect it to any pin of MCU, then just make that pin 1 (ON).
Naa its not that simple, first you have to short the DDRB's bit 0 with PINB's bit 0.
For that use DDRB=1; before main() function.
Now connect the negative terminal of LED to ground of MCU, and make PORTB=1.
This will glow the LED.

If you want to glow all LEDs then what will you do?
The answer is simple

DDRB=255;
main(){
PORTB=255;
}



What you have to remember

If DDR register is so necessary then why not permanently set it to 255.
Well thats the thing you have to remember that before using any port in your program don't forget to make its DDR register to 255.
This will permanently short each bit of that particular port.   

Do you remember i said something "copy value from pin"
Yes, you can copy the value (High or Low) of any pin. In embedded systems 0 means 0v and 1 means 5v. So you can check whether a pin is at High or Low state.
Why i need to check the value of any pin?
Suppose you are making some project in which you want that if power comes something shoud happen and if power goes off then some other thing should happen,
or simple you want to make a LFR (Line following Robot) or "DTMF controlled Robot" that requires to read the values from pin then you will need this.

Caution !
Are you getting any clue from these images what i want to say?
Yes, if you are shorting PORT and PIN then dont give input as it may cause short-circuit.



What you have to remember

If you want to take input from a pin then set DDRB=0; and if you want to give output to a pin then set DDRB=255; .


Please Share it! :)

Post a Comment

No comments: