- •Table of Contents
- •Preface
- •Additional Material
- •Basic Electronics
- •1.0 The Atom
- •1.1 Isotopes and Ions
- •1.2 Static Electricity
- •1.3 Electrical Charge
- •1.4 Electrical Circuits
- •1.5 Circuit Elements
- •1.6 Semiconductors
- •Number Systems
- •2.0 Counting
- •2.1 The Origins of the Decimal System
- •2.2 Types of Numbers
- •2.3 Radix Representations
- •2.4 Number System Conversions
- •Data Types and Data Storage
- •3.0 Electronic-Digital Machines
- •3.1 Character Representations
- •3.2 Storage and Encoding of Integers
- •3.3 Encoding of Fractional Numbers
- •3.4 Binary-Coded Decimals (BCD)
- •Digital Logic, Arithmetic, and Conversions
- •4.0 Microcontroller Logic and Arithmetic
- •4.1 Logical Instructions
- •4.2 Microcontroller Arithmetic
- •4.3 Bit Manipulations and Auxiliary Operations
- •4.4 Unsigned Binary Arithmetic
- •4.5 Signed Binary Arithmetic
- •4.6 Data Format Conversions
- •Circuits and Logic Gates
- •5.0 Digital Circuits
- •5.1 The Diode Revisited
- •5.2 The Transistor
- •5.3 Logic Gates
- •5.4 Transistor-Transistor Logic
- •5.5 Other TTL Logic Families
- •5.6 CMOS Logic Gates
- •Circuit Components
- •6.0 Power Supplies
- •6.1 Clocked Logic and Flip-flops
- •6.2 Clocks
- •6.3 Frequency Dividers and Counters
- •6.4 Multiplexers and Demultiplexers
- •6.5 Input Devices
- •The Microchip PIC
- •7.0 The PICMicro Microcontroller
- •7.1 PIC Architecture
- •Mid-range PIC Architecture
- •8.0 Processor Architecture and Design
- •8.1 The Mid-range Core Features
- •8.2 Mid-Range CPU and Instruction Set
- •8.3 EEPROM Data Storage
- •8.4 Data Memory Organization
- •8.5 Mid-range I/O and Peripheral Modules
- •PIC Programming: Tools and Techniques
- •9.0 Microchip’s MPLAB
- •9.1 Integrated Development Environment
- •9.2 Simulators and Debuggers
- •9.3 Programmers
- •9.4 Engineering PIC Software
- •9.5 Pseudo Instructions
- •Programming Essentials: Input and Output
- •10.0 16F84A Programming Template
- •10.1 Introducing the 16F84A
- •10.2 Simple Circuits and Programs
- •10.3 Programming the Seven-segment LED
- •10.4 A Demonstration Board
- •Interrupts
- •11.0 Interrupts on the 16F84
- •11.1 Interrupt Sources
- •11.2 Interrupt Handlers
- •11.3 Interrupt Programming
- •11.4 Sample Programs
- •Timers and Counters
- •12.0 The 16F84 Timer0 Module
- •12.1 Delays Using Timer0
- •12.2 Timer0 as a Counter
- •12.3 Timer0 Programming
- •12.4 The Watchdog Timer
- •12.5 Sample Programs
- •LCD Interfacing and Programming
- •13.0 LCD Features and Architecture
- •13.1 Interfacing with the HD44780
- •13.2 HD44780 Instruction Set
- •13.3 LCD Programming
- •13.4 Sample Programs
- •Communications
- •14.0 PIC Communications Overview
- •14.1 Serial Data Transmission
- •14.2 Parallel Data Transmission
- •14.4 PIC Protocol-based Serial Programming
- •14.5 Sample Programs
- •Data EEPROM Programming
- •15.0 PIC Internal EEPROM Memory
- •15.1 EEPROM Devices and Interfaces
- •15.2 Sample Programs
- •Analog to Digital and Realtime Clocks
- •16.0 A/D Converters
- •16.1 A/D Integrated Circuits
- •16.2 PIC On-Board A/D Hardware
- •16.3 Realtime Clocks
- •16.4 Sample Programs
- •Index
Analog to Digital and Realtime Clocks |
549 |
|||
movf |
PORTB,w |
; Read all Port-B bits |
|
|
movwf |
store1 |
; Store value for later |
|
|
rrf |
store1,f ; Rotate bit into carry flag |
|
||
rlf |
rcvdata,f |
; Rotate carry flag into result |
|
|
|
|
|
; register |
|
decfsz |
bitCount,f |
; Bump counter, skip next |
|
|
|
|
|
; if counter zero |
|
goto |
nextB |
|
|
|
; Value read is stored in rcvdata register |
|
|||
bsf |
PORTB,CLK |
; Final clock pulse |
|
|
Nop |
|
|
|
|
bcf |
PORTB,CLK |
|
|
|
nop |
|
|
|
|
bsf |
PORTB,CS ; Turn off ADC |
|
||
call |
long_delay |
; Time to settle |
|
|
Return |
|
|
|
|
16.2 PIC On-Board A/D Hardware
A few years ago, A/D conversions always required the use of devices such as the ones described in the previous sections. Nowadays, many PIC microcontrollers come with onboard A/D hardware. One of the advantages of using onboard A/D converters is saving interface lines. The circuit shown in Figure16-4 requires devoting three lines to the interface between the ADC0831 and the PIC 16F84. On the other hand, a similar circuit can be implemented in a PIC with internal A/C conversion by simply connecting the analog device to the corresponding PIC port. In the PIC world, where I/O lines are often in short supply, this advantage is not insignificant.
At the time we are writing, PICs equipped with A/D converters have either 8- or 10-bit resolution and can receive analog input in 2 to 16 different channels. The 16F877 with eight analog input channels at a 10-bit resolution is discussed. Nowadays, these PICs are easy to obtain. On the other hand, if the resolution required exceeds 10-bits then the designer has to resort to an independent A/D IC, such as the LTC1298, which has a 12-bit resolution, or to others with even higher numbers of output bits.
16.2.1 A/D Module on the 16F87x
The PICs of the 16F87x family are equipped with an analog-to-digital converter module. The number of lines depends on the specific version of the device: 28-pin devices have five A/D lines and all others have eight lines. The converter uses a sample and hold capacitor to store the analog charge and performs a successive approximation algorithm to produce the digital result. The converter resolution is 10 bits, which are stored in two 8-bit registers. One of the registers has only four significant bits.
The A/D module has highand low-voltage reference inputs that are selected by software. The module can operate while the processor is in SLEEP mode, but only if the A/D clock pulse is derived from its internal RC oscillator. The module contains four registers accessible to the application:
550 |
Chapter 16 |
•ADRESH - Result High Register
•ADRESL - Result Low Register
•ADCON0 - Control Register 0
•ADCON1 - Control Register 1
Of these, it is the ADCON0 register that controls most of the operations of the A/C module. Port-A pins RA0 to RA5 and Port-E pins RE0 to RE2 are multiplexed as analog input pins into the A/C module. In the 28-pin versions of the 16F87x, port pins RA0 to RA5 provide the five input channels. In all other implementations of the 16F87X, Port-E pins RE0 to RE2 provide the three additional channels.
Figure 16-5 shows the registers associated with A/D module operations.
|
REGISTER |
|
|
|
|
|
|
|
|
|
|
|
NAME |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
bits |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
INTCON |
GIE |
PEIE |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PIR1 |
|
ADIF |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PIE1 |
|
ADIE |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ADRESH |
|
A/D Result Register High Byte |
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ADRESL |
|
A/D Result Register Low Byte |
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ADCON0 |
ADSC1 |
ADSC0 |
CHS2 |
CHS1 |
CHS0 |
GO/DONE |
|
ADON |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ADCON1 |
ADFM |
|
|
|
PCFG3 |
PCFG2 |
PCFG1 |
PCFG0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Figure 16-5 Registers Related to A/C Module Operations
The ADCON0 Register
The ADCON0 register is located in bank 0, at address 0x1f. Seven of the eight bits are meaningful in A/D control and status operations. Figure 16-6 is a bitmap of the ADCON0 register.
In Figure 16-6, bits 7 and 6, labeled ADSC1 and ADSC0, are the selection bits for the A/D conversion clock. The conversion time per bit is defined as TAD in PIC documentation. A/D conversion requires a minimum of 12 TAD in a 10-bit ADC. The source of the A/D conversion clock is software selected. The four possible options for TAD are:
1.Fosc/2
2.Fosc/8
3.Fosc/32
4.Internal A/D module RC oscillator (varies between 2 and 6 µs)
Analog to Digital and Realtime Clocks |
551 |
bits: |
7 |
6 |
5 |
4 |
3 |
|
2 |
|
1 |
|
0 |
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ADSC1 |
ADSC0 |
|
CHS2 |
CHS1 |
CHS0 |
|
GO/DONE |
|
|
|
ADON |
|
|
|
|
|
|
|
|
|
|
||||
|
bit 7-6 ADCS1:ADCS0: A/D Conversion Clock Select bits |
||||||||||||
|
|
|
00 = FOSC/2 |
|
|
|
|
|
|
|
|
||
|
|
|
01 = FOSC/8 |
|
|
|
|
|
|
|
|
||
|
|
|
10 = FOSC/32 |
|
|
|
|
|
|
|
|
||
|
|
|
11 = FRC (internal A/D module RC oscillator) |
||||||||||
|
bit 5-3 CHS2:CHS0: Analog Channel Select bits |
|
|||||||||||
|
|
|
000 |
= channel 0, (RA0=AN0) |
|
|
|
|
|
||||
|
|
|
001 |
= channel 1, (RA1=AN1) |
|
|
|
|
|
||||
|
|
|
010 |
= channel 2, (RA2=AN2) |
|
|
|
|
|
||||
|
|
|
011 |
= channel 3, (RA3=AN3) |
|
|
|
|
|
||||
|
|
|
100 |
= channel 4, (RA5=AN4) |
|
|
|
|
|
||||
|
|
|
101 |
= channel 5, (RE0=AN5) | not active |
|
||||||||
|
|
|
110 |
= channel 6, (RE1=AN6) | in 28-pin |
|
||||||||
|
|
|
111 |
= channel 7, (RE2=AN7) | 16F87x PICS |
|
||||||||
|
bit 2 GO/DONE: A/D Conversion Status bit |
|
|
|
|||||||||
|
|
|
If ADON = 1: |
|
|
|
|
|
|
|
|
||
|
|
|
1 = A/D conversion in progress (setting this |
||||||||||
|
|
|
|
bit starts the A/D conversion) |
|
|
|
||||||
|
|
|
0 = A/D conversion not in progress (this bit |
||||||||||
is automatically cleared by hardware when the A/D conversion is complete)
bit 1 Unimplemented: Read as '0' bit 0 ADON: A/D On bit
1 = A/D converter module is operating
0 = A/D converter module is shut-off and consumes no power
Figure 16-6 ADCON0 Register Bitmap
The conversion time is the analog-to-digital clock period multiplied by the number of bits of resolution in the converter, plus the two to three additional clock periods for settling time, as specified in the data sheet of the specific device. The various sources for the analog-to-digital converter clock represent the main oscillator frequency divided by 2, 8, or 32. The third choice is the use of a dedicated internal RC clock that has a typical period of 2 to 6 µs. Since the conversion time is determined by the system clock, a faster clock results in a faster conversion time.
The A/D conversion clock must be selected to ensure a minimum Tad time of 1.6 µs. The formula for converting processor speed (in MHz) into Tad microseconds is as follows:
Tad = Tosc1
Tdiv
552 |
Chapter 16 |
Where Tad is A/D conversion time, Tosc is the oscillator clock frequency in MHz, and Tdiv is the divisor determined by bits ADSC1 and ADSC0 of the ADCON0 register. For example, in a PIC running at 10MHz if we select the Tosc/8 option (divisor equal 8) the A/D conversion time per bit is calculated as follows:
Tad = |
|
1 |
|
= 1.6 |
|
5Mhz |
|
||
|
|
8 |
|
|
In this case, the minimum recommended conversion speed of 1.6 µs is achieved.
However, in a PIC with an oscillator speed of 10MHz, this option produces a conversion speed of 0.8 µs, less than the recommended minimum. In this case we would have to select the divisor 32 option, giving a conversion speed of 3.2 µs.
Table 16.1
A/C Converter Tad at Various Oscillator Speeds
|
|
|
TAD IN MICROSECONDS |
|
|
OPERATION |
ADCS1:ADCS0 |
20MHZ |
10MHZ |
5MHZ |
1.25MHZ |
|
|
|
|
|
|
Fosc/2 |
00 |
0.1 |
0.2 |
0.4 |
1.6 |
Fosc/8 |
01 |
0.4 |
0.8 |
1.6 |
6.4 |
Fosc/32 |
10 |
1.6 |
3.2 |
6.4 |
25.6 |
RC |
11 |
2-6 |
2-6 |
2-6 |
2-6 |
Note: values in bold are within the recommended limits
In Table 16.1, converter speeds of less than 1.6 µs or higher than 10 µs are not recommended. Recall that the Tad speed of the converter is calculated per bit, so the total conversion time in a 10-bit device (such as the 16F87x) is approximately the Tad speed multiplied by 10 bits, plus 3 additional cycles. Therefore, a device operating at a Tad speed of 1.6 µs requires 1.6 µs * 13, or 20.8 µs for the entire conversion.
Bits CHS2 to CHS0 in the ADCON0 register (see Figure 16-6) determine which of the analog channels is selected. This is required, since there are several channels for analog input but only one A/2 converter circuitry. So the setting of this bit field determines which of six or eight possible channels is currently read by the A/C converter. An application can change the setting of these bits in order to read several analog inputs in succession.
Bit 2 of the ADCON0 register, labeled GO/DONE, is both a control and a status bit. Setting the GO/DONE bit starts A/D conversion. Once conversion has started, the bit indicates if it is still in progress. Code can test the status of the GO/DONE bit in order to determine if conversion has concluded.
Bit 0 of the ADCON0 register turns the A/D module on and off. The initialization routine of an A/D-enabled application turns on this bit. Programs that do not use the A/D conversion module leave the bit off to conserve power.
The ADCON1 Register
The ADCON1 register also plays an important role in programming the A/D module. Bit 7 of the ADCON1 register is used to determine the bit justification of the digital re-
Analog to Digital and Realtime Clocks |
553 |
sult. This is possible because the 10-bit result is returned in two 8-bit registers; therefore, the six unused bits can be placed either on the leftor the right-hand side of the 16-bit result. If ADCON1 bit 7 is set then the result is right-justified; otherwise it is left-justified. Figure 16-7 shows the location of the significant bits.
ADRESH
V V V V V V V V
ADRESL
V V 0 0 0 0 0 0
Left-justified (ADFM bit = 0)
ADRESH ADRESL
0 |
0 |
0 |
0 |
0 |
0 |
V |
V |
|
V |
V |
V |
V |
V |
V |
V |
V |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Right-justified (ADFM bit = 1)
Legend:
V = valid digit
0 = digit always cleared
Figure 16-7 Leftand Right-justification of A/D Result
One common use of right justification is to reduce the number of significant bits in the conversion result. For example, an application on the 16F877 that uses the A/D conversion module requires only 8-bit accuracy in the result. In this case, code can left-justify the conversion result, read the ADRESH register, and ignore the low-order bits in the ADRESL register. By ignoring the two low-order bits, the 10-bit accuracy of the A/D hardware is reduced to eight bits and the converter performs as an 8-bit accuracy unit.
The bit field labeled PCFG3 to PCFG0 in the ADCON1 register determines port configuration as analog or digital and the mapping of the positive and negative voltage reference pins. The number of possible combinations is limited by the four bits allocated to this field, so the programmer and circuit designer must select the option that is most suited to the application when the ideal one is not available. Table 16.2 (in the following page) shows the port configuration options.
For example, there is a circuit that calls for two analog inputs, wired to ports RA0 and RA1, with no reference voltages. In Table 16.2 we can find two options that select ports RA0 and RA1 and are analog inputs: these are the ones selected with PCFG bits 0100 and 0101. The first option also selects port RA3 as analog input, even though not required in this case. The second one also selects port RA3 as a positive voltage reference, also not required.
Either option works in this case; however, any pin configured for analog input produces incorrect results if used as a digital source. Therefore, a channel configured for analog input cannot be used for non-analog purposes. On the other hand, a
554 |
Chapter 16 |
Table 16.2
A/D Converter Port Configuration Options
PCFG3: |
An7 |
An6 |
An5 |
An4 |
An3 |
An2 |
An1 |
An0 |
|
|
CHAN/ |
|
|
|
|
|
|
|
|
|
|
|
|
PCFG0 |
Re2 |
Re1 |
Re0 |
Ra5 |
Ra3 |
Ra2 |
Ra1 |
Ra0 |
Vref+ |
Vref- |
Refs |
|
|
|
|
|
|
|
|
|
|
|
|
0000 |
A |
A |
A |
A |
A |
A |
A |
A |
VDD |
VSS |
8/0 |
0001 |
A |
A |
A |
A |
Vre+ |
A |
A |
A |
RA3 |
VSS |
7/1 |
0010 |
D |
D |
D |
A |
A |
A |
A |
A |
VDD |
VSS |
5/0 |
0011 |
D |
D |
D |
A |
Vre+ |
A |
A |
A |
RA3 |
VSS |
4/1 |
0100 |
D |
D |
D |
D |
A |
D |
A |
A |
VDD |
VSS |
3/0 |
0101 |
D |
D |
D |
D |
Vre+ |
D |
A |
A |
RA3 |
VSS |
2/1 |
011x |
D |
D |
D |
D |
D |
D |
D |
D |
VDD |
VSS |
0/0 |
1000 |
A |
A |
A |
A |
Vre+ |
Vre- |
A |
A |
RA3 |
RA2 |
6/2 |
1001 |
D |
D |
A |
A |
A |
A |
A |
A |
VDD |
VSS |
6/0 |
1010 |
D |
D |
A |
A |
Vre+ |
A |
A |
A |
RA3 |
VSS |
5/1 |
1011 |
D |
D |
A |
A |
Vre+ |
Vre- |
A |
A |
RA3 |
RA2 |
4/2 |
1100 |
D |
D |
D |
A |
Vre+ |
Vre- |
A |
A |
RA3 |
RA2 |
3/2 |
1101 |
D |
D |
D |
D |
Vre+ |
Vre- |
A |
A |
RA3 |
RA2 |
2/2 |
1110 |
D |
D |
D |
D |
D |
D |
D |
A |
VDD |
VSS |
1/0 |
1111 |
D |
D |
D |
D |
Vre+ |
Vre- |
D |
A |
RA3 |
RA2 |
1/2 |
|
|
|
|
|
|
|
|
|
|
|
|
Legend:
D = digital input A = analog input
CHAN/Refs = analog channels/voltage reference inputs
channel configured for digital input should not be used for analog data since extra current is consumed by the hardware. Finally, channels to be used for analog-to-dig- ital conversion must be configured for input in the corresponding TRIS register.
SLEEP Mode Operation
The A/D module can be made to operate in SLEEP mode. As mentioned previously, SLEEP mode operation requires that the A/D clock source be set to RC by setting both ADCS bits in the ADCON0 register. When the RC clock source is selected, the A/D module waits one instruction cycle before starting the conversion. During this period, the SLEEP instruction is executed, thus eliminating all digital switching noise from the conversion. The completion of the conversion is detected by testing the GO/DONE bit. If a different clock source is selected, then a SLEEP instruction causes the conver- sion-in-progress to be aborted and the A/D module to be turned off.
16.2.2 A/D Module Sample Circuit and Program
The circuit in Figure 16-8 is designed to demonstrate the use of the A/D converter module in PICs of the 16F87x family.
Analog to Digital and Realtime Clocks |
555 |
+5v |
|
RESET |
|
R=10K |
|
+5v
Pot 1 5K |
1 |
|
|
|
|
|
|
|
|
40 |
|
||
|
|
|
|
!MCLR/VPP 16F877 |
RB7/PGD |
|
|
||||||
|
|
2 |
39 |
|
|||||||||
|
|
|
|
RA0/AN0 |
RG6/PGC |
|
|
|
|
|
|||
|
|
3 |
|
|
|
38 |
|
||||||
|
|
|
RB5 |
|
|
|
|||||||
|
|
|
|
RA1/AN1 |
|
|
|
|
|||||
|
|
4 |
|
|
37 |
|
|||||||
|
|
|
RB4 |
|
|
|
|
|
|||||
|
|
|
|
|
|
|
|
||||||
|
|
|
|
RA2/AN2.VREF- |
|
|
|
|
|
|
|||
|
|
5 |
|
RB3/PGM |
|
|
|
|
|
|
36 |
|
|
|
|
|
|
RA3/AN3/VREF+ |
|
|
|
|
LCD |
||||
|
|
6 |
|
|
|
|
|
|
35 |
||||
|
|
|
RB2 |
|
|
|
|
|
|||||
|
|
|
|
RA4/TOCKI |
|
|
|
|
|
|
|||
|
|
7 |
|
|
|
|
|
|
|
34 |
|||
|
|
|
RB1 |
|
2 rows x 20 |
||||||||
|
|
|
|
R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8 with Figure 16-4, which uses the ADC0831 IC, we notice the economy of resources that results from selecting a PIC with an onboard A/D module. In the circuit of Figure 16-4 three microcontroller I/O ports must be used to connect the converter IC to the PIC. In the circuit of Figure 16-8, the potentiometer is connected directly to a single PIC port, saving two I/O lines. Considering the number of different PIC architectures that are equipped with onboard A/D converters, the circuit designer should explore this possibility before deciding on using a separate converter IC. At the same time, recall that two of the three input lines used by converter ICs can be shared. In a design with more than one converter IC the use of input lines is not a 3 to 1 ratio.
The circuit in Figure 16-8 consists of a 5K potentiometer wired to analog port RA0 of a 16F877 PIC. The LCD display is used to show three digits, in the range 0 to 255,
556 |
Chapter 16 |
that represent the relative position of the potentiometer’s disk. The program named A2DinLCD, in the book’s online software, uses the built-in A/D module.
Programming the A/D module consists of the following steps:
1.Configure the PIC I/O lines to be used in the conversion. All analog lines are initialized as input in the corresponding TRIS registers.
2.Select the ports to be used in the conversion by setting the PCFGx bits in the ADCON1 register. Selects rightor left-justification.
3.Select the analog channels, select the A/D conversion clock, and enable the A/D module.
4.Wait the acquisition time.
5.Initiate the conversion by setting the GO/DONE bit in the ADCON0 register.
6.Wait for the conversion to complete.
7.Read and store the digital result.
The following procedure from the A2DinLCD program initialized the A/D module for the required processing:
;============================
;init A/D module ;============================
;1. Procedure to initialize the A/D module, as follows:
;Configure the PIC I/O lines. Init analog lines as input
;2. Select ports to be used by setting the PCFGx bits in the
;ADCON1 register. Selects rightor left-justification.
;3. Select the analog channels, select the A/D conversion
;clock, and enable the A/D module.
;4. Wait the acquisition time.
;5. Initiate the conversion by setting the GO/DONE bit in the
;ADCON0 register.
;6. Wait for the conversion to complete.
;7. Read and store the digital result.
InitA2D:
Bank1 |
|
; |
Select bank |
for TRISA register |
movlw |
b’00000001’ |
|
||
movwf |
TRISA |
; |
Set Port-A, |
line 0, as input |
;Select the format and A/D port configuration bits in
;the ADCON1 register
;Format is left-justified so that ADRESH bits are the
;most significant
; |
0 |
x |
x |
x |
1 |
1 |
1 |
0 |
<== value installed in ADCON1 |
||
; |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
<== ADCON1 |
bits |
|
; |
| |
|
|
|
|__|__|__|____ RA0 is analog. |
||||||
; |
| |
|
|
|
|
|
|
|
Vref+ |
= |
Vdd |
; |
| |
|
|
|
|
|
|
|
Vref- |
= |
Vss |
;|_________________________ 0 = left-justified
;ADCON1 is in bank 1
Analog to Digital and Realtime Clocks |
557 |
|
movlw |
b’00001110’ |
|
movwf |
ADCON1 ; RA0 is analog. All others digital |
|
;Vref+ = Vdd
;Select D/A options in ADCON0 register
;For a 10Mhz clock the Fosc32 option produces a conversion
;speed of 1/(10/32) = 3.2 microseconds, which is within the
;recommended range of 1.6 to 10 microseconds.
; |
1 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
<== value installed in ADCON0 |
||||
; |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
<== ADCON0 bits |
|
|
||
; |
| |
| |
| |
| |
| |
| |
|
|____ |
A/D |
function |
select |
||
; |
| |
| |
| |
| |
| |
| |
|
|
|
1 = |
A/D ON |
|
|
; |
| |
| |
| |
| |
| |
|__________ |
A/D |
status bit |
|
||||
; |
| |
| |
|__|__|_____________ |
Analog Channel |
Select |
||||||||
; |
| |
| |
|
|
|
|
|
|
|
000 |
= Chanel |
0 |
(RA0) |
;|__|______________________ A/D Clock Select
; |
|
10 = Fosc/32 |
; ADCON0 is in bank 0 |
|
|
Bank0 |
|
|
movlw |
b’10000001’ |
|
movwf |
ADCON0 |
; Channel 0, Fosc/32, A/D enabled |
; Delay for selection to complete |
||
call |
delayAD |
; Local procedure |
return |
|
|
Once the module is initialized, the analog line is read by the following procedure:
;============================
;read A/D line ;============================
;Procedure to read the value in the A/D line and convert
;to digital
ReadA2D: |
|
|
; Initiate conversion |
|
|
Bank0 |
; Bank for ADCON0 register |
|
bsf |
ADCON0,GO |
; Set the GO/DONE bit |
; GO/DONE bit is cleared automatically when conversion ends convWait:
btfsc goto
;At this point conversion has concluded
;ADRESH register (bank 0) holds 8 MSBs of result
;ADRESL register (bank 1) holds 4 LSBs.
;In this application value is left-justified. Only the
;MSBs are read
movf |
ADRESH,W ; Digital value to w register |
return
