Вы находитесь на странице: 1из 24

Embedded System

LEC #04

© Dr. Ahmed Mahrous


Microcontroller versus general-purpose microprocessor
Choosing a microcontroller
There are five major 8-bit microcontrollers. They are: Freescale
Semiconductor's (formerly Motorola) 68HC08/68HCII, Intel's 8051,
Atmel’s AVR, Zilog's Z8, and PIC from Microchip Technology. Each of the
above microcontrollers has a unique instruction set and register set;
therefore, they are not compatible with each other. Programs written
for one will not run on the others. There are also 16-bit and 32-bit
microcontrollers made by various chip makers.
Three criteria in choosing microcontrollers are : (1) meeting the
computing needs of the task at hand efficiently and cost effectively; (2)
availability of software and hardware development tools such as
compilers, assemblers, debuggers, and emulators; and (3) wide
availability and reliable sources of the microcontroller.
A brief history of the PIC microcontroller
In 1989, Microchip
Technology Corporation
introduced an 8-bit micro-
controller called the PIC,
which stands for Peripheral
Interface Controller. This
microcontroller had small
amounts of data RAM, a
few hundred bytes of on-
chip ROM for the program,
one timer, and a few pins
for I/O ports, all on a single
chip with only 8 pins. Simplified View of a PIC Microcontroller
A brief history of the PIC microcontroller
PIC families include 10xxx, 12xxx, 14xxx, 16xxx, 17xxx, and 18xxx. They
are all 8-bit processors, meaning that the CPU can work on only 8 bits
of data at a time. Data larger than 8 bits has to be broken into 8-bit
pieces to be processed by the CPU. One of the problems with the PIC
family is that they are not all 100% upwardly compatible in terms of
software when going from one family to another family. For example,
while the 12xxx/16xxx have 12-bit and 14-bit wide instructions, the PIC
18xxx instruction is 16 bits wide with many new instruction.
PIC microcontroller program ROM
In microcontrollers, the ROM is used to store programs and for that reason
it is called program or code ROM. The PICI 8 program ROM is available in
different memory types, such as flash, OTP, and masked, all of which have
different part numbers. Some of the PIC microcontrollers use UV-EPROM,
for on-chip program ROM. The problem with the UV-EPROM is that it takes
around 20 minutes to erase the chip before it can be programmed again.
This has led Microchip to introduce a flash version of the PIC family.
PIC18Fxxx with flash
Many PIC18 chips have on-chip program ROM in the form of flash memory.
The flash version uses the letter F in the part number to indicate that the
on-chip ROM is flash. The flash version is ideal for fast development
because flash memory can be erased in seconds compared to the 20
minutes or more needed for the UV-EPROM version. For this reason, the
PIC18F has been used in place of the UV-EPROM to eliminate the waiting
time needed to erase the chip, thereby speeding up the development
time.
OTP version of the PIC
OTP (one-time-programmable) versions of the PIC are also available from
Microchip. PIC 16C432 chip uses OTP for program ROM. The letter C
indicates the OTP ROM. When a product is designed and absolutely
finalized, the OTP version of the PIC is used for mass production because it
is cheaper than flash in terms of price per unit.

Masked version of PIC


Microchip Corporation provides a service in which they can burn the
required program into the PIC during the fabrication process of the chip.
This chip is commonly referred to as masked PIC and it is the cheapest of
all types, if the unit numbers are high enough.
PIC microcontroller data RAM and EEPROM
The data RAM size for the PIC18 varies from 256 bytes to 4096 bytes. The
data RAM space has two components: General-Purpose RAM (GPR) and
Special Function Registers (SFRs). Because the SFRs are fixed and every
microcontroller must have them, it is the GPR's size that varies from chip
to chip. For this reason, the Microchip web site gives only the GPR size.

PIC microcontroller I/O pins


The PIC 18 can have from 16 to 72 pins dedicated for I/O. The number of
I/O pins depends on the number of pins in the package itself.
PIC microcontroller peripherals
All the members of the PIC18 family come with ADC (analog-to-digital
converter), timers, and USART (Universal Synchronous Asynchronous
Receiver Transmitter) as standard peripherals. The ADC is 10bit and the
number of ADC channels in each PIC chip varies from 5 to 16, depending
on the number of pins in the package. The PIC 18 can have up to 4 timers
besides the watchdog timer. The USART peripheral allows us to connect
the PIC 18-based system to serial ports such as the COM port of the PC.
Many of the PICI8 family members come with the I2C and CAN bus as well.
THE WREG REGISTER IN THE PIC
PIC microcontrollers have many registers for arithmetic and logic
operations. Among them is the widely used WREG register. WREG
stands for working register. In the CPU, registers are used to store
information temporarily. That information could be a byte of data to be
processed, or an address pointing to the data to be fetched. The 8 bits
of a register are shown in the diagram below. These range from D7
(MSB) to D0 (LSB). Any data larger than 8 bits must be broken into 8-bit
chunks before it is processed.
MOVLW instruction
MOVLW instruction moves 8-bit data into the WREG register. It has the
following format:

