Document 7682126

Download Report

Transcript Document 7682126

PostPC Computing
PIC Analog Input
Tom Igoe, NYU-ITP
Prof. Scott Kirkpatrick, HUJI
Amnon Dekel, Bezalel
5/25/2016
:*Tom Igoe, NYU-IYTP: http://stage.itp.nyu.edu/~tigoe/pcomp/pic/pic-analog.shtml
1
PIC Analog Input
• The analog-to-digital converters on the
PIC16F87x series PICs are of 10-bit
resolution.
• This means that when they measure an
incoming voltage, they compare that voltage
to a reference voltage, and give you the
comparison represented as a 10-bit number
(0-1023). Other PICs have different
resolution ADCs.
*:
5/25/2016
:*Tom Igoe, NYU-IYTP: http://stage.itp.nyu.edu/~tigoe/pcomp/pic/pic-analog.shtml
2
PIC Analog Input
• The ADC's are used by addressing a number
of special function registers. You can
address all these registers yourself if you
want, or you can use the ADCIN command
and have much of the work done for you.
• Only the analog in pins of the PIC are
attached to the ADCs. They're labeled AN0,
AN1, AN2, etc. on the pin diagram of each
PIC.
5/25/2016
:*Tom Igoe, NYU-IYTP: http://stage.itp.nyu.edu/~tigoe/pcomp/pic/pic-analog.shtml
3
PIC Analog Input
• Like other microcontrollers and ADC's, the
PIC expects to see a changing voltage,
typically from 0 to 5V DC.
• The easiest way to do this is by using a
variable resistor sensor, such as a
potentiometer, photocell, force-sensitive
resistor, flex sensor, thermistor, or the like.
5/25/2016
:*Tom Igoe, NYU-IYTP: http://stage.itp.nyu.edu/~tigoe/pcomp/pic/pic-analog.shtml
4
PIC Analog Input
• You can use the following circuits:
5/25/2016
:*Tom Igoe, NYU-IYTP: http://stage.itp.nyu.edu/~tigoe/pcomp/pic/pic-analog.shtml
5
PIC Analog Input
• To use the ADCIN command, first you have to set
up a few things in the special function memory
registers.
• First, we define a few constants at the top of our
program:
DEFINE ADC_BITS 10
• This lets us set the number of bits in the result of
the analog-to-digital conversion. On the 16F876
and '877, we can set it to 8 or 10.
5/25/2016
:*Tom Igoe, NYU-IYTP: http://stage.itp.nyu.edu/~tigoe/pcomp/pic/pic-analog.shtml
6
PIC Analog Input
• Then we set how the ADC gets its timing:
DEFINE ADC_CLOCK 3
• 3 sets it so that the ADC use an internal
resistor-capacitor (RC) circuit as a clock.
5/25/2016
:*Tom Igoe, NYU-IYTP: http://stage.itp.nyu.edu/~tigoe/pcomp/pic/pic-analog.shtml
7
PIC Analog Input
• Then we set the number of microseconds
the program waits between setting the ADC
channel and taking a reading.
– We need to give it a few microseconds after we
set the channel for the ADC to stabilize before
taking a reading.
DEFINE ADC_SAMPLEUS 10
5/25/2016
:*Tom Igoe, NYU-IYTP: http://stage.itp.nyu.edu/~tigoe/pcomp/pic/pic-analog.shtml
8
PIC Analog Input
• Next you need to configure the pins you're
using as inputs using their TRIS register bits.
For example, if we were using all the pins on
port A as analog inputs, we could set them
as follows before our main loop:
TRISA = %11111111
5/25/2016
:*Tom Igoe, NYU-IYTP: http://stage.itp.nyu.edu/~tigoe/pcomp/pic/pic-analog.shtml
9
PIC Analog Input
• Then we need to set some bits in the ADC
configuration register ADCON1:
ADCON1 = %10000010
• The first bit in this register sets the input pins
labeled AN0, AN1, etc. to read analog input
readings instead of digital.
5/25/2016
:*Tom Igoe, NYU-IYTP: http://stage.itp.nyu.edu/~tigoe/pcomp/pic/pic-analog.shtml
10
PIC Analog Input
• Once we've done that, we can use the ADCIN
command. It has the following format:
ADCIN channel, variable
ADCIN 0, adcVar ' Read channel 0 into variable adcVar
• Channel is a byte value representing which analog
input we're using.
• Variable is a word variable that the result will be in.
• It's best to wait a few milliseconds after the ADCIN
command before doing it again, to give time for the
ADC to stabilize.
5/25/2016
:*Tom Igoe, NYU-IYTP: http://stage.itp.nyu.edu/~tigoe/pcomp/pic/pic-analog.shtml
11
INCLUDE "modedefs.bas"
output PORTC.0
adcVar VAR WORD
' Create variable to store result
DEFINE ADC_BITS
10 ' Define ADCIN parameters Set number of bits in result
DEFINE ADC_CLOCK
3
' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 10 ' Set sampling time in microseconds
TRISA = %11111111
' Set PORTA to all input
ADCON1 = %10000010
' Set up ADCON1
Pause 500
' Wait .5 second
main:
ADCIN 0, adcVar ' Read channel 0 into variable adcVar
if adcvar > 0 and adcvar < 100 then led0
Pause 10
' Wait.01 second
goto main
' Do it forever
led0:
high PORTC.0
goto main
5/25/2016
:*Tom Igoe, NYU-IYTP: http://stage.itp.nyu.edu/~tigoe/pcomp/pic/pic-analog.shtml
12
Intro to PIC
5/25/2016
:*Tom Igoe, NYU-IYTP: http://stage.itp.nyu.edu/~tigoe/pcomp/pic/pic-analog.shtml
13
More Info
• Check out Tom Igoe’s web site at NYU-ITP
http://stage.itp.nyu.edu/~tigoe/pcomp/pic/pic-analog.shtml
• PICBasic Pro Compiler Online Manual:
http://microengineeringlabs.com/resources/pbpmanual/
5/25/2016
:*Tom Igoe, NYU-IYTP: http://stage.itp.nyu.edu/~tigoe/pcomp/pic/pic-analog.shtml
14