Search This Blog

Thursday, June 16, 2011

Inside a PIC Microcontroller

Although there are many models of microcontrollers in the PIC family, they all share some
common features, such as program memory, data memory, I/O ports, and timers. Some
devices have additional features such as A/D converters, USARTs and so on. Because of these
common features, we can look at these attributes and cover the operation of most devices in
the family.


Program Memory (Flash)
The program memory is where your program resides. In early microprocessors and microcontrollers
the program memory was EPROM, which meant that it had to be erased using UV
light before it could be reprogrammed. Most PIC microcontrollers nowadays are based on
fl ash technology, where the memory chip can be erased or reprogrammed using a programmer
device. Most PIC microcontrollers can also be programmed without removing them from their
circuits. This process (called in-circuit serial programming, or ISP) speeds up the development
cycle and lowers the development costs. Although the program memory is mainly used
to store a program, there is no reason why it cannot be used to store constant data used in
programs.
PIC microcontrollers can have program memories from 0.5 to over 16 K. A PicBasic program
can have several pages of code and still fi t inside 1 K of program memory. The width of a
14-bit program memory is actually 14 bits. It is interesting to note that PICs are known as
8-bit microcontrollers. This is actually true as far as the width of the data memory is
concerned, which is 8 bits wide. Microchip calls the 14 bits a word, even though a word is
actually 16 bits wide.
When power is applied to the microcontroller or when the MCLR input is lowered to logic 0,
execution starts from the Reset Vector, which is the fi rst word of the program memory.
Thus, the fi rst instruction executed after a reset is the one located at address 0 of the program
memory. When the program is written in assembler language, the programmer has to use
special instructions (called ORG) so that the fi rst executable instruction is loaded into address 0
of the program memory. High-level languages such as PicBasic or PicBasic Pro compile your
program such that the fi rst executable statement in your program is loaded into the fi rst locationof the program memory.

Data Memory (RAM)
The data memory is used to store all of your program variables. This is a RAM, which means
that all the data is lost when power is removed. The data memory is 8 bits wide and this is
why the PIC microcontrollers are called 8-bit microcontrollers.
The data memory in a PIC microcontroller consists of banks, with some models having only
two banks, some models four banks, and so on. A required bank of the data memory can be
selected under program control. 

Register File Map and Special Function Registers
Register File Map (RFM) is a layout of all the registers available in a microcontroller and this
is extremely useful when programming the device, especially when using assembler language.
The RFM is divided into two parts: the Special Function Registers (SFR), and the General
Purpose Registers (GPR). For example, on a PIC16F84 microcontroller there are 68 GPR
registers and these are used to store temporary data.
SFR is a collection of registers used by the microcontroller to control the internal operations
of the device. Depending upon the complexity of the devices, the number of registers in the
SFR varies. It is important that the programmer understands the functions of the SFR registers
fully since they are used both in assembly language and in high-level languages.
Depending on the model of PIC microcontroller used, there could be other registers. For
example, writing and reading from the EEPROM are controlled by SFR registers EECON1,
EECON2, EEADR, and EEDATA. Fortunately, PicBasic and PicBasic Pro compilers provide
simple high-level instructions for writing to and reading from the EEPROM and thus you do
not need to know how to load these registers if you are programming in these languages.
Some of the important SFR registers that you may need to confi gure while programming using
a high-level language are
• OPTION register
• I/O registers
• Timer registers
• INTCON register
• A/D converter registers
The functions and the bit defi nitions of these registers are described in detail in the following
sections. 


OPTION Register
This register is used to set up various internal features of the microcontroller and is named as
OPTION_REG. This is a readable and writable register containing various control bits


to configure the on-chip timer and the watchdog timer. This register is at address 81
(hexadecimal) of the microcontroller and its bit definitions are given in Figure 1.8. The
OPTION REG register is also used to control the external interrupt pin RB0. This pin can be
set up to generate an interrupt—for example, when it changes from logic 0 to logic 1. The
microcontroller then suspends the main program execution and jumps to the interrupt service
routine (ISR) to service the interrupt. Upon return from the interrupt, normal processing
resumes.
For example, to confi gure the INT pin so that external interrupts are accepted on the rising
edge of the INT pin, the following bit pattern should be loaded into the OPTION_REG:
X1XXXXXX
where X is a don’t care bit and can be a 0 or a 1.

