Difference between revisions of "PIC32MX: Digital Outputs"

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...)
 
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
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.
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==
<br><br>
==Digital Output Example==
==Digital Output Example==
This section describes how to set up and write digital outputs using a PIC32MX460F512L.
This section describes how to set up and write digital outputs using a PIC32MX460F512L.
===Sample Code===
===Sample Code===
Program to turn on LEDs based on User Switch of UBW32. swUser must be defined for a switch [[PIC32MX460F512L : Digital Inputs |(digital input) ]] if not using UBW32.
Program to turn on LEDs based on User Switch of NU32. swUser must be defined for a switch [[PIC32MX460F512L : Digital Inputs |(digital input) ]] if not using NU32.


/********************************************************************
First include header files with definitions for generic type definitions, compiler, and for specific PIC. Also include the plib header file.
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;
}
}
}


#include "GenericTypeDefs.h"
#include "Compiler.h"
#include "HardwareProfile.h"
#include <plib.h>

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

Define a constants for 4 output pins. LATx is the function used to write to a pin.
#define PIN_D4 LATDbits.LATD4
#define PIN_D5 LATDbits.LATD5
#define PIN_D6 LATDbits.LATD6
#define PIN_D7 LATDbits.LATD7

Begin main body of program.
int main(void){

Configure the proper PB frequency and number of wait states in this case 80MHz.
SYSTEMConfigPerformance(80000000L);

Set all analog pins to be digital I/O
AD1PCFG = 0xFFFF;

Turn off JTAG so we get the pins back
mJTAGPortEnable(0);

The pins that we want to use as outputs must be initialized as an outputs. TRISx is a register that sets the pin as an input (1) or an output(0). You can set entire ports as inputs / outputs by using TRISX &= Hex number; We use &= hex number to only change the pins that we want to set as inputs / outputs.

Set D4, D5, D6, and D7 as a digital outputs
LATD |= 0x00F0; TRISD &= 0xFF0F;


Use while to set up infinite loop. What we're going to do is blink an LED on the board on and off depending on whether or not the digital input is high or low.
while(1)
{
if(swUser)
{
PIN_D4 = 1;
PIN_D5 = 0;
PIN_D6 = 1;
PIN_D7 = 0;
}
else
{
PIN_D4 = 0;
PIN_D5 = 1;
PIN_D6 = 0;
PIN_D7 = 1;
}
}
} // end main
<br>
<br>
===Circuit Diagram===

Latest revision as of 15:40, 5 January 2010

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