Vous êtes sur la page 1sur 40

Select a Chapter

Microcontrollers
Chapter 4 - A more detailed look at Microcontroller Peripheral
Modules

Chapter Information

This chapter contains the following sections:


4.1 Introduction

4.3 Control/Status registers

4.5.3 The interrupt register INTCON


4.5.4 The analogue to digital (A/D)
conversion block
4.5.5 The ADCON0 register
4.5.6 The ADCON1 register
4.5.7 Using the PIC A/D module

4.4 Multiplexing of microcontroller pins

4.6 The serial interface port

4.2 Registers and how they are used to configure and


control peripheral devices

4.5 A study of the peripheral units within a PIC


microcontroller
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

4.6.1 UART Transmit block


4.6.2 UART Receiver block

Self Assessment
pdfcrowd.com

4.5.1 The timer module used in the PIC


4.5.2 Using the timer module

Author:

Self Assessment

Peter Minns of Northumbria University

Recommended textbooks: A Beginner's Guide to the Microchip PIC, N Gardner


The PIC Trainer User Manual, Flight Electronic Ltd.
The Microchip PIC16C84/16F84 Data Sheets
Notional workload:

10 hours

For tutor support contact:

Peter Minns

4.1 Introduction
In chapter 2 we looked at the types of peripheral that are commonly used in a typical microcontroller.
These were:
Timer modules
Analogue to Digital interfaces
Serial I/O modules
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

In chapter 3 we looked at the instruction set of the microcontroller and used it in a number of key
application areas such as turning on and off digital I/O, loop counting and using sub-programs (or subroutines).
In this chapter we will look at some of these peripheral hardware modules in some detail using the
Microchip PIC as an example of a real microcontroller. The main reasons for choosing the PIC
microcontroller are:

a) It is the one you will be using in the practicals.


b) It is one of the most popular microcontrollers in use today, although, to be fair, there are
other microcontrollers available that should be considered as well. (eg. the Motorola
68x05/68x08, the SGS Thompson ST6/ST7 range of microcontrollers and the Hitachi H8
series.)
Before examining how PIC peripheral modules are used we need to consider how they are configured
and controlled.
Peripheral modules were introduced in chapter 3 to illustrate the PIC instruction set. Here we examine
how they are arranged and used to configure the microcontroller for a particular application.

4.2 Registers and how they are used to configure and control peripheral devices
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

All microcontrollers have a number of internal registers and some can contain over a hundred. Each
register is simply a set of n bit memory locations that can be addressed at a unique memory address
within the memory map of the microcontroller. Typically, these registers are 8 bits wide (byte wide).
Some can be written and read while others can only be read.
The main purpose of these registers is to configure and control the peripheral modules integrated within.
They can be written or read via the instruction set of the microcontroller.

Accessing the Different Devices within the Microcontroller


This is achieved by using special registers within the microcontroller.
A register is a special memory location which can by accessed by the CPU core.
This access is achieved by reading the register (to find out what is in it), or by writing new data into it.
The following figure illustrates a byte wide register which is used to read port A of a PIC microcontroller.
PortA is assumed to have been configured as an input port.

Registers
Here we see data 9Dh read from PortA.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

7 6 5 4

1 0 0 1

MOVF porta,w ; will copy data from PortA


into the CPU W register.
The result of this instruction is that the data at the input port A (which happen to be 9D hex) will be moved
into the internal register W so it can be acted upon by the microcontroller.
In the next example, two PIC assembly instructions are needed to move the data 97 hex out to the output
port B.
MOVLW 97h will move the literal data 97 hex into register W.
MOVWF portb will move the data 97 hex (now contained in the W register) out to the pins of the port B.

Writing to Registers..
Here we see data 97 hex written into the output port B.

open in browser PRO version

7 6 5 4

1 0 0 1

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

MOVLW 97h ; will copy data 10010111


MOVWF portb ; into the portb via W register.

So far we have seen how data can be moved both to and from a port. However, in most microcontrollers
the ports can be configured to be either inputs or outputs under program control.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

The arrangement shown above illustrates how this is done. The Direction control register is used to
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

