You are on page 1of 38

BASICS OF MICROPROCESSORS

Goals of the Chapter

• To introduce the architecture of microprocessors and


microcontrollers
• To introduce the programming and interfacing techniques
• To understand the interfacing circuits for various
application
INTRODUCTION
For a simple control problem
• it is possible to solve it by an electronic control system involving
combinational and sequential logic integrated circuits. Eg: traffic light
system and Simple security protection system for a house
For more complex situation
• A more satisfactory approach in complex digital design involves the
use of a microprocessor-based system to implement a software
solution.
Software is a procedural program consisting of
• a set of instructions to execute logic and aritmetic functions and
• to access input signals and to control output signals.
An advantage of a software solution is that, with out making changes in
hardware, the program can be easily modified to alter the mechatronic system
functionality.
MICROPROCESSOR AND MICROCOMPUTER
 A microprocessor is a single, Very-Large Scale Integration (VLSI) chip
that contains many digital circuits that perform arithmetic, logic,
communication and control functions.
 When a microprocessor is packaged on a printed circuit board with
other components, such as interface and memory chips, the resulting
assembly is reffered to as a microcomputer or single-board computer.
 Microcomputer has four major sections
I. CPU: to carry out programs instructions
II. Input & Output interfaces: to handle communication between
the computer and the outside world.
III. Memory: to hold program instructions and data.
IV. System Bus: Digital signals move from one section to another
along paths
MICROCOMPUTER
MICROCOMPUTER -- CENTRAL PROCESSING UNIT- CPU
• Processes the data by executing a program stored in the memory.
• Consists of:
 Arithmetic and Logic Unit (ALU):- performs the data manipulations
or executes mathematical functions on data structured as binary
words.
 Registers:- are memory locations with in the microprocessor and
used to store information involved in program execution.
 Control unit:- determines the timing and sequence of operations.
Responsible for the control of address, data and control buses.
CENTRAL PROCESSING UNIT (CPU) -- REGISTER
1. Accumulator register: temporary holding register for data to be
operated on by ALU and after operation holds the results.
2. Status register/flag register: carries information concerning the
results of the latest process carried out in ALU.
3. Program counter register: it allows the CPU to keep track of its
position in the program.
4. Memory address register: contains the address of data.
5. Instruction register: stores instructions.
6. General purpose register: temporary storage of data or addresses,
involved in the transfer between registers.
7. Stack pointer register: content of this register form an address which
defines the top of stack in RAM. Stack is a special area of the
memory
MICROCOMPUTER – SYSTEM BUSES
Digital signals move from one section to another along pathes called
system Buses.
 A bus in a physical sence, is just a number of conductors along which
electrical signals can be carried.It might be tracks on a printed circuit
board or wires in a ribbon cable.
 Data bus: Used to transport data from/to the CPU and the memory or the
input/output devices. Data length could be 4, 8, 16, 32, or 64 bit.
 Adress bus:- carries signal which indicates where data is to be found.
Each memory location has an address that must be specified before the
contents of that location can be accessed.
 Control bus:- Used to synchronize the operation of the different elements.
It transmits read and write signals, system clock signals, and other control
signals.
MICROCOMPUTER – SYSTEM BUSES
MICROCOMPUTER – MEMORY
• Memory units store binary data.
• Size of memory determined by the number of wires in the address bus.
• Memory unit consists of large number of storage cells with each cell
capable of storing 0 or 1 bit.
• Storage cells grouped into a location with each location storing a word.
• In order to access the stored word each location is identified by a
unique word.
• Size of memory specified in terms of number of memory locations
available.
• 1K is 2^10=1024
• 4K memory has 4096 locations.
MICROCOMPUTER – MEMORY: CLASSIFICATION

CMOS
Memories

RAM ROM’s

SRAM DRAM ROM

EPROM EEPROM Flash


Volatile Non-volatile
Content lost when power off Content will be kept safely when power off
MICROCOMPUTER – MEMORY: CLASSIFICATION
 RAM (Random Access Memory):- can be read from or written to at any
time provided power is there.Data is considered volatile because it is
lost when power goes.
 ROM (Read Only Memory):- permanent storage of data that CPU can
read but CPU cannot write data on ROM. It does not require a power
supply to retain its data so called non volatile memory.
 PROM(programmable ROM):- is used for ROM chips that can be
