Académique Documents
Professionnel Documents
Culture Documents
Microcontroller
Introduction
A microcontroller is a small computer on a single
Integrated Circuit containing a processor core, memory,
and programmable input/output peripherals
ATmega16
Desktop PC
Clock Frequency
16 MHz
3GHz
8 bits
32bits
RAM
1KB
1GB
ROM
16KB
160GB
I/O
32 pins
keyboard
20mW
65W
Power
Consumption
Society Of Innovative Electrical
Professionals
Microcontroller
Microprocessor
CPU is stand-alone, RAM, ROM, I/O,
timer are separate
expansive
Less expensive
versatility
general-purpose
single-purpose
Features
Microcontrollers are electronic circuits that can be
programmed to carry out a vast range of tasks
Multifunction, Bi-directional General Purpose I/O
ports with configurable, built-in pull-up resistors
In-System Programmable using ISP, JTAG, or High
Voltage methods
AVR Microcontroller
Lots of microcontroller families
8051 , PIC , AVR ,ARM , etc.
http://www.instructables.com/id/How-to-choose-aMicroController/
Cost: AVR = PIC > 8051 (by 8051 I mean the 8051 family)
Availability: AVR = PIC <8051
Speed: AVR > PIC > 8051 ( preferred in Robotics)
Efficient RISC (Reduced Instruction Set Computer)
architecture, almost 10 times faster than CISC
less instructions you don't need complex instruction decoder, so RISC cores
need less transistors (chip area), less power (excelent for mobile units) and
higher core speeds than CISCs for same technology.
Society Of Innovative Electrical
Professionals
AVR Microcontroller
Atmel's AVR microcontrollers have a RISC core
running single cycle instructions and a well-defined
I/O structure that limits the need for external
components. Internal oscillators, timers, SPI, pull-up
resistors, pulse width modulation, ADC, analog
comparator and timers are some of the features
you will find in AVR devices.
Apart form this almost all AVRs support In System
Programming (ISP) i.e. you can reprogram it without
removing it from the circuit . Also the programmer
used for ISP is easier to build compared to the
parallel programmer required for many old uCs.
Society Of Innovative Electrical
Professionals
Software
10
Different AVRs
AVRs are generally classified in four board groups
tinyAVRs
megaAVRs
XMEGA AVRs
Application Specific AVRs
program memory
Pin packages
11
Different AVRs
12
Software Required
AVR Studio 4 :
we generally use AVR Studio for this
Many other software are also available for the
purpose like MikroC you can use any of them
The greatest advantage of AVR Studio is that it is
provided by Atmel as a freeware
AVR Studio 4 provides a complete set of features
including debugger , Debugging is mostly not
available in other software
13
14
Pin Description
Vcc: Digital Voltage
GND: Ground
PortA (PA7 ..PA0):
Port A also serves as an 8-bit bi-directional I/O port,
if the A/D Converter is not used.
Port pins can provide internal pull-up resistors
(selected for each bit)
Alternate Functions of Port A
Port A serves as the analog inputs to the A/D
Converter.
15
16
17
18
XTAL1:
19
20
Pin Description
Following pins are used for programming of AVR
MOSI, PB5
MISO, PB6
SCK, PB7
RESET
AREF
Care should be taken while using these pins for different
purposes
If these pins are used as an input and are connected to
some sensor or a switch and while programming these are
being pulled low or high controller will not be programmed
So if these pins are being used for some purpose and are
connected to some sensor/switch it should be disconnected
while programming
Society Of Innovative Electrical
Professionals
21
22
I/O Ports
23
I/O Ports
There are three registers associated with each port
24
25
26
27
28
29
30
31
Example 1
Take input from PORTA add it with 5 and send it as an
output on port D
void main()
{DDRA=0b00000000; //configures Port A as input
DDRD=0b11111111; //configures Port D as output
unsigned int x; //declare variable x
x=PINA; //reads and stores input at Port A in variable x
PORTD=x+5; //Multiply x with 5 and outputs it on Port D
}
32
33
34
Programming in C
Following are the few main reason for
programming in C rather then in Assembly
It is easier and less time consuming
C is easier to modify and update
You can use codes available in function libraries
C code is portable to other microcontrollers with
little or no modifications
35
36
C language Statements
37
38
39
Example
Write a AVR program to get a byte of data
from Port B, and then send it to Port C.
40
41
42
43
AND(&)
It should not be confused with the logical
AND(&&) operator
0x35 & 0x0F = 0x05
00110101 & 00001111=00000101
OR(|)
It should not be confused with logical OR(||)
operator
0x04 | 0x68=0x6C
00000100 | 1101000=1101100
Society Of Innovative Electrical
Professionals
44
XOR(^)
0x54 ^ 0x78 = 0x2C
1010100 ^ 1111000 = 0101100
Inverter(~)
~0x55 = 0xAA
~01010101 = 10101010
45
46
47
48
49
#include<avr/io.h>
unsigned char x;
int main(void)
{
DDRA=0b11110000
while(1)
{
x=PINA&0b00001111;
x= x<<4;
PORTA=x&11110000;
}
}
Society Of Innovative Electrical
Professionals
50
51