Вы находитесь на странице: 1из 27

AVR Microcontroller

Prepared by:
Eng. Ashraf Darwish

Ashraf.emad.darwish@gmail.com

Course Outline

Introduction to Microcontroller
AVR microcontroller Architecture.
Introduction to embedded C programming.
I/O Ports.
LCD & Keypad Interface.
Interrupts .
Timers/Counters
Stepper Motors .
Analog to digital converter ADC.
Serial Communication USART.

Introduction

What is the difference between


Microprocessor &
Microcontroller ?
Microprocessor

Microcontroller

CPU + ALU on a single chip

CPU + ALU + Memory + I/O+


on the same chip

Faster

Slower

Stand alone device

General purpose

More Expensive

Cheaper

Ram, Rom, I/O peripheral and


Timers are separate

Ram, Rom, I/O peripheral and


Timers are on the same chip

Multi-operation

Single Operation

Introduction
Microprocessor

Introduction
Microcontroller

Memory Types

The main types of memory are :


1. Random Access Memory (RAM)
I. Static RAM (SRAM)
II. Dynamic RAM (DRAM)
2. Read Only Memory

(ROM)

I. ROM(ROM)
II. PROM(Programmable ROM).
III. EPROM (Erasable PROM).
IV. EEPROM(Electrically Erasable PROM).
3. Flash memory

It is non-volatile memory that stores the program code and it


can be electrically erased

Microcontroller Types

There are three well known


manufacturer for Microcontrollers

ATMEL

(8051, 8052, AVR families )

Microchip
Motorola

(PIC)

Microcontroller series
AT tiny

AT mega

AT xmega

0.58kB program
memory

4256kB program
memory

16384kB program
memory

632-pin package

28100-pin package

4464100-pin
package

Limited features

Extended features

More extended
features

Why AVR ?

Cost Effective.

High capabilities

High speed in compare with 8051


series

User friendly

Work cycle
1.

Write your Idea and determined the needed hardware


components .

2.

Start writing your C code in Codevision and debug your


syntax errors.

3.

Build your code to generate hex file

4.

Simulate your project in proteus to see if there are any


logic error, and ensure that the logic flow of your program
is Okey.

5.

Use your Programmer kit to write your hex file on the chip.

6.

Build up your PCB layout of your Circuit .

AT mega 16 Features

Four I/O ports (32 pins)


16 KB flash memory
512 B EEPROM
Two 8-bit timers
One 16-bit timer
Real time counter
4 PWM channels
8 channel ADC
Analog comparator
Tow wire serial interface
USART
Master/Slave SPI
Six sleep modes

Pins Configuration

Pin Description
Pin

Description

Vcc & GND

5 V , 0 V Supply for the chip

4 I/O Ports

PORTA, PORTB, PORTC, PORTD Each port have 8


pins for general purpose input / output.

RESET

reset the IC

XTAL1 ,
XTAL2

for crystal oscillator

AVCC

Supply voltage for ADC

AREF

is the analog reference pin for the ADC

Embedded C programming
# Include Files: according to
Codevision

AVR header files:


mega8535, mega16, 902313, tiny22,ect.

C header files :

math.h, string.h, stdlib.h, stdio.h,...ect.

Other header files :

Delay.h, lcd.h, spi.h, I2C.h, Gray.h,ect.

Embedded C
programming

C functions
Main

function : void main ( void )

Other functions
Function prototype before the main function

ex :int Sum (int x, int y );


Function itself
ex : int Sum ( int x , int y )
{
int z;
z= x + y ;
return (z);
}

Embedded C
programming

Variable Declaration:
Type
Size (Bits) Range
bit
1
0,1
char
8
-128 to 127
unsigned char 8
0 to 255
signed char 8
-128 to 127
int
16
-32768 to 32767
short int 16
-32768 to 32767
unsigned int 16
0 to 65535
signed int 16
-32768 to 32767

Embedded C
programming

Variable Declaration:
Type
Size (Bits) Range
long int
32 -2147483648 to
2147483647
unsigned long int 32 0 to 4294967295
signed long int
32 -2147483648 to
2147483647
float
32 1.175e-38 to
3.402e38
double
32 1.175e-38 to
3.402e38

Embedded C
programming

Operators:
Assignment operator (x=y) (+=, -=, /=, *=)
Increment/decrement operator ++/- Equal operator (==)
Less than (<) less than or equal (<=)
Greater than (>) less than or equal (>=)
Not equal (!=)
Logical operators:
And (&&)
Bitwise AND (&)
Or (||)
Bitwise OR(|)
Not (!)
Bitwise XOR(^)
Complement (~)
Right shifted (>>)
Left shifted (<<)

Embedded C
programming

If Statement:

If(condition is true)
{
Write your code
}
Else if (another condition is true)
{
Write your code
}
:
.
Else
{
Write your code
}

Embedded C
programming

While statement

While(condition)

{
Do whatever here
}
Do

{
}while(condition);

ATmega16 I/O Ports


DDRx

To select the port (pins ) to


work either input or output

DDRA = 0xFF

output

DDRA = 0x00

input
output on

PORTx

To write value to selected


port

PORTA = 0x55
port A
PORTA.3=1
PinA.3

PINx

To read value from selected


port

Variable = PINA
Variable = PINA.1

out on

Embedded C
programming

Sample Code :
# include <ATmega16.h>
#include <delay.h>
void main (void)
{
DDRA=0x00
DDRB=0xFF
while (1)
{
if (PINA.0==1)
{
PORTB=0xFF
while (PINA.0=1)
{;}
}
if (PINA.0==0)
{
PORTB=0x00;
while (PINA.0=0)
{;}
}
}
}

Embedded C
programming

While statement

While(condition)

{
Do whatever here
}
Do

{
}while(condition);

Embedded C
programming

While statement

While(condition)

{
Do whatever here
}
Do

{
}while(condition);

Embedded C
programming

While statement

While(condition)

{
Do whatever here
}
Do

{
}while(condition);

Embedded C
programming

While statement

While(condition)

{
Do whatever here
}
Do

{
}while(condition);

Embedded C
programming

While statement

While(condition)

{
Do whatever here
}
Do

{
}while(condition);

Вам также может понравиться