programmed by the user.
 EPROM (Erasable & Programmable ROM):- data stored can be erased
with ultraviolet light through a transparent window at the top of the
EPROM IC then new data can be stored on the EPROM.
 EEPROM (Electrically Erasable & Programmable ROM):- Uses a
special programming pin (usually uses higher voltage) to erase then
write new program.
MICROCOMPUTER – INPUT / OUTPUT
• Communication to and from the microprocessor occurs through I/O
devices connected to the bus.
• External computer peripheral I/O devices are keyboards, printers,
displays, modems, network device.
• For Mechatronic applications A/D, D/A and digital I/O provides
interfaces to switches, sensors and actuators.
• Since the speed and characteristic of the peripheral devices differ with
microprocessor, microprocessor are connected via an interface chip.
• Microprocessor accept valid data from interface chip which is indicated
by the interrupt or polling (status bit set to 1).
• Polling is the process of repeated checking each peripheral device to
see if it is ready to send or accept a new byte of data.
MICROCONTROLLER
• The microcontroller contains a microprocessor, memory, I/O
capabilities, and other on-chip resources.
• It is basically a microcomputer on a single IC.
• The components of a microcontroller are the:
▪ CPU
▪ RAM and ROM
▪ Digital Input/Output ports
▪ A/D and D/A converters
▪ Timers
• Popular microcontrollers that have being in great demand for realizing
mechatronics systems are:
▪ Microchip's PIC;
▪ Motorola's 68HC11; and
▪ Intel's 8051.
MICROCONTROLLER
MICROCONTROLLER -- MOTOROLA M68HC11
MICROCONTROLLER -- INTEL 8051
MICROCONTROLLER
 The CPU executes the software stored in ROM and controls all the
microcontroller components.
 The RAM is used to store settings and values used by an executing
program.
 The ROM is used to store the program and any permanent data. A
designer can have a program and data permanently stored in ROM by
the chip manufacturer, or the ROM can be in the form of EPROM or
EEPROM, which can be reprogrammed by the user.
 The digital I/O ports allow binary data to be transferred to and from the
microcontroller using external pins on the IC.
 These pins can be used to read the state of switches and on-off
sensors, to interface to external analog-to-digital (ADC) and digital-to-
analog (DAC) converters, to control digital displays, and to control on-
off actuators.
MICROCONTROLLER
 The I/O ports can also be used to transmit signals to and from other
microcontrollers to coordinate various functions.
 The ADC allows the microcontroller to convert an external analog
voltage (e.g. from a sensor) to a digital value that can be processed or
stored by the CPU.
 The DAC allows the microcontroller to output an analog voltage to a
non-digital device (e.g. a motor amplifier).
 Microcontrollers typically have less than 1 kB to several tens of
kilobytes of program memory, compared with microcomputers where
RAM memory is measured in megabytes or gigabytes.
 Also, microcontroller clock speeds are slower than those used for