I/O Registers
These registers are used for the I/O control. Every I/O port in the PIC microcontroller has two
registers: port data register and port direction control register.
Port data register has the same name as the port it controls. For example, the PIC16F84
microcontroller has two port data registers, PORTA and PORTB. A PIC16F877 microcontroller
has fi ve port data registers, PORTA, PORTB, PORTC, PORTD, and PORTE. Eight bits of data
can be sent to any port, or 8 bits of data can be read from the ports. It is also possible to read
or write to individual port pins. For example, any bit of a given port can be set or cleared, or
data can be read from one or more port pins at the same time.
Ports in a PIC microcontroller are bidirectional. Thus, each pin of a port can be used as an
input or an output pin. Port direction control register confi gures the port pins as either inputs
or outputs. This register is called the TRIS register and every port has a TRIS register named
after its port name. For example, TRISA is the direction control register for PORTA. Similarly,
TRISB is the direction control register for PORTB and so on.
Setting a bit in the TRIS register makes the corresponding port register pin an input. Clearing
a bit in the TRIS register makes the corresponding port pin an output. For example, to make
bits 0 and 1 of PORTB input and the other bits output, we have to load the TRISB register
with the bit pattern.
00000011




Timer Registers
Depending on the model used, some PIC microcontrollers have only one timer, and some may
have up to three timers. In this section we shall look at the PIC16F84 microcontroller, which
has only one timer. The extension to several timers is similar and we shall see in the projects
section how to use more than one timer.
The timer in the PIC16F84 microcontroller is an 8-bit register (called TMR0), which can
be used as a timer or a counter. When used as a counter, the register increments each time a
clock pulse is applied to pin T0CK1 of the microcontroller. When used as a timer, the register
increments at a rate determined by the system clock frequency and a prescaler selected by
register OPTION_REG. Prescaler rates vary from 1:2 to 1:256. For example, when using a
4-MHz clock, the basic instruction cycle is 1 s (the clock is internally divided by four). If we
select a prescaler rate of 1:16, the counter will be incremented at every 16 s.
The TMR0 register has address 01 in the RAM.

 A timer interrupt is generated when the timer overfl ows from 255 to 0. This interrupt can be
enabled or disabled by our program. Thus, for example, if we need to generate interrupts at
intervals of 200 s using a 4-MHz clock, we can select a prescaler value of 1:4 and enable
timer interrupts. The timer clock rate is then 4 s. For a time-out of 200 s, we have to send
50 clocks to the timer. Thus, the TMR0 register should be loaded with 256 – 50 206—i.e.,
a count of 50 before an overfl ow occurs.
The watchdog timer’s oscillator is independent from the CPU clock and the time-out is 18 ms.
To prevent a time-out condition the watchdog must be reset periodically via software. If the
watchdog timer is not reset before it times out, the microprocessor will be forced to jump to
the reset address. The prescaler can be used to extend the time-out period and valid rates are
1, 2, 4, 8, 16, 32, 64, and 128. For example, when set to 128, the time-out period is about 2 s. The watchdog timer can be disabled during programming of the
device if it is not used.


USART Module
The Universal Synchronous Asynchronous Receiver Transmitter (USART) module
in the 16F87x family is also known as a Serial Communications Interface, or SCI. The
USART module is used in communicating with devices and systems that support
RS-232 communications, including computers and terminals. It can be configured as
an asynchronous full duplex device, as a synchronous half-duplex master, or as a synchronous
half-duplex slave. In the synchronous mode, the USART is useful in communicating
with analog-to-digital and digital-to-analog integrated circuits or for
accessing serial EEPROMS.
  A/D Module

Until recently, A/D conversions required the use of dedicated devices, usually in the
form of an integrated circuit component. Mid-range PICs now come with on-board A/D
hardware. One of the advantages of using on-board A/D converters is saving interface
lines. Interfacing with a hardware IC usually requires three to four lines. A similar
function can be implemented with on-board A/C hardware with a single line. Since I/O
lines are often needed in PIC circuits, the advantage of on board A/C hardware is significant.
Mid-range PICs equipped with A/D converters have either 8- or 10-bit resolution
and can receive analog input in 2 to 16 different channels. For example, the 16F877
contains eight analog input channels at a 10-bit resolution. An A/D converter uses a
sample-and-hold capacitor to store the analog charge and performs a successive approximation
algorithm to produce the digital result. When the converter resolution
is 10 bits these are stored in two 8-bit registers, one of them having only four significant
bits.
The A/D module has high- and low-voltage reference inputs which are selected by
software. The module can operate while the processor is in the SLEEP mode, but
only if the A/D clock pulse is derived from its internal RC oscillator.


Master Synchronous Serial Port (MSSP) Module
Some mid-range PICs come equipped with hardware modules to implement serial protocols,
including SPI and I2C. The module that provides these interfaces is named the
Master Synchronous Serial Port, or MSSP. The MSSP module can operate in either the
slave or the master mode, as well as in a free-bus mode, also called the multi-master
function.
The MSSP module is useful for communicating with other peripheral or
microcontroller devices. The peripheral devices can be serial EEPROMs, shift registers,
display drivers, A/D converters, etc.



No comments:

Post a Comment