Total Pageviews

Saturday, 4 April 2015

The AVR Architecture and I/O Registers



Topic: Introduction to Atmega 8 and its basic architectures to start with!!!

Refer to this data sheet, while I describe it to you.



Okay, Here you can see some of the properties of Atmega8 Ic that you need to know for now:

1. Operating Voltages: (2.7v to 5.5v) for atmega8L (Low power atmega) and (4.5v to 5.5v) for Atmega8.

** This should be very clear from the first, these digital ICs work like logical 0 for 0 v to 2.5v and logical 1 for 2.5v to 5.5v and above 5.5 the MCU won't recognize the i/p voltage.

2. (0-8)mhz Crystal for Atmega8L and (0-16)mhz Crystal for Atmega8.

** Crystal is the clock frequency needed for the MCU for its processing. These are basically the heart-beat of the system. Clock Frequency is  analogous to the processor speed of our PCs. 

3.   There are 32 Register Blocks of 8 bit each (as Atmega is 8bit mcu).

4.   6 channel, 10 bit ADC and 3 channel PWM are available.


I/O Registers of Atmega8


There are 3 Ports in Atmega 8 viz Port B, Port C and Port D. 
Each of the pins of the ports can be used as General Purpose Input/Output Pins.
They are used for sensing an input device like switches, temperature/humidity sensor, IR etc or running an output device like Leds, Motors etc.

N.B: A particular pin can be used as either input or output at a time.

To work with the I/O pins, three registers of 8 bits each have to be configured viz., DDRX, PORTX and PINX. (here 'X' indicates the name of the register B,C or D)

Data Direction Register (DDRX) : 
A MCU doesnot know what type of device is interfaced to its pins unless the programmer mentions it inside the code. Thus, we have to set its corresponding DDR bits as 0 or 1 in our code. Thus DDRX code is used to inform the MCU that it should give voltage to a certain pin or check the status of the pin.

For Eg. :  Let there is a Led (output device) connected to PB0, So the first bit of the PORTB register should be 1 and the rest are 0s. 

Note: 
  • If a pin has nothing on it or it has an input device, that pin should be given 0 any way.

  • The register bits are arranged from MSB to LSB as follows.



PORTX Register :
This register is used to give a logical 1 or 0 output at the output pins. Logical 1 represents voltages between 2.5 and 5.5. logical 0 repesents voltages between 0 and 2.5.

PINX Register :
This register is used to check the status of an input pin. It reads the analog or digital data from switches and sensors.

Sample Code:

1. Blinking Leds code.

#include<avr/io.h>     // Header File for AVR MCU.
#include <util/delay.h>    // Header File for delay functioning.

int main(void)
{
     DDRB=0b00001111;       // PB0 to PB3 are selected as the output pins, i.e., leds are connected to pins 14,15,16,17 of atmega8.  
while(1)                   //This is for the the infinity loop.
{
PORTB=0b00001111;       //Switch on all 4 LEDs
_delay_ms(2000);                       //Delay of 2sec=2000 ms
PORTB=0b00000000;       //Switch off all 4 LEDs
_delay_ms(2000);                       //Delay of 2sec=1000 ms
}  
}

Circuit Diagram:



   


Monday, 23 March 2015

Introduction to Microcontrollers and AVR





Topic: What is a Microcontroller(µC) and how it differs from Microprocessors!!!

MICROPROCESSORS :

Microprocessor is an IC which has only the CPU inside them i.e. only the processing powers. There are no Ram, Rom or other peripherals on chip. So to make a microprocessor functional, the user  has to add them externally. That is why we can easily add/change Ram or hard disk without bothering about the microprocessor such as Intel’s Pentium 1,2,3,4, core 2 duo, i3, i5 etc. The processing speed of MPUs now a days are above 1Ghz.


MICROCONTROLLERS : 

It has a CPU, in addition with a fixed amount of RAM, ROM and other peripherals all embedded on a single chip. At times it is also termed as a mini computer or a computer on a single chip. The processing speed is in the range of Mhz, like AVR microcontrollers mostly run with a 12mhz/16mhz on board crystal oscillator which serves as the processing speed or simply the heart beat of the system.

**In brief, Microprocessor Units (MPU) are much faster than Microcontroller Unit (MCU), but MCUs are way too cheap. Although they can't be compared with price and neither can they replace each other's existence.