microcomputers.
APPLICATION OF MICROCONTROLLER
MICROCONTROLLER -- PIC MICROCONTROLLER
• In this course we will discuss the PIC microcontroller unit (PIC MCU)
manufactured by Microchip Technology, Inc.
• There are many other microcontrollers on the market today (such as
those made by Atmel (AVR), Freescale (HCS12), Intel (MCS-51
family), and Motorola (68HC)).
• The PIC microcontrollers are selected due to their widespread use
(several billions of PIC MCUs have been manufactured so far), low
cost, and ease of use.
MICROCONTROLLER -- PIC MICROCONTROLLER
• We will focus specifically on the PIC16F84 device, which is a
• low-cost 8-bit microcontroller
• EEPROM flash memory for program and data storage.
• It does not have a built-in ADC, DAC or serial communication
capability, but it supports 13 digital I/O lines and serves as a good
learning platform because of its low cost and ease of programming.
MICROCONTROLLER -- FEATURES OF THE PIC16F84
• It has 35 single word instructions.
• It has 13 I/O pins for individual direction control.
• Operates on a regulated 5V supply can be operated with dry cells for
long hours.
• The PIC16F84 is available as an 18-pin dual-in-line package.
• These features are now summarized below.
▪ 8-bit wide data bus CMOS microcontroller;
▪ 1792 bytes of flash EEPROM program memory
▪ 68 bytes of RAM data memory;
▪ 64 bytes of non-volatile EEPROM data memory;
▪ 1024 (1 kB) instructions capability;
▪ 4 MHz clock speed (maximum 10 MHz);
▪ 15 special function hardware registers;
▪ 36 general purpose registers (SRAM);
MICROCONTROLLER -- FEATURES OF THE PIC16F84
The PIC16F84 is packaged on an 18-pin IC that has the pin schematic
(pinout) shown below. The figure also shows the minimum set of external
components recommended for the PIC to function properly.
MICROCONTROLLER -- FEATURES OF THE PIC16F84
• The five pins RA0 through RA4 are digital I/O pins collectively
referred to as PORTA, and
• The eight pins RB0 through RB7 are digital I/O pins collectively
referred to as PORTB.
• In total, there are 13 I/O lines, called bi-
directional lines because each can be
individually configured in software as an
input or output.
• Power and ground are connected to the
PIC through pins Vdd and Vss. The
voltage levels (e.g. Vdd = 5V and Vss =
0V) can be provided using a d.c. power
supply or batteries (e.g. four AA batteries
in series or a 9 V battery connected
through a voltage regulator).
MICROCONTROLLER -- FEATURES OF THE PIC16F84
• The master clear pin (MCLR) is active low and provides a reset
feature. Grounding this pin causes the PIC to reset and restart the
program stored in EEPROM.

• The PIC clock frequency can be


controlled using different methods,
including an external RC circuit, an
external clock source, or a clock
crystal.
• Figure above shows the use of a clock
crystal to provide an accurate and
stable clock frequency at relatively low
cost. Connecting a 4 MHz crystal
across the OSC1 and OSC2 pins with
the 22 pF capacitors grounded as
shown, sets the clock frequency.
PROGRAMMING A PIC USING ASSEMBLY LANGUAGE
• To use a microcontroller in mechatronics system design, software
must be written, tested, and stored in the microcontroller ROM.
• Usually, the software is written and compiled using a personal
computer (PC) and then downloaded to the microcontroller ROM as
machine code.
• If the program is written in assembly language, the PC must have
software called a cross-assembler that generates machine code for
the microcontroller.
• An assembler is software that generates machine code for the
microprocessor in the PC, whereas a cross-assembler generates
machine code for a different microprocessor, in this case the
microcontroller.
PROGRAMMING A PIC USING ASSEMBLY LANGUAGE

• Various software development tools can assist in testing and


debugging assembly language programs written for a microcontroller.
• Simulator: This is software that runs on a PC and allows the
microcontroller code to be simulated (run) on the PC. Most
programming errors can be identified and corrected during simulation.
• Emulator: This is a hardware that connects a PC to the
microcontroller in a prototype mechatronics system. It usually consists
of a printed circuit board connected to the mechatronics system
through ribbon cables.
• The emulator can be used to load and run a program on the actual
microcontroller attached to the mechatronics system hardware
(containing sensors, actuators, and control circuits).
• The emulator allows the PC to monitor and control the operation of the
microcontroller while it is embedded in the mechatronics system.
PROGRAMMING A PIC USING ASSEMBLY LANGUAGE
• The assembly language used to program a PIC16F84 consists of 35
commands that control all functions of the PIC.
• Every microcontroller brand and family has its own specific instruction
set that provides access to the resources available on the chip.
PROGRAMMING A PIC USING C
 PIC programs can be written in a form of C called CC5X. The CC5X
compiler can compile these programs, producing their assembly
language equivalents, and this assembly code can then be converted
to hexadecimal machine code (hex code) that can be downloaded
directly to the PIC flash EEPROM through a programming device
attached to a PC. Once loaded, the program begins to execute when
power is applied to the PIC.
 NB:Here an introduction is present to some of the basic programming principles, and then provide some
examples. A compiler manual is available on the Internet and it is a necessary supplement to this section if
the user needs to solve problems requiring more functionality.

 The first set of codes defines the PICmicro device. This is most
commonly done in C by
#include "16F84.h"
 This statement will make reference to the header file of the 16F84
device for register address and other information. It must be noted
that the header file must be contained within the same folder as the
program codes.
PROGRAMMING A PIC USING C
 One of the first tasks in the main function is to initialize and define the
ports. This is done by the following codes.

