<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://hades.mech.northwestern.edu//index.php?action=history&amp;feed=atom&amp;title=C_Example%3A_Analog_Inputs</id>
	<title>C Example: Analog Inputs - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://hades.mech.northwestern.edu//index.php?action=history&amp;feed=atom&amp;title=C_Example%3A_Analog_Inputs"/>
	<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=C_Example:_Analog_Inputs&amp;action=history"/>
	<updated>2026-05-16T04:37:26Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=C_Example:_Analog_Inputs&amp;diff=4966&amp;oldid=prev</id>
		<title>LIMS at 20:47, 25 June 2007</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=C_Example:_Analog_Inputs&amp;diff=4966&amp;oldid=prev"/>
		<updated>2007-06-25T20:47:23Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;__TOC__&lt;br /&gt;
==Code==&lt;br /&gt;
Program to light different LEDs dependent upon position of potentiometer&lt;br /&gt;
&lt;br /&gt;
First include header file with definitions for specific PIC.&lt;br /&gt;
Set fuses.  HS is type of external clock, low voltage protection&lt;br /&gt;
(LVP) is off, and the watchdog timer (WDT) is off.&lt;br /&gt;
External clock frequency of 20 MHz is specified.&lt;br /&gt;
&lt;br /&gt;
   #include &amp;lt;18f4520.h&amp;gt;&lt;br /&gt;
   #fuses HS,NOLVP,NOWDT,PUT&lt;br /&gt;
   #use delay(clock=20000000)&lt;br /&gt;
&lt;br /&gt;
Define pin names to be used in the main program. See header file&lt;br /&gt;
for currently defined pin names. Also set values for high and low&lt;br /&gt;
voltage cutoffs.  Note that voltages must be defined between 0 and&lt;br /&gt;
255 (8 digit binary) which corresponds to a range of 0-5V.&lt;br /&gt;
&lt;br /&gt;
   #define LED_0 PIN_B5&lt;br /&gt;
   #define LED_1 PIN_B4&lt;br /&gt;
   #define LED_2 PIN_A5&lt;br /&gt;
   #define lowcutoff 77   // 1.5V&lt;br /&gt;
   #define highcutoff 178   // 3.5V&lt;br /&gt;
&lt;br /&gt;
Begin main body of program.&lt;br /&gt;
&lt;br /&gt;
   void main(void) {&lt;br /&gt;
&lt;br /&gt;
Introduce the variable &amp;quot;read&amp;quot; as an 8-bit number (same as int8)&lt;br /&gt;
&lt;br /&gt;
      int read;&lt;br /&gt;
&lt;br /&gt;
Setup analog inputs.  AN0 can be replaced by ALL_ANALOG, AN1, AN2,&lt;br /&gt;
etc.&lt;br /&gt;
&lt;br /&gt;
      setup_adc_ports(AN0_ANALOG);&lt;br /&gt;
      setup_adc( ADC_CLOCK_INTERNAL );&lt;br /&gt;
&lt;br /&gt;
Set which analog channel is active.&lt;br /&gt;
&lt;br /&gt;
      set_adc_channel( 0 );&lt;br /&gt;
&lt;br /&gt;
Use while to set up infinite loop.&lt;br /&gt;
&lt;br /&gt;
      while(TRUE) {&lt;br /&gt;
&lt;br /&gt;
Initialize the output LEDs as all off.&lt;br /&gt;
&lt;br /&gt;
         output_high(LED_0);&lt;br /&gt;
         output_high(LED_1);&lt;br /&gt;
         output_high(LED_2);&lt;br /&gt;
&lt;br /&gt;
Read the analog input and assign the value to the &amp;quot;read&amp;quot; variable.&lt;br /&gt;
&lt;br /&gt;
         read = read_adc();&lt;br /&gt;
&lt;br /&gt;
Check the &amp;quot;read&amp;quot; value and light a certain LED depending on its&lt;br /&gt;
value.&lt;br /&gt;
      &lt;br /&gt;
         if(read&amp;lt;lowcutoff)&lt;br /&gt;
            output_low(LED_0);&lt;br /&gt;
         else if((read&amp;gt;lowcutoff)&amp;amp;(read&amp;lt;highcutoff))&lt;br /&gt;
            output_low(LED_1);&lt;br /&gt;
         else if(read&amp;gt;highcutoff)&lt;br /&gt;
            output_low(LED_2);&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
==Associated Circuitry==&lt;/div&gt;</summary>
		<author><name>LIMS</name></author>
	</entry>
</feed>