You are on page 1of 47

Microcontroller And Interfacing

Samrat Subedi Kantipur Engineering College Samratsubedi@kec edu np
Samrat Subedi 1

Introductory Concept
- Introduction to Embedded System - Program writing, Compilation and simulation writing - Introduction to Top View Simulator

Basic inputs and outputs Interfacing Seven segment Display Keypad Interfacing Basics f B i of LCD and it interfacing d its i t f i Timer and Counter Serial Communications Interrupt Handling Introduction to Proteus
Samrat Subedi 2

Why do we need to learn y Microprocessors/controllers?

The microprocessor is the core of computer systems. Nowadays many communication, digital entertainment, portable devices, are controlled by them. A designer should know what types of components he needs, ways to reduce production costs and product reliable. .
Samrat Subedi 3

The necessary tools for a microprocessor/controller

CPU: Central Processing Unit I/O: Input /Output Bus: Address bus & Data bus Memory: RAM & ROM Timer Interrupt Serial Port Parallel Port
Samrat Subedi 4

General-purpose microprocessor
CPU for Computers No RAM, ROM, I/O on CPU chip itself ExampleIntels x86, Motorolas 680x0

CPU GeneralPurpose Microprocessor

Data Bus

Many chips on mothers board



I/O Port


Serial COM Port

Address Bus General-Purpose Mi G lP Microprocessor S t System

Samrat Subedi 5

Microcontroller :
A smaller computer On-chip RAM, ROM, I/O ports... ExampleMotorola s 6811, Intel s 8051, Zilog s ExampleMotorolas 6811 Intels 8051 Zilogs Z8 and PIC 16X

CPU I/O Port


A single chip g p
Serial Timer COM Port Microcontroller

Samrat Subedi

Microprocessor vs Microcontroller vs.

Microprocessor CPU is stand-alone, RAM, ROM, I/O, timer are separate designer can decide on the amount of ROM, RAM and I/O ports. p expansive versatility general-purpose Microcontroller CPU, RAM, ROM, I/O and timer are all on a single chip fix amount of on-chip ROM, RAM, I/O ports cost for applications in which cost, power and space are critical single-purpose

Samrat Subedi

Embedded System
Embedded system means the processor is embedded into that application. An embedded product uses a microprocessor or microcontroller to do one task only. onl In an embedded system, there is only one application software that is typically burned into ROM. Exampleprinter, keyboard, video game player

Samrat Subedi

Three criteria in Choosing a Microcontroller g

1. 1 meeting the computing needs of the task efficiently and cost effectively speed, the amount of ROM and RAM, the number of I/O ports and timers size, packaging, power consumption timers, si e packaging po er cons mption easy to upgrade p cost per unit 2. availability of software development tools assemblers, debuggers, C compilers, emulator, simulator, technical support 3. wide availability and reliable sources of the microcontrollers.

Samrat Subedi

Block Diagram
External interrupts Interrupt Control On-chip ROM for program code

On-chip RAM

Timer 1 Timer 0

Counter p Inputs

CPU Serial Port


Bus Control

4 I/O Ports

P0 P1 P2 P3


Address/Data Add /D t
Samrat Subedi 10

Samrat Subedi


Pin Description of the 8051

P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1 6 P1.7 RST (RXD)P3.0 (TXD)P3.1 (INT0)P3.2 (INT1)P3.3 (T0)P3.4 (T1)P3.5 (WR)P3.6 (WR)P3 6 (RD)P3.7 XTAL2 XTAL1 GND 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 Vcc P0.0(AD0) ( ) P0.1(AD1) P0.2(AD2) P0.3(AD3) P0.4(AD4) P0.5(AD5) P0 5(AD5) P0.6(AD6) P0.7(AD7) EA/VPP ALE/PROG PSEN P2.7(A15) P2.6(A14) P2.5(A13) P2.4(A12) P2 4(A12) P2.3(A11) P2.2(A10) P2.1(A9) P2.0(A8)


Samrat Subedi


Feature of 8051
Eight bit CPU with register A and B 16 bit Program Counter bi P C Internal Ram of 128 bytes 4 Kb Flash Memory 4 I/O Ports Two 16 bit Timer (T0 and T1) Full duplex Serial Communication Six Interrupts

Samrat Subedi


Pins of 80511/4 80511/4

Vccpin 40 Vcc provides supply voltage to the chip. The voltage source is +5V. GNDpin 20ground XTAL1 and XTAL2pins 19,18

Samrat Subedi


Figure (a). XTAL Connection to 8051

