Vous êtes sur la page 1sur 67

Chapter 1

Introduction to Microcontroller
Review on Microprocessor System

 The µ P system components include a


CPU, RAM, ROM and I/O ports
 Its interconnections comprise an address
bus, a data bus and control lines
 These buses are required to handle the
data transfer between the CPU, memory
and ports

2
Review on Microprocessor System

ROM EEPROM

RAM

Serial I/O

A/D Input Input Parallel I/O


and and
Analog

r oss ec or por ci M
output output
I/O ports ports Timer
D/A
PWM

Figure 1.1: Microprocessor-based system

3
Review on Microprocessor System

 Program execution-
- The ROM and RAM contain program and data in
numbered locations.
- Each instruction and operand are stored in
sequential locations.
- The program instruction codes are fetched into the
CPU and decoded.

4
Review on Microprocessor System

 Program execution (con..)


- The CPU then sets up the internal and external
control lines as necessary and carries out the
operation specified in the program.
- The instructions are executed in order of their
addresses, unless the instruction itself causes a jump
to another point in the program, or an interrupt is
received.

5
Review on Microprocessor System
RAM
CPU 1 Contents Address

Program counter 3274 Instruction 3724


2 Operand 3725
Instruction register Inst. Code Operand 3726
4 Next instr. 3727
Data register Operand etc 3728
3
5

Data
Processing 6
Data byte 5821
7 8 Data byte 5822
Data register Result Data byte 5823

Figure 1.2: Program execution sequence 6


Review on Microprocessor System
 PC Engineering Applications
- Can be used as a standard hardware platform in a variety of
engineering systems by fitting special interfacing hardware and
programming the PC to control an external system through I/O.
- increasingly used in manufacturing systems; control a machine
tool, robot or assembly system.
- considered to be powerful computer machines.

7
Review on Microprocessor System

 PC Engineering Applications
- PC also has the advantage of using a standard
operating system and programming languages that allow
control programs to be written in high level languages
such as ‘C’ or Visual Basic.
- Graphical programming tools are also available for
designing control and instrumentation applications
without any conventional programming at all.

8
The Microcontroller
 The microprocessor can provide, in a simplified form, all
the main elements of the conventional microprocessor
system on one chip.
 Less complex applications can be designed and built
quickly and cheaply.
 A working system can consist of a microcontroller chip
and just a few external components for feeding data in
and out, and generating the clock.

9
The Microcontroller
 A Microcontroller Application
- Used to control a small system.
- calculator, automatic washing machine, message
display, electronic lock or similar application.

10
The Microcontroller
ROM EEPROM

RAM

Analog Serial I/O


In A/D

Parallel I/O

er o C UP C Timer

Filter
Analog
Microcontroller PWM Out
Digital
PWM

Figure 1.3: Microcontroller-based system

11
The Microcontroller
 Programming a Microcontroller
- Normally have ROM program memory, into which the
program must be loaded before the chip is placed in
the application circuit.
- The program is written and converted to machine code
in the host, and downloaded via a serial data link.
- The chip is then removed from the programming unit
and inserted into the application hardware.

12
Microcontroller Vs Microprocessor

13
Microcontroller Vs Microprocessor
Perspective Microprocessors Microcontrollers

Hardware • Single-chip CPU • Single-chip computer


Architecture • High RAM-ROM ratio • High ROM-RAM ratio
• External components are required • Built-in interrupt, timer and I/O
to implement interrupt, timer and devices
I/O operations

Applications • Used as CPU in computer • Used in control-oriented systems


systems for general processing requiring minimum components

Instructions Set • Powering and processing • Simple and compact instructions


Features intensive instructions • More suitable for single-bit I/O
• More suitable for large volume operations
data handlings • AND, OR, XOR operations at bit
• AND, OR, XOR operations at bit level is easy
level is less easy

14
Microcontroller Vs Microprocessor

Microprocessor Microcontroller

 high density  Low density


 higher PCB area  Less PCB area
 high cost  Low cost
 Flexible  Specific Architecture
 high power consumption  Low power consumption
 More reliable

15
Picking a PIC
Microcontroller
Choosing a PIC microcontroller for your application
requires taking into account a variety of factors.

These factors include:


 The number of I/O pins required
 The peripherals needed (i.e. CCP module, USART, etc.)
 The memory size (program memory, RAM, EEPROM)
 Microcontroller speed
 Physical size (form factor)

