uart

40
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API 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.2 Registers and how they are used to configure and control peripheral devices 4.3 Control/Status registers 4.4 Multiplexing of microcontroller pins 4.5 A study of the peripheral units within a PIC microcontroller 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.6 The serial interface port 4.6.1 UART Transmit block 4.6.2 UART Receiver block Self Assessment

Upload: lavanyachandran

Post on 30-Sep-2015

1 views

Category:

Documents


0 download

DESCRIPTION

ds

TRANSCRIPT

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Select a Chapter

    Microcontrollers Chapter 4 - A more detailed look at Microcontroller PeripheralModules

    Chapter Information

    This chapter contains the following sections:

    4.1 Introduction

    4.2 Registers and how they are used to configure andcontrol peripheral devices

    4.3 Control/Status registers

    4.4 Multiplexing of microcontroller pins

    4.5 A study of the peripheral units within a PICmicrocontroller

    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.6 The serial interface port 4.6.1 UART Transmit block 4.6.2 UART Receiver block

    Self Assessment

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    4.5.1 The timer module used in the PIC 4.5.2 Using the timer module

    Self Assessment

    Author:

    Recommended textbooks:

    Notional workload:

    For tutor support contact:

    Peter Minns of Northumbria University

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

    The PIC Trainer User Manual, Flight Electronic Ltd.

    The Microchip PIC16C84/16F84 Data Sheets

    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 modulesAnalogue to Digital interfacesSerial I/O modules

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    In chapter 3 we looked at the instruction set of the microcontroller and used it in a number of keyapplication areas such as turning on and off digital I/O, loop counting and using sub-programs (or sub-routines).

    In this chapter we will look at some of these peripheral hardware modules in some detail using theMicrochip PIC as an example of a real microcontroller. The main reasons for choosing the PICmicrocontroller 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 Motorola68x05/68x08, the SGS Thompson ST6/ST7 range of microcontrollers and the Hitachi H8series.)

    Before examining how PIC peripheral modules are used we need to consider how they are configuredand controlled.

    Peripheral modules were introduced in chapter 3 to illustrate the PIC instruction set. Here we examinehow 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

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    All microcontrollers have a number of internal registers and some can contain over a hundred. Eachregister is simply a set of n bit memory locations that can be addressed at a unique memory addresswithin 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.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    7 6 5 4 3 2 1 0

    1 0 0 1 1 1 0 1

    9 | D

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

    7 6 5 4 3 2 1 0

    1 0 0 1 0 1 1 1

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    9 | 7

    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 microcontrollersthe ports can be configured to be either inputs or outputs under program control.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    The arrangement shown above illustrates how this is done. The Direction control register is used to

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    configure the port pins for either input or output, depending upon whether the corresponding bit is set orcleared.

    If a particular bit in the Direction control register is a 1, the corresponding bit in port A will be configuredas an input. If the bit in the Direction control register is a 0, the corresponding bit in port A will beconfigured 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 aTRIS register bit sets the corresponding port pin bit as an input and hence tri-state. Writing a 0 into aTRIS register bit sets the corresponding port pin as an output.

    The arrangement is illustrated below.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    The TRIS (TRI-State control register) is loaded with a byte of data by moving the data to it from the Wregister.

    Hence, if the following instruction sequence was used:

    MOVLW F0h MOVWF TRISA

    7 6 5 4 3 2 1 0

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

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    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, theycan be used in a number of different ways. The programmer must configure these devices to operate inthe 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.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    In this figure a control register is used to enable an interrupt by setting bit 7 of the control register to logic1. Inside the microcontroller this enables an AND gate whose output is connected to pin 0 of a Statusregister.

    The actual interrupt input pin of the microcontroller is connected to the other input of the AND gate so thatwhen an interrupt does occur it will set the status bit 0 to logic 1. By reading the status register theprogrammer can identify if an interrupt has occurred since if bit 0 is set, then an interrupt has occurred onthe input interrupt pin of the microprocessor.

    Notice that the status register also allows the programmer to see whether the interrupt has been enabledor not by reading bit 7 of the status register.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    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 statusof the interrupt condition. (Has an interrupt occurred or not?)

    Most microprocessors and microcontrollers have a STATUS register containing a number of assortedbits 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 anumber of bits special to the PICs architecture. See page 36 of Nigel Gardner's Beginner's Guide tothe Microchip PIC for more details on this.

    Most microcontrollers have a number of status registers, not just the one for the CPU status. Some haveregisters with both status and control bits in them (for example, the INTCON register in the PIC range ofmicrocontrollers). See the PIC16C84 data sheet for a preview of this particular register. It containsinterrupt 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 devicesto reduce the total number of pins needed. The microcontroller must contain internal multiplexers to dothis and these multiplexers are controlled through the use of registers.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    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 theinput: either PA0 (sel A = 1) or D0 (sel A = 0). Likewise input pin bit 7 can be configured to be either portA 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 additionalpins would result in a larger product size. So most microcontrollers have multiplexed pin functions.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

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

    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 youwill be invited to compare the PIC architecture with that of a number of other microcontrollers, but moreon that later.

    We will start by looking at the general architecture of the PIC.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    The PIC series of microcontrollers is based on a Harvard 8 bit architecture. The Harvard architectureseparates the instruction and data buses so that the processor can be executing an instruction while, atthe 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 onedata path, unlike more advanced MIMD computers such as the CRAY or the more modest Transputer).

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    The performance is further advanced by the use of a two-stage pipeline for instructions (see page 10 ofthe PIC16c84 data sheet included with the PIC Trainer User Manual.This pipeline allows the processorto execute each instruction in one clock cycle. The exception is a branch instruction, which takes twoclock cycles to complete.

    The instruction word is 14 bits wide with a separate 8 bit data bus. Some of the high-end PIC deviceshave a 16 bit wide instruction word.

    Some PIC devices come with EPROM memory and can be reprogrammed a number of times byexposing the windowed chip to a strong ultra-violet light source for about 20 minutes. A limited number ofPICs make use of electrically erasable memory (EEPROM) that allow them to be re-programmed withinthe circuit.

    The 16c84 PIC used in the PIC trainer kit uses EEPROM program memory so it can be reprogrammedwithin the circuit.

    The turquoise blocks in the PIC block diagram are the main elements of the microcontroller and aretypical of any microcontroller.

    The ALU (Arithmetic Logic Unit) performs the Add/Subtract and shift operations required by anymicroprocessor.

    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 whetherthe last instruction produced a zero result (Z bit = 1), or there was a carry (C bit =1), or an interruptoccurred.

    The stack register is used to keep track of the return address from a subroutine or interrupt, as well asany registers that the programmer may also wish to save there.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Most microcontrollers need to be configured for a particular mode of operation (such as microprocessormode) and for the function of some of the peripheral devices. The configuration, once set, will notchange during the life of the device in its final application and can therefore be set in EPROM memoryduring the programming of the device. The configuration fuses are not fuses in the real sense, but just anumber of EPROM memory locations that are programmed. For more information about configuring thePIC16c84, see page 35 of the PIC16c84 data sheet.

    Like most microcontrollers the PIC chip includes the clock logic. The oscillator circuit is designed to beconnected to an external crystal or an external resistor-capacitor network (for low cost applications). Seethe 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 setof 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 workhorseregister within the PIC.

    2. Status registers. These contain such things as the carry bit and the zero bit, as well as otherfunctions 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 Aand a Port B. Other PICs may have more, depending upon the complexity and number of pinsavailable on the device.

    4. Option register: This is used to set up the mode used by the PIC's internal timer module, as well assome other functions.

    5. INTCON register: This register is used to set up interrupt status in some of the PIC peripheralmodules, 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

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    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 atspecific addresses within the memory map.

    Look up the memory map of the PIC16c84 and determine:

    1. The EPROM address area2. The RAM address area3. The addresses for the Ports A and B4. The address for the reset vector5. The address for the peripheral interrupt vector6. 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.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Count the number of input pulses arrivingProduce 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 ofthe PIC to increment an 8 bit counter in real time. Most microcontrollers do this as well.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    In the PIC, the counter can be incremented on every clock pulse, or the clock signal can be gated into apre-scaler unit so that the clock signal is divided down to a lower frequency. In this way, the timer can bemade 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 onetimer 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 maximumvalue 1111 1111 (255 decimal, FF hex) before rolling over to zero, 0000 000. The action of roll-over tozero 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 timercounter has rolled over.

    The OPTION register can be used to select the source for the clock pulses going to the timer counterRTCC. 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 aPIC using a 4MHz clock will count at 1MHz rate.

    Option Register bits (associated with the RTCC)

    bits 7 6 5 4 3 2 1 0

    RTS RTE PSA PS2 PS1 PS0

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    RTCC 0 0 0 0 2

    WDT 1 0 0 1 4

    edge 0 > 1 0 0 1 0 8

    edge 1 > 0 1 0 1 1 16

    internal 0 1 0 0 32

    external 1 1 0 1 64

    1 1 0 128

    1 1 1 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', asshown.

    Example To use the timer unit with the internal clock source on a zero to one transition and a 64 divider pre-scaler, the OPTION register should be loaded with 0000 0101.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    What would be the value to load into the OPTION register in order to use the timer unit with a 128 pre-scaler 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 beset. This flag bit can be polled (a process of repeatedly reading the flag bit to determine whether it hasset) 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 isinitiated via an event on an interrupt input pin. This interrupt pin can be either one of the PIC interruptinputs (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 somesort.

    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 setat power-on reset.

    Strt

    chktmr

    MOVLW MOVWF CLRF BTFSS GOTO

    87h OPTION TMR0 INTCON,TOIF chktmr

    ;find out what this configuration is?

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

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    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 rightthrough from 0000 0000 to 1111 1111. When it rolls over to 0000 0000 again, the INTCON TOIF bit willbe set (and stay set until it is cleared by the bit set instruction shown). This will produce the maximumdelay 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 notrequired since the PIC main program will be interrupted when the timer overflow occurs. However, theclearing 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 writableregister that contains the various enable bits for all of the interrupt sources. The idea of an interruptenable register is to allow the many interrupt sources to be enabled or disabled. The INTCON registeralso allows the source of the interrupt to be identified.

    The INTCON register will differ between the different PIC devices. This is because each PICtype 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 eachinterrupt source as it is required.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    The INTCON register bits for the 16C84 PIC are illustrated in the above figure. The function of each bit isdescribed 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 theinterrupt can be determined by reading the INTCON register to find out which bit was set and hence

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    which RB port input has changed. Bit 0 should then be cleared with a software write to the INTCONregister.

    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.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    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, theinterrupt 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 atimer interrupt occurs, the timer interrupt flag bit (bit 2) will set to 1, and a read of the INTCON register willconfirm 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 beencompleted. This is needed because writes to EE memory are slow and valuable processor time can beotherwise 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.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    This bit is used to turn all of the above interrupts on or off. Maskable interrupts are interrupts that can beenabled or disabled.

    An interrupt that is "non-maskable" is one that cannot be disabled. Non-maskable interrupts usually havea 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 bitsuccessive approximation converter that uses an internal 5 Volt reference source. It is possible to usean 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

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    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 inputthat 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 and3 as

    Vin(min) = 19.53 mV and Vin(max) = 4.98 V

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    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

    ADCON0 ADCON1 ADRES TRISA PORTA INTCON

    Functions

    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.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

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

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    selected for a particular application. The fastest clock is selected with bits 7 and 6 set to 0 andproduces a clocking rate that is half the oscillator frequency (ie, a 4MHz clock would give an A/Dconversion 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 bythe PIC to determine whether the conversion is complete. A conversion is complete when bit 2 goes tologic 0. If the interrupt bit ADIE (bit 6 of the INTCON register in the 16C71 PIC) is set, the A/D converterwill 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 whichport 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 theADCON1 register is very important since it is reset (all bits zero) on power up and this configures port Abits as analogue inputs. This means that if the 16C7x PIC port A pins are being used for digital I/O thedevice will not be able to control the digital I/O pins in this state.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    As you can see from the above figure, Port A can be configured to give a number of possible mixes ofanalogue 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:

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    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 donewith the digital count value. Note the branch and skip instruction to poll the done flag bit for end ofconversion.

    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 theA/D converter.

    Note that in this example, the Sample and Hold amplifier is not part of the PIC A/Dmodule, being a separate external device in series with the analogue input channel.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    4.6 The Serial Interface Port

    The serial interface used on the PIC is very similar to most Universal Asynchronous/Synchronous ReceiverTransmitter (UASRT) devices.

    General description of a UART

    A UART is a Universal Asynchronous Receiver/Transmitter device that can transmit and receive serial dataover a serial (two wire) cable. Most computers support a UART. The PC uses a 16450 device that supportsbaud rates of up to 115,200 bits per second.

    The UART contains two basic blocks:

    Transmit block to send data to another remote deviceReceive 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 willbe 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 STOPbit will strip off the START/STOP bits and retrieve the 8 data bits.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    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 ratewhich is set up during the configuration of the UART. The receiver "synchronises itself" to the incoming data bydetecting 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.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    The required baud rate is set up by writing to the Baud Rate Register (not shown in the diagram). Typical baudrates 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 requiredtransmit/receive clock rate required.

    Care must be taken when selecting the baud rate since the crystal chosen for the processor clock can result insignificant errors. The manufacturer's data sheet must be consulted to find the best match between requiredbaud rate and processor clock.

    Data to be transmitted is loaded into the Transmit Data Register where it is transferred into the Transmit ShiftRegister along with the Start and Stop bits. It is then transmitted serially at the selected baud rate via the TXpin.

    When the last bit (the stop bit) has left the shift register a flag bit TXIF is set to indicate that the shift register isempty 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 transmitshift register is empty and a user interrupt service routine can be called to deal with the next datatransmission. In this way the processor does not need to waste time polling the TXIF flag to determine when tosend the next data bit.

    4.6.2 The UART Receiver Block

    The receiver block is illustrated below.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF APIThe receiver block, like the transmitter block, has access to the baud rate generator. Usually the transmit and

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    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 registerat 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 areloaded 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 indicatethat the receive data register is full. The processor can poll this RXIF bit to find out when there is data in thereceive data register.

    Alternatively, if the receiver interrupt enable bit (RXIE) is set, an interrupt will be generated to indicate that thereceiver data register is full and an appropriate interrupt service routine can be called to deal with the new datareceived. 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.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    This project is part funded by the European Social Fund.Text & images 1999 Northumbria University unless otherwise stated.

    website www.unn.ac.uk