configure the port pins for either input or output, depending upon whether the corresponding bit is set or
cleared.
If a particular bit in the Direction control register is a 1, the corresponding bit in port A will be configured
as an input. If the bit in the Direction control register is a 0, the corresponding bit in port A will be
configured as an output.
Each digital port has a corresponding direction control register to allow for its configuration.
The PIC microcontroller has a direction control register called TRIS (TRI-State) since writing a 1 into a
TRIS register bit sets the corresponding port pin bit as an input and hence tri-state. Writing a 0 into a
TRIS register bit sets the corresponding port pin as an output.
The arrangement is illustrated below.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

The TRIS (TRI-State control register) is loaded with a byte of data by moving the data to it from the W
register.
Hence, if the following instruction sequence was used:
MOVLW F0h
MOVWF TRISA
7 6 5 4 3
I

I O O O O

That is, port A bits 7 to 4 would be inputs and port A bits 3 to 0 would be outputs.
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

What would the following sequence result in?


MOVWL A5h
MOVWF TRISA

Show Answer
Hide Answer

4.3 Control/Status Registers


Many of the peripheral devices contained within a microcontroller have multiple functionality. That is, they
can be used in a number of different ways. The programmer must configure these devices to operate in
the way required and to do this, a number of special types of registers are required.
These are:
Control registers to configure the device to operate in a particular way. eg. the TRIS registers.
Status registers to allow a particular device to be monitored to find out its current status.
The following figure illustrates a typical arrangement.
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

In this figure a control register is used to enable an interrupt by setting bit 7 of the control register to logic
1. Inside the microcontroller this enables an AND gate whose output is connected to pin 0 of a Status
register.
The actual interrupt input pin of the microcontroller is connected to the other input of the AND gate so that
when an interrupt does occur it will set the status bit 0 to logic 1. By reading the status register the
programmer can identify if an interrupt has occurred since if bit 0 is set, then an interrupt has occurred on
the input interrupt pin of the microprocessor.
Notice that the status register also allows the programmer to see whether the interrupt has been enabled
or not by reading bit 7 of the status register.
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

or not by reading bit 7 of the status register.


The Control register can be used to control the enabling/disabling of the interrupt.
The status register can be read to determine the state (status) of the interrupt enable and the status
of the interrupt condition. (Has an interrupt occurred or not?)
Most microprocessors and microcontrollers have a STATUS register containing a number of assorted
bits which can be read to determine the state of the microprocessor CPU.
The PIC has an 8 bit status register containing bits such as the carry bit and the zero bit as well as a
number of bits special to the PICs architecture. See page 36 of Nigel Gardner's Beginner's Guide to
the Microchip PIC for more details on this.
Most microcontrollers have a number of status registers, not just the one for the CPU status. Some have
registers with both status and control bits in them (for example, the INTCON register in the PIC range of
microcontrollers). See the PIC16C84 data sheet for a preview of this particular register. It contains
interrupt enable bits (control) and flag bits (status).

4.4 Multiplexing of microcontroller pins


We have already seen that a typical microcontroller can share pins between internal peripheral devices
to reduce the total number of pins needed. The microcontroller must contain internal multiplexers to do
this and these multiplexers are controlled through the use of registers.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

In this example the input and output pins (7 and 0 respectively) are sharing two functions.
Output pin 0 can be either port A bit PA0 or data bus bit D0; bit 2 of the register is used to select the
input: either PA0 (sel A = 1) or D0 (sel A = 0). Likewise input pin bit 7 can be configured to be either port
A bit PA7 (sel B = 1) or timer T1 output (sel B = 0).
Thus two I/O pins can be used to support a number of peripheral devices.
The additional complexity is not usually a problem in integrated circuit fabrication, whereas additional
pins would result in a larger product size. So most microcontrollers have multiplexed pin functions.
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Up to now we have been largely looking at microcontrollers in general. We will spend the rest of this
chapter looking at a particular range of microcontrollers designed and manufactured by the Microchip
Corporation. The Microchip PIC has become something of a leader in the microchip market. It uses a
very up-to-date architecture (Harvard) and is available in a wide range of different levels of
sophistication, from very simple basic devices (16C54/84) through to the more sophisticated
microcontrollers (17C42). It is not the most sophisticated microcontroller on the market - try Motorola
home page, and look for the 68MC08 with CAN architecture, or the Hitachi H8/3048 for examples of
these.

