You are on page 1of 74

Chapter 1

Week 1

Microcontroller PIC16F84

Chapter 1 Outline
Review on Microprocessor Systems Microprocessor Vs Microcontroller The Microcontroller The PIC 16F84 Microcontroller Clock Generator Reset Central Processing Unit (CPU) Memory Organization Ports Timer Bus System
Chessda

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
Chessda

Microprocessor System (Cont..)

ROM

EEPROM

Microprocessor

RAM

Serial I/O Input and output ports Parallel I/O Timer PWM

A/D

Analog I/O
D/A

Input and output ports

Figure 1.1: Microprocessor-based system


Chessda

Microprocessor System (Cont..) 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.

Chessda

Microprocessor System (Cont..) 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.

Chessda

Microprocessor System (Cont..)


RAM CPU
Program counter Instruction register Data register 3 3274 Inst. Code Operand 5 2 4 1 Contents Address

Instruction Operand Operand Next instr. etc

3724 3725 3726 3727 3728

Data Processing
Data register 7 Result 8

6 Data byte Data byte Data byte 5821 5822 5823

Figure 1.2: Program execution sequence

Chessda

Microprocessor System (Cont..)

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.

Chessda

Microprocessor System (Cont..)

PC Engineering Applications (Cont..)


- 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.

Chessda

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.
Chessda

The Microcontroller (Cont..)

A Microcontroller Applications
- Used to control a small system. - calculator, message display, electronic lock or similar applications & etc.

Chessda

The Microcontroller (Cont..)


ROM EEPROM

RAM

Analog In

CPU Core

A/D

Serial I/O

Parallel I/O

Timer Filter Digital PWM

Microcontroller

PWM

Analog Out

Figure 1.3: Microcontroller-based system

Chessda

The Microcontroller (Cont..)

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.

Chessda

CISC and RISC (Cont..)


RISC (Reduced Instruction Set Computer) and CISC (Complex Instruction Set Computer) refer of the instruction set of a microcontroller In a CISC microcontroller, instruction word are more than 8 bits wide (12, 14 or 16 bits) and the instructions occupy one word in the program memory. RISC processors (e.g. PIC) have no more than about 35 instructions and offer higher speed
Chessda

CISC and RISC (Cont..)


CISC microcontrollers have 8-bit wide instructions and they usually have over 200 instructions. Some instructions (e.g. branch) occupy more than one program memory location.

Chessda

CISC and RISC


Basically, two types of architectures are used in microcontrollers: Von Neumann architecture and Harvard architecture. Von Neumann architecture is used by a very large percentage of microcontrollers and here all memory space is on the same bus and instruction and data are treated identically In Harvard architecture, code and data storage are on separate buses and allows code and data to be fetched simultaneously.
Chessda

CISC and RISC (Cont..)

Chessda

Microcontroller Applications
Automotive industries Controlling home appliances Controlling industrial instruments Remote sensors Electric door locks Safety devices Smart cards Many more.
Chessda

Microcontroller Vs Microprocessor
ROM EEPROM

Microprocessor

RAM

Serial I/O Input and output ports Parallel I/O Timer PWM

A/D

Analog I/O

D/A

Input and output ports

Microprocessor-based system

ROM

EEPROM

CPU Core

RAM Serial I/O Parallel I/O

Analog In

A/D

Microcontroller-based system
Filter Digital PWM

Timer
PWM

Microcontroller

Analog Out

Chessda

Microcontroller Vs Microprocessor (Cont..)


Perspective Microprocessors Microcontrollers

Hardware Architecture

Single-chip CPU High RAM-ROM ratio External components are required to implement interrupt, timer and I/O operations
Used as CPU in computer systems for general processing Powering and processing intensive instructions More suitable for large volume data handlings AND, OR, XOR operations at bit level is less easy

Single-chip computer High ROM-RAM ratio Built-in interrupt, timer and I/O devices
Used in control-oriented systems requiring minimum components Simple and compact instructions More suitable for single-bit I/O operations AND, OR, XOR operations at bit level is easy
Chessda

Applications

Instructions Set Features

Microcontroller Vs Microprocessor (Cont..)

Microprocessor

Microcontroller Low density Less PCB area Low cost Specific Architecture Low power consumption

high density higher PCB area high cost Flexible high power consumption

Chessda

The PIC 16F84 Microcontroller PIC16F84 belongs to a class of 8-bit microcontrollers of RISC architecture. It is belongs to a mid-range family of the PICmicro microcontroller devices.

Chessda

PIC Microcontroller

Chessda

PIC Microcontroller
Free-run Counter

Data Memory RAM

Data Memory EEPROM