Using a quartz crystal oscillator We can observe the frequency on the XTAL2 pin. C2 XTAL2 30pF C1 XTAL1 30pF p GND

Samrat Subedi


Pins of 80512/4 80512/4

RSTpin 9reset It is an input pin and is active highnormally low. low
The high pulse must be high at least 2 machine cycles.

It is a power-on reset. power on

Upon applying a high pulse to RST, the microcontroller will reset and all values in registers will be lost.

Samrat Subedi


Figure (b). Power-On RESET Circuit PowerVcc

+ 10 uF 30 pF 11.0592 MHz 8.2 82K 30 pF 18 X2 31 19 EA/VPP X1


Samrat Subedi


Pins of 80513/4 80513/4

/EA i 31external access EApin 31 t l
There is no on-chip ROM in 8031 and 8032 . The /EA pin is connected to GND to indicate the code is stored externally. /PSEN ALE are used for external ROM. For 8051, /EA pin is connected to Vcc. / means active low. /PSENpin 29program store enable This is an output pin and is connected to the OE pin of the ROM. ROM
Samrat Subedi 18

Pins of 80514/4 80514/4

ALEpin 30address latch enable It is an output pin and is active high. 8051 port 0 p p provides both address and data. The ALE pin is used for de-multiplexing the address and data by connecting to the G pin of the 74LS373 latch. l t h I/O port pins The four ports P0 P1 P2 and P3 P0, P1, P2, P3. Each port uses 8 pins. bi-directional. All I/O pins are bi directional.
Samrat Subedi 19

Pins of I/O Port

The 8051 has four I/O ports Port 0 pins 32-39P0P0_0P0_7 Port 1pins 1-8 P1P1_0P1_7 P 2 i 21 28 P2P2 0 P2 7 Port 2pins 21-28P2P2_0P2_7 Port 3pins 10-17P3P3_0P3_7 Each port has 8 pins. Named P0_X X=0,1,...,7, P1_X, P2_X, P3_X ExP0_0 is the bit 0LSBof P0 ExP0_7 is the bit 7MSBof P0 These 8 bits form a byte. Each port can be used as input or output (bi direction). (bi-direction)

Samrat Subedi 20

Hardware Structure of I/O Pin

Each pin of I/O ports Internal CPU buscommunicate with CPU A D latch store the value of this pin D l t h is controlled by Write to latch latch i t ll d b W it t l t h Write to latch1write data into the D latch 2 Tri-state buffer TB1: controlled by Read pin Read pin1really read the data present at the pin TB2: controlled by Read latch : co o ed ead a c Read latch1read value from internal latch A transistor M1 gate Gate=0: Gate 0: open Gate=1: close Samrat Subedi 21

D Latch:

Samrat Subedi


A Pin of Port 1
Read latch TB2 Vcc Load(L1) Internal CPU bus Write to latch

Clk Q

P1.X pin M1

TB1 Read pin

Samrat Subedi

8051 IC

Writing 1 to Output Pin P1 X 1 P1.X

Read latch TB2 Vcc Load(L1) 2. output pin is

1. write a 1 to the pin i h i

Internal CPU bus Write to latch

Vcc 1 0
M1 P1.X
Clk Q

P1.X pin

output 1

TB1 Read pin

Samrat Subedi

8051 IC

Writing 0 to Output Pin P1 X 0 P1.X

Read latch TB2 Vcc Load(L1) 2. output pin is

1. write a 0 to the pin i h i

Internal CPU bus Write to latch

ground 0 1
M1 P1.X
Clk Q

P1.X pin

output 0

TB1 Read pin

Samrat Subedi

8051 IC

Reading High at Input Pin High

Read latch 1. write a 1 to the pin MOV P1,#0FFH Internal CPU bus TB2 Load(L1) 1 1 Vcc 2. MOV A,P1 external pin=High

P1.X 0 M1

P1.X pin

Write to latch


TB1 Read pin 3. Read p pin=1 Read latch=0 Write to latch=1 8051 IC Samrat Subedi 26

Reading Low at Input Pin Low

Read latch 1. write a 1 to the pin MOV P1,#0FFH P1 #0FFH Internal CPU bus 1 0 TB2 Load(L1) Vcc 2. MOV A,P1 external pin=Low

P1.X 0 M1

P1.X pin

Write to latch


TB1 Read pin 3. Read p pin=1 Read latch=0 Write to latch=1 8051 IC Samrat Subedi 27

