Difference between revisions of "Example Writeup: Analog Input"

From Mech
Jump to navigationJump to search
Line 5: Line 5:
== Overview ==
== Overview ==


You can assign from one to thirteen pins to be used as analog inputs. These pins are named AN0 through AN12. The set that you use as analog inputs must start with AN0 and must be contiguous, i.e. if you use AN0 and AN3 as analog inputs, you can't use AN1 and AN2 for a different purpose. A statement such as setup_adc_ports(AN0_TO_AN3) selects the range of pins to be used as analog inputs.


The input voltage range is 0 to 5V. Voltages outside this range can damage the PIC. The 0 to 5V range is converted by the PIC's ADC to an 8 or 10 bit positive integer, i.e. to a value 0-255 or 0-1023. A statement such as #DEVICE ADC=10 selects 8-bit or 10-bit ADC resolution.

There is really only one ADC in the PIC, and a bunch of analog switches to connect any one of the thirteen input pins to it. After a change of selected pin, e.g. with set_adc_channel(3) to select input pin AN3, a 10uS delay is recommended so that the ADC can lock in and digitize the new value, before you read it with read_ADC(). You can read_adc() with no delay if you haven't recently changed selected input pin.

In the demo program AnalogInput.c a 10-bit ADC conversion is done, with the low order 8 bits displayed on port D and the high 2 bits displayed on pins C0 and C1.


== Circuit ==
== Circuit ==

Revision as of 12:31, 26 January 2008

Original Assignment

The goal of this assignment is to provide sample code and circuits that clearly demonstrate how to use the analog input capabilities of the PIC 18F4520 by giving a few examples.

Overview

You can assign from one to thirteen pins to be used as analog inputs. These pins are named AN0 through AN12. The set that you use as analog inputs must start with AN0 and must be contiguous, i.e. if you use AN0 and AN3 as analog inputs, you can't use AN1 and AN2 for a different purpose. A statement such as setup_adc_ports(AN0_TO_AN3) selects the range of pins to be used as analog inputs.

The input voltage range is 0 to 5V. Voltages outside this range can damage the PIC. The 0 to 5V range is converted by the PIC's ADC to an 8 or 10 bit positive integer, i.e. to a value 0-255 or 0-1023. A statement such as #DEVICE ADC=10 selects 8-bit or 10-bit ADC resolution.

There is really only one ADC in the PIC, and a bunch of analog switches to connect any one of the thirteen input pins to it. After a change of selected pin, e.g. with set_adc_channel(3) to select input pin AN3, a 10uS delay is recommended so that the ADC can lock in and digitize the new value, before you read it with read_ADC(). You can read_adc() with no delay if you haven't recently changed selected input pin.

In the demo program AnalogInput.c a 10-bit ADC conversion is done, with the low order 8 bits displayed on port D and the high 2 bits displayed on pins C0 and C1.

Circuit

Code

 /*
 AnalogInput.c m.peshkin 2007-12-24
 Range of input voltages on pins AN0 to AN11 is 0 to 5 volts only.
 This maps to 0-255 ADC values if ADC is in 8 bit mode,
 or to 0-1023 ADC values if ADC is in 10 bit mode.
 There is only one ADC, with a switch to connect it to up to 12 input pins named AN0 to AN11.
 After you switch inputs, wait 10uS for the ADC to settle onto the new input
 */
 #include <18f4520.h>
 #DEVICE ADC=10                   // set ADC to 10 bit accuracy, or it could be just 8
 #fuses HS,NOLVP,NOWDT,NOPROTECT
 #use delay(clock=20000000)
 int16 value;      // if you selected 10 bit accuracy, don't use an 8 bit int
 void main() {
   setup_adc_ports(AN0_TO_AN3);             // Enable analog inputs; choices run from just AN0, up to AN0_TO_AN11
   setup_adc(ADC_CLOCK_INTERNAL);      // the range selected has to start with AN0  
   while (TRUE) {  
     set_adc_channel(0);         // there's only one ADC so select which input to connect to it; here pin AN0
     delay_us(10);                    // wait 10uS for ADC to settle to a newly selected input
     value = read_adc();         // now you can read ADC as frequently as you like      
     output_d(value>>2);      // on port D show only the most significant 8 of the 10 bits; tricky >> means shift right 2 bits
     output_bit(PIN_C0, (value & 2)>>1);    // display the two least significant bits; tricky & is bitwise AND
     output_bit(PIN_C1, (value & 1));
     delay_ms(10);
   }
 }