Presentation is loading. Please wait.

Presentation is loading. Please wait.

PIC Analog Input Tom Igoe, NYU-ITP

Similar presentations


Presentation on theme: "PIC Analog Input Tom Igoe, NYU-ITP"— Presentation transcript:

1 PIC Analog Input Tom Igoe, NYU-ITP
PostPC Computing PIC Analog Input Tom Igoe, NYU-ITP Prof. Scott Kirkpatrick, HUJI Amnon Dekel, Bezalel 5/20/2019 *: Tom Igoe, NYU-IYTP:

2 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/20/2019 *: Tom Igoe, NYU-IYTP:

3 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/20/2019 *: Tom Igoe, NYU-IYTP:

4 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/20/2019 *: Tom Igoe, NYU-IYTP:

5 PIC Analog Input You can use the following circuits: 5/20/2019
*: Tom Igoe, NYU-IYTP:

6 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/20/2019 *: Tom Igoe, NYU-IYTP:

7 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/20/2019 *: Tom Igoe, NYU-IYTP:

8 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/20/2019 *: Tom Igoe, NYU-IYTP:

9 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 = % 5/20/2019 *: Tom Igoe, NYU-IYTP:

10 PIC Analog Input Then we need to set some bits in the ADC configuration register ADCON1: ADCON1 = % The first bit in this register sets the input pins labeled AN0, AN1, etc. to read analog input readings instead of digital. 5/20/2019 *: Tom Igoe, NYU-IYTP:

11 PIC Analog Input ADCIN channel, variable
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/20/2019 *: Tom Igoe, NYU-IYTP:

12 adcVar VAR WORD ' Create variable to store result
INCLUDE "modedefs.bas" output PORTC.0 adcVar VAR WORD ' Create variable to store result DEFINE ADC_BITS ' Define ADCIN parameters Set number of bits in result DEFINE ADC_CLOCK ' Set clock source (3=rc) DEFINE ADC_SAMPLEUS ' Set sampling time in microseconds TRISA = % ' Set PORTA to all input ADCON1 = % ' Set up ADCON1 Pause ' Wait .5 second main: ADCIN 0, adcVar ' Read channel 0 into variable adcVar if adcvar > 0 and adcvar < 100 then led0 Pause ' Wait.01 second goto main ' Do it forever led0: high PORTC.0 goto main 5/20/2019 *: Tom Igoe, NYU-IYTP:

13 Intro to PIC 5/20/2019 *: Tom Igoe, NYU-IYTP:

14 More Info Check out Tom Igoe’s web site at NYU-ITP
PICBasic Pro Compiler Online Manual: 5/20/2019 *: Tom Igoe, NYU-IYTP:


Download ppt "PIC Analog Input Tom Igoe, NYU-ITP"

Similar presentations


Ads by Google