Файл: The quintessential PIC microcontroller (S. Katzen, 2000).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 15.06.2025

Просмотров: 5305

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

14. Take the Rough with the Smooth 403

4.SAQ1 switches Vref into the lowest capacitor giving a Vref/16 step at N (161 ). The resulting voltage of −0.0535+0.0625 = +0.009Vref giving a comparator output of logic 0 and SAR1 is cleared and the Vref/16 step is reversed.

The state of the SAR of 0110b or 0.375Vref represents the best 4-bit fit to Vin = 0.4285Vref. The residue 0.0535Vref is the quantizing error. Most

MCUs use an 8-bit capacitor array. In principle the technique can readily be extended to higher resolutions, but in practice the di culty in matching ever greater capacitors and internal logic noise means the majority of processors use 8-bit resolution. However, a few MCU devices5 do have 10 or 12-bit converters. External successive-approximation devices with 12 or more bits resolution, usually using a resistor ladder network, are readily available, but are relatively expensive.

Matching of the array capacitors, o sets and resistance of internal switches, leakage currents and analog comparator non-linearities all contribute to errors in the conversion process. It is beyond the scope of this text to analyze the various measures of error but the device data sheet lists absolute error, defined as the sum of all component error measures, as better than ±1 LSB. This guarantees that the transfer is monotonic; that is the binary code will never move in the reverse direction for any

change ∆Vin of input voltage. This error figure is for Vref = VDD; if Vref is lower than VDD then accuracy deteriorates, although values down to 3 V

will give acceptable results in most cases. Accuracy can be improved, especially when the internal CR oscillator is used, if the conversion is done while the PIC is in its Sleep mode.

The standard PIC ADC module has eight input channels with any one selected for conversion according to the 3-bit Channel Select code CHS2:0 in the ADCON0 register, as shown in Fig. 14.8. 28-pin footprint PICs, such as the PIC16C73, can only access the bottom five channels. The PIC16C71 line of 18-pin footprint6 and 12C67X devices have an earlier 4-channel module with CHS2 missing. The PIC16C774 uses ADCON0[1] as CHS3.

The input analog channels AN4:0 are shared with the Port A digital inputs RA4:0 and AN7:5 with Port E RE2:0 in 40-pin devices. AN3 is special in that it can be used as the reference voltage input if configured accordingly by PCFG0 = 1 (Port ConFiGuration) bit in the ADCON1 register. Like all port configuration registers it is normally set up only once at the beginning of the program and it is therefore located in the less convenient Bank 1 Data memory. Any such low-noise external Vref should be in the range 3 V → VDD + 0.3 V – see Fig. 14.17. For best accuracy it should be as high as possible; a value of 5.12 V will give a 20 mV per bit resolution.

5For example the PIC16F87X devices have a 10-bit ADC and the PIC16C77X have a 12-bit ADC with internal precision positive and negative reference voltages.

6The PIC16C71/710/711/715.


404 The Quintessential PIC Microcontroller

ADCON1

3

2

1

0

PCFG2

PCFG1 PCFG0

File 9Fh

A/D

CONtrol

register 1

VDD

AN3

0

1

AN7/RE2

7

AN6/RE1

6

AN5/RE0

5

Vref

-8

AN4/RA5

4

bit

Sample

A/D

AN3/RA3

3

7K

switch

Vin

converter

50p

AN2/RA2

2

AN1/RA1

1

POWER ON

VDD

CLK

AN0/RA0

1K

0

5p

500n

MUX address

Each channel

C

B

A

A/D

RESult

register

ADRES

Conversion complete

C1

E

A/D CONtrol register

0

1D

ADCON0

7

6

5

4

3

2

1

0

ADCS1

ADCS0

CHS2

CHS1

CHS0

GO/DONE

*

ADON

Analog

channel

address

File 1Fh

00

B

A

*ADIF for

the 4-channel

PIC16C71

line.

F/2

CHS3 for

the 10-channel PIC16C774.

File 1Eh

01

tAD

F/8

ELSE

reads as 0

10

F/32

Internal

Data

bus

11

CR

MUX

to MCU

Read ADRES

Fig. 14.8 The 8-bit 8-channel analog to digital conversion module.

A low cost option is to use the standard supply voltage VDD (PCFG0 = 0) as the reference voltage, thus freeing up AN3 for use as a normal analog input. To reduce noise, a 0.1–1 µF Tantalum electroytic capacitor should be used to decouple noise as close as possible to the VDD pin.

If less than eight analog channels are needed then some of the pins can be assigned as digital I/O port lines using the PCFG2:0 bits as listed in Table 14.3. For example, if PCFG2:0 = 100 then RA1:0 are both analog (AN1:0) and the rest are digital (RA4:2, RE2:0) as applicable, with VDD used as the reference.

On Reset all pins are set to accept analog signals (A). Pins that are reconfigured as digital I/O (D) should never be connected to an analog signal. Such voltages may bias the digital input bu er (see Fig. 11.2 on


14. Take the Rough with the Smooth 405

Table 14.3: Configuring the ADC port pins in the PIC16C73/74 devices.

PCFG2:0

AN7

AN6

AN5

AN4

AN3

AN2

AN1

AN0

RE2

RE1

RE0

RA5

RA3

RA2

RA1

RA0

000

A

A

A

A

A

A

A

A

001

A

A

A

A

Vref

A

A

A

010

D

D

D

A

A

A

A

A

011

D

D

D

A

Vref

A

A

A

100

D

D

D

D

A

D

A

A

101

D

D

D

D

Vref

D

A

A

110

D

D

D

D

D

D

D

D

111

D

D

D

D

D

D

D

D

page 273) into its linear range and the resulting large current could cause irreversible damage.

