LPC Demo Board Lessons
© 2005 Microchip Technology Inc. DS51556A-page 21
3.2.4.2 ADCON0
ADCON0 controls the ADC operation. Bit 0 turns on the ADC module. Bit 1 starts a
conversion and bits <5:2> selects which channel the ADC will operate. VCFG bit< 6>
selects the ADC reference, which may be either V
DD or a separate reference voltage
on V
REF. ADFM bit <7> selects whether the 10 bits are right or left justified in the 16 bits.
For purposes of this lesson, the ADC must be turned on and pointed to RA0. Choose
the internal voltage reference and 8T
OSC conversion clock.
The ADC needs about 5 μs, after changing channels, to allow the ADC sampling
capacitor to settle. Finally, we can start the conversion by setting the GO bit in ADCON0.
The bit also serves as the DONE
flag. That is, the ADC will clear the same bit when the
conversion is complete. The answer is then available in ADRESH:ADRESL.
This lesson takes the high order 4 bits of the result and copies them to the display LEDs
attached to PORTC.
See the Analog-to-Digital section in the PIC16F685/687/689/690 Data Sheet
(DS41262) for more details on the ADC module.
REGISTER 3-2: ADCON0 – A/D CONTROL REGISTER (ADDRESS: 1Fh)
R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0
ADFM VCFG CHS3 CHS2 CHS1 CHS0 GO/DONE
ADON
bit 7 bit 0
bit 7 ADFM: A/D Result Formed Select bit
1 = Right justified
0 = Left justified
bit 6 VCFG: Voltage Reference bit
1 = V
REF pin
0 = V
DD
bit 5-2 CHS<3:0>: Analog Channel Select bits
0000 = Channel 00 (AN0)
0001 = Channel 01 (AN1)
0010 = Channel 02 (AN2)
0011 = Channel 03 (AN3)
0100 = Channel 04 (AN4)
0101 = Channel 05 (AN5)
0110 = Channel 06 (AN6)
0111 = Channel 07 (AN7)
1000 = Channel 08 (AN8)
1001 = Channel 09 (AN9)
1010 = Channel 10 (AN10)
1011 = Channel 11 (AN11)
1100 =CV
REF
1101 =VP6
1110 = Reserved. Do not use.
1111 = Reserved. Do not use.
bit 1 GO/DONE
: A/D Conversion Status bit
1 = A/D conversion cycle in progress. Setting this bit starts an A/D conversion cycle.
This bit is automatically cleared by hardware when the A/D conversion has completed.
0 = A/D conversion completed/not in progress
bit 0 ADON: A/D Enable bit
1 = A/D converter module is enabled
0 = A/D converter is shut off and consumes no operating current
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
- n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Low Pin Count Demo Board User’s Guide
DS51556A-page 22 © 2005 Microchip Technology Inc.
3.2.5 Lesson 5: Variable Speed Rotate
Lesson 5 combines Lessons 3 and 4 by using the Analog-to-Digital Converter (ADC)
to control the speed of rotation.
New Instructions
BTFSS Bit test, skip if set
BTFSC Bit test, skip if clear
A conversion is run on every pass through the main loop. The result controls the length
of the outer loop (see Example 3-5).
EXAMPLE 3-5: VARIABLE SPEED ROTATE EXAMPLE
FIGURE 3-3: VARIABLE SPEED ROTATE PROGRAM FLOW
...
BSF ADCON0,GO ;start conversion
BTFSS ADCON0,GO ;this bit will change to zero when the
;conversion is complete
GOTO $-1
MOVF ADRESH,w ;Copy the display to the LEDs
ADDLW 1
MOVWF Delay2
A2DDelayLoop
DECFSZ Delay1,f ;Delay Loop shortened by the ADResult as
;controlled by the Pot.
GOTO A2DDelayLoop
DECFSZ Delay2,f
GOTO A2DDelayLoop
Initialize I/O Port
Put Up Display
Delay Using ADC
Rotate Display
Reset Display
Did it overflow?
No
Yes
Initialize ADC
Get ADC Result
LPC Demo Board Lessons
© 2005 Microchip Technology Inc. DS51556A-page 23
3.2.6 Lesson 6: Switch Debouncing
Mechanical switches play an important and extensive role in practically every
computer, microprocessor and microcontroller application. Mechanical switches are
inexpensive, simple and reliable. In addition, switches can be very noisy. The apparent
noise is caused by the closing and opening action that seldom results in a clean
electrical transition. The connection makes and breaks several, perhaps even
hundreds, of times before the final switch state settles.
The problem is known as switch bounce. Some of the intermittent activity is due to the
switch contacts actually bouncing off each other. Imagine slapping two billiard balls
together. The hard non-resilient material doesn’t absorb the kinetic energy of motion.
Instead, the energy dissipates over time and friction in the bouncing action against the
forces push the billiard balls together. Hard metal switch contacts react in much the
same way. Also, switch contacts are not perfectly smooth. As the contacts move
against each other, the imperfections and impurities on the surfaces cause the
electrical connection to be interrupted. The result is switch bounce.
The consequences of uncorrected switch bounce can range from being just annoying
to catastrophic. For example, imagine advancing the TV channel, but instead of getting
the next channel, the selection skips one or two. This is a situation a designer should
strive to avoid.
Switch bounce has been a problem even before the earliest computers. The classic
solution involved filtering, such as through a resistor-capacitor circuit, or through
re-settable shift registers (see Figures 3-4 and 3-5). These methods are still effective
but they involve additional cost in material, installation and board real estate. Why
suffer the additional expense when software is free and program memory is abundant.
FIGURE 3-4: FILTERING DEBOUNCE SOLUTION
FIGURE 3-5: SHIFT REGISTER DEBOUNCE SOLUTION
+V
R1
R2
Filtered
Switch
Output
SW
C
1
+V
R1
Filtered
Switch
Output
SW
Debounce
Clock
D
CLK
CLR
Qn

DM164120-1

Mfr. #:
Manufacturer:
Microchip Technology
Description:
Development Boards & Kits - PIC / DSPIC PICkit 2 Low Pin Cnt Demo Board
Lifecycle:
New from this manufacturer.
Delivery:
DHL FedEx Ups TNT EMS
Payment:
T/T Paypal Visa MoneyGram Western Union

Products related to this Datasheet