Difference between revisions of "PIC18F4520: Digital Outputs"

From Mech
Jump to navigationJump to search
Line 51: Line 51:
[[Image:4520pindiagramdigitalports.jpg|center]]
[[Image:4520pindiagramdigitalports.jpg|center]]
===Sample Code===
===Sample Code===
Program to count in binary from 0-15 and display on LEDs.
Program to count in binary from 0-7 and display on LEDs.


First include header file with definitions for specific PIC.
First include header file with definitions for specific PIC.
Line 91: Line 91:
to zero.
to zero.
if(count<15){
if(count<7){
temp = count;
temp = count;
count = temp + 1;}
count = temp + 1;}
Line 98: Line 98:
}
}
}
}

===Circuit Diagram===
===Circuit Diagram===
[[Image:Digitaloutportckt.jpg]]
[[Image:Digitaloutportckt.jpg]]

Revision as of 14:06, 29 June 2007

Working with digital inputs and outputs is fundamental to circuit design, and PIC microcontrollers add versatility to design by allowing programming and re-programming of the logic associated with input and output pins. In this way, one PIC microcontroller can take the place of many pre-programmed digital logic ICs.

Available Pins

With the exception of the positive voltage supply and ground pins, all pins on the PIC18F4520 can be used as digital I/O, however a few other pins (shown in grey below) are commonly utilized for communication instead of digital I/O.

4520pindiagramdigital.jpg

Digital Outputs Example

This section uses an example to describe how to setup and write digital outputs using a PIC184520.

Sample 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);}
  }

Circuit Diagram

Digitaloutckt.jpg

Digital Output Ports Example

The PIC18F4520 has digital I/O pins organized into five ports. Ports A-D consist of eight pins each, while Port E has only three, although some of these pins are primarily used for communication. The diagram below shows the layout of the different ports on the PIC18F4520 microcontroller.

4520pindiagramdigitalports.jpg

Sample Code

Program to count in binary from 0-7 and display on LEDs.

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 variables to be used in main program. Both are defined as 8-bit numbers, with count already being assigned a value while temp is left unassigned.

  int count = 0;
  int temp;

Begin main body of program.

  void main(void) {

Set Port D to be an output (0). The SET_TRIS_X function can also be used to set ports to be inputs (1).

     SET_TRIS_D(0);

Use while to create an infinite loop.

     while(TRUE){

Assign the value of "count" to Port D, thus displaying it on the LEDs connected to Port D.

        OUTPUT_D(count);
        delay_ms(500);

Check the value of "count", and either increment it or reset it to zero.

        if(count<7){
           temp = count;
           count = temp + 1;}
        else {
           count = 0;}
     }
  }

Circuit Diagram

Digitaloutportckt.jpg