Difference between revisions of "NU32v2: Digital Input and Output"

From Mech
Jump to navigationJump to search
(New page: 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 associa...)
 
Line 24: Line 24:
// Configure the proper PB frequency and the number of wait states
// Configure the proper PB frequency and the number of wait states
SYSTEMConfigPerformance(SYS_FREQ);
SYSTEMConfigPerformance(SYS_FREQ);
// LATX sets the value (0 or 1) for each pin of port X
// LATX sets the value (0 or 1) for each pin of port X

Revision as of 14:36, 11 January 2011

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

Simple Example

This example uses two pins as digital outputs and one pin as digital input. When the switch is pressed, one LED will turn on and the other will turn off. When the switch is not pressed, the opposite LED will turn on and the other one will turn off.

/********************************************************************
Super simple Digital Output and Digital Input. 
********************************************************************/

#include "HardwareProfile.h"

// NOTE THAT BECAUSE WE USE THE BOOTLOADER, NO CONFIGURATION IS NECESSARY
// THE BOOTLOADER PROJECT ACTUALLY CONTROLS ALL OF OUR CONFIG BITS

// Define constants for PINS D1 - D2 and C1
#define PIN_D1			LATDbits.LATD1	// Digital Output
#define PIN_D2			LATDbits.LATD2	// Digital Output
#define PIN_C1			PORTCbits.RC1	// Digital Input

int main(void)
{

    // Configure the proper PB frequency and the number of wait states 
	SYSTEMConfigPerformance(SYS_FREQ);

	// LATX sets the value (0 or 1) for each pin of port X
	// LATXbits.LATXY sets the value (0 or 1) for pin Y on port X
	// PORTX gets the value (0 or 1) for each pin of port X
	// PORTXbits.RXY gets the value (0 or 1) for pin Y on port X
	// TRISX declares the pin as either digital output (0) or digital input (1)
	// use &= for setting the inputs
	// use |= for setting outputs

	// Initialize Pins D1 and D2 output. 
	// Initialize pins D1 and D2 as high
	// Need to set TRIS bits 1 and 2 to low for output
	LATD |= 0x0006; TRISD &= 0xFFF9; // initialize 
	
	// Initialize Pin C1 as digital input
	// Need to set TRIS bit 1 to high for input
	// note that all pins are initialized as digital input
	// except for the analog and JTAG pins, but here's how you would do it	
	TRISCbits.TRISC1 = 1;
	
	// Set all analog pins to be digital I/O
	// This line of code only needs to be used if your pins are Analog Input (B port)
    AD1PCFG = 0xFFFF;
	
    while(1)
    {
		// Toggle LEDS based on digital input
		if(PIN_C1)
		{
			PIN_D1 = 1;
			PIN_D2 = 0;
		}	
		else
		{
			PIN_D1 = 0;
			PIN_D2 = 1;
		}
	}
}