CPU
PORT A PORT B

Program Memory FLASH

PIC16F84 Microcontroller Outline


Chessda

PIC 16F84 Pin Descriptions


PIC16F84 has a total of 18 pins. It is most frequently found in a DIP18 type of case but can also be found in SMD case which is smaller from a DIP (Dual in Package). SMD (Surface Mount Devices) suggesting that holes for pins to go through when mounting aren't necessary in soldering this type of a component.

Chessda

PIC 16F84 Pin Description (Cont..)


Pins on PIC16F84 microcontroller have the following meaning: Pin no.1 RA2 Pin no.2 RA3 Pin no.3 RA4 Second pin on port A. Has no additional function Third pin on port A. Has no additional function. Fourth pin on port A. TOCK1 which functions as a timer is also found on this pin Pin no.4 MCLR Reset input and Vpp programming voltage of a microcontroller Pin no.5 Vss Ground of power supply. Pin no.6 RB0 Zero pin on port B. Interrupt input is an additional function. Pin no.7 RB1 First pin on port B. No additional function. Pin no.8 RB2 Second pin on port B. No additional function. Pin no.9 RB3 Third pin on port B. No additional function. Pin no.10 RB4 Fourth pin on port B. No additional function. Pin no.11 RB5 Fifth pin on port B. No additional function. Pin no.12 RB6 Sixth pin on port B. 'Clock' line in program mode. Pin no.13 RB7 Seventh pin on port B. 'Data' line in program mode. Pin no.14 Vdd Positive power supply pole. Pin no.15 OSC2 Pin assigned for connecting with an oscillator Pin no.16 OSC1 Pin assigned for connecting with an oscillator Pin no.17 RA0 Second pin on port A. No additional function Pin no.18 RA1 First pin on port A. No additional function.

Chessda

Clock Generator Oscillator


Oscillator circuit is used for providing a microcontroller a clock. Clock is needed so that microcontroller could execute a program. The most frequently used is crystal and RC oscillator

Chessda

XT Oscillator

Chessda

RC Oscillator
In applications where great time precision is not necessary, RC oscillator offers additional savings during purchase Resonant frequency of RC oscillator depends on supply voltage rate, resistance R, capacity C and working temperature.
Chessda

Reset
Reset is used for putting the microcontroller into a 'known' condition. When microcontroller behave inaccurately under certain undesirable conditions, in order to continue its proper functioning it has to be reset meaning all register would be placed in a starting position.

Chessda

Reset (Cont..)
Microcontroller PIC16F84 knows several sources of resets: Reset during power on, POR (Power-On Reset) Reset during regular work by bringing logical zero to MCLR microcontroller's pin. Reset during SLEEP regime Reset at watchdog timer (WDT) overflow Reset during at WDT overflow during SLEEP work regime

Chessda

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.

Chessda

Central Processing Unit (CPU)


Control and Status registers
Program counter Instruction register .

ALU

Registers Internal Bus

User-Visible Registers
Accumulator Address Register Data Register

CU

Flag Register .

CPU
Chessda

Central Processing Unit (CPU)

Chessda

Central Processing Unit (CPU)

Control Unit (CU)


The CU synchronizes and controls the overall operation of a microprocessor system. It obtains its operational detail from the Instruction Decoder. The instruction decoder (ID) decodes the binary code in an instruction and passes the necessary control signals to the CU to complete the operations specified by the instruction.

Chessda

Central Processing Unit (CPU)

Arithmetic Logic Unit (ALU)


The ALU performs the arithmetic and logical operations within the CPU. Example of arithmetic operations are add, subtract, multiply and divide. In low-end microprocessors, multiply and divide may not be implemented. Example of logical operations are AND, OR, and XOR. The ALU also performs shift and rotate operations that may either be arithmetic or logical in nature.

Chessda

Memory Organization
PIC16F84 has two separate memory blocks, one for data and the other for program. EEPROM memory with GPR and SFR register in RAM memory make up the data block. FLASH memory make up the program block.

Chessda

Program Memory
Program memory has been carried out in FLASH technology which makes it possible to program a microcontroller many times before it's installed into a device. The size of program memory is 1024 locations with 14 bits width where locations zero and four are reserved for reset and interrupt vector.

Chessda

Data Memory
Data memory consists of EEPROM and RAM memories. EEPROM memory consists of 64 eight bit locations whose contents is not lost during loosing of power supply. EEPROM is not directly addressable, but is accessed indirectly through EEADR and EEDATA registers RAM memory for data occupies space on a memory map from location 0x0C to 0x4F which comes to 68 locations.

Chessda

Data Memory (Cont..)


