Académique Documents
Professionnel Documents
Culture Documents
Microcontrollers
Chapter 4 - A more detailed look at Microcontroller Peripheral
Modules
Chapter Information
Self Assessment
pdfcrowd.com
Author:
Self Assessment
10 hours
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
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:
4.2 Registers and how they are used to configure and control peripheral devices
open in browser PRO version
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.
Registers
Here we see data 9Dh read from PortA.
pdfcrowd.com
7 6 5 4
1 0 0 1
Writing to Registers..
Here we see data 97 hex written into the output port B.
7 6 5 4
1 0 0 1
pdfcrowd.com
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.
pdfcrowd.com
The arrangement shown above illustrates how this is done. The Direction control register is used to
open in browser PRO version
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.
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
pdfcrowd.com
Show Answer
Hide Answer
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
pdfcrowd.com
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
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.
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
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
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
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.
pdfcrowd.com
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
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.
bits
RTS
RTE
PSA
PS2
PS1
PS0
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
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.
MOVLW
MOVWF
CLRF
BTFSS
GOTO
87h
OPTION
TMR0
INTCON,TOIF
chktmr
pdfcrowd.com
GOTO
BFC
chktmr
TOIF
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.
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
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
pdfcrowd.com
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.
8 bits
giving 2n = 28
= 256
pdfcrowd.com
pdfcrowd.com
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.
pdfcrowd.com
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
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.
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.
pdfcrowd.com
Strt
conv
MOVLW
MOVWF
MOVLW
MOVWF
BSF
BTFSC
GOTO
MOVF
02h
ADCON1
01h
ADCON0
ADCON0,2
ADCON0,2
conv
ADRESS,W
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.
pdfcrowd.com
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.
pdfcrowd.com
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.
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
pdfcrowd.com
pdfcrowd.com
pdfcrowd.com