Difference between revisions of "C Example: Digital Inputs"

From Mech
Jump to navigationJump to search
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
__TOC__
==Code==
Program to display push-button input as LED output
Program to display push-button input as LED output


Line 31: Line 33:
reading from input (in this case, set the output to the input).
reading from input (in this case, set the output to the input).


if(input(IN0))
if(input(IN0))
output_high(OUT0);
output_high(OUT0);
else if (!input(IN0))
else if (!input(IN0))
output_low(OUT0);
output_low(OUT0);

if(input(IN1))
if(input(IN1))
output_high(OUT1);
output_high(OUT1);
else if (!input(IN1))
else if (!input(IN1))
output_low(OUT1);
output_low(OUT1);

if(input(IN2))
if(input(IN2))
output_high(OUT2);
output_high(OUT2);
Line 46: Line 46:
output_low(OUT2);}
output_low(OUT2);}
}
}
<br>
==Associated Circuitry==

Latest revision as of 21:31, 17 December 2007

Code

Program to display push-button input as LED output

First include header file with definitions for specific PIC. Set fuses. HS is type of external clock, low voltage protection (LVP) is off, and the watchdog timer (WDT) is off. External clock frequency of 20 MHz is specified.

  #include <18f4520.h>
  #fuses HS,NOLVP,NOWDT
  #use delay (clock=20000000)

Define pin names to be used in the main program. See header file for currently defined pin names.

  #define IN0 PIN_B0
  #define IN1 PIN_B1
  #define IN2 PIN_B2
  #define OUT0 PIN_D0
  #define OUT1 PIN_D1
  #define OUT2 PIN_D2

Begin main body of program.

  void main(void) {

Use while to set up infinite loop.

     while(TRUE){

Check each input pin and define action to be taken depending on reading from input (in this case, set the output to the input).

           if(input(IN0))
           output_high(OUT0);
        else if (!input(IN0))
           output_low(OUT0);
        if(input(IN1))
           output_high(OUT1);
        else if (!input(IN1))
           output_low(OUT1);
        if(input(IN2))
           output_high(OUT2);
        else if (!input(IN2))
           output_low(OUT2);}
  }


Associated Circuitry