Difference between revisions of "C Example: Digital Outputs"

From Mech
Jump to navigationJump to search
Line 12: Line 12:
#use delay (clock=20000000)
#use delay (clock=20000000)


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


#define LED_0 PIN_C0
#define LED_0 PIN_C0


// Begin main body of program. Notice main is a function of "void". This
Begin main body of program. Notice main is a function of "void". This
// is a more explicit way of saying main is a function of nothing. Using
is a more explicit way of saying main is a function of nothing. Using
// main() is equivalent. Every program is required to have a function
main() is equivalent. Every program is required to have a function
// called "main".
called "main".


void main(void) {
void main(void) {


// Setup an infinite loop, using a while statement.
Setup an infinite loop, using a while statement.


while(TRUE){
while(TRUE){


// Turn LED on and off by setting its pin low or high, with a delay
Turn LED on and off by setting its pin low or high, with a delay
// between each switching.
between each switching.


output_low(LED_0);
output_low(LED_0);
delay_ms(500);
delay_ms(500);
output_high(LED_0);
output_high(LED_0);
delay_ms(500);}
delay_ms(500);}
}
}

Revision as of 15:27, 25 June 2007

Code

Program to blink one LED on and off every half second.

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 LED_0 PIN_C0

Begin main body of program. Notice main is a function of "void". This is a more explicit way of saying main is a function of nothing. Using main() is equivalent. Every program is required to have a function called "main".

  void main(void) {

Setup an infinite loop, using a while statement.

     while(TRUE){

Turn LED on and off by setting its pin low or high, with a delay between each switching.

        output_low(LED_0);
        delay_ms(500);
        output_high(LED_0);
        delay_ms(500);}
  }