C Example: Digital Inputs

From Mech
Jump to: navigation, search

Contents

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

Personal tools