• Aucun résultat trouvé

<7> = 1, to enable the comparator.

<6> = 0, this is the output bit.

<5> = 0, output to CM1CON0<6>, not to pin 17.

<4> = 0, for non-inverted polarity.

<3> = 0 (no function).

<2> = 1 , use internal reference.

<1:0> = 00, input at pin 18.

Settings of <1:0> for accepting input at other input pins are 01 (pin 15), 10 (pin 14) and 11 (pin 7). To set Comparator 1 as above, the byte is 10000100, or 84 in hex, which is to be stored at 119h.

Similarly for Comparator 2; the byte 10000101, or 85h, when stored at 11Ah sets that comparator to accept input at pin 15.

As we are using the internal reference this must be programmed by setting the VRCON register. This can provide either a 0.6 V constant reference or a variable reference in the range 0 V to 0.71875 × VDD. The reference voltage is applied to the inverting input.

To set up both the comparators for the variable internal reference, the bits in VRCON are as follows:

<7> = 1, to use variable reference for Comparator 1.

<6> = 1, to use variable reference for Comparator 2.

<5> = 0, to select low range (or = 1 for high range).

<4> = 0, constant reference disabled.

<3:0>, four bits to select the range of the variable reference. In the low range the reference is (this value) / 24 × VDD. In the high range it is VDD/4 + (this value) / 32

× VDD.

For example, the word 11001100 (CCh) stored at 118h sets both comparators to use the variable reference in its low range. The value of bits <3:0> is 1100 (decimal 12), so the reference is 12/24 × VDD, that is half the supply voltage.

The output of the comparators is either 0 or 1 and is readable at bit <6> of CM1CON0 and CM2CON0. Both outputs can be read at the same time as bits <7> and <6> of a third register, CM2CON1, at 11Bh.

As well as the registers directly involved with the comparators we must also enable the inputs for analogue voltages. This is done through the analogue select register ANSEL, at 11Eh.

Pin 18 is the AN1 input and pin 15 is the AN5 input so we set bits <1> and <5> to 1. The byte is 00100010, or 22h.

The registers concerned with the comparators are all in Bank2, so the routine for setting up Comparators 1 and 2 with a reference of half the supply voltage is:

bsf status, 6 ; Page 2.

movlw 22h ; Define AN1 (pin 18) and AN5 movwf ansel ; (pin 15) as analogue inputs.

movlw 84h ; Set up Comparator 1.

movwf cm1con0

movlw 85h ; Set up Comparator 2.

movwf cm2con0

movlw CCh ; Set up reference voltage for both movwf vrcon ; comparators.

bcf status, 6 ; Back to page 0.

By the way, all listings in Part 4 assume that the addresses of the registers have been declared by using EQU directives at the beginning of the program (p. 135), or that they are defined by the compiler program. If the controller is already in Page 1 mode when this routine is run, bit 5 of STATUS must be cleared first.

To read the output of Comparator 1 use these lines:

bsf status, 6 ; Page 2.

movlw 40h ; Bit 6 of this byte is ‘1’.

andwf cm1con0, w ; Read the output bit, result in w.

bcf status, 6 ; Back to Page 0.

The ANDing operation sets or clears the zero bit of the STATUS register, which may then be tested in the usual way and suitable action taken.

Analogue input is fed to the converter through any one of 14 channels. For this discussion we will refer only to channel AN0, which is at pin 19. At power-on, all the PIC’s output channels are automatically set as analogue inputs. The analogue selection register ANSEL, which is in Bank 2, is used to define which are to be reset as digital inputs. If bit

<0> is ‘1’ this makes AN0 (pin 19), an analogue input. At the same time it disables the weak pull-up and the interrupt-on-change function.

The next step is to select the input channel and other functions, using ADCON0. The setting used in this book is:

<7> = 0. result of conversion left justified (see below).

<6> = 0, use supply voltage as reference.

<5:2> = 0000, select AN0 as input channel (pin 19). These bits can take other hex values for channels AN0 to AN11 (1011).

<1> = 1. Set this bit to start a conversion, then read it; it stays 1 while the conversion is in progress, and goes low when it is completed. This is the GO/DONE bit.

<0> = 1. This enables the converter, 0 turns it off. In summary, setting <0> sets up the converter on channel AN0 and setting <1> starts the conversion.

The final step is to select the clock rate for the conversion, in the ADCON1 register bits

<6:4> do this. In our circuits there is no hurry so we can use the slowest rate, and in this case can leave these bits clear, as on reset. In other words, nothing to do.

The output is a 10-bit value and so needs two registers to hold it. These are ADRESH and ADRESL. With left justification selected (see above), the 8 most significant bits appear in ADRESH and the 2 least significant bits in bits <7:6> of ADRESL. Bits <5:0> of ADRESL read as 000000. This is how it looks:

ADRESH ADRESL X X X X X X X X X X 0 0 0 0 0 0 10-bit result

If high precision is not required (or is not possible), the content of ADRESL can be ignored. Ignoring this gives 8-bit precision (1 in 256, or approximately 0.4%) which is better than most of our sensors can provide.

Using channel AN0 and reading a low-precision result turns out to be relatively simple as nothing to do to certain of the registers. The only registers to be set are ANSEL in Bank 2 and ADCON0 in Bank 0. The result is read in ADRESH, which is also in Bank 0. Here is a simple routine:

bcf status, 5

bsf status, 6 ; Bank 2

bsf ansel, 0 ; Select channel AN0 bcf ststus, 6 ; Back to Bank 0 bsf adcon0, 0 ; Turn on converter bsf adcon0, 1 ; Start conversion wait:

btssc adcon0, 1 ; If conversion complete goto wait ; If not complete

movf adresh, w ; Read 8-bit result into

; working register.

The result in the working register is ready for processing.