PIC32MX: Digital Outputs

From Mech
Revision as of 14:40, 5 January 2010 by Andrew Long (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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.

Digital Output Example

This section describes how to set up and write digital outputs using a PIC32MX460F512L.

Sample Code

Program to turn on LEDs based on User Switch of NU32. swUser must be defined for a switch (digital input) if not using NU32.

/********************************************************************
Super simple Digital Output. 
********************************************************************/

#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 - D4
#define PIN_D1			LATDbits.LATD1
#define PIN_D2			LATDbits.LATD2
#define PIN_D3			LATDbits.LATD3
#define PIN_D4			LATDbits.LATD4

int main(void)
{

	// 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;

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

	//Set D1, D2, D3, and D4 as a digital output
	LATD |= 0x001E; TRISD &= 0xFFE1;
		
    while(1)
    {

		if(swUser)
		{
			PIN_D1 = 1;
			PIN_D2 = 0;
			PIN_D3 = 1;
			PIN_D4 = 0;
		}
		else
		{
			PIN_D1 = 0;
			PIN_D2 = 1;
			PIN_D3 = 0;
			PIN_D4 = 1;
		}
	}
} 


Personal tools