Other Pins
P1, P2, P1 P2 and P3 have internal pull up resisters pull-up resisters. P1, P2, and P3 are not open drain. P0 has no internal pull up resistors and does not pull-up connects to Vcc inside the 8051. P0 is open drain. Compare the figures of P1.X and P0.X. However, for a programmer, it is the same to program P0, P1, P2 and P3. d All the ports upon RESET are configured as output.
Samrat Subedi 28

A Pin of Port 0
Read latch TB2

Internal CPU bus Write to latch

Clk Q

P0.X pin M1

TB1 Read pin

Samrat Subedi

8051 IC

Port 0 with Pull-Up Resistors Pull- p

Vcc 10 K

P0.0 DS5000 P0.1 P0.2 8751 P0.3 P0 3 P0.4 8951 P0.5 P0.6 P0.7 P0 7

Port 0

Samrat Subedi


Port 3 Alternate Functions

P3 Bit P3 . 0 P3.1 P3 1 P3.2 P3.3 P3 3 P3.4 P3.5 P3.6 P3.7 Function RxD TxD INT0 INT1 T0 T1 WR RD Pin 10 11 12 13 14 15 16 17


Samrat Subedi

RESET Value of Some 8051 Registers:

Register PC ACC B PSW S SP DPTR RAM are all zero. zero.
Samrat Subedi

Reset Value 0000 0000 0000 0000 0007 0000


Registers g
A B R0 DPTR R1 R2 R3 R4 R5 R6 R7 Some 8-bitt Registers of the 8051 Some 8051 16-bit Register PC PC DPH DPL

Samrat Subedi


Memory mapping in 8051

ROM memory map in 8051 family

0000H 0000H


8751 AT89C51

1FFFH 8752 AT89C52

Samrat Subedi


RAM memory space allocation in the 8051 ll ti i th

7FH Scratch pad RAM

30H 2FH Bit-Addressable RAM 20H 1FH 18H 17H 10H 0FH 08H 07H 00H Register Bank 2 (Stack) Register Bank 1 Register Bank 3

Register Bank 0 R i B k

Samrat Subedi


Stack in the 8051

The register used to access the stack is called SP (stack pointer) register. The stack pointer in the 8051 is only 8 bits wide, which means that it can take value 00 to FFH. When 8051 powered up, the SP register contains value 07.
7FH Scratch pad RAM 30H 2FH Bit-Addressable RAM 20H 1FH 18H 17H 10H 0FH 08H 07H 00H

Register Bank 3 Register Bank 2 (Stack) Register Bank 1 Register Bank 0

Samrat Subedi


Writing Simple Program

#include<at89x51.h> #include<at89x51.h> void delay(int a) delay(int { float i; for (i=0;i<100*a ;i++ ); (i ;i<100*a ;i } void main() { while(1 while(1) { P1_1=!P1_1; =!P1 delay(10); delay(10); }}

Save as --

c:\ c:\program Samrat \SDCC\bin>led. h files Subedi \bin>led. files\SDCC 37

See here!!!
C:\ C:\program files\SDCC\bin>SDCC led.c files\SDCC\ led c
C:\ C: C \program fil \SDCC\bi packihx l d ih l d h files\ files SDCC\bin> bin>packihx led.ihx>led.hex kih you will See packihx: packihx: read 107 lines, wrote 58: OK. 58:

Samrat Subedi


And then

Atmel 89C52 12000

Samrat Subedi


Samrat Subedi


Samrat Subedi


Samrat Subedi


Samrat Subedi


Samrat Subedi


Samrat Subedi


Try this
#include<at89x51.h> #include<at89x51.h> h> void delay(int a) delay(int { float i; for (i=0;i<100*a ;i++ ); (i ;i<100*a ;i } void main() { char in=0x04; in=0 04; P0=in; while(1 while(1) { in++; delay(20); delay(20); P0=in; } }

Samrat Subedi



since the TTL output can't source can t above 1mA so the LED will be very dim. he LED will conduct heavily at about 2V and the extra 3V has to be dropped in the TTL circuitry. This causes high power circuitry dissipation in the TTL or the LED fails. The resistor limits the current The resistance can be current. calculated by assuming its voltage is about 2.5V and the TTL output is 0.9V. For 2.2V LED, 1.9V is across the resistor so the 220ohm would limit the current to 8 6mA 8.6mA (1.9/220). For 1.7V LED, 2.4V is across the resistor so it would limit the current to 10.9mA (2.4/220). The resistor should not less than 100ohm or the LED would fail.
Samrat Subedi 47