16
PIC16F87x
Features 16F87x
Instruction width 14 bits
Program Memory Up to 8K x 14
Data Memory (RAM) Up to 368 x 8
EEPROM Up to 256 x 8
Clock Speed Max 20 MHz
Instruction Set 35

17
8-bit Microcontroller
 Motorola – M68HC05, HC08, HC11
 Intel – 8051
 Atmel
 Philips
 Dallas
 Cygnal
 Zilog
 etc
 Microchip – PIC
 Atmel - AVR

18
PIC Product Overview

19
Current Main
Architecture
Von Neuman
Architecture

Harvard Architecture

20
8-bit PIC Microcontroller
 PIC microcontroller
architecture is based on
a modified Harvard
RISC instruction set that
provides an easy
migration path from 6 to
80 pins and from 384
bytes to 128K bytes of
program memory.
 By combining the RISC
features with a modified
Harvard dual-bus
architecture, Microchip's
fast and flexible 10 MIPS
21
Central Processing Unit (CPU)
 CPU is the brain of a microprocessor where all of the arithmetic and
logical operations are performed.
 CPU has a role of connective element between other blocks in the
microcontroller. It coordinates the work of other blocks and executes
the user program.
 The control unit controls the internal operations of the
microprocessor and sends out control signals to other parts of the
microprocessor to carry out the required instructions.
 A generic CPU has a set of registers (memory of CPU), an
arithmetic logic unit (ALU), memory interface and timing circuits

22
Central Processing Unit (CPU)
Control and Status registers
Program counter
Instruction register
….
….
Registers
ALU User-Visible Registers
Internal
Bus Accumulator
Address Register
Data Register
CU Flag Register
….

CPU

23
Central Processing Unit (CPU)
This is a unit which monitors and controls all processes inside the microcontroller. It
consists of several smaller subunits, of which the most important are:

•Instruction Decoder is a part of the electronics which recognizes program


instructions and runs other circuits on the basis of that. The “instruction set”
which is different for each microcontroller family expresses the abilities of this
circuit. It decodes the binary code in an instruction and passes the necessary
control signals to the CU to complete the operations specified by the instruction.

24
Central Processing Unit (CPU)
 Arithmetical Logical Unit (ALU) performs all
mathematical and logical operations upon data.
o Example of arithmetic operations are add, subtract,
multiply and divide. In low-end microprocessors, multiply
and divide may not be implemented.
o Example of logical operations are AND, OR, and XOR.
o The ALU also performs shift and rotate operations that
may either be arithmetic or logical in nature.

25
Central Processing Unit (CPU)
 Accumulator is a SFR closely related to the operation of
the ALU. It is a kind of working desk used for storing all
data upon which some operation should be performed
(addition, shift/move etc.). It also stores the results ready
for use in further processing. One of the SFRs, called a
Status Register (PSW), is closely related to the
accumulator. It shows at any given moment the “status”
of a number stored in the accumulator (number is
greater or less than zero etc.).

26
Central Processing Unit (CPU)
 Register
o Registers refer to a set of internal storage locations
within the CPU. These include:
1. Control and Status Registers that are used by the control
unit to control the operation of CPU, such as:
- Program Counter (PC)
- Instruction Register (IR)

27
Central Processing Unit (CPU)
2. User-Visible Registers that can be accessed by
programmer using machine language, such as:
- General-purpose Register
- Special Function Register
- Address Register
- Data Register
- Flag Register

28
Special Function Register (SFRs) & GPR.

29
Memory System
Memory is part of the microcontroller used
for data storage. The easiest way to
explain it is to compare it with a filing
cabinet with many drawers. Suppose, the
drawers are clearly marked so that it is
easy to access any of them. It is easy
enough to find out the contents of the
drawer by reading the label on the front of
the drawer.

Each memory address corresponds to one


memory location. The content of any
location becomes known by its addressing.
Memory can either be written to or read
from. There are several types of memory
within the microcontroller.

30
Memory System
 There are two groups of memory
Program memory – stores all the program code
and this memory is usually non-volatile (data is
not lost after the removal of power)

Data memory – stores the temporary user data


during the arithmetic and logical operations.

31
PIC Program
Memory – Flash
Program Memory

0000h -> 1FFFh = 8Kbyte of


memory size
8K = 8192 lines of
programming codes.
Each line of code only take 1
word (14bits) of memory size.

32
Memory System
 RAM (Random Access Memory) – a general
purpose memory which usually stores user data.
RAM is volatile.
 ROM (Read Only Memory) – usually holds