4.5 A study of the peripheral units within a PIC microcontroller.


The PIC uses peripherals in a way that is typical of other microcontrollers. As part of the assignment you
will be invited to compare the PIC architecture with that of a number of other microcontrollers, but more
on that later.
We will start by looking at the general architecture of the PIC.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

The PIC series of microcontrollers is based on a Harvard 8 bit architecture. The Harvard architecture
separates the instruction and data buses so that the processor can be executing an instruction while, at
the same time, performing data operations between memory and the processor core. This, in effect,
makes it a simple Multiple Instruction Multiple Data computer (although limited to only one ALU and one
data path, unlike more advanced MIMD computers such as the CRAY or the more modest Transputer).
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

The performance is further advanced by the use of a two-stage pipeline for instructions (see page 10 of
the PIC16c84 data sheet included with the PIC Trainer User Manual.This pipeline allows the processor
to execute each instruction in one clock cycle. The exception is a branch instruction, which takes two
clock cycles to complete.
The instruction word is 14 bits wide with a separate 8 bit data bus. Some of the high-end PIC devices
have a 16 bit wide instruction word.
Some PIC devices come with EPROM memory and can be reprogrammed a number of times by
exposing the windowed chip to a strong ultra-violet light source for about 20 minutes. A limited number of
PICs make use of electrically erasable memory (EEPROM) that allow them to be re-programmed within
the circuit.
The 16c84 PIC used in the PIC trainer kit uses EEPROM program memory so it can be reprogrammed
within the circuit.
The turquoise blocks in the PIC block diagram are the main elements of the microcontroller and are
typical of any microcontroller.
The ALU (Arithmetic Logic Unit) performs the Add/Subtract and shift operations required by any
microprocessor.
The Program Counter (PC) keeps track of the current instruction being executed in program memory.
The Status register is used to determine the status of the current point in the program (such as whether
the last instruction produced a zero result (Z bit = 1), or there was a carry (C bit =1), or an interrupt
occurred.
The stack register is used to keep track of the return address from a subroutine or interrupt, as well as
any registers that the programmer may also wish to save there.
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Most microcontrollers need to be configured for a particular mode of operation (such as microprocessor
mode) and for the function of some of the peripheral devices. The configuration, once set, will not
change during the life of the device in its final application and can therefore be set in EPROM memory
during the programming of the device. The configuration fuses are not fuses in the real sense, but just a
number of EPROM memory locations that are programmed. For more information about configuring the
PIC16c84, see page 35 of the PIC16c84 data sheet.
Like most microcontrollers the PIC chip includes the clock logic. The oscillator circuit is designed to be
connected to an external crystal or an external resistor-capacitor network (for low cost applications). See
the PIC16c84 data sheet for more details about the clock circuit.
The general register stack consists of a number of program registers that are used by the instruction set
of the PIC. The number of registers varies between the different PIC devices.
All PICs have the following registers, which are 8 bits wide:
1. A W work register. This is used for all arithmetic and logic operations and acts as the workhorse
register within the PIC.
2. Status registers. These contain such things as the carry bit and the zero bit, as well as other
functions which are dependent upon the particular PIC device.
3. Port registers: These are used to access the port pins of the PIC. Most PICs will have a Port A
and a Port B. Other PICs may have more, depending upon the complexity and number of pins
available on the device.
4. Option register: This is used to set up the mode used by the PIC's internal timer module, as well as
some other functions.
5. INTCON register: This register is used to set up interrupt status in some of the PIC peripheral
modules, as well as provide flag bits for these devices.
Beyond the register file there is an area of static RAM that can be used as read/write memory, or as
user-defined registers. Unlike some of the other registers (such as the Option register and Status
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

register) these memory locations are not connected to any periperal device or the CPU core.

Task 1
Most of the registers, EPROM, and RAM areas within the PIC device are located at
specific addresses within the memory map.
Look up the memory map of the PIC16c84 and determine:
1.
2.
3.
4.
5.
6.

The EPROM address area


The RAM address area
The addresses for the Ports A and B
The address for the reset vector
The address for the peripheral interrupt vector
The location within the register file map of the Program Counter (PC),
STATUS register, OPTION register and INTCON register.

4.5.1 The Timer module used in the PIC


The timer module has three functions:
Produce a time delay by means of a counter.
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Count the number of input pulses arriving


Produce an output pulse of a specified duration.
The diagram below illustrates the basic arrangement of the PIC's timer module.

The timer module is called a "Real Time Clock Counter" because it can make use of the clock signal of
the PIC to increment an 8 bit counter in real time. Most microcontrollers do this as well.
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

In the PIC, the counter can be incremented on every clock pulse, or the clock signal can be gated into a
pre-scaler unit so that the clock signal is divided down to a lower frequency. In this way, the timer can be
made to count more slowly and hence produce longer delays.
The timer register is called the TMR register referred to as TMRO in the PIC16c84. (There is only one
timer in this particular PIC, but some PICs have more than one).
The TMR register can be loaded with a count value via software. It is then incremented to the maximum
value 1111 1111 (255 decimal, FF hex) before rolling over to zero, 0000 000. The action of roll-over to
zero causes a flag bit to be set in the INTCON register (bit 2, called TOIF).
The microcontroller can read the status of the INTCON register TOIF bit to determine when the timer
counter has rolled over.
The OPTION register can be used to select the source for the clock pulses going to the timer counter
RTCC. See diagram above and table below.
Osc/4 is the clock input signal, running at one quarter the crystal frequency used by the PIC. Therefore a
PIC using a 4MHz clock will count at 1MHz rate.

Option Register bits (associated with the RTCC)

bits

open in browser PRO version

RTS

RTE

PSA

PS2

PS1

PS0

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

RTCC

WDT

edge 0 > 1

edge 1 > 0

16

internal

32

external

64

128

256

RTS signal source, RTE signal active edge, PSA pre-scaler assignment.
Bits 7 and 6 are not associated with the control of the timer so they are not described.
Bit 5, the RTS bit, defines whether the timer clock source is internal (from the clock signal) or external
(from the RTCC pin of the PIC - see PIC16C84 pinout in the PIC trainer manual, pin 3)
Bit 4 defines whether the rising edge or the falling edge will be used to increment the timer counter.
Bit 3 defines the source for the pre-scaler - either the timer counter or the Watchdog timer.
Bits 2, 1 and 0 determine the size of the pre-scaler in terms of 'divide by 2' up to 'divide by 256', as
shown.
Example
To use the timer unit with the internal clock source on a zero to one transition and a 64 divider prescaler, the OPTION register should be loaded with 0000 0101.
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

What would be the value to load into the OPTION register in order to use the timer unit with a 128 prescaler value and external negative edge triggered pulses to clock the timer counter?
Answer: 0011 0110
Remember that when the timer counter overflows to zero, the INTCON register flag bit 2 (TOIF) will be
set. This flag bit can be polled (a process of repeatedly reading the flag bit to determine whether it has
set) or it can be arranged to cause a processor interrupt.
In the second case, an interrupt routine is required. An interrupt routine is like a subroutine only if it is
initiated via an event on an interrupt input pin. This interrupt pin can be either one of the PIC interrupt
inputs (INT on pin 6 of a PIC16C84) or via one of the internal interrupt inputs.
Most of the peripheral modules in the microcontroller can be configured to cause an interrupt of some
sort.

4.5.2 Using the Timer Module:


The following program fragment illustrates how the PIC16C84 timer can be configured and used.
Note that the INTCON register is zero at power-on reset whereas the OPTION register has all its bits set
at power-on reset.
Strt
chktmr

MOVLW
MOVWF
CLRF
BTFSS
GOTO

open in browser PRO version

87h
OPTION
TMR0
INTCON,TOIF
chktmr

;find out what this configuration is?


;clear the timer register to zero
;wait for timer overflow to occur

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

GOTO
BFC

chktmr
TOIF

;clear the timer overflow flag

In this program segment the timer register TMR0 has been set to zero. It will therefore count right
through from 0000 0000 to 1111 1111. When it rolls over to 0000 0000 again, the INTCON TOIF bit will
be set (and stay set until it is cleared by the bit set instruction shown). This will produce the maximum
delay possible for the clock selected and the timer pre-scalarselected.
If the Timer interrupt enable bit 5 (TOIE) is set, then the branch and skip instruction (highlighted) is not
required since the PIC main program will be interrupted when the timer overflow occurs. However, the
clearing of the TOIF must now be done from within the timer interrupt service routine.

4.5.3 The interrupt register INTCON


This register, located at address 0Bh (and 8Bh) in the PIC address space is a readable and writable
register that contains the various enable bits for all of the interrupt sources. The idea of an interrupt
enable register is to allow the many interrupt sources to be enabled or disabled. The INTCON register
also allows the source of the interrupt to be identified.
The INTCON register will differ between the different PIC devices. This is because each PIC
type has different internal modules which require an interrupt enable bit and a flag bit.
At power-on reset, all interrupt sources are disabled so that the user can selectively enable each
interrupt source as it is required.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

The INTCON register bits for the 16C84 PIC are illustrated in the above figure. The function of each bit is
described below.
Bit 0 RBIF RB port change interrupt flag.
When 0, none of the RB7 - RB0 pins has changed state.
When 1, at least one of the RB7 - RB0 bits has changed state.
This bit provides a quick way of detecting if an interrupt has occurred. When it is set, the source of the
interrupt can be determined by reading the INTCON register to find out which bit was set and hence
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

which RB port input has changed. Bit 0 should then be cleared with a software write to the INTCON
register.
Bit 1 INTF RB/INT Interrupt Flag bit
When 0, the RB/INT did not occur (normal condition).
When 1, an interrupt occurred on RB/INT input.
This is the external interrupt input to the PIC.
Bit 2 TOIF Timer Overflow interrupt flag bit.
When 0, timer did not overflow.
When 1, a timer overflow occurred.
This bit must be read to find the interrupt source and written to to clear the interrupt flag bit.
Bit 3 RBIE RB port change interrupt enable bit.
When 0, disables the RB change interrupt.
When 1, enables the RB change interrupt.
Used in conjunction with the RB change interrupt bit 0.
Bit 4 INTE RB0/INT interrupt enable bit.
When 0, disables interrupts on the RB/INT pin of the PIC.
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

When 1, enables interrupts on the RB/INT pin of the PIC.


This bit enables interrupts on the RB0/INT input pin of the PIC. It is used in conjunction with bit 1, the
interrupt flag bit. When enabled (bit 3 = 1) an interrupt on the RB0/INT pin will set the interrupt flag bit to 1
(bit 1 = 1). The PIC can read this bit (bit 1) in software to determine the interrupt source.
Bit 5 TOIE Timer Overflow interrupt enable bit.
When 0,disables the Timer Interrupt Enable bit 2.
When 1, enables the Timer Interrupt Enable bit 2.
If you want to use the timer and cause an interrupt when it overflows, you need to set this bit to 1. When a
timer interrupt occurs, the timer interrupt flag bit (bit 2) will set to 1, and a read of the INTCON register will
confirm the interrupt source.
Bit 6 EEIE Electrically Erasable Memory Write complete Interrupt Enable bit.
When 0, disables the EE write complete interrupt.
When 1, enables the EE write complete interrupt.
This bit enables/disables the interrupt that tells the user that a write to the EE memory has been
completed. This is needed because writes to EE memory are slow and valuable processor time can be
otherwise consumed by polling the write complete flag.
Bit 7 GIE Global Interrupt Enable bit
When 0, disables all maskable interrupts.
When 1, enables all maskable interrupts.
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

This bit is used to turn all of the above interrupts on or off. Maskable interrupts are interrupts that can be
enabled or disabled.
An interrupt that is "non-maskable" is one that cannot be disabled. Non-maskable interrupts usually have
a higher priority level than maskable interrupts and are used for high priority events such as power fail.

4.5.4 The Analogue to Digital (A/D) Conversion Block


The A/D converter block is available on the 16C7x range of PIC microcontrollers. It is an 8 bit
successive approximation converter that uses an internal 5 Volt reference source. It is possible to use
an external reference voltage source.
The equation used to describe the relationship between the analogue input signal and the digital output
(sometimes known as the count value) is
Vin = Count_Value x Vref -------------------------------------------(1)
2n
Where n is the number of digital output bits.
Common numbers of output bits are:

8 bits

giving 2n = 28

= 256

10 bits giving 2n = 210 = 1024


open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

12 bits giving 2n = 212 = 4096


14 bits giving 2n = 214 = 16,384
16 bits giving 2n = 216 = 65,536
The minimum input voltage that can be resolved by the A/D converter is dictated by the smallest input
that can be applied to change the output count value from zero to a one in the least significant bit.
Therefore:
Vin(min) = 1 x Vref -------------------------------------------(2)
2n
The maximum input is dictated by the full scale output count value when all digital output bits are logic 1.
Therefore:
Vin(max) = (2n - 1) x Vref -------------------------------------------(3)
2n
The (2n - 1) represents the maximum output count value of the A/D converter.
The input range for an 8 bit A/D converter using a 5 V reference can be obtained using equations 2 and
3 as
Vin(min) = 19.53 mV and Vin(max) = 4.98 V
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

4.5.4.1 Quantitisation Error


The quantitisation error in an A/D converter is given by:
Quantitisation error = 0.5 x Full Scale output -------------------------------------------(4)
2n
Therefore for an 8 bit A/D converter using a 5 volt reference the quantitisation error is
(0.5 x 5)/256 = 9.76mV

4.5.4.2 The PIC A/D converter


The A/D converter on the PIC 16C71 has a number of registers to select and control the various options.
The A/D input is fed via port A which must be configured for use as an analogue input port.
The registers associated with the A/D block are:
REG

Functions

ADCON0
ADCON1
ADRES
TRISA
PORTA
INTCON

Channel select, conversion clock on/off, interrupt flag and start/end conversion.
Input selection for analogue or digital.
Result of A/D conversion (count value)
Input/Output select for digital select bits.
Input/Output read/write for digital select bits.
A/D interrupt enable/disable.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Some of these registers are used for other purposes.

4.5.5 The ADCON0 Register

This register is used to set up the A/D converter. The A/D converter has four analogue input channels
that it shares with portA bits 0 to 3.
It also uses a number of possible A/D converter clocking sources so that the conversion time can be
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

selected for a particular application. The fastest clock is selected with bits 7 and 6 set to 0 and
produces a clocking rate that is half the oscillator frequency (ie, a 4MHz clock would give an A/D
conversion clock of 2MHz).
Bit 0 of the ADCON0 register can be used to enable/disable the A/D converter module.
To start a conversion, a logic 1 is written to bit 2 of the ADCON0 register. The same bit can be read by
the PIC to determine whether the conversion is complete. A conversion is complete when bit 2 goes to
logic 0. If the interrupt bit ADIE (bit 6 of the INTCON register in the 16C71 PIC) is set, the A/D converter
will cause an interrupt when the converter has completed a conversion.

4.5.6 The ADCON1 register


Before using the A/D converter the ADCON1 register MUST be configured to make the choice of which
port A pins will be used for analogue inputs and which will be used for digital I/O.
When using a PIC16c7x type of microcontroller (containing an A/D module) the configuration of the
ADCON1 register is very important since it is reset (all bits zero) on power up and this configures port A
bits as analogue inputs. This means that if the 16C7x PIC port A pins are being used for digital I/O the
device will not be able to control the digital I/O pins in this state.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

As you can see from the above figure, Port A can be configured to give a number of possible mixes of
analogue and digital arrangements.
Note that for some configuration settings an external voltage reference can be used.

4.5.7 Using the PIC A/D module


The following assembly program illustrates how the A/D block can be configured and used:
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Strt

conv

MOVLW
MOVWF
MOVLW
MOVWF
BSF
BTFSC
GOTO
MOVF

02h
ADCON1
01h
ADCON0
ADCON0,2
ADCON0,2
conv
ADRESS,W

;required configuration for ADCON1 register


;required configuration for ADCON0 register
;start a conversion
;wait for conversion complete
;transfer count value to W register

The fragment of assembly program shown above can be followed by whatever processing is to be done
with the digital count value. Note the branch and skip instruction to poll the done flag bit for end of
conversion.

Task 2
Look up the A/D Conversion Block diagram for the 16C71 in Nigel Gardner's book
"Beginners Guide to the Microchip PIC". There is also an example of how to use the
A/D converter.
Note that in this example, the Sample and Hold amplifier is not part of the PIC A/D
module, being a separate external device in series with the analogue input channel.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

4.6 The Serial Interface Port


The serial interface used on the PIC is very similar to most Universal Asynchronous/Synchronous Receiver
Transmitter (UASRT) devices.
General description of a UART
A UART is a Universal Asynchronous Receiver/Transmitter device that can transmit and receive serial data
over a serial (two wire) cable. Most computers support a UART. The PC uses a 16450 device that supports
baud rates of up to 115,200 bits per second.
The UART contains two basic blocks:
Transmit block to send data to another remote device
Receive block to receive data from another remote device
The nature of asynchronous transmission is that the data can be transmitted at any time and the receiver will
be able to receive the data. There is no need to synchronise data between transmitter and receiver.
This is achieved by providing "protocol" information with the data in the form of a START bit and a STOP bit.
The receiver looks out for the START bit, collects the data bits (one byte or 8 bits) and on receiving the STOP
bit will strip off the START/STOP bits and retrieve the 8 data bits.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

STOP d7 d6 d5 d4 d3 d2 d1 d1 START
BIT

BIT

The transmitter always adds the START and STOP bits to the data prior to transmitting the data at a baud rate
which is set up during the configuration of the UART. The receiver "synchronises itself" to the incoming data by
detecting the START bit and then identifying the middle of each data bit it receives.

4.6.1 UART Transmitter block


The block diagram below illustrates the basic components of the transmitter block of a UART.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

The required baud rate is set up by writing to the Baud Rate Register (not shown in the diagram). Typical baud
rates are 9600, 19,200, 38,400, 57,600, and 115,200 baud.
The baud rate generator is simply a pre-scaler which uses the processor clock scaled down to the required
transmit/receive clock rate required.
Care must be taken when selecting the baud rate since the crystal chosen for the processor clock can result in
significant errors. The manufacturer's data sheet must be consulted to find the best match between required
baud rate and processor clock.
Data to be transmitted is loaded into the Transmit Data Register where it is transferred into the Transmit Shift
Register along with the Start and Stop bits. It is then transmitted serially at the selected baud rate via the TX
pin.
When the last bit (the stop bit) has left the shift register a flag bit TXIF is set to indicate that the shift register is
empty and that new data can be loaded into the Transmit Data Register.
The TXIF flag can be read by the processor (ie, polled) to discover when the data has been transmitted.
Alternatively, if the transmit interrupt enable bit (TXIE) is set, an interrupt will be generated when the transmit
shift register is empty and a user interrupt service routine can be called to deal with the next data
transmission. In this way the processor does not need to waste time polling the TXIF flag to determine when to
send the next data bit.

4.6.2 The UART Receiver Block


The receiver block is illustrated below.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

The receiver block, like the transmitter block, has access to the baud rate generator. Usually the transmit and
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

receive baud rates will be the same.


Data from another processor UART is received on the RX pin and is routed through to the receive shift register
at the rate specified by the receive baud rate generator.
Once the complete data packet (with start and stop bits) has been received, the data bits d0 through to d7 are
loaded into the receiver data register where they can be read by the processor.
When the last bit of the data packet (stop bit) is received, the receiver interrupt flag (RXIF) is set to indicate
that the receive data register is full. The processor can poll this RXIF bit to find out when there is data in the
receive data register.
Alternatively, if the receiver interrupt enable bit (RXIE) is set, an interrupt will be generated to indicate that the
receiver data register is full and an appropriate interrupt service routine can be called to deal with the new data
received. This, of course, saves on the time waiting for new data by having to poll the RXIF bit.

Click on the button below to go to the Interactive Self Assessment

Contact the tutor


To contact the tutor please click below.
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

This project is part funded by the European Social Fund.


Text & images 1999 Northumbria University unless otherwise stated.
website www.unn.ac.uk

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Vous aimerez peut-être aussi