Introduction to Microcontrollers

students.iitk.ac.in

Introduction to Microcontrollers

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

Introduction to Microcontrollers

Electronics Club IIT Kanpur

Ankit Gupta

March 18, 2010

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Aim of this lecture

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

What are the Microcontrollers

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Aim of this lecture

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

What are the Microcontrollers

Applications of Microcontrollers

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Aim of this lecture

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

What are the Microcontrollers

Applications of Microcontrollers

How to program a Micocontroller

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Contents

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

1 Computers and Microcontrollers

Computers

Microcontrollers

2 AtMega 16

Port I/O

Programming ATmega 16

Applications

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Computers

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

Visibly distinguishable parts :

CPU Data Processing, Storage, Power Distribution ,Hard

Disk

I/O Devices Keyboard, Mouse, Monitor, etc

USB/Serial/Parallel Port

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Microprocessor

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

Figure: Pentium 4

Capable of doing

thousands of

boolean operations

every second

NO STORAGE

MEMORY

NO

INPUT/OUTPUT

DEVICE

Only pins

Present speed ?

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Lets shrink it to a chip

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

Computers Microcontrollers

RAM Working Registers

Memory

Hard Disk

Kbd/Mouse/Monitor I/O Pins

Serial/Parallel/USB USART/SPI

Read following article on wikipedia

http://en.wikipedia.org/wiki/Microcontroller

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Microcontrollers

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

What are Microcontrollers?

Integrated Circuits (ICs)

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Microcontrollers

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

What are Microcontrollers?

Integrated Circuits (ICs)

Programmable You can define what they do.

This is what makes them different from other simpler ICs,

such as AND gates, inverters, etc

Logic tables relating Input and Output can be defined by

you

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Microcontrollers

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

What are Microcontrollers?

Integrated Circuits (ICs)

Programmable You can define what they do.

This is what makes them different from other simpler ICs,

such as AND gates, inverters, etc

Logic tables relating Input and Output can be defined by

you

Many useful features built in to the uC itself

This is what makes them different from the processors on

a computer (called a microprocessor)

Many of the simpler features built into a motherboard are

located in the microcontroller itself, making it

self-sufficient.

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Microcontroller in Everyday Life

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

Figure: Applications

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Microcontroller in Everyday Life

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

Figure: Application of Microcontroller in Automobile

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Microcontroller in Everyday Life

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

Figure: Example of Microwave oven

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Microcontroller in Everyday Life

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

Doesn’t need a monster with 3.2 GHz Intel Core 2 Quad

extreme processor with 2 GB Dual channed DDR3 RAM,

320 GB HDD and Dedicated nVIDIA GEFORCE graphics

!!!.

What is of main concern is cost and size.

What it will be running through out its life is a simple

program that is hardly more than 4KB in size, requiring

less than 128 Bytes of RAM to store few varriables

Electronics Club IIT Kanpur

Introduction to Microcontrollers


AtMega 16

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

40 Pins

4 PORTs

A,B,C,D

4X8 I/O pins

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Ports

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Registers

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

The ports are bi-directional: Each pin can be individually

configured for input or output.

For each port (x = A, B, C or D), there are three relevant

registers.

DDRx

set a specific port pin to output (1) or input (0)

PINx

to read input data from port

PORTx

to write output data to port

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Examples

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

x=A,B,C,D

DDRx

DDRA=00001111; or DDRA=0x0F;

Pin 0,1,2,3 PORTA assigned as output and pin 4,5,6,7 as

output

PINx

DDRB.0=0;

first assign Pin o of port B as input a=PINB.0;

a will store the current value of Pin 0 of port B

PORTx

DDRA=0xFF or DDRA=11111111;

first assign Port A as output PORTA=0xD7; or

PORTA=11010111;

pin 0,1,2,4,6,7

Electronics Club

willIIThave Kanpuroutput Introduction

high

toand Microcontrollers

pin 3 and 5 will have


Let’s write a code

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

Problem:

Suppose we want to this led glow for 500 ms and remain off for

500 ms and repeat and assume led is connected to PORTA.0

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

Code

void main()

{

DDRA=0x00;// or DDRA.0=0

}

{

PORTA=0b00000001;

delay_ms(500);

PORTA=0b00000000;

delay_ms(500);

}

SetPortDirection();

will it blink once ?

Electronics Club IIT Kanpur

Introduction to Microcontrollers


Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

Introduce a while loop

void main()

{

DDRA=0x00;// or DDRA.0=0

while(1)

{

PORTA=0b00000001;

delay_ms(500);

PORTA=0b00000000;

delay_ms(500);

}

}

SetPortDirection();

Electronics Club IIT Kanpur

Introduction to Microcontrollers


More coding

Introduction

to Microcontrollers

1.Binary number display

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

Electronics Club IIT Kanpur

Introduction to Microcontrollers


More coding

Introduction

to Microcontrollers

2.Moving train pattern

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

Electronics Club IIT Kanpur

Introduction to Microcontrollers


How to Program a Microcontroller

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

Steps are

1 Create a project in CVAVR and writing a program in

C

CVAVR

2 Convert/compile it into machine language (.hex

format)

CVAVR

3 Download the hex file in ATMega 16

AVR Studio

For uploading ,we need a programmer

Electronics Club IIT Kanpur

Introduction to Microcontrollers


More ..

Introduction

to Microcontrollers

Electronics

Club IIT

Kanpur

Computers

and Microcontrollers

Computers

Microcontrollers

AtMega 16

Port I/O

Programming

ATmega 16

Applications

Timers

ADC

LCD(Character/Graphical/Mobile lcd)

Motors

UART/SPI/I2c/PS2)

EEPROM,MMC

Electronics Club IIT Kanpur

Introduction to Microcontrollers