You are on page 1of 25

AVR ATMEGA16/32

Your best friend in autonomous


robotics !!! 
Introduction to Microcontrollers
•We can define microcontroller as single chip Computer .

• Microcontrollers are used for some specific purpose ( Ex. –


automated doors at Ansal Mall or at any other place)

whereas computers are used for general purpose ( Gaming , 3d


modeling , etc. )

• Microcontroller has on chip C.P.U. , RAM, some amount of


EEPROM and various on-chip peripherals like ADC n TIMRES which
will be explained to you in upcoming lectures.

Example :
Switching a microwave heating off after a preset time and also
when temperatue exceed preset value
AVR ATMEGA16/32 : D COOLEST MICROCONTROLLER !!!
AVR ATMEGA16/32 is a 8-bit microcontroller .
VOLTAGE REGULATOR IC

•This IC is known as IC 7805

•This IC maintains a constant


voltage of 5V

• It provides ground to our circuit


PIN DESCRIPTION OF ATMEGA 16/32
•Digital IO is the most fundamental mode of connecting a
MCU to external world.

•The interface is done using what is called a PORT.

• A port is the point where internal data from MCU chip


comes out or external data goes in.

•They are present is form of PINs of the IC. Most of the PINs
are dedicated to this function and other pins are used for
power supply, clock source etc .

• Ports are named PORTA, PORTB, PORTC, PORTD etc. The


pin configuration of ATmege16/32 MCU is shown ABOVE
ACCESSING PORTS
Each PORT in AVR has three related Registers
To be
Continued !!!
;)
AVR ATMEGA16/32
continued…
Your best friend in autonomous
robotics !!! 
AVR ATMEGA16/32 : D COOLEST MICROCONTROLLER !!!
AVR ATMEGA16/32 is a 8-bit microcontroller .
INTERNAL PERIPHERALS OF ATMEGA 16/32

Each AVR MCU has several internal peripherals that give powerful
abilities to your projects. For example internal ADC can be used to
convert analog value (say voltage output of some sensor) to a
digital value that you can use.
FEW INTERNAL PERIPHERALS OF ATMEGA 16/32

•Two 8-bit Timer/Counters and One 16-bit Timer/Counter

• 8-channel, 10-bit ADC


TIMERS IN ATMEGA 16/32
What is a timer ?

A timer in simplest term is a register.

Timers generally have a resolution of 8 or 16 Bits. So a 8 bit


timer is 8Bits wide so capable of holding value withing 0-255.

But this register has a magical property ! Its value


increases/decreases automatically at a predefined rate
(supplied by user). This is the timer clock. And this operation
does not need CPU’s intervention.
Since Timer works independently of CPU it can be used to
measure time accurately.

Timer upon certain conditions take some action


automatically or inform CPU.
One of the basic condition is the situation when timer
OVERFLOWS i.e. its counted up to its maximum value (255
for 8 BIT timers) and rolled back to 0.

In this situation timer can issue an interrupt and you must


write an Interrupt Service Routine (ISR) to handle the
event.
Analog to Digital Converter

Most of the physical quantities around us are continuous. By


continuous we mean that the quantity can take any value
between two extreme. For example the oven temperature can
take any value (within certain range). If an electrical quantity is
made to vary directly in proportion to this value (temperature
etc) then what we have is Analogue signal.

Now we have brought a physical quantity into electrical domain.


The electrical quantity in most case is voltage. To bring this
quantity into digital domain we have to convert this into digital
form.

For this purpose we use in-built ADC of AVR ATMEGA 16/32.


Fig: ADC Theory

An ADC converts an input voltage into a number. An ADC has a


resolution. A 10 Bit ADC has a range of 0-1023. (2^10=1024) The
ADC also has a Reference voltage(ARef). When input voltage is GND
the output is 0 and when input voltage is equal to ARef the output
is 1023. So the input range is 0-ARef and digital output is 0-1023.
To much of ATMEGA16/32 ??
Take a break guys 

Come back tomorrow for the all interesting class


of SENSORS .
Make ONE for yourself !!!
Basics of Embedded C
•Each peripheral has a hardware and software part .

•While programming microcontrollers in C most of the time


we have to deal with registers. Most common tasks are
setting and clearing bits in a register and check whether a
bit is 0 or 1 in a given register.

•A register is simply a collection of some bits (mostly 8 bits


in case of 8bit MCUs).

•Either each different bit in a register has some purpose or


the register as a whole holds a value. Registers serves as
connection between a CPU and a Peripheral device (like
ADC or TIMER).
SETTING A BIT IN REGISTER

MYREG|=(1<<ENABLE);
will result in
MYREG|=(1<<5);
which again result in
MYREG|=(0b00100000);

CLEARING A BIT
TESTING STATUS OF A BIT

if(MYREG & (1<<ENABLE))


{
//ENABLE is '1' in MYREG ...
}

else
{
//ENABLE is '0' in MYREG
}
USING ADC

ADC Registers.
As you know the registers related to any particular peripheral
module(like ADC, Timer, USART etc.) provides the communication
link between the CPU and that peripheral. You configure the ADC
according to need using these registers and you also get the
conversion result also using appropriate registers. The ADC has only
four registers.
ADC Multiplexer Selection Register – ADMUX : For selecting the
reference voltage and the input channel.
ADC Control and Status Register A – ADCSRA : As the name says it
has the status of ADC and is also use for controlling it.
The ADC Data Register – ADCL and ADCH : The final result of
conversion is here.
ADC Channels
The ADC in ATmega32 has 8 channels that means you can take samples
from eight different terminal. You can connect up to 8 different sensors and
get their values separately.
Initialization.
We have to configure the ADC by setting up ADMUX and ADCSRA registers.
The ADMUX has following bits.

We will go for 2nd option, REFS REFS Voltage Reference


i.e. Our reference voltage 1 0 Selection
will be Vcc(5v). So we set 0 0
ARef internal Vref
Turned off
ADMUX=(1<<REFS0); 0 1 AVCC
1 0 Reserved
Internal 2.56
1 1
Voltage Reference
The ADCSRA Register.

•ADEN – Set this to 1 to enable ADC

•ADSC – We need to set this to one whenever we need adc to do a conversion.



•ADIF – This is the interrupt bit this is set to 1 by the hardware when conversion is
complete. So we can wait till conversion is complete by polling this bit like

//Wait for conversion to complete

while(!(ADCSRA & (1<<ADIF)));

The loop does nothing while ADIF is set to 0, it exits as soon as ADIF is set to one,
i.e. conversion is complete

•ADPS2-ADPS0 – These selects the Prescaler for ADC. As I said the ADC frequency
must be between 50KHz to 200KHz
We need to select division factor so as to get a acceptable frequency from our 16Mhz
clock. We select division factor as 128.So ADC clock frequency = 16000000/128 =
125000 = 125KHz (which is in range of 50KHz to 200KHz).
So we set ADCSRA as

ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
//Enable ADC with Prescalar=Fcpu/128
READING AN ANALOG VALUE

Unsigned int ReadADC(unsigned int ch)


{
//Select ADC Channel ch must be 0-7
ch=ch&0b00000111;
ADMUX|=ch;

//Start Single conversion


ADCSRA|=(1<<ADSC);

//Wait for conversion to COMPLETE


while(!(ADCSRA & (1<<ADIF)));

//Clear ADIF by writing one to it


ADCSRA|=(1<<ADIF);

return(ADC);
}

You might also like