The data memory can further be broken down into the general purpose RAM and the Special Function Registers (SFRs). The data memory area also contains the data EEPROM memory. This memory is not directly mapped into the data memory, but is indirectly mapped. That is, an indirect address pointer specifies the address of the data EEPROM memory to read/write. The 64 bytes of data EEPROM memory have the address range 0h-3Fh.

Chessda

Data Memory (Cont..)


Register File Map PIC16F84

Chessda

Memory Organization (Cont..)


Program Memory Map and Stack for PIC16F84

Chessda

Program Memory Organization


The PIC16FXX has a 13-bit program counter capable of addressing an 8K x 14 program memory space. For the PIC16F84A, the first 1K x 14 (0000h-03FFh) are physically implemented Accessing a location above the physically implemented address will cause a wraparound For example, for locations 20h,420h, 820h, C20h, 1020h, 1420h, 1820h, and 1C20h, the instruction will be the same. The RESET vector is at 0000h and the interrupt vector is at 0004h.

Chessda

Program Memory Organization (Cont..) General Purpose Register File


Each General Purpose Register (GPR) is 8-bits wide and is accessed either directly or indirectly through the FSR The GPR addresses in Bank 1 are mapped to addresses in Bank 0. As an example, addressing location 0Ch or 8Ch will access the same GPR

Chessda

Program Memory Organization (Cont..) Special Function Register


The Special Function Registers are used by the CPU and Peripheral functions to control the device operation. These registers are static RAM

Chessda

Program Memory Organization (Cont..)

SPECIAL FUNCTION REGISTER FILE SUMMARY (1)


Chessda

Program Memory Organization (Cont..)

SPECIAL FUNCTION REGISTER FILE SUMMARY (2)

Chessda

Program Memory Organization (Cont..)

Chessda

Program Counter
Program counter (PC) is a 13-bit register that contains the address of the instruction being executed. It is physically carried out as a combination of a 5-bit register PCLATH for the five higher bits of the address, and the 8-bit register PCL for the lower 8 bits of the address.

Chessda

Stack
PIC16F84 has a 13-bit stack with 8 levels, or in other words, a group of 8 memory locations, 13 bits wide, with special purpose. Its basic role is to keep the value of program counter after a jump from the main program to an address of a subprogram. In order for a program to know how to go back to the point where it started from, it has to return the value of a program counter from a stack
Chessda

Ports
Port is a group of pins on a microcontroller which can be accessed simultaneously or on which we can set the desired combination of zeros and ones. Physically, port is a register inside a microcontroller which is connected by wires to a pins. Port represent physical connection of CPU and outside world.
Chessda

Ports (Cont..)
Microcontroller uses them in order to monitor or control other components or devices. All port pins can be designated as input or output, according to the needs of the device thats being developed.

Chessda

Port A and TRISA


Port A has adjoined 5 pins. The appropriate register for data direction is TRISA Setting a bit in TRISA register defines the corresponding port pin as input and resetting a bit in TRISA register defines the corresponding port as output.

Chessda

Port A and TRISA (Cont..)

Chessda

Port B and TRISB


Port B has adjoined 8 pins. The appropriate register for data direction is TRISB Setting a bit in TRISB register defines the corresponding port pin as input and resetting a bit in TRISB register defines the corresponding port as output.

Chessda

Port B and TRISB (Cont..)

Chessda

Timer
Physically, timer is a register whose value is continually increasing and reset to 0 when its value has reach the maximum count value.

Chessda

Timer

(cont..)

The Timer0 module of PIC16F84 timer/counter has the following features: 8-bit timer/counter Readable and writable Internal or external clock select Edge select for external clock 8-bit software programmable prescaler Interrupt-on-overflow from FFh to 00h
Chessda

Timer

(cont..)

Chessda

Free-Run Timer TMR0 (Cont..)


Example:

Chessda

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.

Chessda

Memory System (Cont..)


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.

Chessda

Memory System (Cont..)


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.

Chessda

Memory System (Cont..)


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.
Chessda

Memory System (Cont..)


Flash EEPROM very popular and is used in many microcontrollers (e.g. PIC16F84 contain 1K 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.
Chessda

Memory System (Cont..)

Chessda

Bus System
Bus system 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 CPUgenerated 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.

Chessda

Bus System (Cont..)


No of address lines
8 16

Size of memory space

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

20
24 32

Chessda

Bus System (Cont..)


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.
Chessda

Bus System (Cont..)


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.

Chessda

Bus System (Cont..)

Chessda

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

Chessda

I/O Modules (Cont..)


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.
Chessda

I/O Modules (Cont..)

Chessda

You might also like