program of fixed user data. ROM are
programmed at the factory and their contents
cannot be changed by the user. ROM are only
useful if we have developed program and wish
to order several thousand copies of it.

33
Memory System
 EPROM (Erasable Programmable ROM) –
similar to ROM but it can be programmed using
a suitable programming device.
 Has a small clear window on the chip where the
data can be erased under a UV light.
 Many development versions of microcontrollers
are manufactured with EPROM memories where
the user program are usually stored.

34
Memory System
 Some versions of EPROM, known as OTP (One
Time Programmable), can be programmed using
a suitable programmer device but these
memories cannot be erased.
 OTP memories cost less than the EPROM and it
is useful after a project has been developed
completely and it is required to make hundred of
copy of the program memory.

35
Memory System
 EEPROM (Electrical EPROM) – These memory can be
erased and also can be programmed under program
control. EEPROMs are used to save configuration
information, maximum and minimum values,
identification data etc.
 Some microcontrollers have built-in EEPROM memories
(e.g. PIC16F84 contains a 64-byte EEPROM memory
where each byte can be program and erase directly by
software).
 EEPROM memories are usually very slow.

36
Memory System
 Flash EEPROM – very popular and is used in
many microcontrollers (e.g. PIC16F77A contain
8K bytes of flash memory) to store the data
program.
 The data on a flash EEPROM is erased and
programmed using a programming device.
 The entire contents of the memory should be
erased and then reprogrammed.
 Flash EEPROMs are usually very fast.

37
Bus
 System bus is a collection of wires carrying information
within a computer system. There are three types of
system busses: address, data and control busses.
 Address Busses are wires that carry the CPU-generated
address signals out to memory and to I/O devices. It is
an uni-directional.
 The size of the memory space that a microprocessor can
access depends on the number of address lines.

38
Bus
No of address lines Size of memory space

8 2 8 = 256
16 216 = 65536 = 64 K
20 2 20 = 1048576 = 1M
24 2 24 = 16777216 = 16 M
32 2 32 = 4294967296 = 4 G

39
Bus
 Data buses refer to data signals that travel out of and
into the microprocessor. Thus, data bus is bi-directional.
 The number of wires in the data bus depends on the
word size that the microprocessor operates with. An 8-bit
microprocessor will have a data bus consisting of 8 wires
while a 32-bit microprocessor will have a data bus with
32 wires.

40
Bus
 Control Bus consists of wires, some of which carry
signals from the CPU to external devices, while other
carry signals from external devices to the CPU. The
number of wire present in the control bus varies from
microprocessor to microprocessor.
 Example of control buses signals are READ/WRITE,
WAIT, READY and HOLD.

41
Bus

42
I/O Modules
 I/O modules provide interface between the computer
system and the ‘real world’. An external device
connected to an I/O modules is often referred to as a
peripheral device. Three broad classes of I/O devices
are:
 Human-readable I/Os: suitable for communication with
the user. E.g. Monitor, keyboard, mouse, joystick, video
display terminals, printer, scanner, microphone and
speaker

43
I/O Modules
 Machine-readable I/Os: Suitable for communicating with
equipment
E.g. magnetic disk, optical disk, sensors and actuators.
 Communication I/Os: Suitable for communication with
remote devices.
E.g. infra-red (IR) interface, radio frequency (RF)
interface, Bluetooth device and wireless LAN.

44
I/O Modules

45
Serial Communications
 Some microcontrollers contain hardware to implement a
serial asynchronous communications interface.
 The baud rate and the data format can usually be
selected in software.
 If serial input/output hardware is not provided, it is easy
to develop software to implement serial data transfer
using any I/O pin of a microcontroller.
 Some microcontrollers incorporate SPI(Serial 2 Peripheral
Interface) or IC bus interfaces. These enable a
microcontroller to interface to other comptible devices
easily.

46
Serial Communications

47
Serial Communication

48
The Clock
 All microcontrollers require an oscillator (clock) to
operate.
 Most microcontrollers will operate with a crystal and two
capacitors. Some will operate with resonators or with
external resistor-capacitor pair.
 Some microcontrollers have built-in resistor-capacitor
type oscillators and they do not require any external
timing components

49
PIC Clock Source.

The value of capacitors ranges from 15pf to 33 pf

50
The Clock

51
Watchdog Timer
 Watchdog is a mechanism which microcontroller uses to