K is an 8-bit value that can range from 0-255 in decimal, or 00-FF in hex.
The L stands for literal, which means, literally, a number must be used.
In other words, if we see the word literal in any instruction, we are
dealing with an actual value that must be provided right there with the
instruction. This is similar to the immediate value we see in other
microprocessors.
MOVLW instruction
Notice that in MOVLW, the letter L (literal) comes first and then the
letter W (WREG), which means "move a literal value to WREG," the
destination.
The following instruction loads the WREG register with a literal value of
25H (i.e., 25 in hex).

The following instruction loads the WREG register with value 87H (87 in
hex)
ADDLW instruction
The ADD instruction tells the CPU to add the literal value K to register
WREG and put the result back in the WREG register. Notice that in
ADDLW, first comes the letter L (literal) and then the letter W (WREG),
which means "add a literal value to WREG," the destination. The ADDLW
instruction has the following format:

To add two numbers such as 25H and 34H, one can do the following:

Executing the above lines results in WREG = 59H (25H + 34H = 59H)
ADDLW instruction

PIC WREG and ALU Using Literal Value


ADDLW instruction
The following program will add values 12H, 16H, 31H, and 43H:

When programming the WREG register of the PIC microcontroller with a


literal value, the following points should be noted:
1. If values 0 to F are moved into an 8-bit register such as WREG, the
rest of the bits are assumed to be all zeros. For example, in "MOVLW
5H" the result will be WREG = 05H; that is, WREG = 00000101 in binary.
2. Moving a value larger than 255 (FF in hex) into the WREG register will
truncate the upper byte and cause a warning in the .err file.
ADDLW instruction
THE PIC FILE REGISTER
The PIC microcontroller has many other registers in addition to the
WREG register. They are called data memory space to distinguish them
from program (code) memory space. They are also called the file
register. The PIC microcontrollers' file register size ranges from 32 bytes
to several thousand bytes depending on the chip. Notice that the file
register data RAM has a byte-size width, just like WREG. The file register
data RAM in PIC is divided into two sections: (a) Special Function
Registers (SFR), and (b) General Purpose Registers (GPR).
SFRs (Special Function Registers)
They are dedicated to specific functions such as ALU status, timers,
serial communication, I/O ports, ADC, and so on. They are used to
control the microcontroller or peripheral. The PIC SFRs are 8-bit
registers.

GPR (General-Purpose Registers or RAM)


The general-purpose registers are a group of RAM locations in the file
register that are used for data storage and scratch pad. Each location is
8 bits wide and can be used to store any data we want as long as it is 8-
bit.
MOVWF instruction
In instructions MOVWF, the F stands for a location in the file register,
while W means WREG. The MOVWF instruction tells the CPU to move
(copy) the source register of WREG to a destination in the file register
(F). After this instruction is executed, the location in the file register will
have the same value as register WREG. The location in the file register
can be one of the SFRs or a location in the general purpose registers
region. For example, the "MOVWF PORTA" instruction will move the
contents of WREG into the SFR register called PORTA.
MOVWF instruction
The following program first loads the WREG register with value 55H,
then moves this value around to various SFRs of ports B, C, and D:

PORTB, PORTC, and PORTD are part of the special function registers in
the file register. They can be connected to the I/O pins of the PIC
microcontroller. We can also move (copy) the contents of WREG into
any location in the general-purpose registers (RAM) region of the file
registers.
MOVWF instruction
The following program will put 99H into locations 0--4 of the GPR region
in the file register:

The contents of memory after the execution.


EXAMPLE
State the contents of file register RAM locations after the following
program:

Solution: After the execution of MOVWF 12H file Reg RAM location 12H
has value 99H; After the execution of MOVWF 13H file Reg RAM
location 13H has value 85H; After the execution of MOVWF 14H file Reg
RAM location 14H has value 3FH; After the execution of MOVWF 15H
file Reg RAM location 15H has value 63H.
EXAMPLE
The contents of the memory is shown in the following chart :

NOTE
Notice that you cannot move literal (immediate) values directly into the
general-purpose RAM locations in the PIC18. They must be moved there
via WREG.

Вам также может понравиться