Other PIC devices with an ADC module may have di erent settings and numbers of PCFG bits. For example the 8-pin footprint PIC12C67X devices with a 4-channel ADC module can configure individual pins as analog or digital to best use scarce resources. The PIC16C71X line has only the two PCFG bits whilst the PIC16C774 has four.

Vin to A/D converter

+VDD

module

TRIS

buffer

I/O

pin

Data store

bus

1D

Write

to

Port

C1

Data

flipflop

Overvoltage

protection

1D

Write

to

TRIS

C1

Analog

input

mode

*

Digital

TRIS

flipflop

input

buffer

To input

*Port E

latch

Fig. 14.9 Configuring the analog inputs for Port A and Port E.

We can see from Fig. 14.9 that an I/O pin configured as an analog input from ADCON1 simply disables the digital input bu er (compare with Fig. 11.2 on page 273). No other circuitry is a ected. From this we can make the following deductions.


406 The Quintessential PIC Microcontroller

A port pin configured as analog will read as logic 0 due to the disabled digital input bu er.

The TRIS bu er is not a ected and thus the appropriate TRIS bits should be 1; that is the direction of the port pins configured as analog

should be set to input to prevent contention between the nalog Vin and the digital state of the Data flip flop.

The ADC can read an analog voltage at the pin even if that pin has not been configured as analog. However, the still active digital input bu er may consume an excessive current outside of the device’s specification.

Using Fig. 14.8 as the programmer’s model we can now deduce the hardware-software interaction in order to action a conversion. Assuming first that interrupts are not being used, the following steps can be identified:

1.Configure ADC module.

Set up port pins as analog/voltage reference (ADCON1).

Select ADC conversion clock source (ADCON0).

Select ADC input channel (ADCON0).

Turn on ADC module (ADCON0).

2.Wait for the required acquisition time, typically 12 µs.

3.Start conversion by setting the GO/DONE bit.

4.Wait for ADC conversion to complete by polling the GO/DONE bit for low.

5.Read the ADRES register.

6.For next conversion go to step 1 or step 2 as required.

As an example, consider that we wish to read the channel n analog voltage (RAn) of a PIC16C74 and output the equivalent digital value at

Port B. The main crystal is 20 MHz and VDD is to be used as Vref.

The listing of Program 14.1 assumes that the ADC module has been initialized at reset with startup code of the form:

bsf

STATUS,RP0

; Bank

1

clrf

ADCON1

; All port inputs are analog

bcf

STATUS,RP0

;

Back

to Bank

0

movlw

b’10000001’

;

AD clock/32,

Ch0, no convert, ADON

movwf

ADCON0

which sets up the pin configuration according to the PCFGn settings of Table 14.3 to enable all eight ADC channels. The ADCON0 is initialized to 10 000 001b to set the module clock source as the crystal frequency/32;

i.e. 2032 = 625 kHz (giving a conversion time of ≈ 12 +15µs), channel zero

and the module is turned on. With an initial zero value of GO/DONE no conversion is actioned. The initial channel value is irrelevant.

With the module initialized, the subroutine listed in Program 14.1 simply copies the contents of W, truncated to three bits for robustness, into a temporary location TEMP. There it is logic shifted left three places to align the channel number with the CHSn bits in the ADCON0 register.


14. Take the Rough with the Smooth 407

Program 14.1 Taking a reading from channel n.

; ***********************************************************

; *

FUNCTION:

Analog/digital

conversion

at channel n

*

; *

RESOURCE:

Subroutine DELAY_12US,

byte

TEMP

*

;

*

ENTRY

:

Channel number

in W

*

;

*

EXIT

:

Digitized analog value

in

W

*

; ***********************************************************

GET_ANALOG

andlw

b’0111’

;

Isolate address bits

movwf

TEMP

;

Channel number

bcf

STATUS,C

;

Shift channel number left >>3

rlf

TEMP,f

rlf

TEMP,f

rlf

TEMP,w

bcf

ADCON0,CHS0

;

Zero channel bits

bcf

ADCON0,CHS1

bcf

ADCON0,CHS2

addwf

ADCON0,f

call

DELAY_12US

;

Wait 12us to stabilize

bsf

ADCON0,GO

;

Start conversion

GET_ANALOG_LOOP

;

Takes around 15us to finish

btfsc

ADCON0,NOT_DONE;

Check for End Of Conversion

goto

GET_ANALOG_LOOP

movf

ADRES,w

;

Fetch when GO/NOT_DONE zero

return

; *********************************************************

; *

FUNCTION:

Delays 12us at 20MHz

*

; *

ENTRY

:

None

*

;

*

RESOURCE:

None

*

;

*

EXIT

:

W is zero

*

; *********************************************************

DELAY_12US

movlw

d’14’

; Delay constant

DELAY_12US_LOOP

addlw

-1

; Decrement

btfss

STATUS,Z

; Until zero

goto

DELAY_12US_LOOP

return

After clearing the CHS2:0 bits, the shifted channel number can then be added into ADCON0 to set CHS2:0 to the appropriate channel.

Once the channel number has been set up, a 12 µs delay subroutine is called and then the GO/DONE bit in ADCON0 is set to initiate a conversion.7 The completion of the process can then be monitored by polling GO/DONE until this goes low. Notice that the p16c74.inc include file for clarity allows the programmer to use either bit name GO or NOT_DONE

7A conversion may be aborted at any time by clearing GO/DONE.