defend itself against programs getting stuck. As with any
other electrical circuit, so with a microcontroller too can
occur failure, or some work impairment. Unfortunately,
microcontroller also has program where problems can
occur as well. When this happens, microcontroller will
stop working and will remain in that state until someone
resets it. Because of this, watchdog mechanism has
been introduced. After a certain period of time, watchdog
resets the microcontroller (microcontroller in fact resets
itself). Watchdog works on a simple principle: if timer
overflow occurs, microcontroller is reset, and it starts
executing a program all over again. In this way, reset will
occur in case of both correct and incorrect functioning.

52
Watchdog Timer

53
Analogue-to-digital Converter
 Some microcontrollers are equipped with analogue-to-
digital converter circuits. Usually these converters are 8
bits, but some microcontrollers have 10- or even 12-bit
converters.
 A/D converters usually generate interrupts when a
conversion is complete so that the user program can
read the converted data very quickly.
 A/D converters are very useful in control and monitoring
applications since most sensors produce analogue
output voltages.

54
Analogue-to-digital Converter

55
Analogue-to-digital Converter
 External signals are usually fundamentally different from those the
microcontroller understands (Ones and Zeros), so that they have to
be converted in order for the microcontroller to understand them.
An analogue to digital converter is an electronic circuit which
converts continuous signals to discrete digital numbers. This
module is therefore used to convert some analogue value into
binary number and forwards it to the CPU for further processing. In
other words, this module is used for input pin voltage measurement
(analogue value). The result of measurement is a number (digital
value) used and processed later in the program.

56
Program
 Microcontroller needs a program that would be
executed.
 A program is a logical sequence of instructions
stored in the memory of the system.
 In its operations, the CPU fetches an instruction
from memory, decodes the instruction and
carries out the operation or operations specified
by the instruction.

57
A Microcontroller 16F877A

58
World of Numbers

59
World of Numbers
Binary Number System

Hexadecimal Number System

60
World of Numbers
Hexadecimal to Decimal Number Conversion

Decimal to Binary Number Conversion

61
World of Numbers
Comparative table below contains the values of
numbers 0-255 in three different numbering systems.

Marking Numbers The hexadecimal numbering system


is along with binary and decimal number systems
considered to be the most important for us. It is easy to
make conversion of any hexadecimal number to binary
and it is also easy to remember it. However, these
conversions may cause confusion. For example, what
does the statement “It is necessary to count up 110
products on assembly line” actually mean? Depending on
whether it is about binary, decimal or hexadecimal, the
result could be 6, 110 or 272 products, respectively!
Accordingly, in order to avoid misunderstanding, different
prefixes and suffixes are directly added to the numbers.
The prefix $ or 0x as well as the suffix h marks the
numbers in hexadecimal system. For example,
hexadecimal number 10AF may look as follows $10AF,
0x10AF or 10AFh. Similarly, binary numbers usually get
the suffix % or 0b, whereas decimal numbers get the
suffix D.
62
World of Numbers
Byte
A byte or a program word consists of eight bits grouped together. All mathematical
operations can be performed upon them, like upon common decimal numbers. The
largest value has the leftmost bit called the most significant bit (MSB). The rightmost bit
has the least value and is therefore called the least significant bit (LSB). Since eight
zeros and units of one byte can be combined in 256 different ways, the largest decimal
number which can be represented by one byte is 255 (one combination represents
zero).
A nibble is referred to as half a byte. Depending on which half of the byte we are talking
about (left or right), there are “high” and “low” nibbles.

63
Summary
 The CPU communicates with the main system chip via a
shared set of address and data bus lines. The address
lines select the device and location for the data to be
transferred on the data bus.
 The microcontroller provides, in simplified form, most of
the features of a conventional microprocessor system on
one chip.

64
Summary
 The PC consists of data input, storage, processing and
output devices.
 The main unit is a modular system, consisting of the
motherboard, power supply, disk drives and expansion
cards containing interfacing circuits plugged into the
motherboard.
 The motherboard carries the microprocessor (CPU) chip,
RAM module, a BIOS ROM, ISD and keyboard interface.

65
Summary
 The microcontroller consist of:
 CPU
 Memory Unit
 Buses
 Clock (Timer, Watchdog)
 I/O Modules
 Serial Communication
 PWM (Pulse Width Modulation)
 A/D Converter

66
Summary
PIC Applications:
 Automotive industries
 Controlling home appliances
 Controlling industrial instruments
 Remote sensors
 Electric door locks
 Safety devices
 Smart cards
 Many more………….

67

Vous aimerez peut-être aussi