PORTA = 0b.0000.0000; //Initialize all Port A bits (first 5 bits)


TRISA = 0b.0000.0000; //Sets Port A bits as outputs (0- output)
PORTB = 0b. 0000. 0000; //Initialize all Port B bits (all 8 bits)
TRISB = 0b.1111.1111; //Sets Port B bits as inputs (1- input)
 The first statement initializes the pins. If a nibble high is required
initially, these bits can be rewritten as 1s. The first bit written is the
MSB.
 The TRIS statement decides whether the bit is to behave as an input or
as an output. When a bit is given TRIS= 1, it is an input, TRIS = 0
refers to output. In both the statements, the last letter determines the
port. PORTA and TRISA refer to the port A.
PROGRAMMING A PIC USING C
• Thereafter, in between the definition of the PIC device and the main
function, the pins have to be assigned. CC5X allows each pin to be
named. This makes the program logical to read and easy to program.
• The following statement assigns bit 2 of port b the name 'motor'. That
means, in the source codes, Motor will always refer to Port B bit 2.
bit Motor @ PORTB.2;
 Once these are done, the actual programming begins. The
programming uses most of the features of C.
 In order to send outputs, the pin names (assignments) have to be
referred to.
 If Port B bit 2 is to be turned high, the code will read

Motor = 1 ;
 The following code will turn the pin low.

Motor = 0;
PROGRAMMING A PIC USING C
 The rest of the operations such as generation of pulses and duty cycles
follow this. Implementing an output response is the only way of testing
inputs. Hence, there can be conditional statements to check if the bit is
high or low.
 The following code will check a pin assigned the name Trigger.
 If Trigger is high, motor will turn on.
If (Trigger = = l)
{
motor = 1;
}
 C programs are compiled by CC5X and converted into assembly.
These then get transferred into hex files. Hex files are downloaded to
PIC.
 For more information (e.g. detailed descriptions and examples of each assembly statement), refer to
the PIC16F8X data sheet available on Microchip's website (www. microchip. com).
PIC CONTROLLER -- EXAMPLE: SECURITY SYSTEM CIRCUIT
THE CC5X SOURCE CODE – EXAMPLE: SECURITY SYSTEM CIRCUIT
#include "16F84.H“ //Header of the PIC used
bit DOOR_WINDOW @ PORTB . 0 ; //assign bit 0 of Port B to DOOR_WINDOW
bit MOTION @ PORTB . 1 ;
bit C @ PORTB.2;
bit D @ PORTB.3;
bit ALARM @ PORTA. 0 ;
void main()
{
PORTA = Ob.0000.0000 ; //Initialize all Port A bits (Low Nibble)
TRISA = 0b.0000.0000 ; //Sets Port A bits as outputs (0 - output)
PORTB = 0b.0000.0000 ; //Initialize all Port B bits (Low Nibble)
TRISB = 0b.1111.1111 ; //Sets Port B bits as inputs (1 - input)
while (1) //Keeps alarm always running
if(C = = 0&&D = = l && DOOR_WINDOW = = 1) //Operating State 1
{
ALARM = 1 ;
if (C = = l && D = = 0)
{
i f (DOOR_WINDOW = = 1 | |MOTION = = l ) // Operating State 2
{
ALARM = 1 ;
}
}
}
}
PIC CONTROLLER -- EXAMPLE: LED
 Design a PIC-based circuit and write a CC5X code for switching two
LEDs alternately.
THE CC5X SOURCE CODE – EXAMPLE: LED
#include "16F84.H" //Header of the PIC used
bit switch @ PORTB.O;
bit LED1 @ PORTA. 1;
bit LED2 @ PORTA.2;
void main()
{
PORTA=Ob.0000.0000 ; //Initialize all Port A bits (Low Nibble)
TRISA= 0b.0000.0000 ; //Sets Port A bits as outputs (0 - output)
PORTB=0b.0000.0000 ; //Initialize all Port B bits (Low Nibble)
TRISB=0b.1111.1111 ; //Sets Port B bits as inputs (1 - input)
int counter = 0;
while(1)
{
if (switch = = l)
{
LED1=1;
LED2 = 0;
counter++;
}
if (counter = = 10)
{
LED2 = 1;
}
else
LED1 = 0 ;
LED2 = 0;
}
}
~END~

You might also like